// Elemental Rotator v1.2 (3.3.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 = 230;
var finalWidth = 739;

// Initial States (animate in, set to final if no animation)
var initialOpacity = 0;
var initialTop = finalTop;
var initialLeft = finalLeft; // change to finalWidth to scroll left to show next item on right
var initialHeight = finalHeight;
var initialWidth = finalWidth;

// Post States (animate out, set to final if no animation)
var postOpacity = 0;
var postTop = finalTop; 
var postLeft = finalLeft;  // change to -finalWidth to scroll left to show next item on right
var postHeight = finalHeight;
var postWidth = finalWidth;

// Set Animation Time and Interval
var animationTime = 1000;
var animationInterval = 8000; // default 4000 ms, set to 0 to skip automatic transitions // timer is automatically cancelled if rotateFwd or rotateBak are explictly called
var animationTimerID = 0;

$(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')); }
				
		if($(container).css('position') != 'relative' || $(container).css('position') != 'absolute'){ $(container).css({position:'relative'}); }
		$(container).css({overflow: 'hidden', width: finalWidth, height: finalHeight}); // Set container Size
		$(container+' '+elementType).css({position:'absolute', display:'none', top: initialTop, left: initialLeft, opacity: initialOpacity, width: initialWidth, height: initialHeight}); // Set All to Initial State
		$(container+' '+elementType+':first').css({display:'block', top: finalTop, left: finalLeft, opacity: finalOpacity, width: finalWidth, height: finalHeight}); // Set 1st to Final State
		
		// For Rotating Links
		if($(elementType).is('a')) {
			if(!$('.mask_overlay').length){ // Create Mask If No Mask Detected
				$(slideshow).append('<a class="mask_overlay" href="" style="display:block; position:absolute; top:0px; left:0px; z-index:2; width:'+finalWidth+'; height:'+finalHeight+';"></a>');
			}
		}
		maskOverlay(container,elementType);
		
		if(animationInterval) animationTimerID = setInterval( 'rotateFwd("'+container+'")', animationInterval);
	});
	
	$('#rotateFwd').click(function(e) {
		clearInterval(animationTimerID);															
		animationTimerID = 0;
		e.preventDefault();
		rotateFwd('.eRotator');
	});
	
	$('#rotateBak').click(function(e) {
		clearInterval(animationTimerID);															
		animationTimerID = 0;
		e.preventDefault();
		rotateBak('.eRotator');
	});

});


function rotateFwd(container){
	$(container+' '+elementType).stop(false, true);
	// add and remove class animating and do not animate quick clicks
	var eFirst = $(container+' '+elementType+':first');
	eFirst.remove().insertAfter(container+' '+elementType+':last');
	eFirst = $(container+' '+elementType+':first');
	var eLast = $(container+' '+elementType+':last');
	eLast.animate({top:postTop+'px', left:postLeft+'px', opacity:postOpacity, width:postWidth, height:postHeight}, animationTime); // Animate Out
	eFirst.css({top:initialTop+'px', left:initialLeft+'px'}).animate({top:finalTop+'px', left:finalLeft+'px', opacity:finalOpacity, width:finalWidth, height:finalHeight}, animationTime); // Animate In
	// For Rotating Links
	maskOverlay(container,elementType);
}

function rotateBak(container){
	$(container+' '+elementType).stop(true, true);
	var eFirst = $(container+' '+elementType+':first');
	var eLast = $(container+' '+elementType+':last');
	eLast.remove().insertBefore(container+' '+elementType+':first');
	eFirst.animate({top:initialTop+'px', left:initialLeft+'px', opacity:postOpacity, width:postWidth, height:postHeight}, animationTime); // Animate Out
	eLast.css({top:postTop+'px', left:postLeft+'px'}).animate({top:finalTop+'px', left:finalLeft+'px', opacity:finalOpacity, width:finalWidth, height:finalHeight}, animationTime ); // Animate In
	// For Rotating Links
	if($(elementType).is('a')) { // NEEDS TESTING
		$('.mask_overlay').attr('href',eLast.attr('href')).attr('target',eLast.attr('target'));
		$('.mask_overlay img').attr('alt',eLast.children('img').attr('alt'));
	}
}

function maskOverlay(container,elementType) {
	if($(elementType).is('a')) {
		$('.mask_overlay').attr('href',$(container+' '+elementType+':first').attr('href')).attr('target',$(container+' '+elementType+':first').attr('target'));
		$href = $('.mask_overlay').attr('href');
		if($href == "" || $href == "#"){ // No Link, Do nothing when clicked.
			$('.mask_overlay').css({cursor:'default'}).click(function(e){ e.preventDefault(); });
		} else { // Re-enable Link
			$('.mask_overlay').css({cursor:'pointer'}).click(function(e){ parent.location=''+$href+''; });
		}
		$('.mask_overlay img').attr('alt',$(container+' '+elementType+':first').children('img').attr('alt'));
	}
}