$(function() {

    $("#page-wrap").append("<img src='/images/ajax-loader.gif' id='ajax-loader' />");
    
    var $el, leftPos, newWidth,
        $mainNav = $("#main-nav"),
        pageOffset = $mainNav.offset().left,
        $mainContent = $("#main-content"),
        URL = '',
        siteURL = "http://" + top.location.host.toString(),
        hash = window.location.hash,
        $ajaxSpinner = $("#ajax-loader");
                
    function resetMagicLine() {
        if ($("li.current_page_item").length > 0) {
            $magicLine
                .width($("li.current_page_item").width())
                .css("left", $("li.current_page_item a").offset().left - pageOffset)
                .data("origLeft", $magicLine.offset().left - pageOffset)
                .data("origWidth", $magicLine.width());
        }
    };
    
    function hashizeLinks() {
        $("a[href^='"+siteURL+"']").each(function() {
            $el = $(this);
            
            if ($.browser.msie) {
                $el.attr("href", "#/" + this.pathname)
                .attr("rel", "internal");
            } else {
                $el.attr("href", "#" + this.pathname)
                 .attr("rel", "internal");
            }
        });
    };
        
    $(window).resize(function() {
        pageOffset = $mainNav.offset().left;
    });
    
    $mainNav.append("<li id='magic-line'></li>");
    
    var $magicLine = $("#magic-line");
    
    resetMagicLine();
        
    $("#main-nav li").find("a").hover(function() {
        $el = $(this);
        leftPos = $el.offset().left - pageOffset;
        newWidth = $el.parent().width();
        
        $magicLine.stop().animate({
            left: leftPos,
            width: newWidth
        });
    }, function() {
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });    
    });
    
    hashizeLinks();            
    $("a[rel='internal']").live("click", function() {
    
    
        $ajaxSpinner.fadeIn();
        $("#content-inside").animate({ opacity: "0.1" });
    
        $el = $(this);
        
        if ( $el.parents("#main-nav").length == 1 ) {  
        
            $("li.current_page_item").removeClass("current_page_item");
            $el.parent().addClass("current_page_item");
            resetMagicLine();
        }  
        
        URL = $(this).attr("href").substring(1);
        URL = URL + " #content-inside";

        $mainContent.load(URL, function() {
            $ajaxSpinner.fadeOut();
            $("#content-inside").animate({ opacity: "1" });
            $("body").removeClass().addClass($el.text());
            hashizeLinks();   
        });
    
    });
    
    if (hash) {
        $ajaxSpinner.fadeIn();
        $("#content-inside").animate({ opacity: "0.1" });
        
        if ($("li.current_page_item").length > 0) {
                
            $(".current_page_item").removeClass("current_page_item");
            $("a[href="+hash+"]").parent().addClass("current_page_item");
        }
        
        hash = hash.substring(1);
        URL = hash + " #content-inside";
        $mainContent.load(URL, function() {
            $ajaxSpinner.fadeOut();
            $mainContent.animate({ opacity: "1" });
            resetMagicLine();
            hashizeLinks();   
        });
    } 

});