
    /*Creamos la variable global map que contendr? el mapa*/
  var map = "";
  var ArrayMarcas = new Array();   
  var IconosMarcas = new Array();   
  var MarcaDestacada;
  var TextoEnlaceAnterior;
  var CenterAnterior;
  var ZoomAnterior = 0;  
  
  var LatN_Max = 0;
  var LatS_Max = 0;
  var LonE_Max = 0;
  var LonW_Max = 0;
  
  var PointsBorrados = false;
    
  var ArrayDatosOfertas = new Array();
  
  var cad_depu = "";
    
  
  
    
  function carga_Mapa() 
  {
  
////cad_depu = cad_depu + "Carga mapa<br>";
  
    if (GBrowserIsCompatible()) 
    {
      creaIconos();
      
      map = new GMap(document.getElementById("map"));
      map.addControl(new GLargeMapControl()); // controles izq
      //map.addControl(new GSmallMapControl()); // controles izq small
      map.addControl(new GMapTypeControl()); // botones map, satellite e Hybrid
            
      map.hideControls();
      
      
      GEvent.addListener(map, "mouseover", function()
                          {
                            map.showControls();
                          });
                          
      GEvent.addListener(map, "mouseout", function()
                          {
                            map.hideControls(); 
                            map.closeInfoWindow();
                          });
    
//// arr: CArgamos marcas y enlaces sólo una vez porque las cargamos todas. Por eso no hace falta volver a cargar
////   usando los eventos zoomend y moveend 
////      cargaMarcas_y_Enlaces();
   
      GEvent.addListener(map, "zoomend", function()
                          {
                            compruebaCambios();
                          });
            
      GEvent.addListener(map, "moveend", function()
                          {
                            compruebaCambios();
                          });            

                          
      // The infowindow contents can be modified as it opens.
      
      GEvent.addListener(map,"infowindowprepareopen", function(iwtabs) {
        cambiaInfoWindowZonasNatura(iwtabs);        
      });                          

      
      
      //// La diferencia entre GPoint(lng,lat)  y GLatLng(lat,lng) es que los parametros est?n cambiados
      //map.centerAndZoom(new GPoint(-68.127081,-16.511597), 15);
////alert('ZoomIni=' + ZoomIni);
      map.centerAndZoom(new GLatLng(LatitudIni,LongitudIni), ZoomIni);
      //map.setCenter(new GLatLng(-68.127081,-16.511597), 15);
      
      //map.openInfoWindow(map.getCenter(),document.createTextNode("Hello, world"));      
      
      
      //cargaMarcas_y_Enlaces();
    }
    
  }
  
  
  function cambiaInfoWindowZonasNatura(iwtabs){
        var contenido_infowindow = iwtabs[0].contentElem.innerHTML;
        var contenido_final = "";
        
        //// para no hacer nada con las marcas de las ofertas que se que tienen ese estilo
        if (contenido_infowindow.indexOf("mapaInf_cont_izq")>0){
          return;
        }        
        
        var estilos = "";
        var temp = contenido_infowindow.toLowerCase();
        ////alert (temp);
        //return;
        
        var div = temp.indexOf("<div");
        var div_fin = temp.indexOf(">",div);
        var div_cierre = temp.indexOf("</div>",div_fin);        

        
        contenido_final = "<div class=\"mapaInf_cont_izq\">";
        
        //// El título
        contenido_final = contenido_final + "<div class=\"mapaInf_tituloZona\">";
        contenido_final = contenido_final + contenido_infowindow.substring(div_fin+1,div_cierre);
        contenido_final = contenido_final + "</div>";
        
        
        div = temp.indexOf("<div",div_cierre);
        div_fin = temp.indexOf(">",div);
        div_cierre = temp.indexOf("</div",div_fin);
        ///// Con esto no hacemos nada porque es el link que sale verde
        
        //// Ahora buscamos la descripcion
        div = temp.indexOf("<div",div_cierre);
        div_fin = temp.indexOf(">",div);
        div_cierre = temp.indexOf("</div",div_fin);
                
        contenido_final = contenido_final + "<div class=\"mapaInf_desc\">";
        contenido_final = contenido_final + contenido_infowindow.substring(div_fin+1,div_cierre);
        contenido_final = contenido_final + "</div>";        
                
        
        contenido_final = contenido_final + "</div>";        
                   
        iwtabs[0].contentElem.innerHTML = contenido_final;  
  }
  
  
  
  function creaIconos()
  {

    // Icono defecto
    IconosMarcas["defecto"] = new GIcon();
    IconosMarcas["defecto"].image = "http://n2000.raar.es/cubic/ap/temas/n2000/modulos/Oferta/plantillas/imagenes/ico_cat.png";
    IconosMarcas["defecto"].shadow = "";
    IconosMarcas["defecto"].iconSize = new GSize(29, 35);
    IconosMarcas["defecto"].shadowSize = new GSize(37, 34);
    IconosMarcas["defecto"].iconAnchor = new GPoint(15, 35);    
    IconosMarcas["defecto"].infoWindowAnchor = new GPoint(16, 36);    
    IconosMarcas["defecto"].infoShadowAnchor = new GPoint(18, 25);    
/*
    // Icono categoríaX
    IconosMarcas["categoriaX"] = new GIcon();
    IconosMarcas["categoriaX"].image = "http://n2000.raar.es/cubic/ap/temas/n2000/modulos/Oferta/plantillas/imagenes/ico_catX.png";
    IconosMarcas["categoriaX"].shadow = "";
    IconosMarcas["categoriaX"].iconSize = new GSize(20, 29);
    IconosMarcas["categoriaX"].shadowSize = new GSize(37, 34);
    IconosMarcas["categoriaX"].iconAnchor = new GPoint(10, 29);    
    IconosMarcas["categoriaX"].infoWindowAnchor = new GPoint(11, 30);    
    IconosMarcas["categoriaX"].infoShadowAnchor = new GPoint(18, 25);    
*/    
    
    return true;
  }
  
   
  function compruebaCambios(){
  
    var limites = map.getBounds();
    var NivelZoom = map.getBoundsZoomLevel(limites); //Tomamos el nivel del zoom del mapa  

    if (NivelZoom >= 8 ){
      cargaEnlaces();
    }
    
    if (NivelZoom == ZoomAnterior){
      return true;
    }
    
    if (NivelZoom>=8 && ZoomAnterior<8){
      //// Poner marcas
      cargaOfertas();
      
      if (ver_zonas_natura){
        map.addOverlay(zonas_natura_geo_rss);      
      }
    }    
    
    if (NivelZoom<8 && ZoomAnterior>=8){
      //// Quitar marcas
      quitaOfertas();      
      map.removeOverlay(zonas_natura_geo_rss);    
    }
    
    
    
    ZoomAnterior = NivelZoom;
    
  }  
   
   
   

  function cargaOfertas()
  {  
    var currentTime = new Date();  
    var cad_url = "http://n2000.raar.es/cubic/ap/cubic.php?accion=Oferta_BusqXML&time99=" + currentTime.getTime();
      
////cad_depu = cad_depu + " Carga ofertas<br>";          
      
    if (id_oferta>0){
      cad_url = cad_url + "&ofer=" + id_oferta;
    }
 
    
    var cad_temp = '';
    GDownloadUrl(cad_url, function(data, responseCode)
                          {
    
////alert(data);    
                            var xml = GXml.parse(data);

                            var ofertas = xml.getElementsByTagName("ofertas")[0];
                            var ind_temp;
                            if (ofertas != null)
                            {
                              var TotalOfertas = ofertas.childNodes.length;

                              var lat_act;
                              var lon_act;
                              var titulo_act = " ";
                              var desc_breve_act = " ";
                              var promotor = " ";
                          

                              for (ind_temp = 0; ind_temp < TotalOfertas ; ind_temp++)
                              {
                                var oferta = ofertas.childNodes.item(ind_temp);
////cad_depu = cad_depu + " Hay ofertas<br>";          

                                lat_act = oferta.childNodes.item(0).childNodes[0].data;
                                lon_act = oferta.childNodes.item(1).childNodes[0].data;
                                titulo_act = oferta.childNodes.item(2).childNodes[0].data;                                
                                desc_breve_act = oferta.childNodes.item(3).childNodes[0].data;                                
                                promotor = oferta.childNodes.item(4).childNodes[0].data;                                
                              
                                ArrayDatosOfertas[ind_temp] = new Array();
                                ArrayDatosOfertas[ind_temp]['lat'] = lat_act;
                                ArrayDatosOfertas[ind_temp]['lon'] = lon_act;
                                ArrayDatosOfertas[ind_temp]['titulo'] = titulo_act;
                                ArrayDatosOfertas[ind_temp]['desc_breve'] = desc_breve_act;
                                ArrayDatosOfertas[ind_temp]['promotor'] = promotor;
                                ArrayDatosOfertas[ind_temp]['enlace'] = "no_enlace_xml";                                

                                // A?adimos las marcas
                                var point = new GLatLng(parseFloat(lat_act), parseFloat(lon_act));     
                                addMarca(point,ind_temp,titulo_act,desc_breve_act,promotor);


                              }
                            }
                            cargaEnlaces();
                          }
                )

    return true;
  
  } //// fin cargaOfertas()
   
   
  function quitaOfertas()
  {
    for (i=0;i<ArrayMarcas.length;i++){
      map.removeOverlay(ArrayMarcas[i]);
    } 
  }  
   
   

  ///// Carga los enlaces de las ofertas que se ven actualmente en el mapa
  function cargaEnlaces()
  {
    var limites = map.getBounds();
    var SW = limites.getSouthWest();
    var NE = limites.getNorthEast();
    var LatN = NE.lat();
    var LatS = SW.lat();
    var LonE = NE.lng();
    var LonW = SW.lng();    

    
////cad_depu = cad_depu + " Carga enlaces<br>";    
    
    var cad_temp;
    cad_temp = '';
    
////alert("carga enlaces ");    
    for (var ind in ArrayDatosOfertas)
    {
/*    
if (ind == 1){    
alert(" y hay ofertas ");    
}
*/
      LatTemp = ArrayDatosOfertas[ind]['lat'];
      LonTemp = ArrayDatosOfertas[ind]['lon'];      
   
      if (LatTemp<LatN && LatTemp>LatS && LonTemp<LonE && LonTemp>LonW)
      {
      
////      cad_depu = cad_depu + " " + LatTemp + " " + LonTemp;
        //cad_temp += '<div class="mapa_enlaces_texto"><a onmouseout="GEvent.trigger(ArrayMarcas[' + ind + '],\'mouseout\');" onmouseover="GEvent.trigger(ArrayMarcas[' + ind + '],\'mouseover\');" href="' + ArrayDatosAlojamientos[ind]['enlace'] + '" id="enlace_' + ind + '">' + ArrayDatosAlojamientos[ind]['texto'] + '</a></div>';
        ////cad_temp += '<div class="mapa_enlaces_texto"><a href="' + ArrayDatosOfertas[ind]['enlace'] + '" id="enlace_' + ind + '">' + ArrayDatosOfertas[ind]['titulo'] + '</a></div>';
        cad_temp += '<div class="mapa_enlaces_texto">' + ArrayDatosOfertas[ind]['titulo'] + '</div>';        
      }
    }    

    
    if (cad_temp == ""){
////alert (" pero no hay enlaces");    
////alert("no hay enlaces");
      cad_temp = "Nada que mostrar";
    }
    
    
////    alert ("depu: " + depu);
    

    document.getElementById("mapa_enlaces").innerHTML = cad_temp;    

////    document.getElementById("depuracion").innerHTML = cad_depu;        

    
    
/*    
    document.getElementById("mapa_enlaces").visibility = "hidden";
    document.getElementById("mapa_enlaces").visibility = "visible";    
*/    
    return true;        
  }
  
  
  
  
  

  function addMarca(point,num_marca,titulo,desc_breve,promotor)
  {
    var ico_actual;
    
    ico_actual = "default";
    
  
    //ArrayMarcas[num_marca] = new GMarker(point,{icon: IconosMarcas["defecto"], title:num_marca});
    //ArrayMarcas[num_marca] = new GMarker(point,{icon: IconosMarcas["aceituna1"]});
    ArrayMarcas[num_marca] = new GMarker(point,{icon: IconosMarcas[ico_actual]});
////arr    ArrayMarcas[num_marca].enlace = enlace;
    
    GEvent.addListener(ArrayMarcas[num_marca], "click", function(){
                          //location.href = enlace;
                          //alert(enlace);
                          //ArrayMarcas[num_marca].openInfoWindowHtml('<div style="width: 150px;"><b>' + nombre_casa + "</b><br>" + desc_casa + "<br><a href=" + enlace + ">[m?s informaci?n]</a></div>");
                          ArrayMarcas[num_marca].openInfoWindowHtml(infoMarca(titulo,desc_breve,promotor));
                          });      
                          
     
    map.addOverlay(ArrayMarcas[num_marca]);
    
  }
  

  
  function infoMarca(titulo,desc_breve,promotor)
  { 
    var cad_temp;
    cad_temp = "";
    
    // Contenedor izquierdo
    // inicio contenedor izquierdo
    cad_temp = cad_temp + "<div class='mapaInf_cont_izq'>";
    
    // titulo y descripcion
////    cad_temp = cad_temp + "<div class='mapaInf_titulo'>" + titulo + "("+promotor+")</div>";
    cad_temp = cad_temp + "<div class='mapaInf_titulo'>" + titulo + "</div>";
    
    cad_temp = cad_temp + "<div class='mapaInf_desc'>" + promotor + "</div>";
    
    /*
         * La descripción la quito porque ocupa mucho espacio y el recuadro con la información de la marca sale muy grande.
    cad_temp = cad_temp + "<div class='mapaInf_desc'>" + desc_breve + "</div>";
    */
    // fin contenedor izquierdo
    cad_temp = cad_temp + "</div>";
    
    
    return cad_temp;
    
  }  //// fin de "infoMarca"
  



