Event.observe(window,'load',function(){
  posi_souris_une = false;
  setTimeout("intervalle_reguliers()",9000);
  Event.observe("laune","mouseover",function(){posi_souris_une = true;});
  Event.observe("laune","mouseout",function(){posi_souris_une = false;});
  Event.observe("une_0","mouseover",change_la_une);
  Event.observe("une_1","mouseover",change_la_une);
  Event.observe("une_2","mouseover",change_la_une);
  etat_liste_plus = false;
  $('valid_vote').setAttribute("type", "button");
  Event.observe("valid_vote","click",envoie_vote);
  tab_apercu=['DATE_ASTUCES','NOTE_ASTUCES','NB_CONSULTATIONS','ALEATOIRE'];
  
  for(i=0;i<tab_apercu.length;i++){
  	if($(tab_apercu[i])){
		var onglet=$(tab_apercu[i]).parentNode;
		Event.observe(onglet,"click",change_apercu);
    }
  }
  actualise_fleche_init();
  if($("flecheHaut"))Event.observe('flecheHaut','click',renvoie_explore_site);
  if($("flecheBas"))Event.observe('flecheBas','click',renvoie_explore_site);
  if($("flecheGauche"))Event.observe('flecheGauche','click',renvoie_explore_site);
  if($("flecheDroite"))Event.observe('flecheDroite','click',renvoie_explore_site);
  });


function change_info(event){
var element = Event.element(event);
if(element.id == "li_queFaire"){
	$('sondage').style.display = "none";
	$('que_faire').style.display = "block";	
	$('li_queFaire').className = "actif";
	$('li_sondage').className = "";
}  
else if(element.id == "li_sondage"){
	$('que_faire').style.display = "none";
	$('sondage').style.display = "block";
	$('li_sondage').className = "actif";
	$('li_queFaire').className = "";
}
}

function intervalle_reguliers(event){
	if(!posi_souris_une){
	if($('une_2').className == "vignette_une_actif"){
		$('une_2').className = "vignette_une";
		$('bloc_une_2').style.display = "none";
		$('une_0').className = "vignette_une_actif";
		$('bloc_une_0').style.display = "block";
	}
	else if($('une_1').className == "vignette_une_actif"){
		$('une_1').className = "vignette_une";
		$('bloc_une_1').style.display = "none";
		$('une_2').className = "vignette_une_actif";
		$('bloc_une_2').style.display = "block";
	}
	else if($('une_0').className == "vignette_une_actif"){
		$('une_0').className = "vignette_une";
		$('bloc_une_0').style.display = "none";
		$('une_1').className = "vignette_une_actif";
		$('bloc_une_1').style.display = "block";
	}
	}	
	setTimeout("intervalle_reguliers()",9000);
}

function testerRadio(radio){ //renvoi la valeur du radio button sélectionné.
	var reponse = "";
	for (var i = 0; i < radio.length; i++) {
		if (radio[i].checked) 
			reponse = radio[i].value;
		}
	return reponse;
}

function envoie_vote(){	
	var vote = testerRadio(document.getElementsByName("vote"));
	if(vote != ""){
	var xhr = getXMLHttpRequest();   
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 1) {
			
		}	
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) { //si le serveur répond
			if(xhr.responseText == 'ok'){
				$("div_valid_vote").update("Merci d'avoir voté");
			}
			else alert("Erreur veuillez recommencer plus tard");						 			    
		}		
	};
	xhr.open("POST", "sondage.php", true);  //envoie les informations à la page verif.php avec post 
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  //pour préparer le serveur à la méthode post	
	xhr.send("vote=" + vote + "&JS=actif");
	}
}

function change_la_une(event){
	var element = Event.element(event);
	if($('bloc_' + $(element).id)){
	if($('une_0').className == "vignette_une_actif"){
		$('une_0').className = "vignette_une";
		$('bloc_une_0').style.display = "none";
	}
	if($('une_1').className == "vignette_une_actif"){
		$('une_1').className = "vignette_une";
		$('bloc_une_1').style.display = "none";
	}
	if($('une_2').className == "vignette_une_actif"){
		$('une_2').className = "vignette_une";
		$('bloc_une_2').style.display = "none";
	}
	
	$(element).className = "vignette_une_actif";
	$('bloc_' + $(element).id).style.display = "block";
}}


