/*
	use: $('parent element').autocontentslider({settingsMap}, function(){});
*/

(function($) {
	$.fn.autocontentslider = function(settings, callback) {
		settings = jQuery.extend({
			 slide:		3000			// every n ms			 
			,speed:		1200			// speed of the slides (ms)

			,size:		false			// size of slide elements (if false the size will be determent by the first child) (top||bottom = height || left||right = width)
			,direction:	'left'			// Direction of animation (left, right, bottom/down, top/up)
			
			,shown:		1				// number of shown (int)
			,types:		'*'				// Child elements that will be rotated
		},settings);
		
		var objParent			= this;
		var intTotal			= 0;

		var arrPositions		= new Array();
		var arrAllChildren 		= new Array();
		var arrShownChildren	= new Array();
		
		var blnInvert			= false;
		
		function _start_scroll(){
			setTimeout(function(){
				for(var i=1; settings.shown+1 > i; i++)
					_animate_slide(i);
								
				_animate_last_slide();
				_start_scroll();  
			},(settings.slide+settings.speed));
		}		
		
		function _animate_slide(num){
			var ccsMap	= {};
			ccsMap[settings.direction] = ((settings.size*-1)+(settings.size*(num-1)))+'px';
			
			arrAllChildren[arrPositions[num]].animate(ccsMap, settings.speed);
			if(arrPositions[num] >= intTotal)	arrPositions[num] = 1;
			else								arrPositions[num]++;
		}
				
		function _animate_last_slide(){
			var ccsMap	= {};
			var last	= settings.shown+1;
			var next	= (last+1);
			
			ccsMap[settings.direction] = (settings.size*(settings.shown-1));
			
			arrAllChildren[arrPositions[last]].animate(ccsMap, settings.speed, function(){ 
				if(typeof(callback)==='function')
					callback.apply();
				
				arrAllChildren[arrPositions[next]].css(settings.direction, (settings.size*settings.shown)+'px');
				if(arrPositions[next] >= intTotal)	arrPositions[next] = 1;
				else								arrPositions[next]++;
			});
			
			if(arrPositions[last] >= intTotal)	arrPositions[last] = 1;
			else								arrPositions[last]++;
		}
						
		objParent.children(settings.types).each(function(){
			$(this).css({'position' : 'absolute', 'top' : 0, 'left' : 0});
			
			intTotal++;
			arrPositions[intTotal]		= intTotal;
			arrAllChildren[intTotal]	= $(this);
			
			if(arrShownChildren.length < settings.shown)
				arrShownChildren[arrShownChildren.length] = $(this);
		});
		
		if(intTotal == settings.shown+1 && (arrPositions[intTotal+1] == undefined || arrAllChildren[intTotal+1] == undefined)){
			arrPositions[intTotal+1] 	= arrPositions[1];
			arrAllChildren[intTotal+1]	= arrAllChildren[1];
		}
		
		switch (settings.direction.toLowerCase()){
			case 'bottom':
			case 'down':
				blnInvert 			= true;
			case 'up':	
			case 'top':	
				settings.direction 	= 'top';
				settings.size		= (settings.size == false)? arrShownChildren[0].height() : settings.size;
			break;
			
			case 'right':
				blnInvert 			= true;
			default:
			case 'left':	
				settings.direction 	= 'left';
				settings.size		= (settings.size == false)? arrShownChildren[0].width() : settings.size;
			break;
		}

		settings.size = blnInvert? (settings.size*-1) : settings.size;
		objParent.children(settings.types).css(settings.direction, (settings.size*settings.shown)+'px');
		objParent.css({'overflow' : 'hidden', 'width' : objParent.width()+'px', 'height' : objParent.height()+'px'});
		
		for(var i=0; arrShownChildren.length > i; i++)
			arrShownChildren[i].css(settings.direction, (settings.size*i));

		if(intTotal > settings.shown)
			_start_scroll();
	}
})(jQuery); 
