$(document).ready(function()
{
		var masks = $("p.mask").hide();

	$("#middle .project .image").hover(function()
	{
		$(this).find(".mask").stop().fadeIn(500);
	}, function()
	{
		$(this).find(".mask").stop().fadeOut(500);
	});
	
	//Set the correct number of blobs for the controls
	$("#photobanner .inner p img").each(function()
	{
		$("#controls li:first").clone().appendTo("#controls ul");
	});
	
	$("#controls li:first").addClass("current");
	$("#controls li:last").remove();
	$("#controls ul").css("margin-left", ($("#controls ul").width() / 2)*-1)
	
	var interval = setInterval(function()
	{
		$("#next a").click();
	}, 5000);
	
	$("#next a").click(function()
	{
		clearInterval(interval);
		if($("#photobanner .inner p").is(":animated"))
		{
			//alert("animated")
		}
		else
		{
			//alert("not animated");
			var current = $("#controls li").index($("#controls .current"));
			
			if($("#photobanner .inner p").css("left") != (((( $("#photobanner .inner p img:first").width() * $("#photobanner .inner p img").size() )*-1)+$("#photobanner .inner p img:first").width()) + "px") )
			{
				$("#photobanner .inner p").animate({ left: "-=" + $("#photobanner .inner p img:first").width() }, 500);
				$("#controls li").removeClass("current").eq((current+1)).addClass("current");
				interval = setInterval(function()
				{
					$("#next a").click();
				}, 5000);
			}
			else
			{
				$("#controls li").removeClass("current").eq(0).addClass("current");
				$("#photobanner .inner p img:first").clone().appendTo("#photobanner .inner p");
				$("#photobanner .inner p").animate({ left: "-=" + $("#photobanner .inner p img:first").width() }, 500, function()
				{
					$("#photobanner .inner p img:last").remove();
					$("#photobanner .inner p").css("left", 0);
					interval = setInterval(function()
					{
						$("#next a").click();
					}, 5000);
				});
			}
		}
		return false;
	});
	
	$("#prev a").click(function()
	{
		clearInterval(interval);
		if($("#photobanner .inner p").is(":animated"))
		{
			//alert("animated")
		}
		else
		{
			var current = $("#controls li").index($("#controls .current"));
			
			//alert("not animated");
			if($("#photobanner .inner p").css("left") != "0px")
			{
				//alert("normal")
				$("#photobanner .inner p").animate({ left: "+=" + $("#photobanner .inner p img:first").width() }, 500);
				$("#controls li").removeClass("current").eq((current-1)).addClass("current");
				interval = setInterval(function()
				{
					$("#next a").click();
				}, 5000);
			}
			else
			{
				$("#controls li").removeClass("current").eq($("#photobanner .inner p img").size()-1).addClass("current");
				$("#photobanner .inner p img:first").clone().appendTo("#photobanner .inner p");
				$("#photobanner .inner p").css("left", (((( $("#photobanner .inner p img:first").width() * $("#photobanner .inner p img").size() )*-1)+$("#photobanner .inner p img:first").width())));
				$("#photobanner .inner p").animate({ left: "+=" + $("#photobanner .inner p img:first").width() }, 500, function()
				{
					$("#photobanner .inner p img:last").remove();
					interval = setInterval(function()
					{
						$("#next a").click();
					}, 5000);
				});
			}
		}
		return false;
	});
	
	$("#controls li a").click(function()
	{
		clearInterval(interval);
		var clicked = $("#controls li a").index($(this));
		var current = $("#controls li a").index($("#controls .current a"));
		var total = $("#controls li").size();
		
		if(clicked == current)
		{
			
		}
		else if((clicked == (current+1)) || ((current == (total-1)) && (clicked == 0)))
		{
			$("#next a").click();
		}
		else if((clicked == (current-1)) || ((clicked == (total-1)) && (current == 0)))
		{
			$("#prev a").click();
		}
		else
		{
			$("#photobanner .inner p").animate({ left: "-=" + ($("#photobanner .inner p img:first").width()*(clicked-current)) }, 500);
			var currentBlob = $("#controls li").index($("#controls .current"));
			$("#controls li").removeClass("current").eq(currentBlob + (clicked-current)).addClass("current");
			interval = setInterval(function()
			{
				$("#next a").click();
			}, 5000);
		}
		return false;
	});
});
