var map;
var iNbMarkers = 0;
var typeSelectHtl = 1;
var curTypeid = 0;
var minLatitude = 90;
var maxLatitude = -90;
var minLongitude = 180;
var maxLongitude = -180;

function displaySelectHtl(typeid, custid, hotelid, lang, userid) {
  var element = document.getElementById("selecthtl"); if (element) element.innerHTML = "";
  var element1 = document.getElementById("status"); if (element1) element1.innerHTML = msg[0];
  typeSelectHtl = typeid;
  ajaxEngine.sendRequest( 
			 'displayHotelInfo',
			 "id="+custid,		
			 "hotelid="+hotelid,
			 "lang="+lang,
			 "userid="+userid,
			 "bMoreInfo=1");
}

function creerMarker(id, hotelid, typeid, point, lang, userid) {
  var marker = new GMarker(point, getIcons(typeid));
  GEvent.addListener(marker, "click", function() {
		       marker.openInfoWindowHtml('<div id="status"></div><div id="selecthtl">'+msg[0]+'</div>');
		       displaySelectHtl(typeid, id, hotelid, "FR", userid);
		     });
  return marker;
}

function ShowMarkers(id, searchby, zoomid, lang, zoneid, bcenter, typeid, userid) {

  var bounds = map.getBounds();
  
  var url = "/scripts/recupHotelInfo.php?servlet=advsearch&zoneid="+zoneid+"&id="+id;
  GDownloadUrl(url, function(data, responseCode) {
		 var xml = GXml.parse(data);
		 
		 if (xml.documentElement) {
		   var markers = xml.documentElement.getElementsByTagName("marker");
		   for (var i = 0; i < markers.length; i++) {
		     
		     if (typeid==0 || typeid==markers[i].getAttribute(searchby)) {
		       var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
					       parseFloat(markers[i].getAttribute("lng")));
		       
		       map.addOverlay(creerMarker(id, 
						  markers[i].getAttribute("hotelid"),
						  markers[i].getAttribute(searchby),
						  point,
						  lang, userid));

		       minLatitude = Math.min(minLatitude,markers[i].getAttribute("lat"));
		       maxLatitude = Math.max(maxLatitude,markers[i].getAttribute("lat"));
		       minLongitude = Math.min(minLongitude,markers[i].getAttribute("lng"));
		       maxLongitude = Math.max(maxLongitude,markers[i].getAttribute("lng"));
		       centerLatitude = (minLatitude+maxLatitude)/2;
		       centerLng = (minLongitude+maxLongitude)/2;


		       iNbMarkers++;
		     }
		   }
		   if (bcenter) map.setCenter(new GLatLng(centerLatitude,centerLng), zoomid);
		 }
	       });
}

function ShowAllMarkers(id,lang,userid,partid) {
  
  mgr = new GMarkerManager(map, {borderPadding:1});

  // Niveau Pays (zoom 0 a 12)
  var mk = [];
  iNbMk = 0;

  for (idx=0;idx<a.length;idx++)
    {
      if (y[idx] == 0 || ((partid>0 && w[idx] == 1) || (partid == 0 && v[idx] == 1)))
	{
	  if (partid == 0 || e[idx]==1)
	    {

	      eval('mk_'+iNbMk+' = new GMarker(new GLatLng(b['+idx+'], c['+idx+']), {icon:getIcons(e['+idx+']), title:d['+idx+']});');
	      eval('GEvent.addListener(mk_'+iNbMk+',"click",function() { mk_'+iNbMk+'.openInfoWindowHtml(\'<div id="status"></div><div id="selecthtl">'+msg[0]+'</div>\');displaySelectHtl(e['+idx+'],id,\''+a[idx]+'\',\''+lang+'\',\''+userid+'\'); });');
	      eval('mk.push(mk_'+iNbMk+');');
	      iNbMk++;
	    }
	}
    }

  for (idx=0;idx<cities.length;idx++)
    {
      if ((partid>0 && w[cities[idx]] > 1) || (partid==0 && v[cities[idx]] > 1))
	{
	  if (id == 2 && u[cities[idx]] == "BEAULIEU SUR DORDOGNE") zmlev = 17; else zmlev = 13;
	  eval('mk_'+iNbMk+' = new GMarker(new GLatLng(g['+cities[idx]+'], h['+cities[idx]+']), {icon:getIcons(99), title:u['+cities[idx]+']});');
	  eval('GEvent.addListener(mk_'+iNbMk+',"click",function() { map.setCenter(new GLatLng(g['+cities[idx]+'], h['+cities[idx]+']),'+zmlev+'); });');
	  eval('mk.push(mk_'+iNbMk+');');
	  iNbMk++;
	}
    }
  mgr.addMarkers(mk, 0, 12);

  // Niveau Ville (zoom 13 a 17)
  var iNbMarkers = 0;
  var mk = [];
  for (idx=0;idx<a.length;idx++)
    {
      if (partid == 0 || e[idx]==1)
	{
	  eval('mk_'+iNbMk+' = new GMarker(new GLatLng(b['+idx+'], c['+idx+']), {icon:getIcons(e['+idx+']), title:d['+idx+']});');
	  eval('GEvent.addListener(mk_'+iNbMk+',"click",function() { mk_'+iNbMk+'.openInfoWindowHtml(\'<div id="status"></div><div id="selecthtl">'+msg[0]+'</div>\');displaySelectHtl(e['+idx+'],id,\''+a[idx]+'\',\''+lang+'\',\''+userid+'\'); });');
	  eval('mk.push(mk_'+iNbMk+');');
	  iNbMk++;
	  iNbMarkers++;
	}
    }
  mgr.addMarkers(mk, 13, 17);
  mgr.refresh();

  if (document.getElementById("nbmarkers")) { document.getElementById("nbmarkers").innerHTML = iNbMarkers; }
}

function load(id, searchby, zoomid, lang, zoneid, bcenter, typeid, userid, upzoneid,partid) {
  var minLatitude = 90;
  var maxLatitude = -90;
  var minLongitude = 180;
  var maxLongitude = -180;
   
  if (GBrowserIsCompatible()) {

    map = new GMap2(document.getElementById("map"));
     
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    if (id != 48) { map.addControl(new GOverviewMapControl()); }
    map.enableContinuousZoom();
    map.enableDoubleClickZoom(); 
    map.setCenter(new GLatLng(47, 3), 5);

    if (typeof(Long[zoneid])!="undefined" && typeof(Lat[zoneid])!="undefined") {
      map.setCenter(new GLatLng(Lat[zoneid], Long[zoneid]), zoomid);
      map.openInfoWindowHtml(new GLatLng(Lat[zoneid], Long[zoneid]), "<div id='status'></div><div id='selecthtl'>Cochez ci-dessous les types d'h&eacute;bergement recherch&eacute;s</div>");
      map.closeInfoWindow();
    } else if (typeof(Long[upzoneid])!="undefined" && typeof(Lat[upzoneid])!="undefined") {
      map.setCenter(new GLatLng(Lat[upzoneid], Long[upzoneid]), zoomid);
      map.openInfoWindowHtml(new GLatLng(Lat[upzoneid], Long[upzoneid]), "<div id='status'></div><div id='selecthtl'>Cochez ci-dessous les types d'h&eacute;bergement recherch&eacute;s</div>");
      map.closeInfoWindow();
    }
     
    ShowAllMarkers(id,lang,userid,partid);

  }
}
