$(document).ready(function() {
	$("#nav a, a.teaser, #subnav a, .sectionLinks 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
					);
				}
			);
	
	
	var section = location.hash.substring(1)
	section = section.replace(/\/$/, "");

	if (section) {
		// hide all sections except selected
		$(".section:visible").not('#' + section).hide();

		// set selected section link seleceted
		if ($(".sectionLinks").length) {
			$(".sectionLinks a[href='#" + section + "/']").children(":first-child").attr( "src", $(".sectionLinks a[href='#" + section + "/']").children(":first-child").attr("src").replace(/\./, "-over.") );
		}

	} else {
	
		// hide all sections except first
		$(".section:visible").not('.first').hide();

		// set first section link seleceted
		if ($('.sectionLinks').length) {
			$('.sectionLinks').children(":first-child").children(":first-child").attr( "src", $('.sectionLinks').children(":first-child").children(":first-child").attr("src").replace(/\./, "-over.") );
		}		
	}
	
	// section clicks
	$('.sectionLinks a').click(function(){
			// show the section
			var theDiv = $(this).attr("href").substring(1);
			theDiv = theDiv.replace(/\/$/, "");
			showSection(theDiv);

			// reset
			$('.sectionLinks a').each(function() {
				$(this).children(":first-child").attr( "src", $(this).children(":first-child").attr("src").replace(/-over\./, ".") );
			});
			// set selected
			$(this).children(":first-child").attr( "src", $(this).children(":first-child").attr("src").replace(/\./, "-over.") );
			$(this).children(":first-child").attr( "originalsrc" , $(this).children(":first-child").attr("src") );
			
			
	});
	
	
	// section text clicks
	$('.sectionTextLinks a').click(function(){
			// show the section
			var theDiv = $(this).attr("href").substring(1);
			theDiv = theDiv.replace(/\/$/, "");
			showSection(theDiv);
	
	});
	


});

window.onload = function() {
		// preload
		$("#nav a img, a.teaser img, #subnav a img, .sectionLinks 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).show();
	}
