//* Elemental Rotator (5.19.09) `BKS */

// 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 = 160;
var finalWidth = 475;

// 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 = 2000;
var animationInterval = 4000;

$().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
		setInterval( 'eRotator("'+container+'")', animationInterval);
	});
});

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