
//gérer un zoom par défaut
//gérer un zoom forcé
var map     = null;
var dir     = null;
var cluster =  null;

var markers =  new Array();
var markers_mgr   =  new Array;

var paramcarte;
var gmaplang   ='fr';


var computedbounds=null;
var computedcenter=null;

var urlflux;

var debug   =  false;
var debug_msg= new Array();

var iconList=new Array();

iconList[0] = new GIcon();
iconList[0].image="design/element/default.png";
iconList[0].iconSize = new GSize(36,40);
iconList[0].iconAnchor = new GPoint(16,16);
iconList[0].infoWindowAnchor = new GPoint(16,16);

iconList[1] = new GIcon();
iconList[1].image="design/element/marker_carosserie.png";
iconList[1].iconSize = new GSize(36,40);
iconList[1].iconAnchor = new GPoint(16,16);
iconList[1].infoWindowAnchor = new GPoint(16,16);

iconList[2] = new GIcon();
iconList[2].image="design/element/marker_transmission.png";
iconList[2].iconSize = new GSize(36,40);
iconList[2].iconAnchor = new GPoint(16,16);
iconList[2].infoWindowAnchor = new GPoint(16,16);

iconList[3] = new GIcon();
iconList[3].image="design/element/marker_propulsion.png";
iconList[3].iconSize = new GSize(36,40);
iconList[3].iconAnchor = new GPoint(16,16);
iconList[3].infoWindowAnchor = new GPoint(16,16);

iconList[4] = new GIcon();
iconList[4].image="design/element/marker_direction.png";
iconList[4].iconSize = new GSize(36,40);
iconList[4].iconAnchor = new GPoint(16,16);
iconList[4].infoWindowAnchor = new GPoint(16,16);

iconList[5] = new GIcon();
iconList[5].image="design/element/marker_autres.png";
iconList[5].iconSize = new GSize(36,40);
iconList[5].iconAnchor = new GPoint(16,16);
iconList[5].infoWindowAnchor = new GPoint(16,16);




iconList[6] = new GIcon();
iconList[6].image="design/element/marker_carton.png";
iconList[6].iconSize = new GSize(36,40);
iconList[6].iconAnchor = new GPoint(16,16);
iconList[6].infoWindowAnchor = new GPoint(16,16);

iconList[7] = new GIcon();
iconList[7].image="design/element/marker_equipement.png";
iconList[7].iconSize = new GSize(36,40);
iconList[7].iconAnchor = new GPoint(16,16);
iconList[7].infoWindowAnchor = new GPoint(16,16);



iconList[8] = new GIcon();
iconList[8].image="design/element/marker_outillage.png";
iconList[8].iconSize = new GSize(36,40);
iconList[8].iconAnchor = new GPoint(16,16);
iconList[8].infoWindowAnchor = new GPoint(16,16);

iconList[9] = new GIcon();
iconList[9].image="design/element/marker_traitement.png";
iconList[9].iconSize = new GSize(36,40);
iconList[9].iconAnchor = new GPoint(16,16);
iconList[9].infoWindowAnchor = new GPoint(16,16);

iconList[10] = new GIcon();
iconList[10].image="design/element/marker_connexe.png";
iconList[10].iconSize = new GSize(36,40);
iconList[10].iconAnchor = new GPoint(16,16);
iconList[10].infoWindowAnchor = new GPoint(16,16);

iconList['X'] = new GIcon();
iconList['X'].image="design/element/marker_tous.png";
iconList['X'].iconSize = new GSize(36,40);
iconList['X'].iconAnchor = new GPoint(16,16);
iconList['X'].infoWindowAnchor = new GPoint(16,16);





//#OPTIONS

function initMap()
{
   if (GBrowserIsCompatible())
   {
      map = new GMap2(document.getElementById("map"));
      loadMarkers();
   }

}

function setMapFlux(url)
{
   urlflux=url;
   addDebugInfo("INFO","setMapFlux() - Modification de l'url : "+url);
}

function setLang(lang)
{
   gmaplang=lang;
   addDebugInfo("INFO","setLang() Modification de la langue : "+lang);
}



function loadMarkers()
{
   $.ajax({type: "GET",url: urlflux,success: function(xml)
   {
      var listem     =  $("marker",xml);
	  
	 if(listem.size()==0){
		alert("aucune des entreprises de l'annuaire ne repond au(x) critere(s) demande(s)") 
		 return;
	 }
		 
	  
      addDebugInfo("INFO","loadMarkers() - Chargement du flux terminé : "+listem.length+" points");
      paramcarte     =  $("param",xml);

      manageParam()


      var temp_computedbounds =  $("bounds",xml);
      computedbounds =  new GLatLngBounds(new GLatLng(parseFloat(temp_computedbounds.attr('minlat')),parseFloat(temp_computedbounds.attr('minlng'))),new GLatLng(parseFloat(temp_computedbounds.attr('maxlat')),parseFloat(temp_computedbounds.attr('maxlng'))));

      var temp_computedcenter =  $("center",xml);
      computedcenter =  new GLatLng(parseFloat(temp_computedcenter.attr('lat')),parseFloat(temp_computedcenter.attr('lng')))


      map.setCenter(computedcenter,13);


      $(listem).each(function()
      {
         var temp_point=new GLatLng(parseFloat($(this).attr('lat')),parseFloat($(this).attr('lng')));

         addMarker({point:temp_point,titre:$(this).attr('titre'),domaine:$(this).attr('domaine'),html:$(this).text()});
      });
      addDebugInfo("INFO","loadMarkers() - Ajout des points terminés");


      cluster=new ClusterMarker(map, { markers:markers_mgr } );
      cluster.fitMapToMarkers();
      addDebugInfo("INFO","loadMarkers() - Fin du clustering des points");

      map.setZoom(map.getBoundsZoomLevel(computedbounds));
      addDebugInfo("INFO","loadMarkers() - setZoom via computedbounds");
			//alert($("span:contains('cartographiques')").size())
   }})
}

