﻿
//function GoogleDisponible() {
// alert(GBrowserIsCompatible());
// return GBrowserIsCompatible();
//}

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;
        }
        
        //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]));
            }
        }
    }
}

function createMarker(hotel) {
    hotel.marker = new GMarker(hotel.LatLng, hotel.Icon);
    var nombre = GMnombre;
    var direccion = GMdireccion;
    var masinfo = GMmasinfo;
    GEvent.addListener(hotel.marker, "click", function() {
        hotel.marker.openInfoWindowHtml('<Table> <tr> <td width="150" class="texto"><b>' + hotel.nombre + "</b><p>" + (hotel.direccion) + ' </p> <a href="#linkHotel' + hotel.Cbu + '">' + 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) {
    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;
    
    //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";
}
