rotateInterval = ""
rotationDuration = 6000;
$(document).ready(function() {		
	//Load the slideshow
	// Number of rotation panels
	var numPanels = document.getElementsByName("rotation").length;
	
	for(i=1;i<=numPanels;i++){
		//$('div#rotation_' + i + ' div.panel').hoverIntent(function(){pauseRotation()},function(){playRotation()})
		$('div#rotation_' + i + ' div.panel').css({opacity: 0.0});
		$('div#rotation_' + i + ' div.panel:first').css({opacity: 1.0});
	}
	
	$('div.sideFlashRotation').hoverIntent(function(){pauseRotation()},function(){playRotation()})
	//$('#rotation div.panel').hoverIntent(function(){pauseRotation()},function(){playRotation()})
	//Set the opacity of all images to 0
	//This prevents phantom images from displaying during the transition
	//$('div#rotation div.panel').css({opacity: 0.0});
	//Get the first image and display it (gets set to full opacity)
	//$('div#rotation div.panel:first').css({opacity: 1.0});
	
	startRotation(numPanels)
});

function rotate(count) {	
	
	for(i=1;i<=3;i++){
		var current = ($('div#rotation_' + i + ' div.show')?  $('div#rotation_' + i + ' div.show') : $('div#rotation_' + i + ' div.panel:first'));
	
		if(count == 1)
		{	
			//Get next image, when it reaches the end, rotate it back to the first image
			var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotation_' + i + ' div.panel:first') :current.next()) : $('div#rotation_' + i + ' div.panel:first'));
		} else {
			var next = ((current.prev().length) ? ((current.prev().hasClass('show')) ? $('div#rotation_' + i + ' div.panel:last') :current.prev()) : $('div#rotation_' + i + ' div.panel:last'));
		}
		
		//Set the fade in effect for the next image, the show class has higher z-index
		next.css({opacity: 0.0})
		.addClass('show')
		.animate({opacity: 1.0}, 1000);

		//Hide the current image
		current.animate({opacity: 0.0}, 1000)
		.removeClass('show');
	}
	pauseRotation()
	playRotation()
};

function pauseRotation()
{
	/*if($('div#rotation_1 div.panel').length > 1){
		clearInterval(rotateInterval);
	} else if ($('div#rotation_2 div.panel').length > 1){
		clearInterval(rotateInterval);
	} else if ($('div#rotation_3 div.panel').length > 1){
		clearInterval(rotateInterval);
	}*/
	clearInterval(rotateInterval);
}

function playRotation()
{
	if($('div div.panel').length > 1) rotateInterval = setInterval('rotate(1)',rotationDuration);
}

function startRotation(num)
{
	this.num = num;
	if($('div#rotation_1 div.panel').length > 1){
		rotateInterval = setInterval('rotate(1)',rotationDuration);
	} else if ($('div#rotation_2 div.panel').length > 1){
		rotateInterval = setInterval('rotate(1)',rotationDuration);
	} else if ($('div#rotation_3 div.panel').length > 1){
		rotateInterval = setInterval('rotate(1)',rotationDuration);
	}
}
