﻿var map;
var gdir;
var bInfoWindow = true;


// FUNZIONE PRINCIPALE PER CARICAR LA MAPPA
function LoadMap()
{
    if (GBrowserIsCompatible() && document.getElementById("divMap") != null)
    {
        map = new GMap2(document.getElementById("divMap"));

        gdir = new GDirections(map);
        GEvent.addListener(gdir, "error", handleErrors);
        GEvent.addListener(gdir, "addoverlay", addInfos);

        var point = new GLatLng(43.543921, 13.38566);
        map.setCenter(point, 12);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        addMarkerInfos(marker);
        
        map.addControl(new GSmallMapControl());
    }
}


//CALCOLA UN PERCORSO TRA UN PUNTO DI PARTENZA E UN PUNTO DI ARRIVO
function setDirections(fromAddress, toAddress)
{
    gdir.load("from: " + fromAddress + " to: " + toAddress);
}


//AGGIUNGE LE INFORMAZIONI AL MARKER DI ARRIVO (FUNZIONE CHIAMATA DALL'EVENTO)
function addInfos()
{
    var marker = gdir.getMarker(1);
    if (marker != null)
        addMarkerInfos(marker);
}


//AGGIUNTE LE INFORMAZIONI DELL'ACCADEMIA AD UN MARKER SULLA MAPPA
function addMarkerInfos(marker)
{
    marker.openInfoWindowHtml('<img alt="Agenzia Immobiliare Service Studio" src="Images/logo_map.png" align="left"/><div style="padding: 6px 0 0 0">Via N.Sauro, 23/A<br/>60020, Agugliano (AN)</div>');
    GEvent.addListener(marker, "infowindowclose", function() {bInfoWindow = false;});
    GEvent.addListener(marker, "infowindowopen", function() {bInfoWindow = true;});
    GEvent.addListener(marker, "click", function() {if (bInfoWindow) {marker.closeInfoWindow()} else {marker.openInfoWindowHtml('<img alt="Nuova Accademia della Cucina Marchigiana" src="Images/logo_text.jpg" /><br />piazza Maggini, 3 - 60020 Agugliano (AN)')}});
}


//FUNZIONE PER GESTIRE GLI ERRORI DEL CALCOLO DEL PERCORSO
function handleErrors()
{
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     //alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
     alert("Non è stato possibile trovare nessun luogo geografico per l'indirizzo di partenza specificato. Ciò potrebbe essere dovuto al fatto che l'indirizzo è relativamente nuovo o forse non corretto." );
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     //alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
     alert("Non è stato possibile calcolare il percorso richiesto. Errore imprevisto e sconosciuto.");
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
     //alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
     alert("Non è stato specificato nessun percorso da calcolare.");
   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. \n Error code: " + gdir.getStatus().code);
     alert("La chiave di Google data è invalida o non corrisponde al dominio a cui è stata assegnata.");
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     //alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);    
     alert("Inserisci un punto di partenza valido.");
   else //alert("An unknown error occurred.");
        alert("Inserisci un punto di partenza valido.");
}


