﻿var isExplorer = (typeof window.ActiveXObject != 'undefined');
var isMozilla = (typeof document.implementation != "undefined") && (typeof document.implementation.createDocument != "undefined");


var map = null;
var map2 = null;
var marker;
var arrEdificios = [];
var currentZoom;
var latlngEntity;
var infowindowMain;
var arrInfowindow;
var markersArray=[];
var contentEntityArray = [];
var latlngbounds = new google.maps.LatLngBounds();
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
var infoWindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();
var content1;

var imageEntityActivo = new google.maps.MarkerImage('App_Themes/images/mapsEstudiosCaveActivo.png',
                            new google.maps.Size(56, 42),
                            new google.maps.Point(0, 0),
                            new google.maps.Point(28, 38));

var imageEntityInactivo = new google.maps.MarkerImage('App_Themes/images/mapsEstudiosCaveInactivo.png',
                            new google.maps.Size(56, 42),
                            new google.maps.Point(0, 0),
                            new google.maps.Point(28, 38));

var shadow = new google.maps.MarkerImage('App_Themes/images/mapsEstudiosCaveSombra.png',
                            new google.maps.Size(56, 42),
                            new google.maps.Point(0, 0), 
                            new google.maps.Point(28, 38));

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function edificio(idEdificio, nombre, lat, lng, contentInfo, urlCorta) {

 
    this.idEdificio = idEdificio;
    this.nombre = nombre;
    this.lat = lat;
    this.lng = lng;
    this.contentInfo = contentInfo;
    this.urlCorta = urlCorta;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function loadMap(intZoom) {


    if (document.getElementById("SlideMaps").innerHTML != "")
    {
        return false;
    }
    deleteOverlays();
    currentZoom = intZoom;

  //  if (arrEdificios == null || arrEdificios.length == 0)
        loadEdificios(intZoom);
  /*  else
        loadDataEdificios(intZoom);*/
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function loadEdificios(intZoom)
{
 
   
 var strAjax = "";
    strAjax = "AJAX_SEARCH_EDIFICIOS_INFO";
    $.ajax({
        type: "POST",
        url: document.location.href,
        data: "ajaxRequest=" + strAjax + "&xmlhttp=true",
        datatype: "xml",
        success: function (xml)
        {
            objXmlPageEdificiosInfo = xml;

            var edif = objXmlPageEdificiosInfo.getElementsByTagName("edificio");

            if (edif.length == 4)
                arrEdificios = new Array(6);
            else
            {
                var countCentroVia = 0;
                for (i = 0; i < edif.length; i++)
                {
                    if (isExplorer)
                        idEd = edif[i].getElementsByTagName("idEdificio")[0].text;
                    else
                        idEd = edif[i].getElementsByTagName("idEdificio")[0].childNodes[0].nodeValue;

                    if (eval(idEd) == 7)
                    {
                        countCentroVia++;
                        break;
                    }
                }
                if (countCentroVia == 0)
                    arrEdificios = new Array(edif.length);
                else
                    arrEdificios = new Array(edif.length + 2);
            }

            var countArray = 0;
            // edificio aida
            for (i = 0; i < edif.length; i++)
            {

                var contentInfo = "";
                if (isExplorer)
                    idEd = edif[i].getElementsByTagName("idEdificio")[0].text;
                else
                    idEd = edif[i].getElementsByTagName("idEdificio")[0].childNodes[0].nodeValue;

                if (eval(idEd) == 4)
                {
                    if (isExplorer)
                    {
                        contentInfo += "<div style=\"font-weight:bold;font-size:12px\">" + edif[i].getElementsByTagName("nombre")[0].text + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("direccion")[0].text + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("telefono")[0].text + "</div>";
                        contentInfo += "<div style=\"\"><a href='mailto:" + edif[i].getElementsByTagName("email")[0].text + "' class='linkNull'>" + edif[i].getElementsByTagName("email")[0].text + "</a></div>";
                    }
                    else
                    {
                        contentInfo += "<div style=\"font-weight:bold;font-size:12px\">" + edif[i].getElementsByTagName("nombre")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("direccion")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("telefono")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\"><a href='mailto:" + edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "' class='linkNull'>" + edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "</a></div>";
  
                    }
             

                    arrEdificios[countArray] = new edificio(4, "Edificio Aida", 41.652551, -0.886783, contentInfo, "edificio-aida");
                    countArray++;
                }  // edificio verdi
                else if (eval(idEd) == 5)
                {
                    if (isExplorer)
                    {
                        contentInfo += "<div style=\"font-weight:bold;font-size:12px\">" + edif[i].getElementsByTagName("nombre")[0].text + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("direccion")[0].text + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("telefono")[0].text + "</div>";
                        contentInfo += "<div style=\"\"><a href='mailto:" + edif[i].getElementsByTagName("email")[0].text + "' class='linkNull'>" + edif[i].getElementsByTagName("email")[0].text + "</a></div>";
  
                    }
                    else
                    {
                        contentInfo += "<div style=\"font-weight:bold;font-size:12px\">" + edif[i].getElementsByTagName("nombre")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("direccion")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("telefono")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\"><a href='mailto:" + edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "' class='linkNull'>" + edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "</a></div>";
  
                    }


                    arrEdificios[countArray] = new edificio(5, "Edificio Verdi", 41.65222052958531, -0.8848768472671509, contentInfo, "edificio-verdi");
                    countArray++;
                } // edificio trovador
                else if (eval(idEd) == 6)
                {
                    if (isExplorer)
                    {
                        contentInfo += "<div style=\"font-weight:bold;font-size:12px\">" + edif[i].getElementsByTagName("nombre")[0].text + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("direccion")[0].text + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("telefono")[0].text + "</div>";
                        contentInfo += "<div style=\"\"><a href='mailto:" + edif[i].getElementsByTagName("email")[0].text + "' class='linkNull'>" + edif[i].getElementsByTagName("email")[0].text + "</a></div>";
                    }
                    else
                    {
                        contentInfo += "<div style=\"font-weight:bold;font-size:12px\">" + edif[i].getElementsByTagName("nombre")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("direccion")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("telefono")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\"><a href='mailto:" + edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "' class='linkNull'>" + edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "</a></div>";
  
                    }

                    arrEdificios[countArray] = new edificio(6, "Edificio Trovador", 41.65136676586814, -0.8698993921279907, contentInfo, "el-trovador");
                    countArray++;
                } // naves centrovia
                else if (eval(idEd) == 7)
                {
                    if (isExplorer)
                    {
                        contentInfo += "<div style=\"font-weight:bold;font-size:12px\">" + edif[i].getElementsByTagName("nombre")[0].text + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("direccion")[0].text + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("telefono")[0].text + "</div>";
                        contentInfo += "<div style=\"\"><a href='mailto:" + edif[i].getElementsByTagName("email")[0].text + "' class='linkNull'>" + edif[i].getElementsByTagName("email")[0].text + "</a></div>";
  
                    }
                    else
                    {
                        contentInfo += "<div style=\"font-weight:bold;font-size:12px\">" + edif[i].getElementsByTagName("nombre")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("direccion")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\">" + edif[i].getElementsByTagName("telefono")[0].childNodes[0].nodeValue + "</div>";
                        contentInfo += "<div style=\"\"><a href='mailto:" + edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "' class='linkNull'>" + edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue + "</a></div>";
  
                    }

                    arrEdificios[countArray] = new edificio(7, "Naves centrovia", 41.61722298052624, -1.0727119445800781, contentInfo, "naves-centrovia");
                    countArray++;
                    arrEdificios[countArray] = new edificio(7, "Naves centrovia", 41.600489293095904, -1.092667579650879, contentInfo, "naves-centrovia");
                    countArray++;
                    arrEdificios[countArray] = new edificio(7, "Naves centrovia", 41.60272364384112, -1.0844063758850098, contentInfo, "naves-centrovia");
                    countArray++;
                }
            }
            loadDataEdificios(intZoom);
        }
    });

}






//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function loadDataEdificios(intZoom) {

    if (arrEdificios.length > 0) {
        
        var myOptions = {
            zoom: intZoom,
            maxZoom: 18,
            minZoom: 7,
            center: new google.maps.LatLng(arrEdificios[0].lat, arrEdificios[0].lng),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            navigationControl: true,
            mapTypeControl: false,
            scaleControl: false,
            streetViewControl: true
        };

        map = new google.maps.Map($("#SlideMaps").get(0), myOptions);
       

        google.maps.event.addListener(map, 'bounds_changed', function ()
        {
            google.maps.event.clearListeners(map, 'bounds_changed');
        });

        google.maps.event.addListener(map, 'dragend', function () { comprobateZoom() });
        google.maps.event.addListener(map, 'zoom_changed', function () { comprobateZoom() });
        google.maps.event.addListener(map, 'click', function () { closeInfoWindow(); });

        for (var a = 0; a < arrEdificios.length; a++) 
        {
          
            var contentString = "<div style=\"font-family:'Gill Sans','Gill Sans MT',Calibri,'Trebuchet MS',sans-serif;font-size:11px;color:gray;height:80px\">";
            contentString += "<div style=\"padding:4px;margin:2px;border:0px solid #c9cfcb;border-radius:6px;-moz-border-radius:6px;-webkit-border-radius:6px;-khtml-border-radius:6px;\">";
            contentString += arrEdificios[a].contentInfo;
            contentString += "<div style=\"\"><a href='comoLlegar.aspx?Ed=" + arrEdificios[a].urlCorta + "'><b>Cómo llegar</b></a></div>";
            contentString += "</div></div>";
		                

            var imgMark = imageEntityInactivo;

            
            if (eval(document.getElementById("hdCurrentEdificio").value) > 0) {
                if (eval(arrEdificios[a].idEdificio) == eval(document.getElementById("hdCurrentEdificio").value))
                    imgMark = imageEntityActivo;
            }

            latlngEntity = new google.maps.LatLng(arrEdificios[a].lat, arrEdificios[a].lng);
         
            marker = new google.maps.Marker({
                map: map,
                position: latlngEntity,
                icon: imgMark,
                shadow: shadow
            });
            
            markersArray.push(marker);
            attachMakerMessage(marker, contentString, arrEdificios[a].idEdificio);
            contentEntityArray.push(contentString);



            //centrar mapa sobre los puntos
            if (eval(document.getElementById("hdCurrentEdificio").value) == 0 || eval(document.getElementById("hdCurrentEdificio").value) != 7)
            {
                if (a < 3)
                    latlngbounds.extend(latlngEntity);
            }
            else if (eval(document.getElementById("hdCurrentEdificio").value) > 0 && eval(document.getElementById("hdCurrentEdificio").value) == 7)
            {
                if (a >= 3)
                    latlngbounds.extend(latlngEntity);
            }
        }

        map.fitBounds(latlngbounds);
        map.setCenter(latlngbounds.getCenter());
        bounds = map.getBounds();
        map.setCenter(latlngbounds.getCenter(), map.getZoom());
       
    }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function comprobateZoom() {
  /*  if (map.getZoom() < 15 && currentZoom >= 15)
        loadMap(map.getZoom());
    else if (currentZoom < 15 && map.getZoom() >= 15)
        loadMap(map.getZoom());*/
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function deleteOverlays() {
    if (markersArray) 
    {
        for (i in markersArray)
            markersArray[i].setMap(null);

        markersArray.length = 0;
    }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function setIconDetaultMarkers() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setIcon(imageEntityInactivo);
            markersArray[i].setZIndex(0);
        }
    }
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function changeImageMarker(idEdificio) 
{
    closeInfoWindow();
    setIconDetaultMarkers();

 
    if (arrEdificios)
        for (i in arrEdificios) 
        {
            if (eval(arrEdificios[i].idEdificio) == eval(idEdificio)) 
            {
                markersArray[i].setIcon(imageEntityActivo);
               markersArray[i].setZIndex(99);
            }
        }
   }

//----------------------------------------------------------
function attachMakerMessage(marker, contentInfo, idEdificio) {
    google.maps.event.addListener(marker, 'click', function () {
        openInfoWindow(marker, contentInfo, idEdificio);
    });
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function closeInfoWindow() {
    infoWindow.close();
   
    if (directionsDisplay) {
        directionsDisplay.setMap();
        directionsDisplay.setPanel();
        document.getElementById("HowReach").innerHTML = "";
        document.getElementById("HowReach").style.display = "none";
    }

}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function openInfoWindow(marker, contentInfo, idEdificio) {
    changeImageMarker(idEdificio);
 
    infoWindow.setContent(contentInfo);
    infoWindow.open(map, marker);
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function loadEdificiosHowToGo()
{
    var intZoom = 14;

  
    deleteOverlays();

  
    var strAjax = "";
    strAjax = "AJAX_SEARCH_EDIFICIOS_INFO";
    $.ajax({
        type: "POST",
        url: document.location.href,
        data: "ajaxRequest=" + strAjax + "&xmlhttp=true",
        datatype: "xml",
        success: function (xml)
        {
            objXmlPageEdificiosInfo = xml;

            var edif = objXmlPageEdificiosInfo.getElementsByTagName("edificio");

            if (edif.length == 4)
                arrEdificios = new Array(6);
            else
            {
                var countCentroVia = 0;
                for (i = 0; i < edif.length; i++)
                {
                    if (isExplorer)
                        idEd = edif[i].getElementsByTagName("idEdificio")[0].text;
                    else
                        idEd = edif[i].getElementsByTagName("idEdificio")[0].childNodes[0].nodeValue;

                    if (eval(idEd) == 7)
                    {
                        countCentroVia++;
                        break;
                    }
                }
                if (countCentroVia == 0)
                    arrEdificios = new Array(edif.length);
                else
                    arrEdificios = new Array(edif.length + 2);
            }
            
            var countArray = 0;
            // edificio aida
            for (i = 0; i < edif.length; i++)
            {

                var contentInfo = "";
                if (isExplorer)
                    idEd = edif[i].getElementsByTagName("idEdificio")[0].text;
                else
                    idEd = edif[i].getElementsByTagName("idEdificio")[0].childNodes[0].nodeValue;

                if (eval(idEd) == 4)
                {
                    if (isExplorer)
                    {
                        contentInfo = edif[i].getElementsByTagName("direccion")[0].text + "<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("telefono")[0].text + "<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("email")[0].text + "<br/><br/>";
                    }
                    else
                    {
                        contentInfo = edif[i].getElementsByTagName("direccion")[0].childNodes[0].nodeValue; +"<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("telefono")[0].childNodes[0].nodeValue; +"<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue; +"<br/><br/>";
                    }
                    arrEdificios[countArray] = new edificio(4, "Edificio Aida", 41.652551, -0.886783, contentInfo, "edificio-aida");
                    countArray++;
                }  // edificio verdi
                else if (eval(idEd) == 5)
                {
                    if (isExplorer)
                    {
                        contentInfo = edif[i].getElementsByTagName("direccion")[0].text + "<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("telefono")[0].text + "<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("email")[0].text + "<br/><br/>";
                    }
                    else
                    {
                        contentInfo = edif[i].getElementsByTagName("direccion")[0].childNodes[0].nodeValue; +"<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("telefono")[0].childNodes[0].nodeValue; +"<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue; +"<br/><br/>";
                    }
                    arrEdificios[countArray] = new edificio(5, "Edificio Verdi", 41.65222052958531, -0.8848768472671509, contentInfo, "edificio-verdi");
                    countArray++;
                } // edificio trovador
                else if (eval(idEd) == 6)
                {
                    if (isExplorer)
                    {
                        contentInfo = edif[i].getElementsByTagName("direccion")[0].text + "<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("telefono")[0].text + "<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("email")[0].text + "<br/><br/>";
                    }
                    else
                    {
                        contentInfo = edif[i].getElementsByTagName("direccion")[0].childNodes[0].nodeValue; +"<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("telefono")[0].childNodes[0].nodeValue; +"<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue; +"<br/><br/>";
                    }
                    arrEdificios[countArray] = new edificio(6, "Edificio Trovador", 41.65136676586814, -0.8698993921279907, contentInfo, "el-trovador");
                    countArray++;
                } // naves centrovia
                else if (eval(idEd) == 7)
                {
                    if (isExplorer)
                    {
                        contentInfo = edif[i].getElementsByTagName("direccion")[0].text + "<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("telefono")[0].text + "<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("email")[0].text + "<br/><br/>";
                    }
                    else
                    {
                        contentInfo = edif[i].getElementsByTagName("direccion")[0].childNodes[0].nodeValue; +"<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("telefono")[0].childNodes[0].nodeValue; +"<br/><br/>";
                        contentInfo += edif[i].getElementsByTagName("email")[0].childNodes[0].nodeValue; +"<br/><br/>";
                    }
                    arrEdificios[countArray] = new edificio(7, "Naves centrovia", 41.61722298052624, -1.0727119445800781, contentInfo, "naves-centrovia");
                    countArray++;
                    arrEdificios[countArray] = new edificio(7, "Naves centrovia", 41.600489293095904, -1.092667579650879, contentInfo, "naves-centrovia");
                    countArray++;
                    arrEdificios[countArray] = new edificio(7, "Naves centrovia", 41.60272364384112, -1.0844063758850098, contentInfo, "naves-centrovia");
                    countArray++;
                }
            }
            loadDataEdificiosHowToGo(intZoom);
        }
    });

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function loadDataEdificiosHowToGo(intZoom)
{
    if (arrEdificios.length > 0)
    {
        var edi;

        for (i = 0; i < arrEdificios.length; i++)
        {
            if( eval(arrEdificios[i].idEdificio) == eval(document.getElementById("hdCurrentEdificio").value))
            {
                edi = arrEdificios[i];
                break;
            }
        }

        var myOptions = {
            zoom: intZoom,
            center: new google.maps.LatLng(edi.lat, edi.lng),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            navigationControl: true,
            mapTypeControl: false,
            scaleControl: false,
            streetViewControl: true
        };

        map2 = new google.maps.Map($("#MapsGoogle").get(0), myOptions);


        google.maps.event.addListener(map, 'bounds_changed', function ()
        {
            google.maps.event.clearListeners(map2, 'bounds_changed');
        });

        google.maps.event.addListener(map2, 'dragend', function () { comprobateZoom() });
        google.maps.event.addListener(map2, 'zoom_changed', function () { comprobateZoom() });
        google.maps.event.addListener(map2, 'click', function () { closeInfoWindow(); });

       
            var contentString = ""
            var imgMark = imageEntityActivo;


           
            latlngEntity = new google.maps.LatLng(edi.lat, edi.lng);

            marker = new google.maps.Marker({
                map: map2,
                position: latlngEntity,
                icon: imgMark,
                shadow: shadow
            });
    }
}
//========================================================================
function changeAddress( address, mode )
{
    document.getElementById("directionFrom").value = address;
    howToGo(mode);
}

//========================================================================
function howToGoDirection(mode)
{
    document.getElementById("HowReach").innerHTML = "";
    document.getElementById("HowReach").style.display = "none";
 document.getElementById("div_Mess_Dir").innerHTML = "";
    var initAddress = document.getElementById("directionFrom").value;
 
    if (geocoder)
    {
        geocoder.geocode({ 'address': initAddress + " , Spain" }, function (results, status)
        {
            if (status == google.maps.GeocoderStatus.OK)
            {
                if (results.length == 1)
                {
                    howToGo(mode);
                }
                else
                {
                    messageDirections = "<br/><br/>Hay varias coincidencias, sobre la búsqueda solicitada. Seleccione una:<br/><br/>";

                    for (var a = 0; a < results.length; a++)
                    {
                        messageDirections += "  <li><a href='javascript:changeAddress(\"" + results[a].formatted_address + "\",\"" + mode + "\");' >" + results[a].formatted_address + "</a></li></br>";
                    }

                    document.getElementById("div_Mess_Dir").innerHTML = messageDirections;
                    document.getElementById("div_Mess_Dir").style.display = "block";
                }
            }
            else
            {
                messageDirections = "<br/><br/>No se ha encontrado ninguna coincidencia sobre la búsqueda solicitada.";
                document.getElementById("div_Mess_Dir").innerHTML = messageDirections;
                document.getElementById("div_Mess_Dir").style.display = "block";
            }
        });
    }
    else
    {
        messageDirections = "sin geocoder";
        document.getElementById("div_Mess_Dir").innerHTML = messageDirections;
        document.getElementById("div_Mess_Dir").style.display = "block";
    }
}


//========================================================================
function howToGo(mode)
{
    var strTravelMode = google.maps.DirectionsTravelMode.DRIVING;
    if (mode == "BICYCLING")
        strTravelMode = google.maps.DirectionsTravelMode.BICYCLING;
    else if (mode == "WALKING")
        strTravelMode = google.maps.DirectionsTravelMode.WALKING;
    else if (mode == "DRIVING")
        strTravelMode = google.maps.DirectionsTravelMode.DRIVING;

    var edi;
    for (i = 0; i < arrEdificios.length; i++)
    {
        if (eval(arrEdificios[i].idEdificio) == eval(document.getElementById("hdCurrentEdificio").value))
        {
            edi = arrEdificios[i];
            break;
        }
    }


    var lat = edi.lat;
    var lon = edi.lng;

    var latlngEd = new google.maps.LatLng(lat, lon);
    var start = document.getElementById("directionFrom").value;
    var end = latlngEd;

    var request = {
        origin: start,
        destination: end,
        travelMode: strTravelMode
    };

    directionsService.route(request, function (result, status)
    {
        if (status == google.maps.DirectionsStatus.OK)
        {
            directionsDisplay.setMap(map2);
            directionsDisplay.suppressMarkers = true;
            directionsDisplay.setDirections(result);
            directionsDisplay.setPanel(document.getElementById("HowReach"));
            document.getElementById("HowReach").style.display = "inline";
        }
    });
}
