rotateInterval = ""
rotationDuration = 5000;
$(document).ready(function() {		
	//Load the slideshow

	$('#rotation div.panel').hoverIntent(function(){pauseRotation()},function(){playRotation()})
	$('#controls div.control').click(function(){switchPanel($(this))})

	//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});
	
	setControls()
	playRotation()
});



function switchPanel(obj) {	
	//Get the first image
	var idstr = $(obj).attr('id').split('_')[1]
	var currentPanel = $('#panel_' + idstr)
	if(!($(currentPanel).hasClass('show')))
	{
		var current = $('#rotation div.show');
		var next = $('#panel_' + idstr);

		//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');
		setControls()
		pauseRotation()
		playRotation()
	}
};

function rotate(count) {	
	//Get the first image
	var current = ($('div#rotation div.show')?  $('div#rotation div.show') : $('div#rotation div: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 div:first') :current.next()) : $('div#rotation div:first'));
	} else {
		var next = ((current.prev().length) ? ((current.prev().hasClass('show')) ? $('div#rotation div:last') :current.prev()) : $('div#rotation div: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');
	setControls()
	
};

function pauseRotation()
{
	if($('div#rotation div.panel').length > 1) clearInterval(rotateInterval)
}

function playRotation()
{
	if($('div#rotation div.panel').length > 1) rotateInterval = setInterval('rotate(1)',rotationDuration);
}

function setControls()
{
	var cur = $('#rotation div.show').attr('id').split('_')[1]
	$('#controls div.control').each(function(){
		$(this).removeClass('active');
		$(this).fadeTo(0,.6)
	})
	$('#controls div#control_' + cur).addClass('active')
	$('#controls div#control_' + cur).fadeTo(0,1)
}
