var Gmap = new function()
{

  var $map;
  var options;
  
  this.init = function($map) {
    // extension des options par défaut par les metadatas de la div $map
    Gmap.options = $.extend($.maplage.defaults.gmap, $map.metadata());
    Gmap.options.mapCenter = [Gmap.options.lat, Gmap.options.lng];

    //dbg($map.metadata());

    // application du script google map à la div
    $map.jmap(Gmap.options);
    
    // raccourci vers la map
    Gmap.$map = $.jmap.GMap2;
    
    // s'il y a un objet à afficher... c'est un lieu
    if (Gmap.options.object)
      Gmap.addLieu(Gmap.options.object);
    
    // s'il y a une commune à afficher...
    if (Gmap.options.commune)
      Gmap.addCommune(Gmap.options.commune);
    
    // s'il y a un lieu à afficher...
    if (Gmap.options.lieu)
      Gmap.addLieu(Gmap.options.lieu);
    
    // s'il y a des lieux à afficher...
    if (Gmap.options.lieux)
      Gmap.addLieux(Gmap.options.lieux);
    
    // s'il y a un visio à afficher...
    if (Gmap.options.visio)
      Gmap.addVisio(Gmap.options.visio);
    
    // s'il y a des visios à afficher...
    if (Gmap.options.visios)
      Gmap.addVisios(Gmap.options.visios);
    
    // s'il faut charger en ajax d'autres points
    if (Gmap.options.url) {
      setTimeout(function() { Gmap.loadMarkers(); }, 1000);
      GEvent.addListener(Gmap.$map, "moveend", Gmap.loadMarkers);
      GEvent.addListener(Gmap.$map, "zoomend", Gmap.loadMarkers);  // FIXME ca marche po
    }
		// hack pour placer les crédits sur plusieurs lignes
	  setTimeout(function(){
		  $("#gmap > div:eq(1)").css("white-space","normal");
		}, 500);
  }
  
  this.addCommune = function(commune)
  {
    return Gmap.addMarker(
      commune.lat,
      commune.lng,
      commune.name, 
      {
        icon: Gmap.options.icons.commune,
        marker_id:  "commune_"+commune.id
      }
    );
  }
  this.addLieu = function(lieu)
  {
    return Gmap.addMarker(
      lieu.lat,
      lieu.lng,
      lieu.name, 
      {
        icon: eval("Gmap.options.icons."+lieu.type),
        marker_id:  "lieu_"+lieu.id,
        url:        lieu.url || null,
        type:       "fixed"
      }
    );
  }
  this.addLieux = function(lieux)
  {
    for (it = 0; it<lieux.length; it++) {
      Gmap.addLieu(lieux[it]);
    }
  }
  this.addVisio = function(visio)
  {
    return Gmap.addMarker(
      visio.lat,
      visio.lng,
      visio.name, 
      {
        icon:       Gmap.options.icons.visio,
        marker_id:  "visio_"+visio.id,
        url:        visio.url || null,
        type:       "fixed"
      }
    );
  }
  this.addVisios = function(visios)
  {
    for (it = 0; it<visios.length; it++) {
      Gmap.addVisio(visios[it]);
    }
  }
  
  this.addMarker = function(lat, lng, name, opt)
  {
    var marker = new PdMarker(new GLatLng(lat,lng), opt.icon || Gmap.options.icons.commune, name);
		marker.setUserData({type: opt.type || null});
		
    marker.setTooltip(name);
    
    if (opt.marker_id)
      marker.setId(opt.marker_id);
    
    marker.setOpacity(Gmap.options.marker_opacity);
    marker.setTooltipClass(Gmap.options.marker_tooltip_class);
    
    Gmap.$map.addOverlay(marker);
    
    if (opt.url)
      GEvent.addListener(marker, "click", function() {location.href = opt.url;});
		
    return marker;
  }
  
  this.getMarker = function(id)
  {
    return Gmap.$map.getMarkerById(id);
  }
  
  this.moveMarker = function(marker, lat, lng)
  {
    marker = Gmap.marker(marker);
    marker.setPoint(new GLatLng(lat, lng));
  }
  
  this.loadMarkers = function()
  {
    Gmap.options.bounds = Gmap.$map.getBounds();
    
    $.ajax({
      url:      Gmap.options.url,
      data:     {
          ax:   Gmap.options.bounds.getSouthWest().lat(),
          ay:   Gmap.options.bounds.getSouthWest().lng(),
          bx:   Gmap.options.bounds.getNorthEast().lat(),
          by:   Gmap.options.bounds.getNorthEast().lng()
      },
      success:  function(xml) {
        //var xml = GXml.parse(data);
        Gmap.removeMarkers();
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var type = markers[i].getAttribute("type");
          var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));
          var id = parseFloat(markers[i].getAttribute("id"));
          
          Gmap.addMarker(lat, lng, name, {
            url:      markers[i].getAttribute("url")
          });
        }
      },
      dataType: "xml"
    });
  }
  
  this.removeMarkers = function()
  {
    var marker = Gmap.$map.getFirstMarker();
    while (marker != null)
    {
			var data = marker.getUserData();
			var type = data.type || "unknow";
      if (type != "fixed")
        marker.remove();  
      marker = Gmap.$map.getNextMarker();
    }
  }
  
  this.marker = function(marker)
  {
    if (typeof(marker) != "object")
      var marker = Gmap.getMarker(marker);
    return marker;
  }
}