window.addEvent('domready', function() {
//creation de l'input du moteur de recherche
var div_recherche = new Element("div", {
		"id" : "recherche"
		});
		div_recherche.injectAfter("page");
		div_recherche.setHTML("<span id=\"spanrecherche\">Recherchez dans le site</span>&nbsp;<input onKeyUp=\"RechercheRequest()\" onSubmit='RechercheRequest()' id=\"inputrecherche\" name=\"inputrecherche\" type='text' value=''>");

var div_mouline = new Element("div", {
			"id" : "mouline"
			});
			div_mouline.injectAfter("page");
			div_mouline.setHTML("<img src=\"images/mouline.gif\" style=\"height:30px; width:30px;\">");
});



function AffichePageRequest(pageactive)
{
	$("mouline").effect("opacity", {duration:100}).start(0,1);
		
	parent.location.hash = pageactive;
		
	var url = "page.php";
	var options = {update : $("page"), method : "get", data : "a="+pageactive }; 
	
	var myAjax = new Ajax(url, options).request();
	myAjax.addEvent("onSuccess", function(resultat){
		$("mouline").effect("opacity").start(1,0);
				
		if (window.ie) 
		{
		makeNewHash(pageactive);	//IE
		}
			else
			{
				if (window.opera && window.history)  //opera
				{     
					var div_timer = new Element("div", {
						"styles" : {"visibility" : "hidden"},
						"id" : "test_timer"
						});
						div_timer.injectAfter("page");
				
	  							  
				 	var div_img = new Element("div", {
						"styles" : {"visibility" : "hidden"},
						"id" : "div_img"
						});
						div_img.injectAfter("test_timer");
				
								 				 			  
				  var img = new Element("img", {
							"src" : "javascript:location.href='javascript:onTick()';",
						  "id" : "img"
						});
						img.injectInside("div_img");
					
									  
				 	history.navigationMode = "compatible";
				  timerCookie = window.setTimeout("onTick()", 100);
					$("test_timer").setHTML(1);
				}
				else pollHash(); //les autres firefox, safari
			}
		
		});
}	
	

// affichage page a partir du hash
function AffichePageHash(pageactive) {
	if (parent.location.hash != "")
	{							
		//alert ("dans onFrameLoaded urlactive="+urlactive);
		var urlactive = parent.location.hash.substring(1,11);
		//alert ("urlhash="+urlactive);				
						
		AffichePageRequest(urlactive)
	}
	else {AffichePageRequest(pageactive);}
}
// fin affichage page a partir du hash


// historique IE
function makeNewHash(pageactive) {
		var pageHash = parent.location.hash;
    var iframe = document.createElement("iframe");
			with(iframe){
				name = "historyFrame";
				id = "historyFrame";
				}
			document.appendChild(iframe);			 
    
    new Fx.Style("historyFrame", "opacity").set(0);

    var doc =  document.getElementById("historyFrame").contentWindow.document;
    doc.open("javascript:'<html></html>'");
    doc.write("<html><head><scri" + "pt type=\"text/javascript\">parent.onFrameLoaded(\""+ pageHash + "\");</scri" + "pt></head><body></body></html>");
    doc.close();
}


function onFrameLoaded(diesepage) {
		$("mouline").effect("opacity", {duration:100}).start(0,1);
		if (diesepage == "") diesepage = "#page100000";
	
		if (parseInt(diesepage.length) == 11) var pageactive = diesepage.substring(1,11);
		if (parseInt(diesepage.length) == 10) var pageactive = diesepage ;
						
		var url = "page.php";
		var options = {update : $("page"), method : "get", data : "a="+pageactive }; 
	
		var myAjax = new Ajax(url, options).request();
		myAjax.addEvent("onSuccess", function(resultat){
			parent.location.hash = diesepage;
			new Fx.Style("mouline", "opacity").start(1,0);
			});
}
// fin historique IE


// historique firefox
var expectedHash="";
function handleHistory()
{
  //alert ('expectedHash='+expectedHash);
  if ( parent.location.hash != expectedHash )
  {
    expectedHash = parent.location.hash;
    var newoption = expectedHash.substring(1,11);
    if (newoption == "") newoption = "#page100000";
    //alert (newoption);
    AffichePageRequest(newoption);
  }
}

function pollHash() {
  handleHistory();
  window.setInterval("handleHistory();", 500);
}
// fin historique firefox


//historique opera
function onTick() {
    
    pageactive = document.location.hash.substring(1,11);
    //alert ("pageactive="+pageactive);				
		window.clearTimeout(timerCookie);
   	$("test_timer").setHTML(0);
		
		var url = "page.php";
		var options = {method : "get", data : "a="+pageactive }; 
	
				var myAjax = new Ajax(url, options).request();
				myAjax.addEvent("onSuccess", function(resultat){
					$("mouline").effect("opacity").start(1,0);
					$("page").setHTML(resultat);
        	window.clearTimeout(timerCookie);
        	$("test_timer").setHTML(0);
        	});
		          
    if ($("test_timer").getText == 0) timerCookie = window.setTimeout(onTick, 100);
    $("test_timer").setHTML(1);
}
//fin historique opera


