window.addEvent('domready', function() {
//creation de l'input du moteur de recherche
var div_recherche = new Element("div", {
	"id" : "recherche"
});
div_recherche.injectAfter("page");
var url = "recherchenbl.php";
var myAjax = new Ajax(url).request();
myAjax.addEvent("onSuccess", function(resultat){
	//alert ("saut de ligne dans recherche = "+resultat);
	var ObjJson = Json.evaluate(resultat);
	var recherchenbL = ObjJson.recherchenbL;
	var visible = ObjJson.visible;
	if (visible == "oui"){
	div_recherche.setHTML("<a class='recherche' onSubmit='RechercheRequest()' onclick='RechercheRequest()'><span><img src='images/loupe.png' border='0'>&nbsp;Recherchez dans le site&nbsp;</span></a>"+recherchenbL+"<input onChange=\"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\">");
//*****************************diaporama
var div_init = new Element("div", {
"id" : "init"
});
div_init.injectAfter("page");
div_init.setHTML(0);
var div_compteur = new Element("div", {
"id" : "compteur"
});
div_compteur.injectAfter("page");
var div_nbrephotos = new Element("div", {
"id" : "nbrephotos"
});
div_nbrephotos.injectAfter("page");
//**diapos blocs flottants
for (b=0;b<10;b++) {
var div_init = new Element("div", {
"id" : "init"+b
});
div_init.injectAfter("page");
div_init.setHTML("A");
var div_compteur = new Element("div", {
"id" : "compteur"+b
});
div_compteur.injectAfter("page");
var div_nbrephotos = new Element("div", {
"id" : "nbrephotos"+b
});
div_nbrephotos.injectAfter("page");
}
//*****************************lancement des historiques
if (!window.ie) { //**********firefox, opera, safari
	var div_refreshfx = new Element("div", {
	"id" : "refreshfx"
	});
	div_refreshfx.injectAfter("page");
	div_refreshfx.setHTML(0); //poll hash lancé
	pollHash();
	}
if (window.ie)  makeNewHash(); //*********************ie
//*****************************fin lancement des historiques
//*****************************reload de la page
var paramget = window.location.search;
var paramhash = parent.location.hash;

if ((paramget.indexOf("?a=page",0)==-1) && (paramhash == "")){
var pageactive = "page100000";
window.location.replace("accueil.php#"+pageactive);
}
if ((paramget.indexOf("?a=page",0)!=-1) && (paramhash == "")){
var pageactive = paramget.substring(3,14);
window.location.replace("accueil.php#"+pageactive);
}
if (paramhash.indexOf("page",0)!=-1){
	var pageactive = paramhash.substring(1,11); 
	}
if (paramhash.indexOf("artc",0)!=-1){
	var id = paramhash.substring(5,11);
	VoirPanierRequest(id,"non");
	}
//*****************************fin reload de la page
});
//*********************************************
function AffichePageRequest(pageactive) {
	if ($("init").getText() == "1" ){
	stop();
	}
	$("mouline").effect("opacity", {duration:100}).start(0,1);
	if (window.ie) {
		parent.location.hash = pageactive;
		makeNewHash(); //*********ie
	}
	if (!window.ie) { //**********firefox, opera, safari
		clearInterval(histofirefox);
		parent.location.hash = pageactive;
		var url = "page.php";
		var options = {method : "get", data : "a="+pageactive }; 

		var myAjax = new Ajax(url, options).request();
		myAjax.addEvent("onSuccess", function(resultat){
		$("page").setHTML(resultat);
		$("mouline").effect("opacity").start(1,0);
		verifsidiapo(pageactive);
		//****************
			//AfficheSpecifpageactive);
		//****************
		$("refreshfx").setHTML(1);
		handleHistory();
		});
	}
}	
// historique ie
function makeNewHash() {
		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) {
		//panier
		if (diesepage.indexOf("artc",0)!=-1){
		var id = diesepage.substring(5,11);
		VoirPanierRequest(id,"non");
		}
		//fin panier
		
		//page
		if (diesepage.indexOf("page",0)!=-1){
		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 = {method : "get", data : "a="+pageactive }; 
	
		var myAjax = new Ajax(url, options).request();
		myAjax.addEvent("onSuccess", function(resultat){
			parent.location.hash = diesepage;
			$("page").setHTML(resultat);
			verifsidiapo(pageactive);
			$("mouline").effect("opacity").start(1,0);
		//*************************
  		//AfficheSpecif(pageactive);
  	//*************************
		});
		}//fin page
}
// fin historique ie
// historique firefox, opera, safari
function pollHash() {
  handleHistory();
 	histofirefox = setInterval("handleHistory();", 200);
}
var expectedHash="";
function handleHistory() {
  //alert ('expectedHash='+expectedHash);
  var divrefreshfxspeed = $("refreshfx").getText();
	if (divrefreshfxspeed == "0") {
		if ( parent.location.hash != expectedHash ) {
				expectedHash = parent.location.hash;
				//panier
				if (expectedHash.indexOf("artc",0)!=-1){
				var id = expectedHash.substring(5,11);
				VoirPanierRequest(id,"non");
				}
				//fin panier
				
				//page
				if (expectedHash.indexOf("page",0)!=-1){
				var nouvellepage = expectedHash.substring(1,11);
				var url = "page.php";
				var options = {method : "get", data : "a="+nouvellepage }; 
	
				var myAjax = new Ajax(url, options).request();
				myAjax.addEvent("onSuccess", function(resultat){
				$("page").setHTML(resultat);
				verifsidiapo(nouvellepage);
				//*************************
					//AfficheSpecif(nouvellepage);
				//*************************
				});
				}//fin page
			}	
	}		
	if (divrefreshfxspeed == "1") {
	expectedHash = parent.location.hash;
	$("refreshfx").setHTML(0);
	histofirefox = setInterval("handleHistory();", 200);
	}
}
// fin historique firefox, opera, safari
function RechercheRequest(pageactive) {
	var recherche = $("inputrecherche").getValue();
	if (recherche != "") {
	$("mouline").effect("opacity", {duration:100}).start(0,1);
	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);
			 }
			});
	}
}
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 montre(elmt) {
//alert (elmt);
$(elmt).setOpacity(1);
}
function cache(elmt) {
$(elmt).setOpacity(0);
}
function OriginIdent() {
if (parent.location.hash.indexOf("page",0)!=-1) {
var origine = "page";
}
if (parent.location.hash.indexOf("artc",0)!=-1) {
var origine = "panier";
}
var actif = parent.location.hash.substring(1,11); 
//alert ("origine = "+origine+" page = "+actif);
document.cookie = "origine = "+origine;
document.cookie = "page = "+actif;
}
//formulaire du compte
function FormClientsRequest(mode,cle) {
$("mouline").effect("opacity", {duration:100}).start(0,1);
var url = "ficheclient.php";
var options = {method : "post", data : "mode="+mode+"&cle="+cle }
var myAjax = new Ajax(url,options).request();
myAjax.addEvent("onSuccess", function(reponse){
		$("mouline").effect("opacity").start(1,0);
		$("ficheclient").setHTML(reponse);
		});
}
//creation et modification du compte
function BaseClientsRequest(mode,cle) {
$("mouline").effect("opacity", {duration:100}).start(0,1);
var pageactive = parent.location.hash.substring(1,11);
var id = parent.location.hash.substring(5,11);
var email = $('email').getValue();
var motdepasse = $('motdepasse').getValue();
var nom = $('nom').getValue();
var prenom = $('prenom').getValue();
var telephone = $('telephone').getValue();
var adresse = $('adresse').getValue();
var ville = $('ville').getValue();
var cp = $('cp').getValue();
var pays = $('pays').getValue();
var localisation = $$('input[name=localisation]').getValue();
if (localisation.indexOf("france",0)!=-1) localisation = "france";
if (localisation.indexOf("ue",0)!=-1) localisation = "ue";
if (localisation.indexOf("monde",0)!=-1) localisation = "monde";
if ($('utilcom').checked == true) var utilcom="oui"; else var utilcom="non";
if ($('difcom').checked == true) var difcom="oui"; else var difcom="non";

var url = "majficheclient.php";
var options = {method : "post", data : "email="+email+"&motdepasse="+motdepasse+"&nom="+nom+"&prenom="+prenom+"&adresse="+adresse+"&ville="+ville+"&cp="+cp+"&pays="+pays+"&telephone="+telephone+"&localisation="+localisation+"&mode="+mode+"&cle="+cle+"&utilcom="+utilcom+"&difcom="+difcom}; 
var myAjax = new Ajax(url, options).request();
myAjax.addEvent("onSuccess", function(reponse){
		//alert (reponse);
		new Fx.Style("mouline", "opacity").start(1,0);
	 	if (reponse.indexOf("le système a rencontré les erreurs suivantes",0)!=-1) alert (reponse);
	 	if (reponse.indexOf("le système a rencontré les erreurs suivantes",0)==-1) AffichePageRequest('page950000');
		});
}
//login
function VoirFicheClientRequest() {
	$("mouline").effect("opacity", {duration:100}).start(0,1);
	var id = parent.location.hash.substring(5,11);
	var url = "majficheclient.php";
	var emailconnex = $("emailconnex").getValue();
	var motdepasseconnex = $("motdepasseconnex").getValue();
	//alert ("emailconnex="+emailconnex+"&motdepasseconnex="+motdepasseconnex);
	var options = {method : "post", data : "emailconnex="+emailconnex+"&motdepasseconnex="+motdepasseconnex}; 
	var myAjax = new Ajax(url, options).request();
	myAjax.addEvent("onSuccess", function(reponse){
		$("mouline").effect("opacity").start(1,0);
		if (reponse.indexOf("le système a rencontré les erreurs suivantes",0)!=-1) alert (reponse);
	 	if (reponse.indexOf("le système a rencontré les erreurs suivantes",0)==-1) AffichePageRequest('page950000');
		});
}
function OubliMdPRequest(){
	var emailconnex = $("emailconnex").getValue();
	if (emailconnex == "") alert ("merci de remplir le champ 'adresse mail' avec votre email de connexion")
	else {
				var url = "oublimdppanier.php";
				var options = {method : "post", data : "emailconnex="+emailconnex}; 
				var myAjax = new Ajax(url, options).request();
				myAjax.addEvent("onSuccess", function(resultat){
				alert (resultat);	
				});
			}
}
function SupIdentRequest(){
	var emailconnex = $("emailconnex").getValue();
	if (emailconnex == "") alert ("merci de remplir le champ 'adresse mail' avec votre email de connexion")
	else {
				var url = "supidentpanier.php";
				var options = {method : "post", data : "emailconnex="+emailconnex}; 
				var myAjax = new Ajax(url, options).request();
				myAjax.addEvent("onSuccess", function(resultat){
				alert (resultat);	
				});
			}
}
function DeconnectRequest(){
	var url = "deconnectpanier.php";
	var id = parent.location.hash.substring(5,11);
	var myAjax = new Ajax(url).request();
	myAjax.addEvent("onSuccess", function(resultat){
	alert (resultat);
	if (parent.location.hash.indexOf("page",0)!=-1) AffichePageRequest("page"+id);
	if (parent.location.hash.indexOf("artc",0)!=-1) VoirPanierRequest(id,"non");
	});
}
function AnnulerCreerRequest(){
	if (parent.location.hash.indexOf("page",0)!=-1) {
	var id = parent.location.hash.substring(5,11);;
	}
	if (parent.location.hash.indexOf("artc",0)!=-1) {
	var id = parent.location.hash.substring(7,13);
	}
	if (parent.location.hash.indexOf("page",0)!=-1) AffichePageRequest("page"+id);
	if (parent.location.hash.indexOf("artc",0)!=-1) VoirPanierRequest(id,"non");
}
