// JavaScript Document

function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(50.682456, 6.090111), 14);
		map.addControl(new GLargeMapControl())
        map.addControl(new GMapTypeControl());
		
		var blueIcon = new GIcon();
		blueIcon.image = "http://separation-ag.synchronized.de/cms/images/google_icon.png";
		blueIcon.iconSize = new GSize(29, 20);
		blueIcon.shadow = "http://separation-ag.synchronized.de/cms/images/google_icon_shadow.png";
		blueIcon.shadowSize = new GSize(29, 20);
		blueIcon.iconAnchor = new GPoint(15, 20);
		blueIcon.infoWindowAnchor = new GPoint(9, 2);
		blueIcon.infoShadowAnchor = new GPoint(18, 25);
		markerOptions = { icon:blueIcon };
		
		//var point = new GLatLng(50.682456, 6.090111);
		var point = new GLatLng(50.67972301, 6.08552456);
		var marker1 = new GMarker(point,markerOptions);
		
		GEvent.addListener(marker1, "click", function() {
            marker1.openInfoWindowHtml("<b>Separation AG - optical sorting service</b><br/>Gewerbestrasse 16<br/>B- 4731 Eynatten");
          });
		
		map.addOverlay(marker1);
		
       gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
       GEvent.addListener(gdir, "error", handleErrors);

      //setDirections("San Francisco", "Mountain View", "de");
      }
    }
	
	function setDirections(fromAddress, toAddress, locale) {
      
	  if (fromAddress!="") {
		  
		  //alert("from: " + fromAddress + " to: " + toAddress);
		//  alert(toAddress[0].checked + " - " + toAddress[1].checked);
		   target = document.getElementById('printer');
		  target.innerHTML="<img src='images/print.gif' align='absmiddle' height='16' width='16'>&nbsp;<a target='_blank' href='http://maps.google.de/maps?daddr=" + toAddress + "&amp;geocode=&amp;dirflg=&amp;saddr=" + fromAddress + "&amp;f=d&amp;ie=ISO-8859-1&amp;z=11&amp;pw=2'>Wegbeschreibung drucken</a>";
		  gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
	  } else {
		alert("Bitte geben Sie eine Startadresse ein.");  
	  }
	  
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Der Startort wurde nicht gefunden. Der Ort ist entweder relativ neu oder Sie haben sich vertippt.\nError-Code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("Ihre Anfrage konnte aus einem unbestimmten Grund nicht verarbeitet werden.\n Error-Code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("Fehler bei der HTTP-Übertragung der Formulardaten.\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("Der API-Schlüssel scheint nicht für diese Domain registriet zu sein.\n Error-Code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("Ihre Anfrage konnte aus einem unbestimmten Grund nicht verarbeitet werden.\n Error-Code: " + gdir.getStatus().code);
	    
	   else alert("Ein unbekannter Fehler ist aufgetreten.");
	   
	}

	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...
	}
