var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;
var map;
var gdir;
var geocoder = null;
var addressMarker;
var baseIcon = new GIcon();
baseIcon.image = "/fileadmin/template.v1/_images/google_icon.png";
baseIcon.iconSize = new GSize(32, 32);
baseIcon.iconAnchor = new GPoint(16, 32);
baseIcon.infoWindowAnchor = new GPoint(23,6);

function createMarker(point,name,html,set) {
	var marker = new GMarker(point, {icon:baseIcon});
	if(set=="a"){
		to_htmls[i] = html + '<br /><form action="#" onsubmit="setDirections(this.from.value, this.to.value); return false"><label for="from" style="display:block; float:left; width:35px; height:13px; margin:0px 0px 5px 0px; padding:3px 2px 3px 0px; font:normal 11px/11px Arial, Helvetica, Verdana, Sans-Serif; color:#333;">Start:</label>' +
			'<input type="text" id="from" name="from" style="display:block; float:left; width:120px; height:13px; margin:0px 0px 5px 0px; padding:2px; border:1px solid #4C859B; font:normal 11px/11px Arial, Helvetica, Verdana, Sans-Serif; color:#333;" value="" /><input type="hidden" id="to" name="to" value="An der Trift 4, 56290 Gödenroth" /><br>' +
			'<input value="Route berechnen" type="submit" style="display:block; width:126px; height:20px; margin-left:37px; font:normal 11px/20px Arial, Helvetica, Verdana, Sans-Serif; color:#fff; background-color:#4C859B; border:0px none;"></form></p>';
		html += '<br><a href="javascript:tohere('+i+')" style="color:#4C859B;">Route berechnen</a></p>';
		
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
	}
	gmarkers[i] = marker;
	htmls[i] = html;
	i++;
	return marker;
}
function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function openup(i) {
	GEvent.trigger(gmarkers[i],"click");
}
function gmapload() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("google_map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		map.setCenter(new GLatLng(50.0879, 7.4946), 8, G_HYBRID_MAP);
		
		gdir = new GDirections(map, document.getElementById("google_text"));
		
		var point = new GLatLng(50.0879, 7.4946);
		var marker = createMarker(point,'Adam Anlagen- und Maschinenbau','<p style="font:normal 11px/16px Arial, Helvetica, Verdana, Sans-Serif; color:#333;"><b>Adam Anlagen- und Maschinenbau</b><br />An der Trift 4<br />56290 Gödenroth<br />','a');

		map.addOverlay(marker);
		
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
	}
}

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

function handleErrors(){
	if(gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
		alert("Es konnte keine Adresse für Ihre Eingabe gefunden werden. Vielleicht ist die Adresse zu neu und noch nicht im System oder Sie haben sich vertippt. Error code: " + gdir.getStatus().code);
	}else if (gdir.getStatus().code == G_GEO_SERVER_ERROR){
		alert("Server Fehler - Bitte versuchen Sie es später wieder.  Error code: " + gdir.getStatus().code);
	}else if (gdir.getStatus().code == G_GEO_MISSING_QUERY){
		alert("Die Suche war nicht korrekt ausgefüllt. 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("Geocode Fehler - Bitte versuchen Sie es später wieder. Error code: " + gdir.getStatus().code);
	}else if (gdir.getStatus().code == G_GEO_BAD_KEY){
		alert("Dieser Routenplaner wurde in böswilliger absicht gestohlen. Error code: " + gdir.getStatus().code);
	}else if (gdir.getStatus().code == G_GEO_BAD_REQUEST){
		alert("Leider konnte das Ergebnis nicht zurückgeliefert werden. 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.
	// We don't need that at this point
}

function gettingTheDirectons(){
	var fromDirection = document.getElementById('from').value;
	var toDirection = document.getElementById('to').value;
	setDirections(fromDirection, toDirection);
}