function change_liste_ast(onglet){
if(!etat_liste_plus){
var gif_load = new Element('div',{id:"contenant_les_plus_loader"}).insert(new Element('img',{src:"photos/ajax-loader_bleu.gif"}));
etat_liste_plus = true; //pour ne pas répéter 2 fois la fonction
var div_height = $("contenant_onglet_ast").getHeight() + 6;  //récupère la taille de la largeur en px.
$("contenu_les_plus").style.height = div_height + "px";  //applique la taille à l'aide du style

if(onglet == "DATE_ASTUCES")
choix_plus = "1";
else if(onglet == "NOTE_ASTUCES")
choix_plus = "0";
else if(onglet == "NB_CONSULTATIONS")
choix_plus = "2";
else if(onglet == "ALEATOIRE")
choix_plus = "3";

var xhr = getXMLHttpRequest();   
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 1){                  
			$("contenant_onglet_ast").update(gif_load);
		}	
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) { //si le serveur répond
			if(xhr.responseText != ""){
				Effect.Fade($('contenant_onglet_ast'),{duration: 0.4,afterFinish:function(element){	
				$("contenant_onglet_ast").innerHTML = xhr.responseText;	
				Effect.Appear($('contenant_onglet_ast'),{duration:1,afterFinish: function(element){
					$("contenu_les_plus").style.height = "auto";
					etat_liste_plus = false;
					}});}});
			}
			else alert("Erreur");						 			    
		}		
	};
	xhr.open("POST", "return_accueil.php", true);  //envoi les informations à la page verif.php avec post 
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  //pour préparer le serveur à la méthode post	
	xhr.send("choix_plus=" + choix_plus);
}
}

function Evenement(id,type,fct){var contenant=$(id);if(contenant.attachEvent)contenant.attachEvent("on"+type,fct);else contenant.addEventListener(type,fct,false);}


function change_apercu(event){
var target=event.target||event.srcElement;
if(target.parentNode.id != "actif"){              //on vérifie que l'onglet n'est pas déja actif
change_liste_ast(target.parentNode.id);

for(i=0;i<tab_apercu.length;i++){
if(target.parentNode.id==tab_apercu[i]||target.id==tab_apercu[i]){

if(target.parentNode.id==tab_apercu[i])var a_ref=target.parentNode;
if(target.id==tab_apercu[i])var a_ref=target;

if($('actif').down(0).firstChild.nodeValue=='Dernières'){
	$('actif').update(new Element('a',{id:"DATE_ASTUCES",href:"index.php?apercu=derniere"}).insert(new Element('span').insert("Dernières")));Evenement($("DATE_ASTUCES"),"click",change_apercu);}
else if($('actif').down(0).firstChild.nodeValue=='Mieux notée'){
	$('actif').update(new Element('a',{id:"NOTE_ASTUCES",href:"index.php?apercu=note"}).insert(new Element('span').insert("Mieux notée")));Evenement($("NOTE_ASTUCES").id,"click",change_apercu);}
else if($('actif').down(0).firstChild.nodeValue=='Plus consultée'){
	$('actif').update(new Element('a',{id:"NB_CONSULTATIONS",href:"index.php?apercu=consulte"}).insert(new Element('span').insert("Plus consultée")));Evenement($("NB_CONSULTATIONS").id,"click",change_apercu);}
else if($('actif').down(0).firstChild.nodeValue=='Aléatoire'){
	$('actif').update(new Element('a',{id:"ALEATOIRE",href:"index.php?apercu=aleatoire"}).insert(new Element('span').insert("Aléatoire")));Evenement($("ALEATOIRE").id,"click",change_apercu);}
}
}

$("actif").removeAttributeNode($("actif").getAttributeNode("id"));
$(a_ref.parentNode).id="actif";$(a_ref.parentNode).update(new Element('span').insert(a_ref.down(0).firstChild.nodeValue));
}
return cancelClick(event);
}

