function oninit(){
  if($("div_moteurMenu")) initSidebar();  
  if($("div_moteurMenu")) $$(".lstDuree").each(function(obj){if(obj.getAttribute("selected")=="selected"){$("lst_nbj").value=obj.getAttribute("valeur");$("sp_libDuree").innerHTML=obj.innerHTML;}});
}

// Calendrier JS
function afficheEffaceCalendJs(nomDiv){
  if($(nomDiv).showing==true){
    Effect.BlindUp(nomDiv);       
    $(nomDiv).showing=false;
  }
  else{
    Effect.BlindDown(nomDiv);       
    $(nomDiv).showing=true;
  }
}
function clicCalendJsMenu(evt,jour,mois,annee,etat){
  $("inpt_dateDeb").value=jour+mois+annee;
  afficheEffaceCalendJs("calendrierMenu")
}
// fin calend JS

/* IE6 hover sur les li */
metabarre_sfHover = function() {
 if ($("ul_menuHaut") != null) {
 var sfEls = $("ul_menuHaut").getElementsByTagName("LI");
 for (var i=0; i<sfEls.length; i++) {
  sfEls[i].onmouseover=function() {
  this.className+=" sfhover";
 }
   sfEls[i].onmouseout=function() {
   this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
 }
 }
 }
}
if (window.attachEvent) window.attachEvent("onload", metabarre_sfHover);


function setCGIForLstCommune(li){
    if ( navigator.appName == 'Microsoft Internet Explorer' ){
      //var infos=li.innerText.split("(");
     var commune = li.getElementsByTagName("span")[0].getAttribute("commune_reelle");
    }
    else{
      //var infos=li.textContent.split("(");
      var commune = li.getElementsByTagName("span")[0].getAttribute("commune_reelle");
    }
    document.getElementById("inpt_ville").value=commune;
    document.getElementById("inpt_ville").onfocus=function (evt){this.value='';document.getElementById("inpt_ville_libcommune").value='';}
    document.getElementById("inpt_ville").onblur=function (evt){document.getElementById("inpt_ville_libcommune").value=this.value;}
    
    document.getElementById("inpt_ville_libcommune").value=commune;
    setListByForm($('frmMoteurGeneral'));
}

function setChmpCommuneAutoCompleter(reponse){
    var suffixe;
    var jsonarray=eval(reponse);
    var infos=[];
    for (var i=0;i<jsonarray.length;i++){
        (jsonarray[i].nb>1)?suffixe="s":suffixe="";
       infos.push(jsonarray[i].commune+" ("+Math.round(jsonarray[i].nb)+" réponse"+suffixe+")<span commune_reelle=\""+jsonarray[i].communeReelle+"\" />");   }
    
    /**** champ commune ****/
    var typeahead = new Autocompleter.Local('inpt_ville', 'divLstCommunes_inpt_ville', infos, {
        frequency: 0.5,
        updateElement: setCGIForLstCommune
    });
}


function setChmpPrixToReglette(reponse){
    
    eval("var infosPrix="+reponse);
 
    /**** reglette de prix *****/
      var reglette = $('slider_prix');
      if (reglette){
      var vals=new Array();   
      var pxMini=parseInt(infosPrix.prix_mini);
      var pxMaxi=parseInt(infosPrix.prix_maxi);
      
      if (!pxMini)pxMini=0;
      if ((!pxMaxi && !pxMini) || ($("inpt_h").checked==true || $("inpt_e").checked==true || $("inpt_c").checked==true)){
        $("div_moteurMenu_budget").hide();
      }else{
	      $("div_moteurMenu_budget").show();
	      $("infoPrix_prixMini").innerHTML=pxMini+"&nbsp;&euro;";
	      $("infoPrix_prixMaxi").innerHTML=pxMaxi+"&nbsp;&euro;";
	        for (var i=0;i<=2500;i+=25)vals.push(i);
              new Control.Slider(reglette.select('.handle'), reglette, {
                range: $R(0, 2500),
                  sliderValue: [pxMini, pxMaxi],
                  restricted: true,
                  values: vals,
                  onSlide: function(values) {
                    $("infoPrix_prixMini").innerHTML=values[0];
                    $("infoPrix_prixMaxi").innerHTML=values[1];
                  },
                  onChange: function(values) {
                    if (!window.pxmin || window.pxmin!=values[0] || window.pxmax!=values[1]){
                        window.pxmin=values[0];
                        window.pxmax=values[1];
                        $("inpt_pxmini").value=values[0];
                        $("inpt_pxmaxi").value=values[1];
                        setListByForm($('frmMoteurGeneral'));
                    }
                  }
                });
        }
 
	}
}

