/*
	Kwicks for jQuery (version 1.5.1)
	Copyright (c) 2008 Jeremy Martin
	http://www.jeremymartin.name/projects.php?project=kwicks
	
	Licensed under the MIT license:
		http://www.opensource.org/licenses/mit-license.php

	Any and all use of this script must be accompanied by this copyright/license notice in its present form.
*/

(function($){
	$.fn.kwicks = function(options) {
		var defaults = {
			defaultKwick: 0,
			event: 'mouseover',
			spacing: 0,
			duration: 500
         
		};
		var o = $.extend(defaults, options);
		var W = 'width'; // W = Width or Height
		var L = 'left'; // L = Left or Top
		
		return this.each(function() {
         var imgPos = -222;
			container = $(this);
			var kwicks = container.children('div');
			var normW = kwicks.eq(0).css(W).replace(/px/,''); // normW = Normal Width or Height
			if(!o.max) {
				o.max = (normW * kwicks.size()) - (o.min * (kwicks.size() - 1));
			} else {
				o.min = ((normW * kwicks.size()) - o.max) / (kwicks.size() - 1);
			}
			// set width of container ul
         var H = parseInt(kwicks.eq(0).css('height').replace(/px/,''))+1+'px';
         container.css({
            width : (normW * kwicks.size()) + (o.spacing * (kwicks.size() - 1)) + 'px',
            height : H
         });				

			// pre calculate left or top values for all kwicks but the first and last
			// i = index of currently hovered kwick, j = index of kwick we're calculating
			var preCalcLs = []; // preCalcLs = pre-calculated Left or Top's
			var prePosLs = []; // 
			for(i = 0; i < kwicks.size(); i++) {
				preCalcLs[i] = [];
            //prePosLs[i] = kwicks.eq(i).find('img').css('left').replace(/px/,'');
				// don't need to calculate values for first or last kwick
				for(j = 1; j < kwicks.size() - 1; j++) {
					if(i == j) {
						preCalcLs[i][j] = j * o.min + (j * o.spacing);
					} else {
						preCalcLs[i][j] = (j <= i ? (j * o.min) : (j-1) * o.min + o.max) + (j * o.spacing);
					}
				}
			}
         
			// loop through all kwick elements
			kwicks.each(function(i) {
				var kwick = $(this);
				// set initial width or height and left or top values
				// set first kwick
				if(i === 0) {
					kwick.css(L, '0px');
				} 
				// set last kwick
				else if(i == kwicks.size() - 1) {
					kwick.css('right', '0px');
				}
				// set all other kwicks
				else {
               kwick.css(L, preCalcLs[o.defaultKwick][i]);
				}
            if(o.defaultKwick == i) {
               kwick.css(W, o.max + 'px');
               kwick.addClass('active');
            } else {
               kwick.css(W, o.min + 'px');
            }
				kwick.css({
					margin: 0,
					position: 'absolute'
				});
				
				kwick.bind(o.event, function() {
					// calculate previous width or heights and left or top values
					var prevWs = []; // prevWs = previous Widths or Heights
					var prevLs = []; // prevLs = previous Left or Tops
					var prevPos = []; // prevLs = previous Left or Tops
					kwicks.stop().removeClass('active');
					for(j = 0; j < kwicks.size(); j++) {
						prevWs[j] = kwicks.eq(j).css(W).replace(/px/, '');
						prevLs[j] = kwicks.eq(j).css(L).replace(/px/, '');
						prevPos[j] = kwicks.eq(j).find('img').css(L).replace(/px/, '');
					}
					var aniObj = {};
					aniObj[W] = o.max;
					var maxDif = o.max - prevWs[i];
					var prevWsMaxDifRatio = prevWs[i]/maxDif;
					kwick.addClass('active').animate(aniObj, {
						step: function(now) {
							// calculate animation completeness as percentage
							var percentage = maxDif != 0 ? now/maxDif - prevWsMaxDifRatio : 1;
                     percentage = Math.round(percentage*100)/100;
							// adjsut other elements based on percentage
                     //console.log(percentage);
							kwicks.each(function(j) {
								if(j != i) {
									kwicks.eq(j).css(W, prevWs[j] - ((prevWs[j] - o.min) * percentage) + 'px');
                           kwicks.eq(j).find('p.zitat').fadeOut('slow');
                           // zu
                           kwicks.eq(j).find('img').css('left', Math.round(prevPos[j] - ((prevPos[j] - imgPos) * percentage)) + 'px');
								} else {
                           // auf
                           kwicks.eq(j).find('img').css('left', Math.round(parseInt(prevPos[j]) - (parseInt(prevPos[j]) * percentage)) + 'px');
                        }
								if(j > 0 && j < kwicks.size() - 1) { // if not the first or last kwick
									kwicks.eq(j).css(L, prevLs[j] - ((prevLs[j] - preCalcLs[i][j]) * percentage) + 'px');
								}
							});
                  
						},
						duration: o.duration,
                  //complete: function() { kwick.find('p.zitat').fadeIn('slow'); }
				  complete: function() { kwick.find('p.zitat').css('display', 'block') }
					});
				});
			});
         
         //container.find('.active .zitat').fadeIn('slow');
		 container.find('.active .zitat').css('display', 'block');
         container.find('.active img').css('left','0px');
		});
	};
})(jQuery);
