$(function() {
       
    $("#container").append("<img src='http://www.feelix.co.uk/wp-content/themes/feelix/images/ajax-loader.gif' id='ajax-loader' />"); /* Add Ajax spinner to page */
            
    var $mainContent = $("#container")/* targets the div to refresh */,
        URL = '',
        siteURL = "http://" + top.location.host.toString()/* pulls url into variable */,
        $internalLinks = $("a[href^='"+siteURL+"']")/* targets all internal links */,
        hash = window.location.hash,
        $ajaxSpinner = $("#ajax-loader"),
        $el, $allLinks = $("a");
     	
	function hashizeLinks() {
		// TODO: Replace with this one day:
		//        $("a[href^='"+siteURL+"']:not([class~=lightbox])").each(function() {
        $("a[href^='"+siteURL+"']:not([class=thickbox])").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-menu-item").removeClass("current-menu-item");
        $allLinks.removeClass("current_link");
        URL = $el.attr("href").substring(1);
        URL = URL + " #inner";
        $mainContent.load(URL, function() {
            $el.addClass("current_link").parent().addClass("current-menu-item");
            $ajaxSpinner.fadeOut();
            $mainContent.animate({ opacity: "1" });
            hashizeLinks();   
        });
    });
        
    if ((hash) && (hash != "#/")) {
        $("a[href*='"+hash+"']").trigger("click");
    }

});
