//<![CDATA[
var localizaciones = [];
var marcadores = [];

var tocado = false;
document.onclick = function() { tocado = true; };

// ------------------------------------------------------------------------------------------------------------------------
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';
  ref = ref.split(','); ref[3] = nombre; ref = ref.join(',');
  return ref;
}
// ------------------------------------------------------------------------------------------------------------------------
function dicentro_fnc(map) {
  var center = GCentroLee(map);
  var tm = GTipoMapaLee(map);
  var ref = 'centro='+ center.x + ',' + center.y + ',' + GZoomLee(map)+',Lugar%20indicado,,'+tm;
  var latLngStr = '( ' + center.x + ' , ' + center.y + ' )';
  var latLngDegStr = '( ' + decimal2grados(center.x) + ' , ' + decimal2grados(center.y) + ' )';
  document.getElementById("message").innerHTML = latLngStr+' '+latLngDegStr;
  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>';
  if (!ref.match(/poi/gi)) {
    ref = ref.replace(/name=localiza/g, 'name=poi');
    ref = ref.replace(/_cms_localiza/g, '_cms_poi');
    gmaps = '<A HREF="http://maps.google.es/maps?f=q&amp;source=s_q&amp;hl=es&amp;geocode=&amp;q=&amp;ie=UTF8&amp;ll='+center.y+','+center.x+'&amp;z='+GZoomLee(map)+'" '+ conraton + '><IMG SRC="/comun/img/ico_gmaps.gif" STYLE="vertical-align: top" ALT="Obtener enlace gmaps" /></A>';
    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 = gmaps + tomtom + googea + oziexp;
  }
}
// ------------------------------------------------------------------------------------------------------------------------
function diclick(map) {
  GEvent.addListener(map, "click", function(point) {
    var latLngStr = '(' + point.x + ', ' + point.y + ')';
    document.getElementById("message").innerHTML = latLngStr;
  });
}
// ------------------------------------------------------------------------------------------------------------------------
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 didireccionPunto(map, direccion, centrar, icono, point) {
      if (!point) {
        if (/,/.test(direccion)) {
          nombre=localizaciones[direccion];
          direccion = "("+nombre.replace( /<BR>$/, '')+")"+direccion.replace( /^[^,]*,/, '');
          didireccion(map, direccion, centrar, icono);
        }
      } else {
        txt = localizaciones[direccion]+direccion;
        txt = txt.replace( /[ ,](\d\d\d\d\d)[ ,]/gi, "<br>$1 "); 
        marker = marcador(map, point, txt, icono);
        if (centrar) {
          GCentroPon(map, point);
          GInfoWindow(map, marker, localizaciones[direccion]+direccion);
        }
        if (marcadores[localizaciones[direccion]]&&marcadores[localizaciones[direccion]]!="") {
          GPuntoQuita(map, marcadores[localizaciones[direccion]]);
        }
        marcadores[localizaciones[direccion]]=marker;
      }
}
// ------------------------------------------------------------------------------------------------------------------------
function didireccionNombre(direccion) {
  dirNom = Array();
  if (/^\([^\)]*\)/.test(direccion)) {
    dirNom['nombre'] = direccion.split(/\)/); dirNom['nombre'] = dirNom['nombre'][0]; dirNom['nombre'] = dirNom['nombre'].substring(1); if (dirNom['nombre']!='') dirNom['nombre'] += "<BR>";
    dirNom['direccion'] = direccion.replace( /^\([^\)]*\)/, '');
  } else { dirNom['nombre'] =''; dirNom['direccion'] = direccion; }
  return dirNom;
}
// ------------------------------------------------------------------------------------------------------------------------
function IconoTam(icono) {
  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; }
  Tam = Array(); Tam['x'] = TamX;  Tam['y'] = TamY;
  return Tam;
}
// ------------------------------------------------------------------------------------------------------------------------
function GZoomLee(map) { return map.getZoom(); }
function GCentroPon(map, point) { map.setCenter(point); }
// ------------------------------------------------------------------------------------------------------------------------
//]]>