function RechercheRequest(pageactive)
{
	$("mouline").effect("opacity", {duration:100}).start(0,1);
	var recherche = $("inputrecherche").getValue();
	
	var url = "recherche.php";
	var options = {method : "post", data : "inputrecherche="+recherche }; 
	  
	  //alert ("inputrecherche="+recherche);
		
		var myAjax = new Ajax(url, options).request();
		myAjax.addEvent("onSuccess", function(resultat){
				new Fx.Style("mouline", "opacity").start(1,0);
			 //alert (resultat);
			 if (resultat!="vide")
			 {
			 //$("#rubrique").css("paddingLeft","20px");
			 $("rubrique").setHTML(resultat);
			 }
			 if (resultat=="") AffichePageHash(pageactive);
			});
}

function EnvoiMessageRequest()
{
	$("mouline").effect("opacity", {duration:100}).start(0,1);
	
	var nom = $("nom").getValue();
	var tel = $("tel").getValue();
	var email = $("email").getValue(); 
	var message = $("message").getValue();
	
	var url = "envoimessage.php";
		var options = {method : "post", data : "nom="+nom+"&tel="+tel+"&email="+email+"&message="+message }; 
	
		var myAjax = new Ajax(url, options).request();
		myAjax.addEvent("onSuccess", function(resultat){
				new Fx.Style("mouline", "opacity").start(1,0);
			 //alert (resultat);
				if (resultat=="envoye")
				alert("Votre message a été envoyé avec succes.\nUne copie vous est adressée a l'adresse que vous avez indiquée");
				else alert(resultat);
				});
}		

function ChoixLangRequest(lang,page)
{
	$("mouline").effect("opacity", {duration:100}).start(0,1);
	
	var url = "choixlang.php";
		var options = {method : "post", data : "lang="+lang }; 
	
		var myAjax = new Ajax(url, options).request();
		myAjax.addEvent("onSuccess", function(resultat){
				new Fx.Style("mouline", "opacity").start(1,0);
			 //alert (resultat);
				AffichePageRequest(page) ;
				});
}

function survol(action,classe,a)
{
	//alert ("classe = "+classe+", action = "+action+", page = "+a);
	//el.setProperty('class','yourClassName');
	if (action=="over")
	{
		if (classe=="inactifMd" || classe=="actifMd")
		{
		document.getElementById("div"+a).className = "survolMd" ;
		document.getElementById("span"+a).className = "survolMd" ;
		document.getElementById("lien"+a).className = "survolMd";
		}
		if (classe=="inactifSMd" || classe=="actifSMd")
		{
		document.getElementById("div"+a).className = "survolSMd" ;
		document.getElementById("span"+a).className = "survolSMd";
		document.getElementById("lien"+a).className = "survolSMd";
		}
		if (classe=="inactifMg" || classe=="actifMg")
		{
		document.getElementById("div"+a).className = "survolMg" ;
		document.getElementById("span"+a).className = "survolMg" ;
		document.getElementById("lien"+a).className = "survolMg";
		}
		if (classe=="inactifSMg" || classe=="actifSMg")
		{
		document.getElementById("div"+a).className = "survolSMg" ;
		document.getElementById("span"+a).className = "survolSMg";
		document.getElementById("lien"+a).className = "survolSMg";
		}
		if (classe=="inactifMh" || classe=="actifMh")
		{
		document.getElementById("div"+a).className = "survolMh" ;
		document.getElementById("span"+a).className = "survolMh" ;
		document.getElementById("lien"+a).className = "survolMh" ;
		}
		if (classe=="inactifMb" || classe=="actifMb")
		{
		document.getElementById("div"+a).className = "survolMb" ;
		document.getElementById("span"+a).className = "survolMb" ;
		document.getElementById("lien"+a).className = "survolMb" ;
		}
	}
	
	if (action=="out")
	{
		document.getElementById("div"+a).className = classe ;
		document.getElementById("span"+a).className = classe ;
		document.getElementById("lien"+a).className = classe ;
	}
}

function AccesProtegeRequest(pageactive)
{
	$("mouline").effect("opacity", {duration:100}).start(0,1);
	
	var url = "espaceprotege.php";
	var options = {method : "post", data : "nomespace="+escape(nomespace)+"&mdpespace="+mdpespace }; 
	
	var myAjax = new Ajax(url, options).request();
	myAjax.addEvent("onSuccess", function(resultat){
			$("mouline").effect("opacity").start(1,0);
			var ObjJson = Json.evaluate(resultat);
			var retour = ObjJson.retourespace.info;
			if (retour=="Bienvenue sur l'Espace Client") AffichePageRequest("page970000");
			if (retour=="Erreur d'identification") AffichePageRequest(pageactive);
			alert (retour);
			});
}			

function QuitteAccesProtegeRequest()
{
	$("mouline").effect("opacity", {duration:100}).start(0,1);
	var c1 = "oui";
	var c2 = "oui";
	
	var url = "espaceprotege.php";
	var options = {method : "post", data : "accesclient="+c1+"&supprimer="+c2 }; 
	
	var myAjax = new Ajax(url, options).request();
	myAjax.addEvent("onSuccess", function(resultat){
			$("mouline").effect("opacity").start(1,0);
			var ObjJson = Json.evaluate(resultat);
			var retour = ObjJson.retourespace.info;
			AffichePageRequest("page100000");
			alert (retour);
			});
}