function cancelClick(e){
	if(e&&e.stopPropagation&&e.preventDefault){e.stopPropagation();e.preventDefault();}
    else if(e&&window.event){window.event.cancelBubble=true;window.event.returnValue=false;}return false;}


function renvoie_explore_site(event){
	var xhr = getXMLHttpRequest();   
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 1) {
			
		}	
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) { //si le serveur répond
			if(xhr.responseText != ''){		
				var target = event.target || event.srcElement;	
     			var doc = eval('(' + xhr.responseText + ')'); 
				var id_expl = $("contenant_fixe_explorer").title;
				
				if(target.id == "flecheBas"){
					first = id_expl.split("_");
					id_expl = first[1];
					id_expl = id_expl + "1";			
					$("contenant_fixe_explorer").update(eval("doc.expl_" + id_expl));
					$("contenant_fixe_explorer").title = "expl_" + id_expl;
					actualise_fleche(id_expl, doc);		
				}
				if(target.id=="flecheHaut"){
					first = id_expl.split("_");
					id_expl = first[1];
					id_expl = id_expl.substring(0,id_expl.length-1);
					$("contenant_fixe_explorer").update(eval("doc.expl_" + id_expl));
					$("contenant_fixe_explorer").title = "expl_" + id_expl;			
					actualise_fleche(id_expl, doc);
					}
				if(target.id=="flecheGauche"){
					first = id_expl.split("_");
					id_expl = first[1];
					id_expl = parseInt(id_expl) - 1;	
					$("contenant_fixe_explorer").update(eval("doc.expl_" + id_expl));	
					$("contenant_fixe_explorer").title = "expl_" + id_expl;		
					actualise_fleche(id_expl+"", doc);
					}
				if(target.id=="flecheDroite"){
					first = id_expl.split("_");
					id_expl = first[1];
					id_expl = parseInt(id_expl) + 1;	
					$("contenant_fixe_explorer").update(eval("doc.expl_" + id_expl));
					$("contenant_fixe_explorer").title = "expl_" + id_expl;			
					actualise_fleche(id_expl+"", doc);
					}
			}
			else alert("Erreur d'exploration");						 			    
		}		
	};
	xhr.open("POST", "return_accueil.php", true);  //envoie les informations à la page verif.php avec post 
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  //pour préparer le serveur à la méthode post	
	xhr.send("explore_site=yes");
	
	function actualise_fleche(elem, doc){
		//fleche du bas 
		var elem_b = elem + "1";
		try{
		if(eval("doc.expl_" + elem_b) != undefined)$('flecheBas').style.display="";
		else $('flecheBas').style.display="none";
		}
		catch(e){$('flecheBas').style.display="none";
		}
		
		//flèche du haut
		if (elem.length != undefined){
			var elem_h = elem.substring(0, elem.length - 1);
			try {
				if (eval("doc.expl_" + elem_h) != undefined) $('flecheHaut').style.display = "";
				else $('flecheHaut').style.display = "none";
			} 
			catch (e){ $('flecheHaut').style.display = "none";}
		}
		else{
			$('flecheHaut').style.display = "none";
		}
		
		//flèche de droite
		var elem_d = parseInt(elem) + 1;
		try{
		if(eval("doc.expl_" + elem_d) != undefined)$('flecheDroite').style.display="";
		else $('flecheDroite').style.display="none";
		}
		catch(e){$('flecheDroite').style.display="none";
		}
		
		//flèche de gauche
		var elem_g = parseInt(elem) - 1;
		try{
		if(eval("doc.expl_" + elem_g) != undefined)$('flecheGauche').style.display="";
		else $('flecheGauche').style.display="none";
		}
		catch(e){$('flecheGauche').style.display="none";
		}
	}
}

function actualise_fleche_init(){
	$('flecheGauche').style.display = "none";
	$('flecheHaut').style.display = "none";
}
