// eRotate, The Elemental Rotator (2.22.10)  
// copyright Blue Ridge Solutions, Inc. www.blueridges.com
//
// INSTRUCTIONS
// Create container with a unique id
// Place your element(s) of choice within the container and add class="slide" to each slide
// Intialize the plugin with $('#slideshow').eRotate();
// Pass in any of the settings below to achieve a desired effect 
// Default setup: $('#slideshow').eRotate({finalWidth:600, finalHeight:400, fxInterval:4000});

(function($){ 
	$.fn.eRotate = function(options){ 
		var settings = {
			// Set slide element type (img, p, *, .class)
			elementType : '.slide', 
			randomStart : 0,
			pushLeft : 0,
			slideLeft: 0,
			
			// Final States (after fx, completed state)
			finalTop : 0,
			finalLeft : 0,
			finalOpacity : 1,
			finalWidth : 600,
			finalHeight : 400,
			
			// Initial States (animate in, set to final if no fx)
			initialOpacity : 0,
			initialTop : null,
			initialLeft : null, 
			initialWidth : null,
			initialHeight : null,
			
			// Post States (animate out, set to final if no fx)
			postOpacity : null,
			postTop : null, 
			postLeft : null, 
			postWidth : null,
			postHeight : null,
			
			// Set fx Time and Interval
			fxSpeed : 2000,
			fxTime : null,
			fxInterval : 4000, // default 4000 ms, set to 0 to skip automatic transitions // timer is automatically cancelled if rotateFwd or rotateBak are explictly called
			fxTimerID : 0,
			
			fxDelay : 0,
			fxResume : null,
			fxHasten : null
		};
		
		return this.each(function(){  
			var $slideshow = $(this);
			
			// override settings with parameters
			if (options) $.extend(settings, options);
			
			if(settings.initialTop == null) settings.intialTop = settings.finalTop;
			if(settings.initialLeft == null) settings.initialLeft = settings.finalLeft; // change to settings.finalWidth to scroll left to show next item on right
			if(settings.initialHeight == null) settings.initialHeight = settings.finalHeight;
			if(settings.initialWidth == null) settings.initialWidth = settings.finalWidth;
			if(settings.postOpacity == null) settings.postOpacity = settings.initialOpacity;
			if(settings.postTop == null) settings.postTop = settings.finalTop;
			if(settings.postLeft == null) settings.postLeft = settings.finalLeft; // change to -finalWidth to scroll left to show next item on right
			if(settings.postHeight == null) settings.postHeight = settings.finalHeight;
			if(settings.postWidth == null) settings.postWidth = settings.finalWidth;
			if(settings.fxTime == null) settings.fxTime = settings.fxSpeed;
			if(settings.fxResume == null) settings.fxResume = settings.fxInterval * 4;
			if(settings.fxHasten == null) settings.fxHasten = settings.fxTime / 10;
			
			// Setup Predefined FX
			if(settings.slideLeft == 1 || settings.pushLeft == 1){
				settings.finalOpacity = 1;
				settings.initialOpacity = 1;
				settings.postOpacity = 1;

				settings.finalLeft = 0;
				settings.initialLeft = settings.finalWidth;
				settings.postLeft = -settings.finalWidth;
			}
			
			if(settings.pushLeft == 1){
				settings.initialWidth = 0;
				settings.postWidth = settings.finalWidth;
				
			}
			
			// Apply settings.initial States and Interval
			var container ="";
			container = $slideshow.attr('id');
			container = "#"+container;
	   
			// Set Random Starting Point
			if(settings.randomStart == 1){
				rand = Math.round($(container+' '+settings.elementType).length*Math.random())-1;
				for (i=0;i<rand;i++) { $(container+' '+settings.elementType+':first').remove().insertAfter($(container+' '+settings.elementType+':last')); }
			}
			
			// Setup initial styles
			if($(container).css('position') != 'relative' || $(container).css('position') != 'absolute'){ $(container).css({position:'relative'}); }
			$(container).css({overflow: 'hidden', width: settings.finalWidth, height: settings.finalHeight}); // Set container Size
			initialSettings(container);
			
			// Set forward rotation
			if($(container+' '+settings.elementType).length > 1){
				if(settings.fxInterval) settings.fxTimerID = setInterval(function(){rotateFwd(container);},settings.fxInterval);
			}

			// Setup buttons - NEEDS TESTING
			$(container+' .fwd_btn, .fwd_btn[rel='+container+']').css({zIndex:5}).click(function(e) {
				clearInterval(settings.fxTimerID);
				settings.fxTime = settings.fxHasten;
				settings.fxTimerID = 0;
				settings.fxDelay = 1;
				e.preventDefault();
				rotateFwd(container);
				//settings.fxTimerID = setInterval(function(){rotateFwd(container);}, settings.fxResume); 
			});
			
			$(container+' .bak_btn, .bak_btn[rel='+container+']').css({zIndex:5}).click(function(e) {
				clearInterval(settings.fxTimerID);															
				settings.fxTime = settings.fxHasten;
				settings.fxTimerID = 0;
				e.preventDefault();
				rotateBak(container);
			});
			
			$(container+' .stop_btn, .stop_btn[rel='+container+']').click(function(e) {
				clearInterval(settings.fxTimerID);															
				settings.fxTimerID = 0;
				e.preventDefault();
			});
			
			$(container+' .start_btn, .start_btn[rel='+container+']').click(function(e) {
				clearInterval(settings.fxTimerID);															
				settings.fxTime = settings.fxSpeed;
				settings.fxTimerID = setInterval(function(){rotateFwd(container);},settings.fxInterval);
				e.preventDefault();
			});
		
			// Functions
			function initialSettings(container){
				$(container+' '+settings.elementType).css({position:'absolute', top: settings.initialTop, left: settings.initialLeft, opacity: settings.initialOpacity, width: settings.initialWidth, height: settings.initialHeight}); // Set All to settings.initial State
				$(container+' '+settings.elementType+':first').css({top: settings.finalTop, left: settings.finalLeft, opacity: settings.finalOpacity, width: settings.finalWidth, height: settings.finalHeight}); // Set 1st to settings.final State
			}
			
			function rotateFwd(container){
				$(container+' '+settings.elementType).stop(false, true);
				initialSettings(container);
				// add and remove class animating and do not animate quick clicks
				var eFirst = $(container+' '+settings.elementType+':first');
				eFirst.remove().insertAfter(container+' '+settings.elementType+':last');
				eFirst = $(container+' '+settings.elementType+':first');
				var eLast = $(container+' '+settings.elementType+':last');
				eLast.css({zIndex:0}).animate({top:settings.postTop+'px', left:settings.postLeft+'px', opacity:settings.postOpacity, width:settings.postWidth, height:settings.postHeight}, settings.fxTime); // Animate Out
				eFirst.css({zIndex:1, top:settings.initialTop+'px', left:settings.initialLeft+'px'}).animate({top:settings.finalTop+'px', left:settings.finalLeft+'px', opacity:settings.finalOpacity, width:settings.finalWidth, height:settings.finalHeight}, settings.fxTime); // Animate In
			
				/*if(settings.fxDelay){
					clearInterval(settings.fxTimerID);															
					settings.fxTime = settings.fxSpeed;
					settings.fxTimerID = setInterval(function(){rotateFwd(container);}, settings.fxInterval); 
				}*/
			}
			
			function rotateBak(container){
				$(container+' '+settings.elementType).stop(true, true);
				var eFirst = $(container+' '+settings.elementType+':first');
				var eLast = $(container+' '+settings.elementType+':last');
				eLast.remove().insertBefore(container+' '+settings.elementType+':first');
				eFirst.css({zIndex:0}).animate({top:settings.initialTop+'px', left:settings.initialLeft+'px', opacity:settings.postOpacity, width:settings.postWidth, height:settings.postHeight}, settings.fxTime); // Animate Out
				eLast.css({zIndex:1,top:settings.postTop+'px', left:settings.postLeft+'px'}).animate({top:settings.finalTop+'px', left:settings.finalLeft+'px', opacity:settings.finalOpacity, width:settings.finalWidth, height:settings.finalHeight}, settings.fxTime ); // Animate In
			}
		}); 
	}; // eRotate Plugin
})(jQuery);
