// JavaScript Document
	var analytics=0;
	var map;
	var gdir;
	var geocoder = null;

	function load_googlemaps()
	{
		if(typeof address!="undefined")
		{
			load();
		}
	}
	
	function unload_googlemaps()
	{
		if(typeof address!="undefined")
		{
			GUnload();
		}
	}



	function load()
	{
		if (GBrowserIsCompatible())
		{
			map = new GMap2(document.getElementById("map"));
			map.enableScrollWheelZoom();
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			geocoder = new GClientGeocoder();
			geocoder.getLatLng(address,function(point){drawMap(point, address,tooltiptext,1);});
    
      gdir = new GDirections(map, document.getElementById("directions"));
			GEvent.addListener(gdir, "load", onGDirectionsLoad);
			GEvent.addListener(gdir, "addoverlay", onGDirectionsAddOverlay);
			GEvent.addListener(gdir, "error", handleErrors);
//		GEvent.addListener(map, "click", function(marker, point) {   alert(point.toString());});
		}
	}

	function drawMap(point,address,text,center)
	{
/*		var icon = new GIcon();
		icon.image = "logo.gif";
		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icon.iconSize = new GSize(57, 61);
		icon.shadowSize = new GSize(1, 1);
		icon.iconAnchor = new GPoint(0, 61);
		icon.infoWindowAnchor = new GPoint(50, 6);
*/
//		var marker = new GMarker(point,icon);
		if (typeof address_x!="undefined" && typeof address_y!="undefined")
	   		point = new GLatLng(address_x, address_y); 
		if (center==1) map.setCenter(point, 13);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(text);});
		if (center==1) marker.openInfoWindowHtml(text);
	}

	function drawRoute()
	{
		if (typeof address_x!="undefined" && typeof address_y!="undefined")
			gdir.load("from: "+document.getElementById("startort").value+" to: "+address_x+", "+address_y);
		else
      			gdir.load("from: "+document.getElementById("startort").value+" to: "+address);

		return false;
	}
	
	function handleErrors(){
		if (document.getElementById("startort").value == "")
			alert("Bitte geben Sie eine Startadresse an.");
		else if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			alert("Die von Ihnen angebebene Adresse konnte leider nicht gefunden werden.");
		else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
			alert("Serverfehler, bitte versuchen Sie es später erneut.");
			
		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("Die Route konnte leider nicht erstellt werden.");
		
		else alert("Unbekannter Fehler - die Route konnte leider nicht erstellt werden.");
	}


	function onGDirectionsLoad(){ 
//		alert("Routendaten berechnet...");
	}

	function onGDirectionsAddOverlay(){ 
//		alert("Route eingezeichnet...");
		geocoder.getLatLng(address,function(point){drawMap(point, address,tooltiptext,0);});

		document.getElementById("directions").style.height="200px";
		window.scrollBy(0, 1000);

		if (analytics==1)
			urchinTracker("/anfahrt/route_berechnet");
	}


	function clearFormular()
	{
		if(document.getElementById("startort").value=="Strasse, Hausnummer, Plz., Ort" || document.getElementById("startort").value=="street, house number, postal code, city")
			document.getElementById("startort").value ="";
	}
