/**
 * @author Dedalo
 */
google.load("maps", "2.x");

var html = [];
var reasons = [];
var gdir;
var marcador;




function getDirections(){
    marcador.show();
    marcador.closeInfoWindow();
    var opts = {};
    // ==== set the start and end locations ====
    var saddr = document.getElementById("saddr").value
    var daddr = document.getElementById("daddr").value
    gdir.load("from: " + saddr + " to: " + daddr, opts);
}


// === create a GDirections Object ===
//var gdir = new GDirections(map, document.getElementById("directions"));

// === Array for decoding the failure codes ===


/*
 // === catch Directions errors ===
 GEvent.addListener(gdir, "error", function(){
 var code = gdir.getStatus().code;
 var reason = "Code " + code;
 if (reasons[code]) {
 reason = reasons[code]
 }
 
 alert("Failed to obtain directions, " + reason);
 });
 */
// Llama a esta función cuando la página se ha cargado.
function initialize(){
    var map = new google.maps.Map2(document.getElementById("map"));
    map.setCenter(new google.maps.LatLng(lat, lng), 17);
    map.addControl(new GLargeMapControl());
    var mapControl = new GMapTypeControl();
    map.addControl(mapControl);
    
    var office = new GLatLng(lat, lng);
    var tinyIcon = new GIcon();
    tinyIcon.image = "imagenes/botones_logos/logo_mapa.png";
    tinyIcon.shadow = "imagenes/botones_logos/logo_mapa_sombra.png";
    tinyIcon.iconSize = new GSize(280, 90);
    tinyIcon.shadowSize = new GSize(280, 90);
    tinyIcon.iconAnchor = new GPoint(198, 90);
    tinyIcon.infoWindowAnchor = new GPoint(198, 90);
    // Set up our GMarkerOptions object
    markerOptions = {
        icon: tinyIcon
    };
    marcador = new GMarker(office, markerOptions);
    map.addOverlay(marcador);
    
    
    // The info window version with the "to here" form open
    html = '<br>Directions: <b>To here<\/b>' +
    '<br>Start address:<form action="javascript:getDirections()">' +
    '<input type="text" SIZE=40px MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
    '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
    '<input type="hidden" id="daddr" value="' +
    name +
    "@" +
    office.lat() +
    ',' +
    office.lng() +
    '"/>';
    
    /*
    
    GEvent.addListener(marcador, "click", function(){
        marcador.hide();
        //marcador.openInfoWindowHtml(html);
    });
    
    GEvent.addListener(marcador, "mouseup", function(){
        marcador.show();
    });
    */
	gdir=new GDirections(map, document.getElementById("texto"));
	
    reasons[G_GEO_SUCCESS] = "Success";
    reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value.";
    reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
    reasons[G_GEO_UNAVAILABLE_ADDRESS] = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
    reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
    reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
    reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed.";
    reasons[G_GEO_BAD_REQUEST] = "A directions request could not be successfully parsed.";
    reasons[G_GEO_MISSING_QUERY] = "No query was specified in the input.";
    reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
    
    GEvent.addListener(gdir, "error", function(){
        var code = gdir.getStatus().code;
        var reason = "Code " + code;
        if (reasons[code]) {
            reason = reasons[code]
        }
        
        alert("Failed to obtain directions, " + reason);
    });
}

google.setOnLoadCallback(initialize);

