<!-- By Dylan Wagstaff, http://www.alohatechsupport.net -->
//Edited by Lonita Dueck.
//Added if statment to check for more than one image before proceeding. 
//changed setInterval to setTimeout to fix jQuery animate() queueing animations while on another tab until the tab is re-focused.
//http://api.jquery.com/animate/#notes-0

function theRotator(){
	$('div.rotator ul li').css({opacity: 0.0});
	$('div.rotator ul li:first').css({opacity: 1.0});
	//Added if statement to only run the rotator if there is more than one image
	if ($('div.rotator ul li').length > 1) {
  	//Changed setInterval to setTimeout. The jQuery animate() function does not correctly handle the setInterval animation queue
		setTimeout('rotate()', 4000);
	}
}

function rotate() {	
	var current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));
    if ( current.length == 0 ) current = $('div.rotator ul li:first');
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	var nextId = next.find('img').attr('class');
	$('body').attr('id',nextId);
	current.animate({opacity: 0.0}, 1000, function(){setTimeout('rotate()', 4000);})	.removeClass('show');
	//put a setTimeout in the animate() callback to loop the animation. This calls rotate() only after the current animation is finished.
	//This fixes the issue with the animation pausing and then displaying all paused animations rapidly to catch up with the current animation.
	
};


$(document).ready(function() {		
	//Load the slideshow
	theRotator();
	$('div.rotator').fadeIn(1000);
  $('div.rotator ul li').fadeIn(1000); // tweek for IE
});

