$(window).load(
    function() {
		// getPage();
	}
);

$(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
	$('#topmenu ul a', '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
	 	$('this').removeClass('selected');
	 	$('this').addClass('selected');
	 	
	 	//hide the content and show the progress bar
	 	$('#content').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(document.location.hash) getPage();    //if (hash) 
}

function getPage() {
	$('#loading').fadeIn();
	//generate the parameter for the php script
	var loadpage = document.location.hash;
	
	// Default to main page if none specified
	if (!loadpage) loadpage = "#main";
	
	loadpage = loadpage.replace(/^.*#/, '');
	var data = 'page=' + encodeURIComponent(loadpage);
	$.ajax({
		url: "loader.php",	
		type: "GET",		
		data: data,		
		cache: false,
		success: function (html) {	

			_gaq.push(['_trackPageview', loadpage+'.inc.php']);
			
			//hide the progress bar
			$('#loading').fadeOut();	

			//check if header is big
			if( loadpage != "main" && $("#logo").hasClass("big") ) {
				$('#logo').slideUp(1000);
				$('#logo').queue(function() {
					$(this).removeClass('big');
			 		$(this).addClass('small');
					$(this).dequeue();
					});
				$('#bg_lines').animate({top:"-50px"},'slow');
				$('#logo').slideDown('slow');
			}

			else if( loadpage == "main" && $("#logo").hasClass("small") ) {
				$('#logo').slideUp('slow');
				$('#logo').queue(function() {
					$(this).removeClass('small');
			 		$(this).addClass('big');
					$(this).dequeue();
					});
				$('#bg_lines').animate({top:"0px"},'slow');
				$('#logo').slideDown(1000);
			}
			
			//add the content retrieved from ajax and put it in the #content div
			$('#content').html(html)
			
			//display the body with fadeIn transition
			$('#content').fadeIn('slow');
			
			$("#watch").animate({scrollTop : 500},'slow');

		}		
	});
}
