﻿var map;
var gdir;
var geocoder = null;
var addressMarker;

function LoadMap(sLanguage)
{
  if (GBrowserIsCompatible() && document.getElementById("divMap") != null) {
    map = new GMap2(document.getElementById("divMap"));

    gdir = new GDirections(map);
    GEvent.addListener(gdir, "error", handleErrors);
    geocoder = new GClientGeocoder();
    showAddress('Via Filottrano 45, 60027, Osimo (AN)', sLanguage);
    
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl()) 
  }
}

function showAddress(address, sLanguage)
{
    if (geocoder)
       {
           geocoder.getLatLng(address,
              function(point) {
                  if (!point)
                      alert(address + " non trovato");
                  else {
                      //var centerPoint = new GLatLng(43.570315, 13.517290);
                      var centerPoint = new GLatLng(point.lat() + 0.012, point.lng() - 0.013);
                      map.setCenter(centerPoint, 13);
                      var marker = new GMarker(point);
                      map.addOverlay(marker);
                      html = "<b>Ima Srl</b>";
                      html += "<div style='font-size: 88%'>Via Filottrano, 45<br />60027, Osimo (AN) - Italia<br />Tel +39.071.7200010<img src='Images/logo_mini.gif' style='margin-left: 45px' /><br />";
                      if (sLanguage == "it")
                          html += "<div style='padding-top: 6px'><a href='http://maps.google.com/maps?saddr=&daddr=Via Filottrano 45+60027+Osimo (AN)' target='_blank'>Ottieni indicazioni stradali</a></div>";
                      else
                          html += "<div style='padding-top: 6px'><a href='http://maps.google.com/maps?saddr=&daddr=Via Filottrano 45+60027+Osimo (AN)&hl=en' target='_blank'>Get driving directions</a></div>";
                      html += "</div>";
                      marker.openInfoWindowHtml(html);
                  }
              }
          )
       }
}


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...
}



