rotateInterval = ""
rotationDuration = 7000;
$(document).ready(function() {	
	//Load the slideshow
	
	//$('#controls').fadeTo(0,.55)
	$('div.imgRotation').hoverIntent(function(){$('#controls').fadeTo('fast',1)},function(){$('#controls').fadeTo('fast',.55)})
		
	$('#rotation div.panel').hoverIntent(function(){pauseRotation()},function(){playRotation()})
	
	$('#controls img.ctrlBtn').click(function(){goToImg($(this))})
	
	//$('#control-left').click(function(){if($('div#rotation div.panel').length > 1) rotate(-1)})
	//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});
	
	playRotation()
});

function rotate(count) {	
	//Get the first image
	var current = ($('div#rotation div.show')?  $('div#rotation div.show') : $('div#rotation 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 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})
	next.addClass('show')
	next.animate({opacity: 1.0}, 1000);
	imgID = next.attr('id').split('panel_'); // panel_2
	imgID = imgID[1]; //2
	$('div#controls img#' + imgID).attr('src','images/rotation_active.gif');
	
	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	cimgID = current.attr('id').split('panel_'); // panel_2
	cimgID = cimgID[1]; //2
	$('div#controls img#' + cimgID).attr('src','images/rotation_inactive.gif');
	current.removeClass('show');
	
	pauseRotation()
	playRotation()
};

function goToImg(obj) {
	//Get the img ID
	var imgID = obj.attr('id');
	var current = $('div#rotation div.show');
	var next = $('div#rotation div#panel_' + imgID);
	
	//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 div.panel').length > 1) clearInterval(rotateInterval)
}

function playRotation()
{
	if($('div#rotation div.panel').length > 1) rotateInterval = setInterval('rotate(1)',rotationDuration);
}

function changeimg(id){
	$('div#controls img').attr('src','images/rotation_inactive.gif');
	$('div#controls img#' + id).attr('src','images/rotation_active.gif');
}


