var numCases = 0;
var current = 0;
var currentCase;
var currentTitle;
var cases;
var timer;
var demotimer;

$(document).ready(function()
{
	numCases = $("div#case_highlight img.caseimage").length;
	current = Math.floor(Math.random() * numCases);
	
	$("div#case_highlight img#navleft").bind("click", navLeft);
	$("div#case_highlight img#navright").bind("click", navRight);
	$("div.casetitle").hoverIntent(casetitleOver, casetitleOut);
	
	timer = new Timer(10000, navRight);
	timer.startTimer();
	demotimer = new Timer(1000, caseDemo);
	demotimer.startTimer();
	
	function navLeft()
	{
		timer.stopTimer();
		
		currentTitle.css("height", "30px");
		currentTitle.css("top", "230px");
		
		var previousCase = currentCase;
		var previousTitle = currentTitle;
		if(current == 0)
		{
			current = ($("div#case_highlight img.caseimage").length) - 1;
		}
		else
		{
			current = current - 1;
		}
		currentCase = $("div#case_highlight img#case-" + current);
		currentTitle = $("div#case_highlight div#casetitle-" + current);
		
		previousCase.fadeOut(2000);
		previousTitle.fadeOut(2000);
		
		currentTitle.css("display", "block");
		currentCase.fadeIn(2000);
		
		previousCase.css("display", "none");
		previousTitle.css("display", "none");
		
		timer.startTimer();
	}
	
	function navRight()
	{
		timer.stopTimer();
		
		currentTitle.css("height", "30px");
		currentTitle.css("top", "230px");
		
		var previousCase = currentCase;
		var previousTitle = currentTitle;
		if(current >= ($("div#case_highlight img.caseimage").length) - 1)
		{
			current = 0;
		}
		else
		{
			current = current + 1;
		}
		currentCase = $("div#case_highlight img#case-" + current);
		currentTitle = $("div#case_highlight div#casetitle-" + current);
		
		previousCase.fadeOut(2000);
		previousTitle.fadeOut(2000);
		
		currentTitle.css("display", "block");
		currentCase.fadeIn(2000);
		
		previousCase.css("display", "none");
		previousTitle.css("display", "none");
		
		timer.startTimer();
	}
	
	function casetitleOver() { timer.stopTimer(); demotimer.stopTimer(); $(this).animate({ height: "100px", top: "160px" }, 1000); }
	function casetitleOut() { $(this).animate({ height: "30px", top: "230px" }, 1000); timer.startTimer(); }
	
	function caseDemo()
	{
		$(currentTitle).animate({ height: "100px", top: "160px" }, 1000);
		demotimer.stopTimer();
		demotimer = new Timer(2000, function() { $(currentTitle).animate({ height: "30px", top: "230px" }, 1000); demotimer.stopTimer(); });
		demotimer.startTimer();
	}
	
	currentCase = $("div#case_highlight img#case-" + current);
	currentTitle = $("div#case_highlight div#casetitle-" + current);
	currentTitle.css("display", "block");
	currentCase.fadeIn("slow");
});