// PageLoad function
// Necessite le ready.js inititialisant le chargement de page

// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser

var g_loading_timer = 0;
var g_loading_count = 0;
var g_transp = 0;

function pageload(hash) {
	//alert("pageload: " + hash);
	// hash doesn't contain the first # character.
			

	if(hash && hash!="") {
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like aao.
			hash = encodeURIComponent(hash);
		}	
		
		var url = hash;
		//alert(hash);			
		var reg=new RegExp("(fr|gb|en|it|de|nl)([^\/]+)/?(.*)","gi");
		//alert(reg);	
		var langue = url.replace(reg, "$1");
		//alert(langue);	
		var num_page = url.replace(reg, "$2");
		
		var variables="langue="+langue;
		variables=variables+"&num_page="+num_page;
		variables=variables+"&email="+num_page;
		variables=variables+"&sid="+Math.random();
		
		var recup_variables = url.replace(reg, "$3");
		var l_variables=recup_variables.split("/");
		//alert("variables: " +variables);
		var nb_variables= l_variables.length;
		if(nb_variables>1){
			for(i=0;i<nb_variables;i++){
				variables=variables+"&"+l_variables[i]+"="+l_variables[i+1];
				i++;
			}
		}
		
	} else {
		var reg=new RegExp("(.*)/(fr|gb|en|it|de|nl)/(.*)-(.*).html","gi");
		var url = location.href;
		var langue = url.replace(reg, "$2");
		if(typeof(langue)=="undefined" || langue=="" || langue==url) langue = "fr";
		var num_page = url.replace(reg, "$4");
		if(typeof(num_page)=="undefined" || num_page=="" || num_page==url) num_page = "00";
		var variables="num_page="+num_page;
		
		variables=variables+"&langue="+langue;
		variables=variables+"&sid="+Math.random();			  
	}
	

			if(typeof(g_image_loader)=="boolean" && g_image_loader==true){
				
				$('#fancybox-overlay').css({
					'background-color':'#fff',
					'opacity':'0',
					'display':'block'
				});
				
				$('#fancybox-loading').css({
					'display':'block'
				});
				
				g_loading_timer = setInterval(function(){
					var pos = -g_loading_count*40;
					$('#fancybox-loading div:eq(0)').css({'top':pos+'px'});
					$('#fancybox-overlay').css({
						'opacity':g_transp
					});
					g_loading_count++;
					g_transp += 0.01;
					if(g_transp>=0.5)g_transp = 0.5;
					if(g_loading_count==12)g_loading_count=0;
				},100);
			}
			
			
	$.ajax({ 
		type: "GET", 
		url: "/moteurajax/affichage/affiche_page_beta_v02.asp", 
		data: variables, 
		cache: false,
		success: function(retour){
			if(typeof(u_ip)!="undefined" && (u_ip=="58.244.253.78" || u_ip=="90.10.110.59") ){
				//prompt('',retour);
			}
			
			$("#page").empty().html(retour);
			var id_page = "#"+langue+num_page;
			if(ga_exist==true){_gaq.push(['_trackPageview',id_page]);}
			if(window.page_chargee){page_chargee(langue,num_page);}
			if(typeof(g_image_loader)=="boolean" && g_image_loader==true){
				$('#fancybox-overlay').css({
					'opacity':'0.5'
				});
				$('#fancybox-overlay').animate({
					'opacity':'0'
				},2000,function(){
					$(this).css({'display':'none'});
				});
				$('#fancybox-loading').css({
					'display':'none'
				});
				clearInterval(g_loading_timer);
				g_loading_count = 0;
			}
		},
		error: function(request,error) {
		  if (error == "timeout") {
		   $("#page").append("Le temps de la requête est dépassé, merci de relancer la page");
		  }
		  else {
		   $("#page").empty().append("ERREUR 1: " + "<br>" + request.responseText + "<br>" + error);
		  }
		  
		  
		  $('#fancybox-overlay').css({
					'opacity':'0.5'
				});
				$('#fancybox-overlay').animate({
					'opacity':'0'
				},2000,function(){
					$(this).css({'display':'none'});
				});
				$('#fancybox-loading').css({
					'display':'none'
				});
				clearInterval(g_loading_timer);
				g_loading_count = 0;
		 } 
	});
}


// Fonction de chargement des pages dynamique
function ajax_page(v_url,v_div,v_data,v_type) {	
	v_data=v_data+"&sid="+Math.random();
	
	$.ajax({ 
		type: v_type, 
		url: v_url, 
		data: v_data, 
		dataType: "html",
		cache: false,
		success: function(retour){ 
			$(v_div).empty().append(retour); 
		},
		error: function(request,error) {
		 // $("#recipient").addClass("hide");
		  if (error == "timeout") {
		   $(v_div).append("Le temps de la requête est dépassé, merci de relancer la page");
		  }
		  else {
		   $(v_div).empty().append("ERREUR 2: " + error+"on <br>"+v_url);
		  }
		 } 
	});
	
}



