$(document).ready(function(){
	$("#home-feature li").click(function(){
		stoprotate = true;
		
		if($(this).hasClass('show'))
		{
			return;
		}
		
		var file = $(this).attr("id");
		
		$("#home-feature .image img").each(function(){
			if($(this).attr("src") == file)
			{
				if($(this).hasClass("active"))
				{
					return;
				}
				$("#home-feature .image img.active").fadeOut().addClass("old").removeClass("active");
				$(this).addClass("active").removeClass("old");
				$(this).fadeIn();
			}
		});
		
		$("#home-feature li.show").removeClass("show");
		$(this).addClass("show");
	});
	
	theRotator();
});

var zindex = 0;
var stoprotate = false;
var count = 1;

function theRotator()
{
	$("#home-feature li").each(function(){
		if(count > 1)
		{
		--zindex;
		var file = $(this).attr("id");
		$("#home-feature .image").append("<img class=\"old\" src=\""+file+"\" style=\"z-index: "+zindex+"; display: none;\" alt=\"\" />");
		}
		++count;
	});
	
	$("#home-feature li:first").addClass("show");
	
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',5000);
}

function rotate() {
	if(stoprotate == false)
	{
	//Get the first image
	var current = ($('#home-feature li.show')?  $('#home-feature li.show') : $('#home-feature li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#home-feature li:first') :current.next()) : $('#home-feature li:first'));	
	
	var file = next.attr("id");
		
	$("#home-feature .image img").each(function(){
		if($(this).attr("src") == file)
		{
			if($(this).hasClass("active"))
			{
				return;
			}
			$("#home-feature .image img.active").fadeOut().addClass("old").removeClass("active");
			$(this).addClass("active").removeClass("old");
			$(this).fadeIn();
		}
	});
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.addClass('show');

	//Hide the current image
	current.removeClass('show');
	}
};
