/*
	accordion (version 0.1b) for jQuery (version 1.5.1)
	Copyright (c) 2009 Georgi Nachev / jooorooo
    Powered by webmedia ltd
	
*/

(function($){
	$.fn.accordion = function(options) {
		var defaults = {
			isVertical: false,
			sticky: false,
			defaultAccordion: 0,
			event: 'mouseover',
            event1: 'mouseout',
			spacing: 0,
			duration: 500,
			auto: false
		};
		var o = $.extend(defaults, options);
		var WoH = (o.isVertical ? 'height' : 'width'); // WoH = Width or Height
		var LoT = (o.isVertical ? 'top' : 'left'); // LoT = Left or Top
		
		return this.each(function() {
			container = $(this); 
			var accordion = container.children('li'); 
            //for image
            var image = $('img', accordion);
            var marginLeft = image.css('marginLeft').replace(/px/, '');
            //end for image
			var normWoH = accordion.eq(0).css(WoH).replace(/px/,''); // normWoH = Normal Width or Height
			if(!o.max) {
				o.max = (normWoH * accordion.size()) - (o.min * (accordion.size() - 1));
			} else {
				o.min = ((normWoH * accordion.size()) - o.max) / (accordion.size() - 1);
			}
			// set width of container ul
			if(o.isVertical) {
				container.css({
					width : accordion.eq(0).css('width'),
					height : (normWoH * accordion.size()) + (o.spacing * (accordion.size() - 1)) + 'px'
				});				
			} else {
				container.css({
					width : (normWoH * accordion.size()) + (o.spacing * (accordion.size() - 1)) + 'px',
					height : accordion.eq(0).css('height')
				});				
			}

			// pre calculate left or top values for all accordion but the first and last
			// i = index of currently hovered Accordion, j = index of Accordion we're calculating
			var preCalcLoTs = []; // preCalcLoTs = pre-calculated Left or Top's
			for(i = 0; i < accordion.size(); i++) {
				preCalcLoTs[i] = [];
				// don't need to calculate values for first or last Accordion
				for(j = 1; j < accordion.size() - 1; j++) {
					if(i == j) {
						preCalcLoTs[i][j] = o.isVertical ? j * o.min + (j * o.spacing) : j * o.min + (j * o.spacing);
					} else {
						preCalcLoTs[i][j] = (j <= i ? (j * o.min) : (j-1) * o.min + o.max) + (j * o.spacing);
					}
				}
			}
			
			// loop through all Accordion elements
			accordion.each(function(i) { 
				var Accordion = $(this);
				// set initial width or height and left or top values
				// set first Accordion
				if(i === 0) {
					Accordion.css(LoT, '0px');
				} 
				// set last Accordion
				else if(i == accordion.size() - 1) {
					Accordion.css(o.isVertical ? 'bottom' : 'right', '0px');
				}
				// set all other accordion
				else {
					if(o.sticky) {
						Accordion.css(LoT, preCalcLoTs[o.defaultAccordion][i]);
					} else {
						Accordion.css(LoT, (i * normWoH) + (i * o.spacing));
					}
				} 
				// correct size in sticky mode
				if(o.sticky) {
					if(o.defaultAccordion == i) {
                        image.eq(i).css("marginLeft", '0px');
						Accordion.css(WoH, o.max + 'px');
						Accordion.addClass('active');
					} else {
						Accordion.css(WoH, o.min + 'px'); 
					}
				}
				Accordion.css({
					margin: 0,
					position: 'absolute'
				});
				

				Accordion.bind(o.event, function() {
					// calculate previous width or heights and left or top values
					var prevWoHs = []; // prevWoHs = previous Widths or Heights
					var prevLoTs = []; // prevLoTs = previous Left or Tops
					accordion.stop().removeClass('active');
                    
                    ////////////
                    
                    image.eq(i).animate({marginLeft: '0px'}, o.duration/2);
                    //image.eq(i).animate({marginLeft: '0px', duration: o.duration});
                    
                    /////////////
					for(j = 0; j < accordion.size(); j++) {
						prevWoHs[j] = accordion.eq(j).css(WoH).replace(/px/, '');
						prevLoTs[j] = accordion.eq(j).css(LoT).replace(/px/, '');
					}
					var aniObj = {};
					aniObj[WoH] = o.max;
                    
					var maxDif = o.max - prevWoHs[i];
					var prevWoHsMaxDifRatio = prevWoHs[i]/maxDif; 
                    
                    //image.addClass('active').animate({marginLeft: '175px'}, o.duration)
                    

                    //////////////
					Accordion.addClass('active').animate(aniObj, {
						step: function(now) {
							// calculate animation completeness as percentage
							var percentage = maxDif != 0 ? now/maxDif - prevWoHsMaxDifRatio : 1;
							// adjsut other elements based on percentage
							accordion.each(function(j) {
								if(j != i) { 
									accordion.eq(j).css(WoH, prevWoHs[j] - ((prevWoHs[j] - o.min) * percentage) + 'px');
								}
								if(j > 0 && j < accordion.size() - 1) {  // if not the first or last Accordion
									accordion.eq(j).css(LoT, prevLoTs[j] - ((prevLoTs[j] - preCalcLoTs[i][j]) * percentage) + 'px');
								}
							});
						},
						duration: o.duration,
						easing: o.easing
					});
				});
			});
			if(o.auto) {
				container.bind("mouseleave", function() {
					var prevWoHs = [];
					var prevLoTs = [];
					accordion.removeClass('active').stop();
					for(i = 0; i < accordion.size(); i++) {
						prevWoHs[i] = accordion.eq(i).css(WoH).replace(/px/, '');
						prevLoTs[i] = accordion.eq(i).css(LoT).replace(/px/, '');
                        //image.eq(i).animate({marginLeft: marginLeft + 'px'}, o.duration);
                        image.eq(i).animate({marginLeft: marginLeft + 'px'}, o.duration);
					}
					var aniObj = {};
					aniObj[WoH] = normWoH;
					var normDif = normWoH - prevWoHs[0];
					accordion.eq(0).animate(aniObj, {
						step: function(now) {
							var percentage = normDif != 0 ? (now - prevWoHs[0])/normDif : 1;
							for(i = 1; i < accordion.size(); i++) {
								accordion.eq(i).css(WoH, prevWoHs[i] - ((prevWoHs[i] - normWoH) * percentage) + 'px');
								if(i < accordion.size() - 1) {
									accordion.eq(i).css(LoT, prevLoTs[i] - ((prevLoTs[i] - ((i * normWoH) + (i * o.spacing))) * percentage) + 'px');
								}
							}
						},
						duration: o.duration,
						easing: o.easing
					});
				});
			}
		});
	};
})(jQuery);