var map;
var gdir;
var geocoder = null;
var addressMarker;

var printLinkNodeValue="#";


function initialize() {
  if (GBrowserIsCompatible()) {   
  	//statische Karte:   
    map = new GMap2(document.getElementById("angezeigteKarte"));
    
    //Startansicht setzen:
    map.setCenter(new GLatLng(50.175359,8.612423), 13);
    
    //Kartentoolbar und Zoom:
    GEvent.addListener(map,"click", function(overlay,latlng) {     
    var myHtml = "The GLatLng value is: " + map.fromLatLngToDivPixel(latlng) + " at zoom level " + map.getZoom();
      map.openInfoWindow(latlng, myHtml);
    });
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    
    //Marker setzen:
    var marker = new GMarker(new GLatLng(50.177040,8.608600));
    map.addOverlay(marker);
    var html="<img src='../images/googlemaps/arguk-logo.gif'" +
         "width='84' height='40'><br>" +
         "<strong style='font-family:Times New Roman, Times, serif;font-size:11pt;color:#000000;letter-spaching:1px;margin-left:15px;'>Umweltlabor GmbH</strong><br>" +
         "<div style='margin-left:4px'>Krebsm&uuml;hle 1, 61440 Oberursel<br>Tel. 06171 / 71817</div>";
		marker.openInfoWindowHtml(html);

  }
}


function getPrintLinkNodeValue(fromAddress){
	//URL der Ausdruckversion generieren:
	var startAdresse = encodeURI(fromAddress);	
	printLinkNodeValue = "http://maps.google.de/maps?f=d&hl=de&geocode=&saddr=" +startAdresse+ "+&daddr=%22Arguk+Umweltlabor+GmbH%22,+Krebsm%C3%BChle+1,+61440+Oberursel&sll=51.124213,10.546875&sspn=11.981889,29.882813&ie=UTF8&z=10&pw=2";	
	
	return printLinkNodeValue;	
}


function setPrintLink(fromAddress){
	//Link zum Ausdrucken erzeugen:
	var myDivPrintLink = document.getElementById("divPrintLink");
	
	var hyperlink = document.createElement("a");
	var wohin = document.createAttribute("href");
	var ziel = document.createAttribute("target");
	
	getPrintLinkNodeValue(fromAddress);
	wohin.nodeValue = printLinkNodeValue;	
	hyperlink.setAttributeNode(wohin);
	
	ziel.nodeValue = "_blank";	
	hyperlink.setAttributeNode(ziel);	
	
	var textString = "Anfahtsbeschreibung drucken";
	var linkText = document.createTextNode(textString);
	
	hyperlink.appendChild(linkText);
	myDivPrintLink.appendChild(hyperlink);	
				//alert("Quelltext:\n\n" +myDivPrintLink.innerHTML);
}


   
function setDirections(fromAddress, toAddress, sprache) {
	//Initialisierung Routenplaner:
	gdir = new GDirections(map, document.getElementById("directions"));
  GEvent.addListener(gdir, "load", onGDirectionsLoad);
  GEvent.addListener(gdir, "error", handleErrors);
	
  gdir.load("from: " + fromAddress + " to: " + toAddress,
            { "sprache": sprache });
  
  setPrintLink(fromAddress);	//Link zum Ausdrucken erzeugen
}




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("Zu der eingegebenen Adresse konnte kein geografisches Ergebnis gefunden werden. Möglicherweise ist die Adresse noch relativ neu oder womöglich falsch geschrieben.");
	}
   
	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("Ihr Anfrage konnte aus unbekanntem Grund nicht erfolgreich verarbeitet werden.");
	}
 
 	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);
   
  }

//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + 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. \n Error code: " + gdir.getStatus().code);
  }

	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("Die Routenanfrage konnte nicht geparsed werden.");
  }
  
	else{
				//alert("An unknown error occurred.");
		alert("Ihre Anfrage konnte leider wegen eines unbekannter Fehlers nicht ausgeführt werden.");
	}
	return false;
 
}



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...
}