$(document).ready(function(){
	/**
	 *  Gestion de l'arrivée sur le site
	 */
	hash = location.hash;
	pageLoad(hash);

	/**
	 *  Gestion du changement de hash
	 */
	$(window).hashchange( function() {
		hash = location.hash;
		pageLoad(hash);
	});

	/**
	 * traitement des liens rev set to ajaxLink
	 */
	$('a[rev=ajaxLink]').click(function () { 

		//récupération de l'url
		var hash = this.rel;
		location.hash = hash;

		return false;
	});


	/**
	 * fonction de chargement d'une page connue
	 */
	function pageLoad(hash) {
		//if hash value exists, run the ajax
		if (hash) getPage(hash);
	}

	/**
	 *  fonction AJAX 
	 */
	function getPage(hash) {
		//remove the # value
		hash = hash.replace(/^.*#/, '');
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like aao.
			hash = encodeURIComponent(hash);
		}
		//generate the parameter for the php script
		var data = 'page=' + hash.replace(/^.*#/, '');
		$.ajax({ 
			url: "/common/controller/loader.php",
			type: "GET",		
			data: data,		
			cache: false,
			success: function (html) {

			// Si le prochain contenu ne contient pas de l'ajax dans l'ajax
			if (html.indexOf("ajax") == -1) { 

				$(".content").fadeOut(500,function(){
					$(".content").html(html);
					$(".content").show();});

				// Si le prochain contenu contient de l'ajax dans l'ajax
			} else {

				if ($(".ajax").length != 0){
					$(".ajax").fadeOut(500,function(){ 
						$(".content").html(html);
						$(".content").show();});
				} else {
					$(".content").fadeOut(500,function(){
						$(".content").html(html);
						$(".content").show();});
				}
			}
		}
		});
	}
});
