$(document).ready(function () {
     
    //Check if url hash value exists (for bookmark)
    $.history.init(pageload);   
         
    //highlight the selected link
    $('a[href=' + document.location.hash + ']').addClass('selected');
     
    //Seearch for link with REL set to ajax
    $('a[rel=ajax]').click(function () {
         
        //grab the full url
        var hash = this.href;
         
        //remove the # value
        hash = hash.replace(/^.*#/, '');
         
        //for back button
        $.history.load(hash);   
         
        //clear the selected class and add the class class to the selected link
        $('a[rel=ajax]').removeClass('selected');
        $(this).addClass('selected');
         
        //hide the content and show the progress bar
        $('#ajax').hide();
        $('#loading').show();
         
        //run the ajax
        getPage();
     
        //cancel the anchor tag behaviour
        return false;
    }); 
});
     
 
function pageload(hash) {
    //if hash value exists, run the ajax
    if (hash) {
		getPage();
		$("#container").hide();  
		$("#menu").css("z-index","11");
		$("#ajax").css("z-index","10");
		$("#loader").css("z-index","10"); 
	}
	else {
		getPage();
	}
}
         
function getPage() {
     
    //generate the parameter for the php script
	if (document.location.hash=='') var data = 'page=about';
	else var data = 'page=' + encodeURIComponent(document.location.hash.replace(/^.*#/, ''));
		
    $.ajax({
        url: "callbacks.php",  
        type: "GET",        
        data: data,     
        cache: false,
        success: function (html) {  
         
            //hide the progress bar
            $('#loading').hide();   
             
            //add the content retrieved from ajax and put it in the #content div
            $('#ajax').html(html);
             
            //display the body with fadeIn transition
            $('#ajax').fadeIn('slow');       
        }       
    });
}
