$(document).ready(function() {
  var cAnchor = document.location.hash;
  if (cAnchor) {
    cAnchor = cAnchor.replace("#","");
    $("#main").hide();
    $.ajax({
        method: "get", 
        url: "/load.php", 
        data: "p="+cAnchor,
        beforeSend: function() {$("#loading").show(); $(".loading").fadeIn("fast");},
        complete: function() {$("#loading").hide(); $(".loading").hide();},
        success: function(html){
          $("#main").html(html);
          $("#main").show();          
          $('#main a[rel*=external]').click( function() {
              window.open(this.href);
              return false;
          });
        }
    });
    $("#menu a").removeClass("selected");
    $("a[href*='"+cAnchor+"']").addClass("selected");
    $(this).addClass("selected");    
  }
  
  $('#menu ul li a').click(function() {
     var surl = $(this).attr("href");
     surl = surl.replace(/\//g,"");
     var cAnchor = document.location.hash;
     if (!(cAnchor == "#"+surl)) {
       $("#main").hide();
       $.ajax({
           method: "get", 
           url: "/load.php", 
           data: "p="+surl,
           beforeSend: function() {$("#loading").show(); $(".loading").fadeIn("fast");},
           complete: function() {$("#loading").hide(); $(".loading").hide();},
           success: function(html){
              $("#main").html("<div id='fadediv'>"+html+"</div>");
              $("#fadediv").hide();
              $("#main").show();
              $("#fadediv").fadeIn("slow", function() { 
                $("#main").html(html);   
                $('#main a[rel*=external]').click( function() {
                    window.open(this.href);
                    return false;
                });
              });
           }
       });
       $("#menu a").removeClass("selected");
       $(this).addClass("selected");
       window.location.href = "#" + surl;
     }
     return false; // don't follow link when script ran  
  }); 
});

