$(function() {

    var $mainContent = $("#main"),
        URL = '',
        jabg = '',
        siteURL = "http://" + top.location.host.toString(),
        $internalLinks = $("a[href^='"+siteURL+"']"),
        hash = window.location.hash,
        //$ajaxSpinner = $("#ajax-loader"),
        $el, $allLinks = $("a");
        
    function hashizeLinks() {
        $("a[href^='"+siteURL+"']").each(function() {
            $el = $(this);
            
            // Hack for IE, which seemed to apply the hash tag to the link weird
            if ($.browser.msie) {
                $el.attr("href", "#/" + this.pathname)
                .attr("rel", "internal");
            } else {
                $el.attr("href", "#" + this.pathname)
                 .attr("rel", "internal");
            }
        });
    };
    
    hashizeLinks();  
        
    $("a[rel='internal']").live("click", function() {
        //$ajaxSpinner.fadeIn();
        $mainContent.animate({ opacity: "0.1" });
        $el = $(this);
        //$(".current_page_item").removeClass("current_page_item");
        //$allLinks.removeClass("current_link");
        URL = $el.attr("href").substring(1);
        jabg = $el.attr("id");
        URL = URL + " #content";
        $mainContent.load(URL, function() {
            $("body").removeClass().addClass(jabg);
            //$ajaxSpinner.fadeOut();
            $mainContent.animate({ opacity: "1" });
            hashizeLinks(); 
            $("a.audiosample").click( changeTrack );
	
			function changeTrack(e) {
				$("#trackname").text($(this).text());
				$("#jquery_jplayer").jPlayer("setFile", $(this).attr("href")).jPlayer("play");
				$(this).blur();
				return false;
			}  
        });
    });
    
    
    if ((hash) && (hash != "#/")) {
        $("a[href*='"+hash+"']").trigger("click");
    }

});
