/*
*	jQuery verticalShift Plugin (compacts element to fit within an element and expands element to full view on mouseover)
*	Examples and help at http://www.husbandman.org/husbandman/scripts/verticalshift
*	Copyright 2011 by Mark Tank
*	version 1.0 (Jan 17, 2011)
*	Dual licensed under the MIT and GPL licenses.
* 	tested with: jQuery v1.4.4
*/


(function($) {

	$.fn.verticalShift = function(options) {
				
		var opt = {
			marginRight: 0,
			opacity: 1
		}
		
		var options = $.extend(opt,options);
		
		//initializing
		var recClass = $(this);
		resize(0);
		recClass.each(function(i) {
			var obj = $(this);
			obj.hover(
				function() {
					//width of the child
					var child = obj.children().first()
					w = child.width();
					w += parseInt(child.css("padding-left")) + parseInt(child.css("padding-right"));
					ml = parseInt(child.css("margin-left"));
					w = (isNaN(ml)) ? w : w+ml;
					resize(w);
					obj.css({'width':w});
					if(options.opacity <= 1) {
						obj.stop(true, true).fadeTo('slow',1);
					}
				},
				function() {
					resize(0);
					if(options.opacity <= 1) {
						obj.stop(true, true).fadeTo('slow',options.opacity);
					}
				}
			)
		});
		
		function resize(exception) {
			var count = (exception > 0) ? recClass.length - 1: recClass.length;
			var totalpadding = parseInt(recClass.css("padding-left")) + parseInt(recClass.css("padding-right"));
			var maxwidth = recClass.parent().width() - options.marginRight;
			maxwidth = (exception > 0) ? maxwidth - exception : maxwidth;
			var reducedwidth = Math.floor((maxwidth / count) - totalpadding);
			var remainder =  maxwidth - (reducedwidth * count);
			var lastwidth = reducedwidth + remainder;
			recClass.css({
				"width":reducedwidth,
				"float":"left",
				"overflow":"hidden"
			});
			recClass.last().css({'width':lastwidth});
			recClass.fadeTo(0,options.opacity);
		}
		
		return recClass;
	}
})(jQuery);
