var normload = true;

$(function () {

    //Check if url hash value exists (for bookmark)
    $.history.init(pageload);  
	
	//dehightlight other links
	if(document.location.hash.replace(/^.*#/, '') != '')
		$('a[rel=ajax]').parent('li').removeClass('active');
  
    //highlight the selected link
    $('a[href='+document.location.hash.replace(/^.*#/, '') + ']').parent('li').addClass('active');
	
	if(document.location.hash.replace(/^.*#/, '') == '' && document.location == 'http://merkcollectors.com/')
		$('#header .center li:eq(0)').addClass('active');

    //Seearch for link with REL set to ajax
    $('a[rel=ajax]').click(function (e) {
	
		e.preventDefault();
		var old_hash = document.location.hash.replace(/^.*#/, '');
		
		normload = false;
         
        //grab the full url
        var hash = $(this).attr('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]').parent('li').removeClass('active');
        $(this).parent('li').addClass('active');
         
        //run the ajax
		if(old_hash != hash)
			getPage();
		
        //cancel the anchor tag behaviour
        return false;
    
	});
    
});
 
function pageload(hash) {

    //if hash value exists, run the ajax
    if(hash && normload) 
		getPage();   

}
         
function getPage() {

    //generate the parameter for the php script    
    var data = 'section=' + document.location.hash.replace(/^.*#/, '');
    
    $.ajax({
        url: "php/loader.php", 
        type: "GET",       
        data: data,    
        cache: false,
        success: function (html) {

			$('#content').fadeOut('fast', function () {
             
				//add the content retrieved from ajax and put it in the #content div
				$('#content').html(html);
				 
				//display the body with fadeIn transition
				$('#content').fadeIn('fast');
				
				//activate de slider
				$(function(){
					if($('#slider').length > 0)
						$("#slider").codeslide({
											speed:			500,
											type:			"fade",
											pageholder:		"#slider_buttons",
											pageclass:		"pager_but",
											pageactive:		"active"
											});
										
				});

			});
		}			
    });
	
}
