
DomLoadedArray = new Array();
DomLoadedForPreviousNext = new Array();

AbsolutePath = "http://localhost/Eversonic/";
AbsolutePathSecondary = "http://88.171.49.57/Eversonic/";
ClickOnLink = false;
LastHash ="";
UpdateMusiques = false;
LoadingPage = false;
BaseHtml = "";

$(document).ready(function(){
	DomLoadedForPreviousNext[document.location.href] = $("#corps").html();
	BaseHtml = $("#corps").html();
	cache = setInterval(function(){
		if(document.location.hash!=LastHash && LoadingPage==false){
			if(!(document.location.hash.replace('#','')[0] == '!'))
			{
				if(document.location.hash==""){
					$("#corps").html(BaseHtml);
				}
				else
				{
					$("#corps").html(DomLoadedArray[document.location.hash.replace('#','')]);
					
					
				}
				ToLoadAfterAjax();
			}
			LastHash = document.location.hash;
		}
	},250);
	
	// PRELOAD FOR CACHE
	PreLoadLinks = function(){	
		UpdateMusiques = false;
		return;
			$("a.load").each(function(){
				if(!$(this).is('.notAtLoading'))
				{
					i = $("a.load").index($(this));
					
					H = $(this).attr('href');
					if(typeof(DomLoadedArray[H]  ) !="undefined")
						return;
						
					setTimeout((function(H){
						$.ajax({
							type : "GET",
							url : H,
							data : "ajaxMod=true",
							success:function(dom){
								DomLoadedArray[H] = dom;
								DomLoadedForPreviousNext[H] = DomLoadedArray[H];
							}
						});
					})(H),800+100*i);
				}
			});
	}
	
	// --------------------------------
	// LOADER CACHE - SHOW
	refreshPage = function(){
		
		H = currentPage;
		
			$.ajax({
				type : "GET",
				url : H,
				data : "ajaxMod=true",
				success:function(dom){
					$("#corps").html(dom);
					$("head title").html($("#corps #titleHeader:last").html());

					currentPage = H;
					ToLoadAfterAjax();
				}
			});
	}
	loadLink = function(obj,endFunction){
		
		H = $(obj).attr('href');
		H = H.replace("http://www.eversonic.fr/","");
		H = H.replace("http://eversonic.fr/","");
			if(	"#" + H == document.location.hash )
				return false; 
			document.location.hash = "#" + H;
			
			if(typeof(DomLoadedArray[H]  ) !="undefined")
			{	
					$("#corps").html(DomLoadedArray[H]);
					currentPage = H;
					$("head title").html($("#corps #titleHeader:last").html());
					// $(document.body).animate({ scrollTop : 0 }, 400 );
					DomLoadedForPreviousNext[H] = DomLoadedArray[H];
					delete(DomLoadedArray[H]);
					ToLoadAfterAjax();
					if(typeof(endFunction) != "undefined")
						endFunction(obj);
					return;
			}
				
			
				

			$.ajax({
				type : "GET",
				url : H,
				data : "ajaxMod=true",
				success:function(dom){
					$("#corps").html(dom);
					$("head title").html($("#corps #titleHeader:last").html());
					// 	$(document.body).animate({ scrollTop : 0 }, 400 );
					DomLoadedArray[H] = dom;
					ToLoadAfterAjax();
					currentPage = H;
					if(typeof(endFunction) != "undefined")
						endFunction(obj);
				}
			});
	}
	
	SetLinksLoad = function(){
		$("a.load").click(function(){
			LoadingPage =true;
			setTimeout(function(){
				LoadingPage  = false;
			},1000);
			LastHash = '#' + $(this).attr('href');
			
			if(!($(this).css('position')=='absolute'))
				$(this).css('position','relative');
			I = new Image();
			I.src = "img/simpleLoad.gif";
			$(this).append('<div class="ajaxLoaderLink"></div>');
			
				$(this).find('.ajaxLoaderLink').animate({
					opacity : 0
				},1500,function(){ $(this).remove(); });

			ClickOnLink = true;
			setTimeout(function(){
				ClickOnLink=false;
			},1500);
			
			loadLink($(this),function(obj){
			});
			return false;
		});
	}
	ToLoadAfterAjax = function(){				
					SetLinksLoad();
					PreLoadLinks();
					setFormAjax();
					setPersonnagesCliquable();
					if(typeof(EversonicChat) != "undefined")
						if(typeof(EversonicChat.clearInterval) != "undefined")
							EversonicChat.clearInterval();
	}
	setTimeout(function(){
		ToLoadAfterAjax();
	},300);
});