function setActionSurMenuHaut(){
  

  $$("li.li_item").each(function(item){
    var idTime;
    var idMenu="elem_lst_lien_bouton_4_"+(parseInt(item.id.split("-")[1])-1);
    
    item.observe("mouseover",function(evt){
      $(idMenu).style.visibility="visible";
    });
    item.observe("mouseout",function(evt){
      idTime=setTimeout("$('"+idMenu+"').style.visibility='hidden'",1000);
    });
    $(idMenu).observe("mouseover",function(evt){
      clearTimeout(idTime);
    });
    $(idMenu).observe("mouseout",function(evt){
      idTime=setTimeout("$('"+idMenu+"').style.visibility='hidden'",1000);
    });
    
  
  });
}

 
 /*-----------------------------------------------------------
    Toggles element's display value
    Input: any number of element id's
    Output: none 
    ---------------------------------------------------------*/
function toggleDisp() {
    for (var i=0;i<arguments.length;i++){
        var d = $(arguments[i]);
        if (d.style.display == 'none')
            d.style.display = 'block';
        else
            d.style.display = 'none';
    }
}
/*-----------------------------------------------------------
    Toggles tabs - Closes any open tabs, and then opens current tab
    Input:     1.The number of the current tab
                    2.The number of tabs
                    3.(optional)The number of the tab to leave open
                    4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
    Output: none 
    ---------------------------------------------------------*/
function toggleTab(num,numelems,opennum,animate) {
    if ($('tabContent'+num).style.display == 'none'){
        for (var i=1;i<=numelems;i++){
            if ((opennum == null) || (opennum != i)){
                var temph = 'tabHeader'+i;
                var h = $(temph);
                if (!h){
                    var h = $('tabHeaderActive');
                    h.id = temph;
                }
                var tempc = 'tabContent'+i;
                var c = $(tempc);
                if(c.style.display != 'none'){
                    if (animate || typeof animate == 'undefined')
/*                      Effect.toggle(tempc,'blind',{duration:0.5, queue:{scope:'menus', limit: 3}}); */
						Effect.toggle(tempc,'appear',{queue:{scope:'menus', limit: 3}});
                    else
                        toggleDisp(tempc);
                }
            }
        }
        var h = $('tabHeader'+num);
        if (h)
            h.id = 'tabHeaderActive';
        h.blur();
        var c = $('tabContent'+num);
        c.style.marginTop = '2px';
        if (animate || typeof animate == 'undefined'){
/*          Effect.toggle('tabContent'+num,'blind',{duration:0.5, queue:{scope:'menus', position:'end', limit: 3}}); */
            Effect.toggle('tabContent'+num,'appear',{queue:{scope:'menus', position:'end', limit: 3}});  
		}else{
            toggleDisp('tabContent'+num);
        }
    }
}

