$(function() {

    var newHash      = "",
        $mainContentWrapper = $("#main-content"),
		$guts		 = $("#guts"),
        $pageWrap    = $("#page-wrap"),
		$mainContentItself = $("#contenthtml"),
        baseHeight   = 0,
        $el;
        
	var isWebKit = WebKitDetect.isWebKit();

    $pageWrap.height($pageWrap.height());
    baseHeight = $mainContentWrapper.height() - $mainContentItself.height();
    
    $("nav").delegate("a", "click", function() {
        window.location.hash = $(this).attr("href");
        return false;
    });
	
    $(window).bind('hashchange', function(){
    
		newHash = window.location.hash.substring(1);
		
		if (newHash) {
			var oldHeight = $("#main-content").height();
			// attempt to avoid nav jump on first page switch
			//alert("setting main content height to " + oldHeight + "px");
			document.getElementById("main-content").style.height = oldHeight + "px";
					
			
			//$("#main-content").style.height = oldHeight;
			//document.getElementById("main-content").style.height = oldHeight + "px";
			//alert("Set content area height to: " + oldHeight + "px");  // set fixed height during initial transition
			
			if ( true )  {    //!isWebKit
				
				// fade out the current content
				$("#contenthtml").fadeOut(500,function() {
					
					// load new content
					$("#guts").load(newHash + " #contenthtml", function() {
						
						// fade in the newly-loaded content (doesn't work??)
						$("#contenthtml").fadeIn(10000, function() {
							
							var newHeight = $("#contenthtml").height();
                            $mainContentWrapper.animate({
                                height: (48+newHeight) + "px" //$mainContentItself.height() + "px"
                            });

						});
					});
				});
			} //if (not WebKit)
			else {
				// for webkit browsers, just load the new content and avoid animation
				//alert("webkit browser!");
				$("#main-content").load(newHash + " #guts");
			}
			
			// highlight nav link corresponding to current page
			$("nav a").removeClass("current");
            $("nav a[href="+newHash+"]").addClass("current");

		}; //if (newHash)

	});

    $(window).trigger('hashchange');

});
