var ongletAffiche=0;

function oninit1(){
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=LISTALLCOMMUNE&val=G",window.setChmpCommuneAutoCompleter);
    appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=PRIXMINIMAXI",window.setChmpPrixToReglette);
    majNombreStructure();
    setActionSurOnglet()
    if(window.gmap) iteaGMAP_withLstCurr();
    setLinkVisuCarto();
    masqueBudget()
}

function setLinkVisuCarto(){
    if (window.iteaGMAP_setElemDeListeClickToVisuCarte)
    iteaGMAP_setElemDeListeClickToVisuCarte({
        callback:function(){
            afficheOngletCarto();
            $("div_liste_titre").scrollTo();
        },
        imgPicto:"/img/punaise_on.png",
        zoom:10} 
    );
}

function majNombreStructure(){
    if (document.getElementById("inpt_g").checked)affParamCGICurrInObjById("divNbMenu_G","NBCURRINLST","G","(",")");
    else document.getElementById("divNbMenu_G").innerHTML="";
    if (document.getElementById("inpt_h").checked)affParamCGICurrInObjById("divNbMenu_H","NBCURRINLST","H","(",")");
    else document.getElementById("divNbMenu_H").innerHTML="";
    if (document.getElementById("inpt_t").checked)affParamCGICurrInObjById("divNbMenu_GE","NBCURRINLST","GE,GS,SE","(",")");
    else document.getElementById("divNbMenu_GE").innerHTML="";
    //if (document.getElementById("inpt_gg").checked)affParamCGICurrInObjById("divNbMenu_GS","NBCURRINLST","SE","(",")");
    //else document.getElementById("divNbMenu_GS").innerHTML="";
    if (document.getElementById("inpt_e").checked)affParamCGICurrInObjById("divNbMenu_E","NBCURRINLST","E","(",")");
    else document.getElementById("divNbMenu_E").innerHTML="";
    if (document.getElementById("inpt_c").checked)affParamCGICurrInObjById("divNbMenu_CA","NBCURRINLST","CA,CC,AN,CV,RT,VV,PR","(",")");
    else document.getElementById("divNbMenu_CA").innerHTML="";
}

function masqueBudget(){
  if (($("inpt_h").checked)&&(!$("inpt_g").checked)&&(!$("inpt_t").checked)&&(!$("inpt_gg").checked)&&(!$("inpt_e").checked)&&(!$("inpt_c").checked)) $("div_moteurMenu_budget").style.display="none";
  else $("div_moteurMenu_budget").style.display="block";
}

function onRefreshList(){
    setActionSurOnglet();
    majNombreStructure();
    effaceCarto();
    traiteGMap();
   if (ongletAffiche==0)afficheOngletListe1();
   if (ongletAffiche==1 || ongletAffiche==2)afficheOngletListe2();
   masqueBudget()
}

function afficheOngletListe1(){
    ongletAffiche=0;
    document.getElementById("div_liste1").style.display="block";
    effaceCarto();
    document.getElementById("div_liste2").style.display="none";
    document.getElementById("div_liste_critereTriHaut").style.display="block";
    document.getElementById("div_liste_navigListeQuePageAjxHaut").style.display="block";
    document.getElementById("div_liste_critereTriBas").style.display="block";
    document.getElementById("div_liste_navigListeQuePageAjxBas").style.display="block";
       
    setClassForObj(document.getElementById("liOngletListe_liste1"),"li_ongletOn");
    setClassForObj(document.getElementById("liOngletListe_carto"),"li_onglet");
    setClassForObj(document.getElementById("liOngletListe_liste2"),"li_onglet");
}

function afficheOngletListe2(){
    ongletAffiche=2;
    document.getElementById("div_liste2").style.display="block";
    effaceCarto();
    document.getElementById("div_liste1").style.display="none";
    document.getElementById("div_liste_critereTriHaut").style.display="block";
    document.getElementById("div_liste_navigListeQuePageAjxHaut").style.display="block";
    document.getElementById("div_liste_critereTriBas").style.display="block";
    document.getElementById("div_liste_navigListeQuePageAjxBas").style.display="block";
        
    setClassForObj(document.getElementById("liOngletListe_liste1"),"li_onglet");
    setClassForObj(document.getElementById("liOngletListe_carto"),"li_onglet");
    setClassForObj(document.getElementById("liOngletListe_liste2"),"li_ongletOn");
}

function afficheOngletCarto(){
    ongletAffiche=1;
    document.getElementById("div_liste1").style.display="none";
    document.getElementById("div_liste2").style.display="none";
    document.getElementById("div_liste_critereTriHaut").style.display="none";
    document.getElementById("div_liste_navigListeQuePageAjxHaut").style.display="none";
    document.getElementById("div_liste_critereTriBas").style.display="none";
    document.getElementById("div_liste_navigListeQuePageAjxBas").style.display="none";

    setClassForObj(document.getElementById("liOngletListe_liste1"),"li_onglet");
    setClassForObj(document.getElementById("liOngletListe_carto"),"li_ongletOn");
    setClassForObj(document.getElementById("liOngletListe_liste2"),"li_onglet");

    afficheCarte();

}

function setActionSurOnglet(){
   document.getElementById("liOngletListe_liste1").onclick=function (evt){afficheOngletListe1();}
   document.getElementById("liOngletListe_liste2").onclick=function (evt){afficheOngletListe2();}
   document.getElementById("liOngletListe_carto").onclick=function (evt){afficheOngletCarto();}
     
   if (ongletAffiche==0)afficheOngletListe1();
   if (ongletAffiche==1)afficheOngletCarto();
   if (ongletAffiche==2)afficheOngletListe2();
 
}


function traiteGMap(){
    if (gmap)gmap.reload();
    iteaGMAP_withLstCurr();
    setLinkVisuCarto();
 }


function effaceCarto(){
    if($("div_carto")){$("div_carto").style.display="none";}
}
function afficheCarte(){
    document.getElementById("div_carto").style.display="block";
    if(window.gmap)window.gmap.checkResize();    
    if(window.gmap)window.gmap.resetCentre();
}

function setCGIForLstCommune(li){
    if ( navigator.appName == 'Microsoft Internet Explorer' ){
      var infos=li.innerText.split("(");
    }
    else{
      var infos=li.textContent.split("(");
    }
    document.getElementById("inpt_ville").value=infos[0];
    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=infos[0];
    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&eacute;ponse"+suffixe+")");
    }
    
    /**** 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);
      
      $("infoPrix_prixMini").innerHTML=pxMini+"&nbsp;&euro;";
      $("infoPrix_prixMaxi").innerHTML=pxMaxi+"&nbsp;&euro;";
      
      for (var i=pxMini;i<=3000;i+=25)vals.push(i);
      new Control.Slider(reglette.select('.handle'), reglette, {
          range: $R(0, 3000),
          sliderValue: [pxMini, pxMaxi],
          restricted: true,
          values: vals,
          onSlide: function(values) {
            $("infoPrix_prixMini").innerHTML=values[0];
            $("infoPrix_prixMaxi").innerHTML=values[1];
          },
          onChange: function(values) { 
            $("inpt_pxmini").value=values[0];
            $("inpt_pxmaxi").value=values[1];
            setListByForm($('frmMoteurGeneral'));         
          }
        });
       }
}


