$(document).ready(function() {
	$("#nav a.nav, #topNav a, #servicesNav 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").match(/-active/)) {
						$(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
					);
				}
			);
	
	
	$('#servicesNav a').click(function(){
		var theDiv = $(this).attr("href").substring(1);
		showSection(theDiv);
		
		// reset nav		
		$('#servicesNav a').each(function() {
			$(this).children(":first-child").attr("src", 
				$(this).children(":first-child").attr("src").replace(/-active/, "")   
			);

		});
		
		// set active
		if ($(this).children(":first-child").attr("src").match(/-over/) ) {
			$(this).children(":first-child").attr("src", 
				$(this).children(":first-child").attr("src").replace(/-over/, "-active")   
			);
		} else {
			$(this).children(":first-child").attr("src", 
				$(this).children(":first-child").attr("src").replace(/\./, "-active.")   
			);
		}

		$(this).children(":first-child").attr("originalsrc", 
			$(this).children(":first-child").attr("src") 
		);
		
		return false;
		
	});
	
	$('#peopleThumbs a').click(function(){
		var theDiv = $(this).attr("href").substring(1);
		showSection(theDiv);
			
		return false;
		
	});

	$("#sections .section:visible").not(".first").hide();
	
	// show initial section
	//initSection();
	
	
});

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


function showSection(theDiv) {
	$("#sections .section:visible").hide();
	$("#" + theDiv).slideDown('fast', initsIFR);
	
	// hilight subnav
	$("#subnav a").each(function() {
			$(this).removeClass('active');
		});
	$('#subnav_' + theDiv).addClass('active');

}


function getQueryVariable(variable) {
	var query = window.location.search.substr(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	} 
}

function initSection() {

	var hashClean = location.hash.substr(1);
	hashClean = hashClean.replace(/\/$/, "");
	var query = getQueryVariable("section");
	
	if (hashClean && document.getElementById(hashClean)) {
		showSection(hashClean);
	} else if (query && document.getElementById(query)) {
		showSection(query);		
	} else {
		// find the first section div
		if ($("#sections").length) {
			showSection( $("#sections").children(":first-child").attr("id"), "first" );	
		}
	}
	
}



