// Elemental Rotator v1.1 (10.12.09)  
// copyright Blue Ridge Solutions, Inc. www.blueridges.com
//
// INSTRUCTIONS
// Create an relative or absolutely positioned container for the rotator/slideshow.
// Give that element the eRotator class and a unique id
// Place your element(s) of choice within the container
// Set the variables below to achieve a desired effect

// Set Animation slideshow and Element Type (img, p, *)
var slideshow = ".eRotator";
var elementType = "img"; 

// Final States (after animation, completed state)
var finalTop = 0;
var finalLeft = 0;
var finalOpacity = 1;
var finalHeight = 258;
var finalWidth = 554;

// Initial States (animate in, set to final if no animation)
var initialOpacity = 0;
var initialTop = finalTop;
var initialLeft = finalLeft;
var initialHeight = finalHeight;
var initialWidth = finalWidth;

// Post States (animate out, set to final if no animation)
var postOpacity = 0;
var postTop = finalTop; // add ['-='+] or ['+='+] to slide
var postLeft = finalLeft; // add ['-='+] or ['+='+] to slide
var postHeight = finalHeight;
var postWidth = finalWidth;

// Set Animation Time and Interval
var animationTime = 1000;
var animationInterval = 4000; // default 4000 ms, set to 0 to skip automatic transitions 
var animationTimerID = 0;

$().ready(function(){ // Apply Initial States and Interval
	
	$(slideshow).each(function(){ 
		var container = $(this).attr('id');
		var container = "#"+container;
					   
		// Set Random Starting Point
		//rand = Math.round($(container+' '+elementType).length*Math.random())-1;
		//for (i=0;i<rand;i++) {$(container+' '+elementType+':first').remove().insertAfter(container+' '+elementType+':last');}
		
		$(container).css({overflow: 'hidden', width: finalWidth, height: finalHeight}); // Set container Size
		$(container+' '+elementType).css({position: 'absolute', top: initialTop, left: initialLeft, opacity: initialOpacity, width: initialWidth, height: initialHeight}); // Set All to Initial State
		$(container+' '+elementType+':first').css({position: 'absolute', top: finalTop, left: finalLeft, opacity: finalOpacity, width: finalWidth, height: finalHeight}); // Set 1st to Final State
		if(animationInterval) animationTimerID = setInterval( 'rotateNext("'+container+'")', animationInterval);
	});
	
	$('#goNext').click(function(e) {
		clearInterval(animationTimerID);															
		animationTimerID = 0;
		e.preventDefault();
		rotateNext('.eRotator');
	});
	
	$('#goPrior').click(function(e) {
		clearInterval(animationTimerID);															
		animationTimerID = 0;
		e.preventDefault();
		rotatePrior('.eRotator');
	});

});


function rotateNext(container){
	var ef = $(container+' '+elementType+':first');
	ef.remove().insertAfter(container+' '+elementType+':last');//.css({top: initialTop, left: initialLeft, opacity: initialOpacity, width: initialWidth, height: initialHeight});
	ef = $(container+' '+elementType+':first');
	var el = $(container+' '+elementType+':last');
	el.animate({top: postTop+'px', left: postLeft+'px', opacity: postOpacity, width: postWidth, height: postHeight}, animationTime); // Animate Out
	ef.animate({top: finalTop+'px', left: finalLeft+'px', opacity: finalOpacity, width: finalWidth, height: finalHeight}, animationTime); // Animate In
}

function rotatePrior(container){
	var ef = $(container+' '+elementType+':first');
	var el = $(container+' '+elementType+':last');
	el.remove().insertBefore(container+' '+elementType+':first');
	ef.animate({top: postTop+'px', left: postLeft+'px', opacity: postOpacity, width: postWidth, height: postHeight}, animationTime); // Animate Out
	el.animate({top: finalTop+'px', left: finalLeft+'px', opacity: finalOpacity, width: finalWidth, height: finalHeight}, animationTime ); // Animate In
}
