var map = null;
var geocoder = null;
var address = "Bismarckstrasse 26 67454 hassloch";
var gdir = null;
var entfernung;

function anfahrt_drucken(){
		staticMap();
		window.print();
}

function staticMap() {
   var poly = gdir.getPolyline();
   
   if (poly) {     
	   	egal = 1;
	   	if (poly.getVertexCount() > 90) {
			egal = poly.getVertexCount() / 90;
			egal = Math.ceil(egal);
	   		//alert("This route has too many vertices: " + poly.getVertexCount() + " egal: " + egal); // Fehlermeldung erscheint immer, obwohl es geht!
	   		//return;
	   	}
	   	
	   	var baseUrl = "http://maps.google.com/staticmap?";
	   				
	   	var params = [];
	   	params.push("center=" + map.getCenter().lat().toFixed(6) + "," + map.getCenter().lng().toFixed(6));
	   	
	   	var markersArray = [];
	   	markersArray.push(poly.getVertex(0).toUrlValue(5) + ",greena");
	   	markersArray.push(poly.getVertex(poly.getVertexCount() - 1).toUrlValue(5) + ",greenb");
	   	
	   	params.push("markers=" + markersArray.join("|"));
	   	
	   	var polyParams = "rgba:0x0000FF80,weight:5|";
	   	var polyLatLngs = [];

	   	for (var j = 0; j + egal < poly.getVertexCount(); j = j + egal) {
	   		polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
	   	}
	   	params.push("path=" + polyParams + polyLatLngs.join("|"));
	   	params.push("zoom=" + map.getZoom());
	   	params.push("size=540x280");
		
	   	var img = document.createElement("img");
	   	img.src = baseUrl + params.join("&") + "&key=ABQIAAAAjA342i0a5QBt90swouj6chTXhIyTIgZeewlrhlSGnfl2ofKzyhSg663PKK4pT9farJfOb8BFRtAQYg";
	   	document.getElementById("staticMapIMG").innerHTML = "";
	   	document.getElementById("staticMapIMG").appendChild(img);
		document.getElementById("staticMapIMG").style.display = "block";
		document.getElementById("gmap").style.display = "none";
   }
   else{
		document.getElementById("staticMapIMG").style.display = "none";
		document.getElementById("gmap").style.display = "block";
   }	
}

function checkAddress(){
	fromAddress = document.getElementById("fromAddress");
	if (window.location.search != "") {
		var Adresse = window.location.search.split("=");
		Adresse = Adresse[1].split("&");
		Adresse = unescape(Adresse[0]);
		Adresse = Adresse.replace(/\+/g, " ");
		if (Adresse != "Ihre Adresse") {
			fromAddress.value = Adresse;
			fromAddress.style.textAlign = 'left';
			setDirections(fromAddress.value);
		}
		else {
			fromAddress.value = Adresse;
		}
	}
	else {
		fromAddress.value = "Ihre Adresse";
		
	}
}

window.onload = initialize;
window.onunload = GUnload;


function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gmap"));
		//map.setMapType(G_HYBRID_MAP);
		map.setMapType(G_NORMAL_MAP);
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		ITD_LatLng = new GLatLng(49.366725, 8.259079);
		ITD_Icon_LatLng = new GLatLng(49.366900, 8.258450);
		map.setCenter(ITD_LatLng, 16);

		//ITD Logo
		var ITD_Icon = new GIcon();
		ITD_Icon.image = "/bilder/itd.png";
		ITD_Icon.iconSize = new GSize(228, 89);
		ITD_Icon.iconAnchor = new GPoint(7, 80);
		ITD_Icon.infoWindowAnchor = new GPoint(1, 1);
		markerOptions = { icon:ITD_Icon, clickable: false };
		marker = new GMarker(ITD_Icon_LatLng, markerOptions);
		map.addOverlay(marker);
		
		gdir = new GDirections(map,document.getElementById("dist"));
		
		checkAddress();
	}
}

function setDirections(fromAddress) {
	
	document.getElementById("staticMapIMG").style.display = "none";
	document.getElementById("gmap").style.display = "block";
	
	if ((fromAddress == "") || (fromAddress == "Ihre Adresse")) {
		document.getElementById("fehler").innerHTML = "Adresse eingeben";
	}
	else {
		//Route zum Lieferort
		document.getElementById("fehler").innerHTML = "";
		GEvent.addListener(gdir, "error", handleErrors);
		var locale = "de_DE";
		gdir.load("from: " + fromAddress + " to: " + address, {"locale": locale});
		map.removeOverlay(marker);
		document.getElementById("dist").style.display = "block";
	}
}


function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		document.getElementById("fehler").innerHTML = "Adresse nicht gefunden";
	//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);
	}
	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);
		
		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);
					
					else 
						alert("An unknown error occurred.");
}
