// Elemental Rotator v1.2 (1.4.10)  

// copyright Blue Ridge Solutions, Inc. www.blueridges.com

//

// INSTRUCTIONS

// Create container with 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 = ".bnr_link"; 



// Final States (after animation, completed state)

var finalTop = 0;

var finalLeft = 0;

var finalOpacity = 1;

var finalHeight = 359;

var finalWidth = 900;



// 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 = 5000; // default 4000 ms, set to 0 to skip automatic transitions // timer is automatically cancelled if rotateFwd or rotateBak are explictly called

var animationTimerID = 0;



jQuery(function(){ // Apply Initial States and Interval

	

	jQuery(slideshow).each(function(){ 

		var container = jQuery(this).attr('id');

		var container = "#"+container;

		var eFirst = jQuery(container+' '+elementType+':first');

		var eLast = jQuery(container+' '+elementType+':last');

   

		// Set Random Starting Point

		//rand = Math.round(jQuery(container+' '+elementType).length*Math.random())-1;

		//for (i=0;i<rand;i++) { eFirst.remove().insertAfter(eLast); }

				

		if(jQuery(container).css('position') != 'relative' || jQuery(container).css('position') != 'absolute'){ jQuery(container).css({position:'relative'}); }

		jQuery(container).css({overflow: 'hidden', width: finalWidth, height: finalHeight}); // Set container Size

		jQuery(container+' '+elementType).css({position:'absolute', display:'none', top: initialTop, left: initialLeft, opacity: initialOpacity, width: initialWidth, height: initialHeight}); // Set All to Initial State

		eFirst.css({display:'block', top: finalTop, left: finalLeft, opacity: finalOpacity, width: finalWidth, height: finalHeight}); // Set 1st to Final State

		

		// For Rotating Links

		if(jQuery(elementType).is('a')) {

			if(!jQuery('.mask_overlay').length){ // Create Mask If No Mask Detected

				jQuery(slideshow).append('<a class="mask_overlay" href="" style="display:block; position:absolute; top:0px; left:0px; z-index:2; width:'+finalWidth+'; height:'+finalHeight+';"></a>');

			}

			jQuery('.mask_overlay').attr('href',eFirst.attr('href')).attr('target',eFirst.attr('target'));

			jQuery('.mask_overlay img').attr('alt',eFirst.children('img').attr('alt'));

		}

		

		if(animationInterval) animationTimerID = setInterval( 'rotateFwd("'+container+'")', animationInterval);

	});

	

	jQuery('#rotateFwd').click(function(e) {

		clearInterval(animationTimerID);															

		animationTimerID = 0;

		e.preventDefault();

		rotateFwd('.eRotator');

	});

	

	jQuery('#rotateBak').click(function(e) {

		clearInterval(animationTimerID);															

		animationTimerID = 0;

		e.preventDefault();

		rotateBak('.eRotator');

	});



});





function rotateFwd(container){

	var eFirst = jQuery(container+' '+elementType+':first');

	eFirst.remove().insertAfter(container+' '+elementType+':last');

	eFirst = jQuery(container+' '+elementType+':first');

	var eLast = jQuery(container+' '+elementType+':last');

	eLast.animate({top: postTop+'px', left: postLeft+'px', opacity: postOpacity, width: postWidth, height: postHeight}, animationTime); // Animate Out

	eFirst.animate({top: finalTop+'px', left: finalLeft+'px', opacity: finalOpacity, width: finalWidth, height: finalHeight}, animationTime); // Animate In

	// For Rotating Links

	if(jQuery(elementType).is('a')) {

		jQuery('.mask_overlay').attr('href',eFirst.attr('href')).attr('target',eFirst.attr('target'));

		jQuery('.mask_overlay img').attr('alt',eFirst.children('img').attr('alt'));

	}

}



function rotateBak(container){

	var eFirst = jQuery(container+' '+elementType+':first');

	var eLast = jQuery(container+' '+elementType+':last');

	eLast.remove().insertBefore(container+' '+elementType+':first');

	eFirst.animate({top: postTop+'px', left: postLeft+'px', opacity: postOpacity, width: postWidth, height: postHeight}, animationTime); // Animate Out

	eLast.animate({top: finalTop+'px', left: finalLeft+'px', opacity: finalOpacity, width: finalWidth, height: finalHeight}, animationTime ); // Animate In

	// For Rotating Links

	if(jQuery(elementType).is('a')) { // NEEDS TESTING

		jQuery('.mask_overlay').attr('href',eLast.attr('href')).attr('target',eLast.attr('target'));

		jQuery('.mask_overlay img').attr('alt',eLast.children('img').attr('alt'));

	}

}

