// 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 = 'div'; 

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

// 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 initialWidth = finalWidth;
var initialHeight = finalHeight;

// 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 postWidth = finalWidth;
var postHeight = finalHeight;

// Set Animation Time and Interval
var animationTime = 2000;
var animationInterval = 4000; // 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
	
	// Livingstone Custom
	if(jQuery.support.opacity) {
		$('#sm_logo').css({ top:'191px', left:'216px', opacity:postOpacity, width:529, height:275 });
	} else {
		$('#sm_logo').css({top:'191px', left:'216px', width:529, height:275 }).hide();
	}
	
	
	$(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>');
			}
			$('.mask_overlay').attr('href',eFirst.attr('href')).attr('target',eFirst.attr('target'));
			$('.mask_overlay img').attr('alt',eFirst.children('img').attr('alt'));
		}
		
		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');
	
	// Remove Livingstone Logo
	if(eFirst.hasClass('first')){
		if(jQuery.support.opacity) { $('#sm_logo').animate({ opacity:postOpacity }, animationTime*.5); } // Animate Out
		else { $('#sm_logo').slideUp(animationTime*.75); } // Animate Out
	}
	// Add Livingstone Logo
	if(eFirst.hasClass('second')){
		if(jQuery.support.opacity) {
			$('#sm_logo').css({top:'191px', left:'216px', width:529, height:275}).animate({opacity:finalOpacity, top:'32px', left:'55px', width:275, height:143}, animationTime*.75); // Animate In
		} else {
			$('#sm_logo').css({top:'191px', left:'216px', width:529, height:275}).show().animate({top:'32px', left:'55px', width:275, height:143}, animationTime*.75); // Animate In
		}
	}
	
	eLast.animate({top:postTop+'px', left:postLeft+'px', opacity:postOpacity, width:postWidth, height:postHeight}, animationTime*1.25); // 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
	if($(elementType).is('a')) {
		$('.mask_overlay').attr('href',eFirst.attr('href')).attr('target',eFirst.attr('target'));
		$('.mask_overlay img').attr('alt',eFirst.children('img').attr('alt'));
	}
}

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'));
	}
}

// JavaScript Document
