$(document).ready(function() {
	$("#nav a, .photoChanger a").hover(
				// over
				function(){
					// store the original image src
					$(this).children(":first-child").attr( "originalsrc" , $(this).children(":first-child").attr("src") );
					
					// add -over to the image unless it is already -over
					if (!$(this).children(":first-child").attr("src").match(/-over/) ) {
						$(this).children(":first-child").attr( "src", $(this).children(":first-child").attr("src").replace(/\./, "-over.") );
					}
					
				},
				
				// out
				function(){
					// replace the original src
					$(this).children(":first-child").attr(
						"src", $(this).children(":first-child").attr("originalsrc")
					);
					
					$(this).children(":first-child").attr(
						"originalsrc", null
					);
				}
			);
	
	
	// hide all sections
	$(".section:visible").not('.first').hide();
	
	$('.sectionChanger a').click(function(){
		var theDiv = $(this).attr("href").substring(1);
		theDiv = theDiv.replace(/\/$/, "");
		showSection(theDiv);
		return false;
	});	

	/*
	$('.projectChanger a').not(".head").click(function(){
		var theDiv = $(this).attr("href").substring(1);
		theDiv = theDiv.replace(/\/$/, "");
		showSection(theDiv);
		$("#headerPhoto").attr("src", "/images/projects/" + theDiv + "1.jpg");
		return false;
	});		
	*/
	
	$('.projectChanger a').not(".head").click(function(){
		var theDiv = $(this).attr("href").substring(1);
		theDiv = theDiv.replace(/\/$/, "");
		showSection(theDiv);
		
		// clear the rotation
		if (typeof(myTimer) !== "undefined") {
			clearTimeout(myTimer);
		}
		
		// start rotation for this project
		photoNumber = 0;
		photoCount = $("#" + theDiv + " .photoChanger a").length;
		rotateImage(theDiv);
		return false;
	});
	

	$('.photoChanger a').click(function(){
	
	
		// clear the rotation
		if (typeof(myTimer) !== "undefined") {
			clearTimeout(myTimer);
		}
	
		var thePhoto = $(this).attr("href").substring(1);
		changePhoto(thePhoto);
		return false;
	});	
	
	// accordian
	$(".projectChanger div").hide();
	$(".projectChanger .head").click(function(){
		$(".projectChanger div:visible").slideUp("fast");
		$(".head img").attr("src", "/images/layout/arrow_right.gif");
		
		$(this).next().slideDown("fast");
		$(this).children("<img>").attr("src", "/images/layout/arrow_down.gif");
		return false;
	});
	
	
});

window.onload = function() {
		// preload
		$("#nav a img").each(
			function() {
				if (!$(this).attr("src").match(/-over/) ) {
					$("<img>").attr( "src", $(this).attr("src").replace(/\./, "-over.") );
				}
			}
		);
}

function showSection(theDiv) {
	$("#sections .section:visible").hide();
	$("#" + theDiv).fadeIn('fast');
}

function changePhoto(thePhoto) {
	$("#headerPhoto").attr("src", "/images/projects/" + thePhoto + ".jpg");
}

function rotateImage(div) {
	photoNumber++;
	if (photoNumber > photoCount) {
		photoNumber = 1;
	}
	changePhoto(div + photoNumber);

	myTimer = setTimeout(function(){rotateImage(div)}, 5000);
}