$(document).ready(function(){
	
	fixHeight();

	// center vertically
	if ($("#scroll").height() > 510 ) {
		$("#content").not(".blog").css('margin-top', ( $("#scroll").height() / 2 ) -255 + 'px' );
	}


	$("#nav a, #subnav a").hover(
		// over
		function(){

			// store the original image src
			$(this).children(":first-child").data( "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").data("originalsrc")
			);
			
			$(this).children(":first-child").data(
				"originalsrc", null
			);
		}
	);
	
	$("a.fade, .blog a").hover(
		function() {
			$(this).stop().animate({color:  "#8A2C0F"}, 250);
		}, 
		
		function() {
			$(this).stop().animate({color:  "#9e9ea0"}, 250);
		}
	);
	
	$("ul.sf-menu").superfish({ 
            animation: {height:'show'},   // slide-down effect without fade-in 
            delay:     500 ,              // 1.2 second delay on mouseout 
			autoArrows: false
        }); 
	
	
});

var resizeTimer = null;
$(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(fixHeight, 100);
});

function fixHeight() {

	$("#scroll").css('height', 
		$(window).height() 
			- $("#header").height() 
			- $("#nav").height()  
			- $("#subnav").height()  
			- $("#shadow").height() 
			- $("#footer").height() 
			+ 25); // padding

	var top = $("#header").height()
			+ $("#nav").height()  
			+ $("#subnav").height()  
			+ $("#shadow").height();

	if ($("#shadow").height() > 6) {
		top -= 20;
	}
			
	$("#scroll").css('top', top );
}


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