﻿var map;
var gdir;
var geocoder = null;
var addressMarker;

function load()
{
  if (GBrowserIsCompatible() && document.getElementById("divMap") != null) {
    map = new GMap2(document.getElementById("divMap"));

    //gdir = new GDirections(document.getElementById("divDirection"));
    gdir = new GDirections(map);
    //GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
    
    //var point = new GLatLng(37.4419, -122.1419);
    //map.setCenter(point, 13);
    //var marker = new GMarker(point);
    //map.addOverlay(marker);
    geocoder = new GClientGeocoder();
    showAddress('Loreto, ancona, 60025, Zona Industriale Brodolini');
    
    //map.addControl(new GSmallMapControl());
    map.addControl(new GLargeMapControl());
    
//    map.addControl(new GOverviewMapControl(),
//    new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(9, 9)));
  }
}

function showAddress(address)
{
   if (geocoder)
   {
      geocoder.getLatLng(address,
          function(point)
          {
             if (!point)
                alert(address + " non trovato");
             else
             {
                map.setCenter(point, 13);
                var marker = new GMarker(point);
                map.addOverlay(marker);
                marker.openInfoWindowHtml('<img alt="Euromet Srl" src="Images/mini_logo.gif" /><span style="line-height: 120%"><br />Zona Ind.le Brodolini,<br/>60025 Loreto (AN)</span>');
             }
          }
      )
   }
}


function setDirections(fromAddress, toAddress, locale)
{
  gdir.load("from: " + fromAddress + " to: " + toAddress,
            { "locale": locale });
}

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.");
   
}

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...
}


