var DEFAULT_LAT = 37.8013;
var DEFAULT_LNG = -0.803233;
var map;

var iconoBase = new GIcon(G_DEFAULT_ICON);
iconoBase.image = "themes/callejeroPlugin/images/icomaps.png";
iconoBase.iconSize = new GSize(17,25);
iconoBase.shadow = "themes/callejeroPlugin/images/icosombra.png";
iconoBase.shadowSize = new GSize(17,25);
iconoBase.iconAnchor = new GPoint(10, 25);

var iconoServicioBase = new GIcon(G_DEFAULT_ICON);
iconoServicioBase.iconSize = new GSize(22,35);
iconoServicioBase.shadow = "themes/callejeroPlugin/images/serv_sombra.png";
iconoServicioBase.shadowSize = new GSize(22,35);
iconoServicioBase.iconAnchor = new GPoint(10, 25);

function getIcono(tipo){
  if(tipo==undefined){
    return new GIcon(iconoBase);;
  }else{
    var iconoServicio = new GIcon(iconoServicioBase);
    iconoServicio.image = "themes/callejeroPlugin/images/serv_"+tipo+".png";
    return iconoServicio;
  }
}

function initPuntos(data){
  map = new GMap2(document.getElementById("map_canvas"));
  //map.setCenter(new GLatLng(window.lat,window.lng),14);
  map.setUIToDefault();
  //map.setMapType(G_HYBRID_MAP);
  var xml = GXml.parse(data);
  var markers = xml.documentElement.getElementsByTagName("marker");
  var bounds = new GLatLngBounds();
  for (var i = 0; i < markers.length; i++) {
    var name = markers[i].getAttribute("nombre");
    var address = markers[i].getAttribute("direccion");
    var type = markers[i].getAttribute("tipo");
    var enlace = markers[i].getAttribute("enlace");
    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
    bounds.extend(point);
    var marker = createMarker(point, name, address, type, enlace);
    map.addOverlay(marker);
  }
  if(markers.length>0){
    map.setCenter(bounds.getCenter());
    map.setZoom(map.getBoundsZoomLevel(bounds));
  }else{
    map.setCenter(new GLatLng(DEFAULT_LAT, DEFAULT_LNG),14);
    var html = "<div class='contentmapa'><strong>Información del sistema:</strong><br /><br/><p>Su búsqueda no ha reportado resultados.</p></div>";
    map.openInfoWindow(map.getCenter(),html);
  }
}

function createMarker(point, name, address, type,enlace) {
  var icono = getIcono(type);
  var marker = new GMarker(point,{ icon:icono });
  var html = "<div class='contentmapa'><strong>" + name + "</strong><br /><br /><p>" + address + "</p>"
  if(enlace!=undefined){
    html+="<a href='javascript:getLocal("+enlace+");'>ver detalle</a>";
  }
  html+="</div>";
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}
