$(document).ready(function() {
	$("#nav a, #peopleNav a, a.hover").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
					);
					
				}
			);
	
	
	// accordian
	$("#nav div").hide();
	$(".head").click(function(){
		
		// hide all
		//$("#nav div:visible").hide("fast");
	
		// show selected
		$(this).next().toggle("fast");
		
		return false;
	});

		// open current section
		if (window.section !== undefined && $("#nav_" + section)) {
		
			if (!$("#nav_" + section).children(":first-child").attr("src").match(/-over/) ) {
				$("#nav_" + section).children(":first-child").attr("src", $("#nav_" + section).children(":first-child").attr("src").replace(/\./, "-over."));
			}

			$("#nav_" + section).next().show();
		}

		if (window.subnav !== undefined && $("#subnav_" + subnav)) {
		
			if (!$("#subnav_" + subnav).children(":first-child").attr("src").match(/-over/) ) {
				$("#subnav_" + subnav).children(":first-child").attr("src", $("#subnav_" + subnav).children(":first-child").attr("src").replace(/\./, "-over."));
			}

		}


	// add peopleNav listner
	$("#peopleNav a").click(function() {
		var theDiv = $(this).attr("href").substring(1);
		theDiv = theDiv.replace('\/','');
		showSection(theDiv);


		// reset all subnav image to the dormant state	
		$("#peopleNav").children("a").each( function() {
				$(this).children(":first-child").attr( 
						"src" , $(this).children(":first-child").attr("src").replace(/-over./, ".")
				)
			}
		);
			
		// set the hot state for the salected subnav
		// remember the pagesrc for the hover out
	
		if (!$(this).children(":first-child").attr("originalsrc").match(/-over/) ) {
		$(this).children(":first-child").attr( 
			{
				"originalsrc" : $(this).children(":first-child").attr("originalsrc").replace(/\./, "-over."),
				"src" : $(this).children(":first-child").attr("src").replace(/\./, "-over.")
			}
		);
		}
		
		
	});
	
	
	var hashClean = location.hash.substr(1);
	hashClean = hashClean.replace('\/','');
	
	if (hashClean) {
		$("#" + hashClean).show();
	} else {
		$("#sections .first").show();
	}
	
});

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


function showSection(theDiv) {
	$("#sections .section:visible").hide();
			
	// show the subpage
	$("#" + theDiv).fadeIn("slow");
}