﻿var map;
var gdir;
var geocoder = null;
var addressMarker;

function GMapInitialize(aMapID,aDirsID) {
  if (GBrowserIsCompatible()) {      
    gmap = new GMap2(document.getElementById(aMapID));
    gdir = new GDirections(gmap, document.getElementById(aDirsID));
    GEvent.addListener(gdir, "load", GMapOnDirectionsLoad);
    GEvent.addListener(gdir, "error", GMapHandleErrors);

    setDirections("Gunzenhausen, Nürnbergerstrasse 10", "Gunzenhausen, Nürnbergerstrasse 10", "de_DE");
  }
}
    
function setDirections(fromAddress, toAddress, locale) {
  gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

function GMapHandleErrors(){
  var statuscode = gdir.getStatus().code;
  var sMsg = "";
  switch (statuscode) {
  case G_GEO_UNKNOWN_ADDRESS:
  case G_GEO_UNKNOWN_DIRECTIONS:
    sMsg = "Konnte keine geographische Position für eine der angegebenen Adressen ermitteln.\n" +
           "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.";
    break;

  case G_GEO_SERVER_ERROR:
    sMsg = "Die Geokodierungs- oder Richtungsanfrage konnte nicht erfolgreich abgeschlossen werden.\n" + 
           "A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.";
    break;
   
  case G_GEO_MISSING_QUERY:
    sMsg = "Entweder fehlt der Parameter 'q' oder er hat einen ungültigen Wert.\n" + 
           "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.";
    break;

//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     sMsg = "The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.";
	     
  case G_GEO_BAD_KEY:
    sMsg = "Der angegebene Schlüssel ist entweder ungültig oder nicht für diese Domain gedacht." +
           "The given key is either invalid or does not match the domain for which it was given.";
    break;

  case G_GEO_BAD_REQUEST:
    sMsg = "Eine Richtungsanfrage konnte nicht erfolgreich durchgeführt werden.\n" + 
           "A directions request could not be successfully parsed.";
    break;

  default:
    sMsg = "Unbekannter Fehler aufgetreten\n" + 
           "An unknown error occurred.";
    break;
  
  }// switch (statuscode)..

  // append error code to all messages
  sMsg += "\nError code:" + statuscode;

  // show message
  alert(sMsg);

}

function GMapOnDirectionsLoad(){ 
  // Use this function to access information about the latest load()
  // results.

  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}

// EOF
    
