
var points = [];
var liste = [];
var _xmlHttp = null;
var marker = [];
var markers = [];
var counter = 0;
var sidebar_html = "";
var marker_html = [];
var to_htmls = [];
var from_htmls = [];
var icon = [];
var map = null;
var paramCarte = [];


icon["drag"] = new GIcon();
icon["drag"].iconSize = new GSize(32,32);
icon["drag"].iconAnchor = new GPoint(16,16);
icon["drag"].infoWindowAnchor = new GPoint(16,16);


function getXMLHTTP(){var xhr=null;
if(window.XMLHttpRequest){xhr = new XMLHttpRequest();}else if(window.ActiveXObject){
   try {xhr = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {
      try {xhr = new ActiveXObject("Microsoft.XMLHTTP");} catch (e1) {xhr = null;}
}}else {location.href+=(location.href.indexOf("?")!=-1)?("&ajax=non"):("?ajax=non");}return xhr;}



function traiteXmlMap(xmlDoc){


   var iconListe=xmlDoc.getElementsByTagName("icone");


    for (var i=0; i < iconListe.length; ++i) {
         n=iconListe[i].getAttribute("id")
         icon[n] = new GIcon();
         icon[n].image = iconListe[i].getAttribute("icone");
         // si on gere plus tard les ombre
      // icon[n].shadow =  iconListe[i].getAttribute("icone");
      // icon[n].shadowSize = new GSize(20,20);

         icon[n].iconSize = new GSize(iconListe[i].getAttribute("iconew"),iconListe[i].getAttribute("iconeh"));
                  icon[n].iconAnchor = new GPoint(iconListe[i].getAttribute("iconew")/2,iconListe[i].getAttribute("iconeh")*1);
         //icon[n].iconAnchor = new GPoint(0,0);
         icon[n].infoWindowAnchor = new GPoint(iconListe[i].getAttribute("iconew")/2,iconListe[i].getAttribute("iconeh")*1);
         //icon[n].infoWindowAnchor = new GPoint(0,0);
    }


   var markersXml=xmlDoc.getElementsByTagName("marker");
   var markersArray=[];

    for (var i=0; i < markersXml.length; ++i) {
         markersArray[i]=[];
         markersArray[i]['lat']=parseFloat(markersXml[i].getAttribute("lat"));
         markersArray[i]['lng']=parseFloat(markersXml[i].getAttribute("lng"));
         markersArray[i]['titre']=markersXml[i].getAttribute("titre");
         markersArray[i]['icon']=markersXml[i].getAttribute("icon");
         markersArray[i]['draggable']=markersXml[i].getAttribute("draggable");
         markersArray[i]['html']=markersXml[i].firstChild.data;
    }
      return(markersArray);
}




function onLoad() {
if (GBrowserIsCompatible()) {
   var mapObj = document.getElementById("map");



   if (mapObj != "undefined" && mapObj != null) {


   map = new GMap2(document.getElementById("map"));
   map.setCenter(new GLatLng(initCenter[0],initCenter[1]),initCenter[2],initCenter[3])


   paramCarte[0]=type=arguments[0];

  // alert(paramCarte[0])

   if(type){valeur=""
   for(i=1;i<arguments.length;i++){
   paramCarte[1]=valeur+=arguments[i]

  if(i<arguments.length-1)valeur+=",";

   }

   url=_adresseRecherche+"?"+type+"="+valeur;}
   else{url=_adresseRecherche;}


   if(_xmlHttp&&_xmlHttp.readyState!=0){_xmlHttp.abort()}
   _xmlHttp=getXMLHTTP();
   if(_xmlHttp){
      _xmlHttp.open("GET",url,true);
      _xmlHttp.onreadystatechange=function() {
         if(_xmlHttp.readyState==4&&_xmlHttp.responseXML) {


            listXml=_xmlHttp.responseXML
               liste = traiteXmlMap(listXml);
            if(liste.length==0)return;




            // on repositionne ici
            center=listXml.getElementsByTagName("center")[0];
            bounds=listXml.getElementsByTagName("bounds")[0];
            param=listXml.getElementsByTagName("param")[0];

            map.setCenter(new GLatLng(parseFloat(center.getAttribute("lat")), parseFloat(center.getAttribute("lng"))), 8, eval(param.getAttribute('map_type')));
            var bds = new GLatLngBounds(new GLatLng(parseFloat(bounds.getAttribute("minlat")),parseFloat(bounds.getAttribute("minlng"))),new GLatLng(parseFloat(bounds.getAttribute("maxlat")),parseFloat(bounds.getAttribute("maxlng"))));
            map.setZoom(map.getBoundsZoomLevel(bds));

            // on ajoute les parametre de base ici
            if(param.getAttribute('map_controls')){
            switch(param.getAttribute('control_size')){
               case "small":  map.addControl(new GSmallMapControl());break;
               case "large": map.addControl(new GLargeMapControl());break;
            }
            }


            //map.addControl(new GMapTypeControl());


            if(param.getAttribute('overview')){
               taille=param.getAttribute('overview').split(",")
               map.addControl(new GOverviewMapControl(new GSize(taille[0],taille[1])));
            }

            map.enableScrollWheelZoom();

            if(param.getAttribute('dblclick_zoom'))map.enableDoubleClickZoom();
            if(param.getAttribute('continuousZoom'))map.enableContinuousZoom();


              for (var i = 0; i < liste.length; i++) {
              if(liste[i]["titre"]){
                  point = new GLatLng(liste[i]["lat"],liste[i]["lng"]);


                  if(liste[i]['draggable']==1){
                    marker[i] = createMarker(point,liste[i]["titre"],liste[i]["html"],'drag', true);
                    GEvent.addListener(marker[i], "dragend"  , function() {var P=this.getPoint();document.formulaire.lon.value=P.x;document.formulaire.lat.value=P.y});
                  }else{

                     marker[i] = createMarker(point,liste[i]["titre"],liste[i]["html"],liste[i]["icon"], false);
                  }
                 map.addOverlay(marker[i]);
              }
              }

                if(document.getElementById("sidebar")!=undefined){document.getElementById("sidebar").innerHTML=sidebar_html}
         }
      }
   _xmlHttp.send(null);

   }

}
}

}


function click_sidebar(idx) {
   markers[idx].openInfoWindowHtml(marker_html[idx]);
}


function showInfoWindow(idx,html) {
map.centerAtLatLng(points[idx]);
markers[idx].openInfoWindowHtml(html);
}
function tohere(idx) {
markers[idx].openInfoWindowHtml(to_htmls[idx]);
}
function fromhere(idx) {
markers[idx].openInfoWindowHtml(from_htmls[idx]);
}
