$(document).ready(function() {
	$("#nav 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
					);
				}
			);
	
	
	// hide all sections
	$(".section:visible").not('.first').hide();
	
	// section clicks
	$('#subnav a').click(function(){
			var theDiv = $(this).attr("href").substring(1);
			theDiv = theDiv.replace(/\/$/, "");
			showSection(theDiv);
			
			$('#subnav a').css('color', '#76746A');
			$('#subnav a').css('background-image', 'url(/images/layout/subnav_bg.gif)');
			$('#subnav a').attr('isSelected', 'false');
			
			$(this).css('color', '#294E4D');
			$(this).css('background-image', 'url(/images/layout/subnav_bg-over.gif)');
			$(this).attr('isSelected', 'true');
			
			
			$('#experienceNav').hide();
			if (theDiv == 'experience') {
				$('#experienceNav').show();
				
				// clear list
				$('#experienceNav a').css('color', '#76746A');
				$('#experienceNav a').attr('isSelected', 'false');
				
				// select first one
				$('#experienceNav').children(':first-child').css('color', '#294E4D');
				$('#experienceNav').children(':first-child').attr('isSelected', 'true');
			}
			
			
			$('#foundersNav').hide();
			if (theDiv == 'founders') {
				$('#foundersNav').show();
				
				// clear list
				$('#foundersNav a').css('color', '#76746A');
				$('#foundersNav a').attr('isSelected', 'false');
				
				// select first one
				//$('#leadershipNav').children(':first-child').css('color', '#294E4D');
				//$('#leadershipNav').children(':first-child').attr('isSelected', 'true');
			}
			
			
			return false;
			
	});
	
	$('#subnav a').hover(function(){
			$(this).css('color', '#294E4D');
			$(this).css('background-image', 'url(/images/layout/subnav_bg-over.gif)')
		}, function(){
			if ($(this).attr('isSelected') != 'true') {
				$(this).css('color', '#76746A');
				$(this).css('background-image', 'url(/images/layout/subnav_bg.gif)')
			}
	
	});
	
	$('#experienceNav').hide();
	
	
	// experienceNav hover
	$('#experienceNav a').hover(function(){
			$(this).css('color', '#294E4D');
		}, function(){
			if ($(this).attr('isSelected') != 'true') {
				$(this).css('color', '#76746A');
			}
	
	});
	
	// experienceNav clicks
	$('#experienceNav a').click(function(){
			var theDiv = $(this).attr("href").substring(1);
			theDiv = theDiv.replace(/\/$/, "");
			showSection(theDiv);
			
			// reset links
			$('#experienceNav a').attr('isSelected', 'false');
			$('#experienceNav a').css('color', '#76746A');
			
			// set selected
			$(this).css('color', '#294E4D');
			$(this).attr('isSelected', 'true');
			
			return false;
	});
	
	
	
	$('#foundersNav').hide();
	
	
	// foundersNav hover
	$('#foundersNav a').hover(function(){
			$(this).css('color', '#294E4D');
		}, function(){
			if ($(this).attr('isSelected') != 'true') {
				$(this).css('color', '#76746A');
			}
	
	});
	
	// foundersNav clicks
	$('#foundersNav a').click(function(){
			var theDiv = $(this).attr("href").substring(1);
			theDiv = theDiv.replace(/\/$/, "");
			showSection(theDiv);
			
			// reset links
			$('#foundersNav a').attr('isSelected', 'false');
			$('#foundersNav a').css('color', '#76746A');
			
			// set selected
			$(this).css('color', '#294E4D');
			$(this).attr('isSelected', 'true');
			
			return false;
	});
	
	
	
});

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