

/*
 * jQuery history plugin
 * 
 * sample page: http://www.mikage.to/jquery/jquery_history.html
 *
 * Copyright (c) 2006-2009 Taku Sano (Mikage Sawatari)
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Modified by Lincoln Cooper to add Safari support and only call the callback once during initialization
 * for msie when no initial hash supplied.
 */


jQuery.extend({
	historyCurrentHash: undefined,
	historyCallback: undefined,
	historyIframeSrc: undefined,
	historyNeedIframe: jQuery.browser.msie && (jQuery.browser.version < 8 || document.documentMode < 8),
	
	historyInit: function(callback, src){
		jQuery.historyCallback = callback;
		if (src) jQuery.historyIframeSrc = src;
		var current_hash = location.hash;
		
		jQuery.historyCurrentHash = current_hash;
		if (jQuery.historyNeedIframe) {
			// To stop the callback firing twice during initilization if no hash present
			if (jQuery.historyCurrentHash == '') {
				jQuery.historyCurrentHash = '#';
			}
		
			// add hidden iframe for IE
			jQuery("body").prepend('<iframe id="jQuery_history" style="display: none;"'+
				' src="javascript:false;"></iframe>'
			);
			var ihistory = jQuery("#jQuery_history")[0];
			var iframe = ihistory.contentWindow.document;
			iframe.open();
			iframe.close();
			iframe.location.hash = current_hash;
		}
		else if (jQuery.browser.safari) {
			// etablish back/forward stacks
			jQuery.historyBackStack = [];
			jQuery.historyBackStack.length = history.length;
			jQuery.historyForwardStack = [];
			jQuery.lastHistoryLength = history.length;
			
			jQuery.isFirst = true;
		}
		if(current_hash)
			jQuery.historyCallback(current_hash.replace(/^#/, ''));
		setInterval(jQuery.historyCheck, 100);
	},
	
	historyAddHistory: function(hash) {
		// This makes the looping function do something
		jQuery.historyBackStack.push(hash);
		
		jQuery.historyForwardStack.length = 0; // clear forwardStack (true click occured)
		this.isFirst = true;
	},
	
	historyCheck: function(){
		if (jQuery.historyNeedIframe) {
			// On IE, check for location.hash of iframe
			var ihistory = jQuery("#jQuery_history")[0];
			var iframe = ihistory.contentDocument || ihistory.contentWindow.document;
			var current_hash = iframe.location.hash;
			if(current_hash != jQuery.historyCurrentHash) {
			
				location.hash = current_hash;
				jQuery.historyCurrentHash = current_hash;
				jQuery.historyCallback(current_hash.replace(/^#/, ''));
				
			}
		} else if (jQuery.browser.safari) {
			if(jQuery.lastHistoryLength == history.length && jQuery.historyBackStack.length > jQuery.lastHistoryLength) {
				jQuery.historyBackStack.shift();
			}
			if (!jQuery.dontCheck) {
				var historyDelta = history.length - jQuery.historyBackStack.length;
				jQuery.lastHistoryLength = history.length;
				
				if (historyDelta) { // back or forward button has been pushed
					jQuery.isFirst = false;
					if (historyDelta < 0) { // back button has been pushed
						// move items to forward stack
						for (var i = 0; i < Math.abs(historyDelta); i++) jQuery.historyForwardStack.unshift(jQuery.historyBackStack.pop());
					} else { // forward button has been pushed
						// move items to back stack
						for (var i = 0; i < historyDelta; i++) jQuery.historyBackStack.push(jQuery.historyForwardStack.shift());
					}
					var cachedHash = jQuery.historyBackStack[jQuery.historyBackStack.length - 1];
					if (cachedHash != undefined) {
						jQuery.historyCurrentHash = location.hash;
						jQuery.historyCallback(cachedHash);
					}
				} else if (jQuery.historyBackStack[jQuery.historyBackStack.length - 1] == undefined && !jQuery.isFirst) {
					// back button has been pushed to beginning and URL already pointed to hash (e.g. a bookmark)
					// document.URL doesn't change in Safari
					if (location.hash) {
						var current_hash = location.hash;
						jQuery.historyCallback(location.hash.replace(/^#/, ''));
					} else {
						var current_hash = '';
						jQuery.historyCallback('');
					}
					jQuery.isFirst = true;
				}
			}
		} else {
			// otherwise, check for location.hash
			var current_hash = location.hash;
			if(current_hash != jQuery.historyCurrentHash) {
				jQuery.historyCurrentHash = current_hash;
				jQuery.historyCallback(current_hash.replace(/^#/, ''));
			}
		}
	},
	historyLoad: function(hash){
		var newhash;
		hash = decodeURIComponent(hash);
		
		if (jQuery.browser.safari) {
			newhash = hash;
		}
		else {
			newhash = '#' + hash;
			location.hash = newhash;
		}
		jQuery.historyCurrentHash = newhash;
		
		if (jQuery.historyNeedIframe) {
			var ihistory = jQuery("#jQuery_history")[0];
			var iframe = ihistory.contentWindow.document;
			iframe.open();
			iframe.close();
			iframe.location.hash = newhash;
			jQuery.lastHistoryLength = history.length;
			jQuery.historyCallback(hash);
		}
		else if (jQuery.browser.safari) {
			jQuery.dontCheck = true;
			// Manually keep track of the history values for Safari
			this.historyAddHistory(hash);
			
			// Wait a while before allowing checking so that Safari has time to update the "history" object
			// correctly (otherwise the check loop would detect a false change in hash).
			var fn = function() {jQuery.dontCheck = false;};
			window.setTimeout(fn, 200);
			jQuery.historyCallback(hash);
			// N.B. "location.hash=" must be the last line of code for Safari as execution stops afterwards.
			//      By explicitly using the "location.hash" command (instead of using a variable set to "location.hash") the
			//      URL in the browser and the "history" object are both updated correctly.
			location.hash = newhash;
		}
		else {
		  jQuery.historyCallback(hash);
		}
	}
});





var empecher = 0;
//var timer = setInterval(stat,60000);
var non = 0;

function empecher_nv_requete (data) {
	if (data == 1) { empecher = 1; }
	else { empecher = 0; }
}

function stat () {
    $.get('stat_haut.php',function(data){
		//$("#stat").fadeOut(500,function(){
			$("#stat").empty();
			$("#stat").append(data);
			//$("#stat").fadeIn(500);
		//});
        });
}

function afficher(data){
	$(".loader").remove(); // On supprime le loader
	//$("#contenu").fadeOut(500,function(){
		$("#contenu").empty();
		$("#contenu").append(data);
		$("title").empty();
		$("title").append(titre);
		// $("#contenu").fadeIn(500);
		stat ();
		empecher_nv_requete(0);
		//timer = setInterval(stat,60000);
		//assignecontenue(data); // voila l'appel
		relance(data);
	//})

}

function Xiti ()
{
	Xt_param = 's=377293&p=';
	try {Xt_r = top.document.referrer;}
	catch(e) {Xt_r = document.referrer; }
	Xt_h = new Date();
	if(parseFloat(navigator.appVersion)>=4)
	{Xt_s=screen;Xt_i+='&r='+Xt_s.width+'x'+Xt_s.height+'x'+Xt_s.pixelDepth+'x'+Xt_s.colorDepth;}
	$("#Xiti").empty();
	$("#Xiti").append('<a href="http://www.xiti.com/xiti.asp?s=377293" title="WebAnalytics"><img width="80" height="15" border="0" alt="" src="http://logv3.xiti.com/bcg.xiti?'+Xt_param+'&hl='+Xt_h.getHours()+'x'+Xt_h.getMinutes()+'x'+Xt_h.getSeconds()+'&ref='+Xt_r.replace(/[<>"]/g, '').replace(/&/g, '$')+'" title="Internet Audience" />');
}
/*
	function pageload(hash) {
		// hash doesn't contain the first # character.
		if(hash) {
page=hash;
			// restore ajax loaded state
			//$("#contenu").load(hash);
				$("#top").append('<div class="loader"></div>'); // On ajoute le loader en haut
				//clearInterval(timer)
					if (/[?]/.test(page)) {
						empecher_nv_requete(1);
						$.ajax({
							url: page+'&ajax=1',
							cache:false,
							success:function(html){
								afficher(html);Xiti();
							},
							error:function(XMLHttpRequest,textStatus, errorThrown){
								afficher("erreur lors du chagement de la page");
							}
						})
						return false; }
					else {
						empecher_nv_requete(1);
						$.ajax({
							url: page+'?ajax=1',
							cache:false,
							success:function(html){
								afficher(html);Xiti();
							},
							error:function(XMLHttpRequest,textStatus, errorThrown){
								afficher("erreur lors du chagement de la page");
							}
						})
					return false;
					}
		} else {
			// start page
			$("#contenu").load('index.php?ajax=1');
		$("title").empty();
		$("title").append(titre);
		}
	}

	function relance(data){

		// set onlick event for buttons
		$("#contenu a").click(function(){
			//
			var hash = $(this).attr("href");
				if ((hash.match(/http/i)) || (hash == '') || (hash == 'connexion.php?page=connexion') || (hash == 'connexion.php?mode=deconnexion')) {  }
				else {
			hash = hash.replace(/^.*#/, '');
			// moves to a new page.
			// pageload is called at once.
			$.historyLoad(hash);
var non = 1;
			return false;
}
		});
}

if (non == 0)
{
	$(document).ready(function(){
		// Initialize history plugin.
		// The callback is called at once by present location.hash.
		$.historyInit(pageload);

		// set onlick event for buttons
		$("#contenu a").click(function(){
			//
					var hash = $(this).attr("href");
				if ((hash.match(/http/i)) || (hash == '') || (hash == 'connexion.php?page=connexion') || (hash == 'connexion.php?mode=deconnexion')) {  }
				else {
			hash = hash.replace(/^.*#/, '');
			// moves to a new page.
			// pageload is called at once.
			$.historyLoad(hash);
			return false;
				    }
		});
	});
}
*/
function majsubdiv (data)
{
	$.ajax({
		url: 'subdivision.php?pays='+data,
		cache:false,
		success:function(html){
		$("#subdivision").empty();
		$("#subdivision").append(html);
		},
		error:function(XMLHttpRequest,textStatus, errorThrown){
			afficher("erreur lors du chagement de la page");
		}
	})
}


function connexion(){
						$.ajax({
							url: 'menu.php?page_nom=Connexion',
							cache:false,
							success:function(html){
							$("#menu").empty();
							$("#menu").append(html);
							},
							error:function(XMLHttpRequest,textStatus, errorThrown){
								afficher("erreur lors du chagement de la page");
							}
						});
						$.ajax({
							url: 'connexion.php?page=connexion-AJAX&ajax=1',
							cache:false,
							success:function(html){

	//$("#contenu").fadeOut(500,function(){
		//$("#contenu").empty();
$("#corps").css({opacity:"0.5"});
		$("#contenu").append('<div class="surpage">'+html+'</div>');
		// $("#contenu").fadeIn(500);
							},
							error:function(XMLHttpRequest,textStatus, errorThrown){
								afficher("erreur lors du chagement de la page");
							}
						});
						return false;
}

function supprimer_connexion()
{
						$.ajax({
							url: 'menu.php?page_nom='+page_url,
							cache:false,
							success:function(html){
							$("#menu").empty();
							$("#menu").append(html);
							},
							error:function(XMLHttpRequest,textStatus, errorThrown){
								afficher("erreur lors du chagement de la page");
							}
						});
	$("#corps").css({opacity:"1.0"});
	$(".surpage").remove();
	return false;
}


function submit_connex() {
$(document).ready(function(){
$("#connex-ajax form").submit(function(){
        var pseudo = $("#pseudo").val();
        var mdp = $("#mdp").val();
        var auto = $("#auto").val();
	$.ajax({
		url: 'connexion.php?mode=connexion-AJAX&ajax=1',
		cache:false,
		type:'POST',
		data: {'pseudo':pseudo, 'mdp':mdp, 'auto':auto},
		success:function(html){
			if (html == "oui") { supprimer_connexion(); }
			else
			{
				clearInterval(timer2)
				function vider_erreur () { $("#erreur_connex").empty(); }
				var timer2 = setInterval(vider_erreur,6000);
				$("#erreur_connex").append('<b>Vous avez fait une erreur.<br />Veuillez recommencer.</b>');
			}
		},
		error:function(XMLHttpRequest,textStatus, errorThrown){
			afficher("erreur lors du chagement de la page");
		}
	});
	return false;
});
});
}

 function deconnexion(){
						$.ajax({
							url: 'connexion.php?mode=deconnexion-AJAX?ajax=1',
							cache:false,
							success:function(html){
						$.ajax({
							url: 'menu.php?page_nom='+page_url,
							cache:false,
							success:function(html){
							$("#menu").empty();
							$("#menu").append(html);
							},
							error:function(XMLHttpRequest,textStatus, errorThrown){
								afficher("erreur lors du chagement de la page");
							}
						});
							},
							error:function(XMLHttpRequest,textStatus, errorThrown){
								afficher("erreur lors du chagement de la page");
							}
						});
						return false;
}

function noter(id, operation){
						$.ajax({
							url: 'ajax.php',
		data: {'id':id, 'operation':operation, 'page':'note'},
							cache:false,
							success:function(html){
							$("#note"+id).empty();
							$("#note"+id).append(html);
							},
							error:function(XMLHttpRequest,textStatus, errorThrown){
								afficher("erreur lors du chagement de la page");
							}
						});
						return false;
}