function manageParam()
{
   if(paramcarte.attr('map_controls')==1)
   {
      addDebugInfo("INFO","manageParam() - Affichage du controle : "+paramcarte.attr('control_size'));
      switch(paramcarte.attr('control_size'))
      {
         case "small":  map.addControl(new GSmallMapControl());break;
         case "large": map.addControl(new GLargeMapControl());break;
      }
   }

   /*
   if(paramcarte.attr('overview')!="")
   {
      addDebugInfo("INFO","manageParam() - Affichage du overview : "+paramcarte.attr('overview'));
      taille=paramcarte.attr('overview').split(",")
      alert(taille[0]+","+taille[1]);
      map.addControl(new GOverviewMapControl(new GSize(taille[0],taille[1])));
   }*/


   if(paramcarte.attr('type_controls')==1)
   {

      addDebugInfo("INFO","manageParam() - Affichage du controle de type de carte");
      map.addControl(new GMapTypeControl());
   }

   if(paramcarte.attr('zoomwheel')==1)map.enableScrollWheelZoom();else map.disableScrollWheelZoom();

   // Gestion du zoom avec double-clic
   if(paramcarte.attr('dblclick_zoom')==1)map.enableDoubleClickZoom();

   // Gestion du zoom continu
   if(paramcarte.attr('continuousZoom')==1)map.enableContinuousZoom();
}

function addMarker(m)
{
	if(m.domaine !="")
	{
		var marker  = new GMarker(m.point,{icon:iconList[m.domaine]});
	}
	else
	{
 		var marker  = new GMarker(m.point,{icon:iconList[0]});
	}
	//var marker  = new GMarker(m.point);

   GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(m.html); });
   
   markers[markers.length]= {point:marker,html:m.html,titre:m.titre};
   markers_mgr[markers_mgr.length]=marker;
   //map.addOverlay(marker);
}


function removeMarkers()
{
   map.clearOverlays();
   markers =  new Array();
   markers_mgr=  new Array();
   cluster.removeMarkers();
   addDebugInfo("INFO","removeMarkers() - Suppression de tous les markers");
}

function removeMarkerById(id)
{

}

function removeMarkerByIndex(i)
{
   var marker=markers[i].point;
   marker.remove();
   markers=(i>markers.length)?false:(markers.slice(0,i).concat(markers.slice(i+1,markers.length)));
   addDebugInfo("INFO","removeMarkerByIndex() - Suppression du "+i+" ène marker");
}

function hideMarkerById(i)
{
   markers[i].point.hide();
   addDebugInfo("INFO","hideMarkerByIndex() - Masquage du "+i+" ène marker");
}

function showMarkerById(i)
{
   markers[i].point.show();
   addDebugInfo("INFO","showMarkerByIndex() - Affichage du "+i+" ène marker");
}

function mapDirectionsFromQuery(depart,arrivee)
{
   dir = new GDirections(map, document.getElementById("gmapdirdiv"));
   GEvent.addListener(dir, "load", onGDirectionsLoad);
   dir.load("from: " + depart + " to: " + arrivee,{ "locale":  gmaplang});

}

function mapDirectionsFromMarkers(i,j)
{
   dir = new GDirections(map, document.getElementById("gmapdirdiv"));
   GEvent.addListener(dir, "load", onGDirectionsLoad);
   addDebugInfo("INFO","mapDirectionsFromMarkers - trajet de : '"+markers[i].titre+"' "+markers[i].point.getLatLng().toString()+" vers : '"+markers[j].titre+"' "+markers[j].point.getLatLng().toString());
   dir.load("from: " + markers[i].point.getLatLng().lat()+","+markers[i].point.getLatLng().lng() + " to: " + markers[j].point.getLatLng().lat()+","+markers[j].point.getLatLng().lng(),{ "locale":  gmaplang});

}

//renvoi la distance en mètres entre deux points
function getDistanceFromMarkers(i,j)
{
   var point1  =  markers[i].point.getLatLng();
   var point2  =  markers[j].point.getLatLng();
   var distance=  point1.distanceFrom(point2);

   return distance;
}
function onGDirectionsLoad()
{

}

function addDebugInfo(typem,message)
{
   var nb=debug_msg.length;
   debug_msg[nb]="["+typem+"] "+message;

   var html=$("#gmapdebugdiv").html()+"<br/>"+"["+typem+"] "+message;
   $("#gmapdebugdiv").html(html);
}