//<![CDATA[
var localizaciones = [];
var marcadores = [];

var tocado = false;
document.onclick = function() { tocado = true; };

// ------------------------------------------------------------------------------------------------------------------------
function GMapa(x, y, ampliacion, tipo) {
  if (GBrowserIsCompatible()) {
    if (tipo=='map'||tipo=='Mapa') tipo = G_NORMAL_MAP;
    else if (tipo=='sat'||tipo=='Sat.'||tipo=='Sat') tipo = G_SATELLITE_MAP;
    else tipo = G_HYBRID_MAP;

    var map = new GMap2(document.getElementById("map"));
//060516    map.addControl(new GSmallMapControl());
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
// para sacar un recuadro con mapa más grande: map.addControl(new GOverviewMapControl());

    map.setCenter(new GLatLng(y, x), ampliacion, tipo);
    dicentro(map);

    map.enableScrollWheelZoom();
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();

    { var adsManager = new GAdsManager(map, "ca-pub-2956922364325861"); adsManager.enable(); }

  }
  return map;
}
// ------------------------------------------------------------------------------------------------------------------------
function permalink(ref) {
  calle = document.forms['callejero'].elements;
  nombre = '';
  for (i=0;i<calle.length; i++) {
    val = calle[i].value;
    if (val!=''&&calle[i].type != 'hidden') {
      if (nombre!='') nombre += '%20';
      nombre += val;
    }
  }
  nombre = nombre.replace(/[, ]+/g, '%20'); if (nombre=='') nombre = 'Lugar%20indicado';
//070921  ref = ref.replace(/,{[^,]*},/g, ',{'+nombre+'},')
  ref = ref.split(','); ref[3] = nombre; ref = ref.join(',');
//070808   window.open(ref, 'gmapa');
//070808   return false;
  return ref;
}
// ------------------------------------------------------------------------------------------------------------------------
function dicentro(map) {
  GEvent.addListener(map, "moveend", function() {
    var center = map.getCenter();
    var tm = map.getCurrentMapType(); tm = tm.getName(true);
//070921    var ref = 'centro='+ center.x + ',' + center.y + ',' + map.getZoom()+',{Lugar%20indicado},,'+tm;
    var ref = 'centro='+ center.x + ',' + center.y + ',' + map.getZoom()+',Lugar%20indicado,,'+tm;
    var latLngStr = '( ' + center.x + ' , ' + center.y + ' )';
    var latLngDegStr = '( ' + decimal2grados(center.x) + ' , ' + decimal2grados(center.y) + ' )';
    document.getElementById("message").innerHTML = latLngStr+' '+latLngDegStr;
//070808    document.getElementById("permalink").innerHTML = '<A HREF=\"index.php?name=localiza&amp;'+ref+'\" ONCLICK=\"return permalink(this.href);\" STYLE=\"color: red; padding-left: 8em;\" TITLE=\"Enlace permanente\n   Una vez centrado el mapa y ajustado el zoom\n   puede usar este enlace para situar cualquier\n   direcci&oacute;n.\" ><BIG>Permalink<\/BIG><\/A>';
    alt = 'Enlace permanente\n   Una vez centrado el mapa y ajustado el zoom\n   puede usar este enlace para situar cualquier\n   direcci&oacute;n.';
    if (tocado) {
      ref = 'index.php?name=localiza&amp;'+ref;
      conraton = 'ONMOUSEOVER=\"this.href=permalink(this.href);\"';
    } else {
      ref = '' + top.location;
      ref = ref.replace(/\/[^\/]*\.htm$/g, '/index.php?name=localiza');
      conraton = '';
    }
    document.getElementById("permalink").innerHTML = '<A HREF=\"'+ref+'\" '+ conraton + ' TITLE=\"' + alt + '\" ONCLICK=\"top.location=this.href; return false;\"><IMG SRC="/comun/img/ico_permalink.gif" STYLE="vertical-align: top" ALT="' + alt + '" /><\/A>';
//091219    if (!ref.match('/poi/')) {
    if (!ref.match(/poi/gi)) {
      ref = ref.replace(/name=localiza/g, 'name=poi');
      ref = ref.replace(/_cms_localiza/g, '_cms_poi');
      tomtom = '<A HREF="'+ref+'&amp;fmt=ov2\" '+ conraton + '><IMG SRC="/comun/img/ico_tomtom.gif" STYLE="vertical-align: top" ALT="Obtener PDI para TomTom" /></A>';
      googea = '<A HREF="'+ref+'&amp;fmt=kml\" '+ conraton + '><IMG SRC="/comun/img/ico_googleearth.gif" STYLE="vertical-align: top" ALT="Obtener PDI para Google Earth" /></A>';
      oziexp = '<A HREF="'+ref+'&amp;fmt=wpt\" '+ conraton + '><IMG SRC="/comun/img/ico_oziexplorer.gif" STYLE="vertical-align: top" ALT="Obtener PDI para Ozi Explorer" /></A>';
      document.getElementById("pois").innerHTML = tomtom + googea + oziexp;
    }
  });
}
// ------------------------------------------------------------------------------------------------------------------------
function diclick(map) {
  GEvent.addListener(map, "click", function(point) {
    var latLngStr = '(' + point.x + ', ' + point.y + ')';
    document.getElementById("message").innerHTML = latLngStr;
  });
}
// ------------------------------------------------------------------------------------------------------------------------
function marcalibre(map) {
  GEvent.addListener(map, 'click', function(overlay, point) {
    if (overlay) {
      map.removeOverlay(overlay);
    } else if (point) {
      map.addOverlay(new GMarker(point));
    }
  });
}
// ------------------------------------------------------------------------------------------------------------------------
function marcador(map, point, texto, icono) {
//070831  if (!icono||icono=="") icono = "/comun/img/ico_bola.gif";
  if (!icono||icono=="") icono = "http://labs.google.com/ridefinder/images/mm_20_green.png";
  var icon = new GIcon();
  icon.image = icono;
  icon.shadow = "";
//060918  if (icono.indexOf('/comun/img/ico_')>0) { TamX = 20; TamY = 16; }

  TamX = 0; TamY = 0;
  if (icono.indexOf('&Tam=')>=0) {
    Tam = icono.substring(icono.indexOf('&Tam=')+5);
    Tam = Tam.split(/&/); Tam = Tam[0];
    Tam = Tam.split(/x/);
    TamX = Tam[0]; TamY = Tam[1]; 
  }
  else if (icono.indexOf('icopoi')>=0) { TamX = 16; TamY = 16; }
  else if (icono.indexOf('ico_')>=0) { TamX = 20; TamY = 16; }
  else if (icono.indexOf('ES_R_')>=0&&icono.indexOf('.gif')>=0) { TamX = 30; TamY = 20; }
  else if (icono.indexOf('ES_R_')>=0&&icono.indexOf('.bmp')>=0) { TamX = 24; TamY = 24; }
  else if (icono.indexOf('mm_20_')>=0) { TamX = 12; TamY = 20; }
  else if (icono.indexOf('/mapfiles/marker')>=0) { TamX = 20; TamY = 34; }
  else if (icono.indexOf('/ms/micons/')>=0) { TamX = 32; TamY = 32; }
  else if (icono.indexOf('/mapfiles/kml/')>=0) { TamX = 32; TamY = 32; }
  else { TamX = 40; TamY = 40; }

  icon.iconSize = new GSize(TamX, TamY);
  icon.shadowSize = new GSize(0, 0);
  icon.iconAnchor = new GLatLng(6, 20);
  icon.infoWindowAnchor = new GLatLng(5, 1);


  if (icono=="") var marker = new GMarker(point);
  else var marker = new GMarker(point, icon);
  map.addOverlay(marker);
      
  var html = "<b>" + texto + "</b>";
  GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
      
  return marker;
}
// ------------------------------------------------------------------------------------------------------------------------
function GPunto(map, x, y, texto, icono) {
  var point = new GLatLng(y, x);
  var marker = marcador(map, point, texto, icono);
//060830  map.addOverlay(marker);
  return marker;
}
// ------------------------------------------------------------------------------------------------------------------------
function decimal2grados(decimal) {
  if (decimal<0) signo = '-'; else signo='';
  resto = Math.abs(decimal);
  grados = Math.floor(resto); resto = 60 * (resto-grados);
  minutos = Math.floor(resto); resto = 60 * (resto-minutos);
  segundos = Math.round(resto);
  return signo + grados + '&deg; '+ minutos + "' " + segundos + '"';
}
// ------------------------------------------------------------------------------------------------------------------------
function didireccion_BAK(map, direccion, centrar) {
  geocoder = new GClientGeocoder();
  geocoder.getLatLng(direccion,
    function(point) {
      if (!point) { alert(direccion + ' no encontrada'); }
      else {
        marker = marcador(map, point, direccion, '');
        if (centrar) {
          map.setCenter(point);
          marker.openInfoWindowHtml(direccion); 
        }
      }
    }
  );
}
// ------------------------------------------------------------------------------------------------------------------------
function didireccion(map, direccion, centrar, icono) {
  if (/^\([^\)]*\)/.test(direccion)) {
    nombre = direccion.split(/\)/); nombre = nombre[0]; nombre = nombre.substring(1); if (nombre!='') nombre += "<BR>";
    direccion = direccion.replace( /^\([^\)]*\)/, '');
  } else nombre ='';
  localizaciones[direccion]=nombre;
  geocoder = new GClientGeocoder();
  geocoder.getLatLng(direccion,
    function(point) {
      if (!point) {
//061001        alert(direccion + ' no encontrada');
        if (/,/.test(direccion)) {
          nombre=localizaciones[direccion];
          direccion = "("+nombre.replace( /<BR>$/, '')+")"+direccion.replace( /^[^,]*,/, '');
          didireccion(map, direccion, centrar, icono);
        }
      } else {
        marker = marcador(map, point, localizaciones[direccion]+direccion, icono);
        if (centrar) {
          map.setCenter(point);
          marker.openInfoWindowHtml(localizaciones[direccion]+direccion); 
        }
        if (marcadores[localizaciones[direccion]]&&marcadores[localizaciones[direccion]]!="") {
          map.removeOverlay(marcadores[localizaciones[direccion]]);
        }
        marcadores[localizaciones[direccion]]=marker;
      }
    }
  );
}
// ------------------------------------------------------------------------------------------------------------------------

//]]>