//<![CDATA[
var map;
var gdir;
var geocoder = null;
var addressMarker;
var marker;

function createMarker(point) { 
    // Create our "tiny" marker icon
    var icon = new GIcon();
    icon.image = "http://www.sirini.it/images/gmaps_logo.png";
    icon.iconSize = new GSize(50,19);
    icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(6, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);

    var marker = new GMarker(point, icon);
    //GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(finestra); });
    return marker;
}

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("googlemaps"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());        
    map.setCenter(new GLatLng(45.49923902986542, 8.948871195316315), 14, G_NORMAL_MAP);
    
    gdir = new GDirections(map, document.getElementById("googlemapsdir"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);

    marker = createMarker(map.getCenter());
    map.addOverlay(marker);
  }
}

function setDirections(inpAddress, inpCity) {
    toAddress = "45.49923902986542, 8.948871195316315";
    locale = "en";
    fromAddress = "";
    if (inpAddress != '') fromAddress += inpAddress;
    if (inpCity != '') fromAddress += (fromAddress != '' ? ',' : '') + inpCity;
    map.removeOverlay(marker);
    gdir.load("from: " + fromAddress + " to: " + toAddress,
            { "locale": locale });
    document.getElementById('googlemapsdir').style.display = '';
}

function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("L'indirizzo/paese inserito non corrisponde ad alcuna zona geografica.\nQuesto puņ essere dovuto al fatto che l'indirizzo č relativamente nuovo o errato.");
    
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("Non č possibile calcolare il percorso a causa di un errore sconosciuto.\nSi prega di verificare i dati e riprovare.");
    
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("Non č stato indicato alcun dato" + gdir.getStatus().code);
    
    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given.");
    
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("Impossibile calcolare il percorso.");
    
    else alert("Non č stato indicato alcun dato");
    
}

function onGDirectionsLoad(){ 
    // Use this function to access information about the latest load()
    // results.
    // e.g.
    // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
    // and yada yada yada...
}

//]]>
