rotateInterval = ""
rotationDuration = 7000;
$(document).ready(function() {
	//Load the slideshow


	//$('#rotation div.panel').hoverIntent(function(){pauseRotation()},function(){playRotation()})
	//$('#controls').fadeOut()
	//$('#rotation').hover(function(){$('#controls').fadeIn()},function(){$('#controls').fadeOut()})
	//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});
	$('#controls .slide').click(function(){rotate(1,$(this).attr('id').split('_')[1])})
	$('#controls #forward').click(function(){rotate(1)})
	
	//Get the first image and display it (gets set to full opacity)
	$('div#rotation div.panel:first').css({opacity: 1.0});
	
	playRotation()
});


function rotate(count,target) {	
	if(target == undefined){
		var target = '';
	}
	//Get the first image
	var current = ($('div#rotation div.show')?  $('div#rotation div.show') : $('div#rotation div.panel:first'));
	
	if(target != '') {
		var next = $('#panel_' + target);
	} else {
		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.panel:first') :current.next()) : $('div#rotation div.panel:first'));
		} else {
			var next = ((current.prev().length) ? ((current.prev().hasClass('show')) ? $('div#rotation div.panel:last') :current.prev()) : $('div#rotation 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);
	
	$('#controls .active').removeClass('active')
	$('#controls #slide_' + $(next).attr('id').split('_')[1]).addClass('active')

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	pauseRotation()
	playRotation()
};

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);
}


