﻿function ToggleLink(idSpan, MensajeMostrar, MensajeEsconder) {
    var spanLink = document.getElementById(idSpan);
    if (spanLink) {
        if (spanLink.innerHTML == MensajeEsconder) {
            spanLink.innerHTML = MensajeMostrar;
        }
        else {
            spanLink.innerHTML = MensajeEsconder;
        }
    }
}

var bounds;

try {
    bounds = new GLatLngBounds();
}
catch (e) {}

var htMapas = new Array();
var htMapasGlobal = new Array();

function ToggleMap(latitud, longitud, idDiv, address, hotelName) {
    if (GoogleDisponible) {
        var DivMapa = document.getElementById('map2' + idDiv);
        if (DivMapa && latitud != '' && latitud*1 != 0) {
            if (DivMapa.style.display == 'none') {
                
                // Lo abrimos
                DivMapa.style.display = '';
                if (!htMapas[idDiv]) {
                
                    // Lo cargamos por primera vez
                    
                    htMapas[idDiv] = true;
                    loadMap(latitud, longitud, idDiv, address, hotelName);
                }
            }
            else {
                
                // Lo cerramos
                
                DivMapa.style.display = 'none';
            }
        }
        else {
            alert("Mapa no disponible");
        }
    }
    else {
        alert("Sorry Google Unavaliable.")
    }
}

function CargarMapaGlobal() {
    if (BusquedaTerminada) {
        if (GoogleDisponible) {
            toggle('map2Global');
            if (!htMapas['map2Global']) {
            
                // Lo cargamos por primera vez
                
                htMapas['map2Global'] = true;
                loadGlobalMap();
            }
        }
        else {
            alert("Sorry Google Unavaliable.")
        }
    }
    else {
        alert("Please wait the end of search.")
    }
}

