$(document).ready(function(){  
   var startingPointOfAnimation = $('.navigationBg').css('left').replace('px','');	 
   var recentHash = "";   
   var loop = setInterval(initialiseStateFromURL, 500);
   if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	   var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	   if (ieversion<=6){
	      return false;
	   }   
   }
	$(document).bind("ajaxSend", function(){
		$(".tooltip").fadeOut();
	})   
   function getCurrentURL(){
	 if(window.history.pushState && window.location.hash == ''){
		 return window.location.pathname;
	 }else{
		 return window.location.hash;		 
	 }	   
   }   
   function initialiseStateFromURL() {
		if (getCurrentURL()==recentHash) {
			//Put code that changes the page title as well as the class on the content encapsulation
			return; // Nothing's changed since last polled.
			
		}else{
			var storeHash =  getCurrentURL();
			storeHash = storeHash.replace('#!', '');
			bringInPage(storeHash);
			whereAmI();          
		}
   }  
   function handleURL(element){
	    dontOverRidePage = false;
		baseUrl = $(element).attr('href').substring(0, $(element).attr('href').lastIndexOf("/") + 1);
		ajaxoveride =  $(element).attr("href").replace(baseUrl, "");
		
		bringInPage(ajaxoveride);
		if(window.history.pushState){
			// HTML 5 change browser View State
			window.history.pushState(ajaxoveride,ajaxoveride,ajaxoveride);		
		}else{
			window.location.hash = '!'+ajaxoveride;
			
		}
   }
	$('.hijackAjax a, .ajaxLink').live('click',function(){	
		handleURL($(this));		
		return false;
	});
	function bringInPage(pageURL){
		 if(dontOverRidePage == true){
			 return
		 }
		 $('#main-page').animate({"opacity": "hide",'filter': '',"top":"-760px"},500,loadContent);
		 $('.wrapper').append('<div id="ajax-loader"></div>');
		 $('#ajax-loader').fadeIn('normal');
		 function loadContent() {
			 $('#main-page').load(pageURL+'&ajaxpage=true&rand='+Math.floor(Math.random()*11),'',showNewContent);
		 }
		 function showNewContent() {
			 $('#ajax-loader').fadeOut('normal', function(){$('#ajax-loader').remove()});
			 $('#main-page').animate({"opacity": "show",'filter': '',"top":"0px"},500);	
			 recentHash = getCurrentURL();				 		 
		 }		 
	}
	function navigationAnimation(element){
		var totalSize = $(element).parent().parent().children('li').length;
		var elementLocation = $(element).parent().index();
		var amountToMove = 43;
		$('.navigationBg').animate({'filter': '',"left": elementLocation*amountToMove+Number(startingPointOfAnimation)+"px"},1000);		
	}
	$('.navigation .right a').click(function(){
		handleURL($(this));
		return false;
	});		
	$('.navigation .left a').click(function(){
		handleURL($(this));
		return false;
	});				 
	 //Function to animate slider based on hash value in url
	 function whereAmI(){	 
	 	if(window.history.pushState){
			var string1 = window.location.pathname;
		}else{
			var string1 = window.location.hash;
		}
		formattedURL = ((string1).toString()).replace(/[^a-zA-Z 0-9]+/g,'');
		if(formattedURL == '' || formattedURL == 'home'){	
			formattedURL = 'home';
			$('.menu li').removeClass('selected');
			handleFrontPage()
		}else{
			$(".footer").delay(500).animate({"bottom":"0px"},1000);					
		}
		$('.navigation li a').each(function(){
			var string2 = $(this).attr('href');
			if(string2.indexOf(formattedURL) != -1){	
				$('.menu li').removeClass('selected');
				var menuItem = $(this).parent().index();
				$($('.menu li').get(menuItem)).addClass('selected');			
				$('.navigation .right a').attr('href',$(this).parent().next().children('a').attr('href'));
				$('.navigation .left a').attr('href',$(this).parent().prev().children('a').attr('href'));
				navigationAnimation($(this));	
			}
		});
	 }

	$('.dialogLink').live('click',function(){
		$('#dialog').dialog('open');
		ajaxoveride = $(this).attr('href');		
		$('.overlay-content').load(ajaxoveride+'&ajaxpage=true&rand='+Math.floor(Math.random()*11),'');
		return false;				
	});			 
});

