﻿//<![CDATA[
        var map;
        var gdir;
        var geocoder = null;
        var addressMarker;
        var latitude = 43.361258;
        var longitude = -8.408643;

        window.onload = function load() 
        {
            var WINDOW_HTML = '<span style="color:Black;"><h4>Inmobiliaria Conchado Puente</h4><p>c/Médico Durán 10 Bajo.<br/>15005 A Coruña<br/>Teléfono: (+34) 981 914 705</p></span>';
        
            if (GBrowserIsCompatible()) 
            {
                var map = new GMap2(document.getElementById("map"));
                map.addControl(new GSmallMapControl());
                map.setCenter(new GLatLng(latitude, longitude), 16);
                geocoder = new GClientGeocoder();                                         

                gdir = new GDirections(map, document.getElementById("directions"));
                GEvent.addListener(gdir, "load", onGDirectionsLoad);
                GEvent.addListener(gdir, "error", handleErrors); 
                
                /*
                // Creamos el icono y especificamos la sombra, dimensiones, etc
                var baseIcon = new GIcon();
                //baseIcon.shadow = "images/gmaps_pointer.png";
                baseIcon.iconSize = new GSize(20, 34);
                //baseIcon.shadowSize = new GSize(37, 34);
                baseIcon.iconAnchor = new GPoint(9, 34);
                baseIcon.infoWindowAnchor = new GPoint(9, 2);
                baseIcon.infoShadowAnchor = new GPoint(18, 25);

                var map_pointer = new GIcon(baseIcon);
                map_pointer.image = "images/contacto/gmaps_pointer.png";
                
                markerOptions = { icon:map_pointer };
                
                var marker = new GMarker(new GLatLng(latitude, longitude), markerOptions); 
                */
                var marker = new GMarker(new GLatLng(latitude, longitude)); 
                map.addOverlay(marker);
                marker.openInfoWindowHtml(WINDOW_HTML);
                GEvent.addListener(marker, "click", function() 
                {
                    marker.openInfoWindowHtml(WINDOW_HTML);
                });
            }  
            window.onunload = GUnload;
        }


        function setDirections(pto_origen) 
        {            
            gdir.load("from: " + pto_origen + " to: " + new GLatLng(latitude, longitude), { "locale": "es_ES" });                               
        }

        function getPoint(response) {
            if (!response || response.Status.code != 200) {
                alert("No ha sido posible encontrar la dirección indicada");
            }
            else {
                place = response.Placemark[0];
                point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
                setDirections(point);
            }
        }

        function handleErrors() {
            if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
                alert("No se encuentra el lugar de origen indicado. Esto puede ser debido a que la dirección es relativamente reciente o incorrecta.");
            else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
                alert("La dirección no ha podido ser procesada satisfactoriamente");
            else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
                alert("Indique una dirección de origen");
            else if (gdir.getStatus().code == G_GEO_BAD_KEY)
                alert("Clave del mapa incorrecta");
            else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
                alert("La dirección indicada no ha podido ser encontrado");
            else alert("Error no reconocido.");

        }

        function onGDirectionsLoad() 
        {
            // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
        }

        function getRuta() {
            var dir = document.getElementById('txtOrigen').value;
            if (document.getElementById('txtCodPostal').value != '')
                dir = dir + ', ' + document.getElementById('txtCodPostal').value;
            if (document.getElementById('txtPoblacion').value != '')
                dir = dir + ', ' + document.getElementById('txtPoblacion').value;
            geocoder.getLocations(dir, getPoint);
        }

//]]>    