function loadGlobalMap() {
    if (GBrowserIsCompatible()) {
        G_NORMAL_MAP.getName = function(short) {
            if (short) {
                return "NOR";
            }
            return GMmap;
            /*he cambiado esto: return GMMap; por:  return GMmap; daba error pq la M estaba en mayusculas*/
        }
        G_HYBRID_MAP.getName = function(short) {
            if (short) {
                return "HIB";
            }
            return GMhybrid;
        }
        G_SATELLITE_MAP.getName = function(short) {
            if (short) {
                return "SAT";
            }
            return GMsatellite;
        }
        G_PHYSICAL_MAP.getName = function(short) {
            if (short) {
              return "PHY";
            }
            return GMphysical;
        }
        
        //Si no hay hoteles, mostrar mensaje y cerrar ventana.
        
        if (!htMapasGlobal.length) {
            alert("Map not available for these hotels.");
        }
        
        //Declarar el mapa con los controles
        
        map = new GMap2(document.getElementById("mapGlobal"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.addControl(new GOverviewMapControl());
        
        //Hacer calculos para determinar los puntos para comenzar.
        
        startLat = (bounds.getSouthWest().lat() + bounds.getNorthEast().lat()) / 2
                   startLng = (bounds.getSouthWest().lng() + bounds.getNorthEast().lng()) / 2
                              startZoom = map.getBoundsZoomLevel(bounds) - 1;
        map.setCenter(new GLatLng(startLat, startLng), startZoom);
        
        //Crear los marcadores para los hoteles
        
        for (i = 0; i < htMapasGlobal.length ; i++) {
            if (htMapasGlobal[i].visible && htMapasGlobal[i].lat != 0 && htMapasGlobal[i].lng != 0) {
                map.addOverlay(createMarker(htMapasGlobal[i]));
            }
        }
        
        cargarMoreControl();
    }
}

function createMarker(hotel) {
    hotel.marker = new GMarker(hotel.LatLng, hotel.Icon);
    var nombre = GMnombre;
    var direccion = GMdireccion;
    var masinfo = GMmasinfo;
    GEvent.addListener(hotel.marker, "mouseover", function() {
      hotel.marker.openInfoWindowHtml('<table><tr><td width="150" class="texto"><b>' + hotel.nombre + "</b><p>" + (hotel.direccion) + ' </p> <a href="' + hotel.link + '" target="_blank">' + masinfo + '</a> </td> <td> <img src="' + hotel.foto + '" width="80"></td></tr></table>');
    });
    return hotel.marker
}

function loadMap(latitud, longitud, idDiv, address, hotelName) {
    if (GBrowserIsCompatible()) {
        G_NORMAL_MAP.getName = function(short) {
            if (short) {
                return "NOR"
            }
            return GMmap;
        }
        G_HYBRID_MAP.getName = function(short) {
            if (short) {
                return "HIB"
            }
            return GMhybrid;
        }
        G_SATELLITE_MAP.getName = function(short) {
            if (short) {
                return "SAT"
            }
            return GMsatellite;
        }
        var Lng = longitud;
        var Lat = latitud;
        var point = new GLatLng(Lat*1, Lng*1);
        var marker = new GMarker(point);
        var map = new GMap2(document.getElementById('map' + idDiv));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.setCenter(point, 15);
        map.addOverlay(marker);
    }
}

function hotelMarker(Cbu, latitude, longitud, direccion, nombre, letra, googleCode, exactitud, foto, link) {
    if ((exactitud != 100))
        this.visible = true;
    else
        this.visible = false;
    this.Cbu = Cbu;
    this.lat = (latitude == '') ? 0 : latitude.replace(",", ".") * 1;
    this.lng = (longitud == '') ? 0 : longitud.replace(",", ".") * 1;
    this.direccion = direccion;
    this.nombre = nombre;
    this.LatLng = new GLatLng(this.lat, this.lng);
    this.marker = new GMarker(this.LatLng);
    this.googleCode = googleCode;
    this.exactitud = exactitud;
    this.foto = foto;
    this.link = link;
    
    //Si el usario pulso zoom, esto se cambia a true. Pulsando el zoom una ves mas
    //le deja con el zoom que comenzo el mapa.
    
    this.zoomCerca = false;
    this.letra = letra;
    
    //Declaraciones para los icones de letras
    //Los numeros son a las especificiones de google
    
    this.Icon = new GIcon();
    this.Icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    this.Icon.iconSize = new GSize(20, 34);
    this.Icon.shadowSize = new GSize(37, 34);
    this.Icon.iconAnchor = new GPoint(9, 34);
    this.Icon.infoWindowAnchor = new GPoint(9, 2);
    this.Icon.infoShadowAnchor = new GPoint(18, 25);
    this.Icon.image = "http://www.google.com/mapfiles/marker" + this.letra + ".png";
  }

// Gestión capas
var layers = [];
function toggleLayer(i) {
  if (layers[i].Visible) {
    layers[i].hide();
  } else {
    if (layers[i].Added) {
      layers[i].show();
    } else {
      map.addOverlay(layers[i]);
      layers[i].Added = true;
    }
  }
  layers[i].Visible = !layers[i].Visible;
}
function cargarMoreControl() {
  // Clase MoreControl para gestionar las capas
  function MoreControl() { }
  MoreControl.prototype = new GControl();
  MoreControl.prototype.initialize = function(map) {
    var container = document.createElement("div");
    container.setAttribute("id", "container");
    container.style.border = "2px solid #333";
    container.style.fontSize = "12px";
    container.style.fontFamily = "Arial, sans-serif";
    container.style.width = "100px";
    container.style.backgroundColor = "#fff";
    container.style.textAlign = "center";
    container.innerHTML = "<strong>" + GMmas + "</strong>";

    var subContainer = document.createElement("div");
    subContainer.setAttribute("id", "subContainer");
    subContainer.style.display = "none";
    subContainer.style.textAlign = "left";
    subContainer.style.padding = "5px";
    for (var i = 0; i < layers.length; i++) {
      var c = layers[i].Visible ? 'checked' : '';
      subContainer.innerHTML += '<label><input type="checkbox" onclick="toggleLayer(' + i + ')" ' + c + ' /> ' + layers[i].Label + '</label><br/>';
    }
    container.appendChild(subContainer);

    map.getContainer().appendChild(container);

    GEvent.addDomListener(container, "mouseover", function() { document.getElementById("subContainer").style.display = "block"; });
    GEvent.addDomListener(container, "mouseout", function() { document.getElementById("subContainer").style.display = "none"; });

    return container;
  }

  MoreControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(285, 7));
  }

  layers[0] = new GLayer("com.panoramio.all");
  layers[0].Label = GMfotografias;
  layers[0].Visible = false;
  layers[0].Added = false;

  layers[1] = new GLayer("com.youtube.all");
  layers[1].Label = GMvideos;
  layers[1].Visible = false;
  layers[1].Added = false;

  layers[2] = new GLayer("org.wikipedia.ru");
  layers[2].Label = GMwikipedia;
  layers[2].Visible = false;
  layers[2].Added = false;

  layers[3] = new GLayer("com.google.webcams");
  layers[3].Label = GMwebcam;
  layers[3].Visible = false;
  layers[3].Added = false;

  map.addControl(new MoreControl());
  map.addMapType(G_PHYSICAL_MAP);
}