function changeNbj(obj){
    if($("inpt_dateDeb").value!=""){
        setParamCgiEtRefreshList("DEB="+$("inpt_dateDeb").value+'&NBJ='+obj.value);
    }else{
        alertAMalibu("Vous devez d'abord choisir une date");
    }
}

 
function initSidebar(){
  initDuree();
  $("sp_libDuree").onclick=function(evt){afficheEffaceCalendJs("ul_dureeSejour");};
  $$(".lstDuree").each(function(obj){obj.onclick=function(evt){definitionDuree(obj);}});
  $("img_fermeCalend").onclick=function(evt){afficheEffaceCalendJs("calendrierMenu");};
  $$(".chk_moteurMenu").each(function(obj){obj.onclick=function(evt){initDuree();setListByForm($("frmMoteurGeneral"));};});

  /**** recherche avancee ****/
  $("div_rechercheAvanceeCrit").hide();
  $("sp_titreRechercheAvancee").onclick=function (evt){
      if (this.showing){
         Effect.BlindUp('div_rechercheAvanceeCrit'); 
         this.showing=false;
     }else{
         Effect.BlindDown('div_rechercheAvanceeCrit'); 
         this.showing=true;
     }
     return false;
  }

}

function initDuree(){
  rechargeCommune();
  if($("inpt_g").checked==true){
    $$(".dureeH").each(function(obj){obj.style.display="none";});
    $$(".dureeG").each(function(obj){obj.style.display="block";});
    $("div_moteurMenu_date").show();
    setCalendrierGeneral("gites40","1","","G","div_calendrierMenu",clicCalendJsMenu,"inpt_dateDeb","","","lst_nbj","1","");
  }
  else if($("inpt_h").checked==true){
    $$(".dureeG").each(function(obj){obj.style.display="none";});
    $$(".dureeH").each(function(obj){obj.style.display="block";});   
    $("div_moteurMenu_date").show();
    setCalendrierGeneral("gites40","1","","H","div_calendrierMenu",clicCalendJsMenu,"inpt_dateDeb","","","lst_nbj","1","");
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=LISTALLCOMMUNE&val=H",setChmpCommuneAutoCompleter);
  }
  else if(($("inpt_e").checked==true || $("inpt_t").checked==true || $("inpt_c").checked==true)){
    $("div_moteurMenu_date").hide();
    $("lst_nbj").value="";
    $("inpt_dateDeb").value="";
  }
  else{
    $$(".dureeG").each(function(obj){obj.style.display="block";});
    $$(".dureeH").each(function(obj){obj.style.display="block";});   
    $("div_moteurMenu_date").show();
    setCalendrierGeneral("gites40","1","","H","div_calendrierMenu",clicCalendJsMenu,"inpt_dateDeb","","","lst_nbj","1","");
  }
  if($("inpt_h").checked==true || $("inpt_e").checked==true || $("inpt_c").checked==true){
    $("div_moteurMenu_budget").hide();
    $("inpt_pxmini").value="";
    $("inpt_pxmaxi").value="";
  }
  else{
    $("div_moteurMenu_budget").show();
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=PRIXMINIMAXI",setChmpPrixToReglette); 
  }
}

function rechargeCommune(){
  if($("inpt_g").checked==true){
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=LISTALLCOMMUNE&val=G",setChmpCommuneAutoCompleter);
  }
  else if($("inpt_h").checked==true){
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=LISTALLCOMMUNE&val=H",setChmpCommuneAutoCompleter);
  }
  else if($("inpt_t").checked==true){
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=LISTALLCOMMUNE&val=T",setChmpCommuneAutoCompleter);
  }
  else if($("inpt_e").checked==true){
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=LISTALLCOMMUNE&val=E",setChmpCommuneAutoCompleter);
  }
  else if($("inpt_c").checked==true){
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=LISTALLCOMMUNE&val=C",setChmpCommuneAutoCompleter);
  }
  else{
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=LISTALLCOMMUNE",setChmpCommuneAutoCompleter);
  }
}

function definitionDuree(obj){
  $("lst_nbj").value=obj.getAttribute("valeur");
  $("sp_libDuree").innerHTML=obj.innerHTML;
  afficheEffaceCalendJs("ul_dureeSejour");  
}

function rechercheAvecDate(){
  if(($("inpt_dateDeb").value!="" && $("lst_nbj").value=="") || ($("inpt_dateDeb").value=="" && $("lst_nbj").value!="")) alertAMalibu("Vous devez d&eacute;finir la date et la dur&eacute;e de votre s&eacute;jour");  
  else setListByForm($('frmMoteurGeneral'));
}
