var nbClick=0;

function oninit(){ 
  if($("div_moteurRechercheContenuBandeau")) chargementMoteurBandeau();
  if($("div_contenuSejoursPassions")) scrollSejoursThemes();
  generePagesEvenements();
  if($("frm_newsletter")) initNewsletter()
  if($("div_moteurRechercheBandeau")) chargementMoteurBandeau();
}

function chargementMoteurBandeau(){ 
  if ($("div_calendrierMoteurContent")) refreshCalDateArr();
  actualiseCommune();
  if ($("inpt_ville")) $("inpt_ville").onfocus=function (evt){$("inpt_ville").value='';};
  if ($("inpt_ville")) $("inpt_ville").onblur=function(evt){if ($("inpt_ville").value=="") $("inpt_ville").value="Commune";};
  if ($("a_sp_moteurRechercher")) $("a_sp_moteurRechercher").onclick=function(evt){validerMoteurRechercheBandeau();}
  if ($("a_sp_plusCriteres")) $("a_sp_plusCriteres").onclick=function(evt){validerMoteurRechercheBandeau();}
  $("lst_commune").onchange=function(evt){$("inpt_distcomm").disabled=false;};
  $("select_paysTouristique").onchange=function(evt){actualiseCommune();};
}



function valideInscNews(){
    envoiMailByAjaxByForm('frm_newsletter','mesg_replace',false);
}

function viderChampNews(obj){
  obj.value="";
}

function enterNews(event){
  if(!event) var event = window.event;
  if(event && event.keyCode==13) checkForm($("frm_newsletter"),valideInscNews);
}

function initNewsletter(){
  $("inpt_inscNews").value = "Votre email";
  $("frm_newsletter").onsubmit = function(evt){return false;};
  $("a_validNews").observe("click",function(evt){checkForm($('frm_newsletter'),valideInscNews);});
  $("inpt_inscNews").onfocus = function(evt){viderChp($("inpt_inscNews"));viderChampNews($("inpt_inscNews"));}
  $("inpt_inscNews").onblur = function(evt){checkByInput(this,'checkMailObligatoire');};
  $("inpt_inscNews").onkeypress = function(evt){enterNews(evt);};
}

/*---------calendrier-------------------*/                      
function clicCalendJs(evt,jour,mois,annee,etat){
  $("inpt_dateDeb").value=jour+"/"+mois+"/"+annee;
  afficheEffaceCalendJsMenu("div_calendrierMoteur");
}

function afficheEffaceCalendJsMenu(nomDiv){
  if($(nomDiv).showing==true){
    Effect.BlindUp(nomDiv);       
    $(nomDiv).showing=false;
  }else{
    Effect.BlindDown(nomDiv);       
    $(nomDiv).showing=true;
  }
}

function afficheEffaceListe(nomDiv){
  if($(nomDiv).style.display == "none"){
    $(nomDiv).style.display = "block";
  }else{
    $(nomDiv).style.display = "none";
  }
}

function refreshCalDateArr(){
		setCalendrierGeneral("gites971","1","","G","div_calendrierMoteurContent",clicCalendJs,"inpt_dateDeb","","","lst_nbj","1","");
		$("img_calendMoteur").onclick=function (evt){afficheEffaceCalendJsMenu('div_calendrierMoteur');}
		$("div_fermeCalend").onclick=function(evt){afficheEffaceCalendJsMenu("div_calendrierMoteur");};
    $("inpt_dateDeb").onfocus=function(evt){afficheEffaceCalendJsMenu("div_calendrierMoteur");};
}

/*----------lancer le moteur de recherche---------*/
function validerMoteurRechercheBandeau(){
  if( ($("inpt_dateDeb").value != "")){ 
    if($("lst_nbj").value != "") 
      setListByForm($('form_moteurRechercheBandeau'));
    else
    alertAMalibu("Veuillez saisir une durée de séjour, s'il vous plait");  
  }else{ 
    setListByForm($('form_moteurRechercheBandeau'));
  }
}

function generePagesEvenements(){
  if($("div_evenements")) setScrollHorizontalOnLstGenerePage("div_evenements","liste_evenements",{idDivPastePage:"div_evenements",position:"bottom"});
}

function rechercheAjax(){
  setListByForm($('form_moteurRecherche'));
}

function scrollSejoursThemes(){
  setScrollHorizontalOnDiv('div_contenuSejoursPassions','ul_themesSejoursPassions',{nextButton:"div_btnThemesSuiv",prevButton:"div_btnThemesPrec"});
  $("ul_themesSejoursPassions").style.left="0";
  setInterval("simuleClicSuivant()",4000);
}

function simuleClicSuivant(){
  if(nbClick<=3){
    $("div_btnThemesSuiv").onclick();
    nbClick++;
  }
  else{
    $("ul_themesSejoursPassions").morph("left:0px;");
    nbClick=0;
  }
}

function actualiseCommune(){
  appelAjaxToFunc("/config_v3/971/G.v3/V4_MODELEFEDE.FRANCAIS/librairie/bloc_html/Communes.php","REG="+$("select_paysTouristique").value+"&ACC=G",function(reponseAjx){
    var tab;
    eval("tab="+reponseAjx);
    var indice = 0;
    var selected = 0;
    $("lst_commune").options.length=0;
    $("lst_commune").options[$("lst_commune").options.length] = new Option("Choisir une commune");
    $("lst_commune").options[$("lst_commune").options.length-1].value="";
    $A(tab.communes).each(function(obj){
      $("lst_commune").options[$("lst_commune").options.length] = new Option(obj);
      $("lst_commune").options[$("lst_commune").options.length-1].value=obj;
      indice++;
      if(obj==$("select_commune").value) selected=indice;
    });
    $("lst_commune").selectedIndex = selected;
  });
}

function defCommune(){
  $("select_commune").value = $("lst_commune").value;
}

