/*
 * 	Auto Slider - jQuery plugin
 *	written by Kurt Robar	
 *
 *	Copyright (c) 2009 Kurt Robar (krobar.org)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 */

(function($) {

	$.fn.autoslide = function(opts){
	  
		// default configuration properties
		var defaults = {
			direction	: 'vertical', //  vertical or horizontal;
			order		: 'ascending', // ascending or decending
			align		: 'center', // center, left, or right
			interval	: 5000,
			speed		: 800			
		}; 
		
		var params = $.extend({}, defaults, opts || {});
		
		return this.each(function() {  
			obj = $(this);
			obj.css('position', 'relative'); // if not already, make container relatively positioned
			var numLI = $("li", obj).length;
			var objWidth = obj.width(); 
			var objHeight = obj.height(); 
			var indexTotal = numLI-1;
			var indexCount = 0;
			var vertical = (params.direction.charAt(0).toLowerCase() == 'v');
			
			// Make each list element a layer
			$("li", obj).each(function(index) {
				element = $(this);
				element.attr('id', 'autoslide_' + index);
			});
			
			var property = {
				width : [],
				height : [],
				vAlign : [],
				hAlign : []
			};
			
			for(i=0; i<=indexTotal; i++) {
				$('#autoslide_' + i).css({'position':'absolute', 'paddingTop':'0', 'paddingBottom':'0'});
				property.width[i] = $('#autoslide_' + i).width();
				property.height[i] = $('#autoslide_' + i).height();
				property.vAlign[i] = ( (objHeight/2) - ( $('#autoslide_' + i).height() /2) ) +'px';
				switch (params.align.charAt(0).toLowerCase()) {
					case 'c':
						property.hAlign[i] = ( (objWidth /2) - ( property.width[i] /2) ) +'px';
						break;
					case 'r':
						property.hAlign[i] = ( (objWidth-2) - property.width[i] ) +'px';
						break;
					case 'l':
						property.hAlign[i] = '0px';
						break;
				}
				if(i==0){
					$('#autoslide_' + i).css({'top': property.vAlign[i], 'left': property.hAlign[i],'zIndex':'1000'});
				} else {
					if(vertical) {
						$('#autoslide_' + i).css({'top': objHeight + 'px', 'left': property.hAlign[i], 'zIndex':'0'});
					} else {
						$('#autoslide_' + i).css({'top': property.vAlign[i], 'left': objWidth+'px', 'zIndex':'0'});
					}
				}
				if(!vertical) {
					$('#autoslide_' + i).css('float','left'); //not sure we need this
				}
			};
			
			var Interval = setInterval(function(){ animate(); }, params.interval);
			
			function animate(){
				var prependE = '';
				var prependS = '';
				var lastCount = indexCount;
				
				if(params.order.charAt(0).toLowerCase() == 'a'){
					indexCount = (indexCount>=indexTotal) ? 0 : indexCount+1;
					prependS = '';
					prependE = '-';
				} else {
					indexCount = (indexCount<=0) ? indexTotal : indexCount-1;
					prependS = '-';
					prependE = '';
				};
				
				var hOffset = (property.height[lastCount] > objHeight) ? property.height[lastCount] : objHeight;
				var vOffset = (property.width[lastCount] > objWidth) ? property.width[lastCount] : objWidth;
				
				
				var anim = {
						tStart 	: prependS + objHeight + 'px',
						tEnd 	: property.vAlign[indexCount],
						lEnd 	: prependE + hOffset +'px'
				};
				
				if(vertical) {
					$('#autoslide_' + lastCount).animate({'top' : anim.lEnd}, params.speed);
					$('#autoslide_' + indexCount).css({'top' : anim.tStart, 'zIndex':'1000'}).animate({ 'top' : anim.tEnd }, params.speed);
				} else {
					anim.tStart	= prependS + vOffset + 'px';
					//anim.lEnd	= prependE + $('#autoslide_' + lastCount).width() +'px';
					//anim.tStart	= prependS + $('#autoslide_' + indexCount).width() + 'px';
					anim.tEnd = property.hAlign[indexCount];
					anim.lEnd	= prependE + vOffset +'px';
					$('#autoslide_' + lastCount).animate({'left' : anim.lEnd}, params.speed);
					$('#autoslide_' + indexCount).css({'left' : anim.tStart, 'zIndex':'1000'}).animate({ 'left' : anim.tEnd }, params.speed);
				};
				
				$('#autoslide_' + lastCount).css('zIndex', '0');
			}; /*  /animate  */
			
		});/*  /return  */
	  
	}; /*  /$.fn.autoslide  */

})(jQuery);