var map;
var gdir;
var marker01;
var marker02;
var marker03;
var geocoder = null;
var addressMarker;

function load() {
	if (GBrowserIsCompatible()) {

		/* Initialize */

		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(47.492363,8.190908),14,G_HYBRID_MAP);
		gdir = new GDirections(map, document.getElementById("route"));
		GEvent.addListener(gdir, "error", handleErrors);
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
	
		/* Controls */
		
		map.enableContinuousZoom();
		map.enableDoubleClickZoom();
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		/* Define Points */
		
		var point01 = new GLatLng(47.492196,8.191563);
		var point02 = new GLatLng(47.490736,8.187502);
		var point03 = new GLatLng(47.488909,8.187395);
		
		/* Define Icons */
		
		var icon01 = new GIcon(); 
		icon01.image = "/googlemaps/ffpFestival.png"; 
		icon01.iconSize = new GSize(124, 88); 
		icon01.iconAnchor = new GPoint(93, 88); 
		icon01.infoWindowAnchor = new GPoint(93, 88);
		
		var icon02 = new GIcon(); 
		icon02.image = "/googlemaps/ffpBusStop.png"; 
		icon02.iconSize = new GSize(139, 75); 
		icon02.iconAnchor = new GPoint(139, 25); 
		icon02.infoWindowAnchor = new GPoint(139, 25);
		
		var icon03 = new GIcon(); 
		icon03.image = "/googlemaps/ffpParkingEntrance.png"; 
		icon03.iconSize = new GSize(132, 67); 
		icon03.iconAnchor = new GPoint(0, 34); 
		icon03.infoWindowAnchor = new GPoint(0, 34);
	
		/* Add Marker */

		marker01 = new GMarker(point01,icon01); 
		map.addOverlay(marker01);
		GEvent.addListener(marker01, "click", function() {
			marker01.openInfoWindowHtml("Free For Peace Festival<br /><strong>FESTIVAL LOCATION</strong><br />5223 Riniken");
		});
		
		marker02 = new GMarker(point02,icon02); 
		map.addOverlay(marker02);
		GEvent.addListener(marker02, "click", function() {
			marker02.openInfoWindowHtml("Free For Peace Festival<br /><strong>BUS STOP</strong><br />5223 Riniken");
		});
		
		marker03 = new GMarker(point03,icon03); 
		map.addOverlay(marker03);
		GEvent.addListener(marker03, "click", function() {
			marker03.openInfoWindowHtml("Free For Peace Festival<br /><strong>PARKING LOT ENTRANCE</strong><br />5223 Riniken");
		});

	}
}

/* Calculate Route */		

function setDirections(fromAddress) {
	gdir.load("from: " + fromAddress + " to: 47.495843,8.19036",
		{ "locale": "de" });
}

/* After the route is determined... */

function onGDirectionsLoad() {
	document.getElementById("route").style.display = 'block';
	map.removeOverlay(marker01);
	map.removeOverlay(marker03);
}

/* Catch and print route errors */

function handleErrors() {
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Die angegebene Startadresse konnte nicht gefunden werden. Dies liegt entweder daran, dass die Adresse noch sehr neu oder fehlerhaft ist.\nError code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("Die Anfrage konnte aus einem nicht näher definierbaren Grund nicht verarbeitet werden.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("Der HTTP q Parameter war entweder nicht vorhanden oder hatte keinen Wert. Bitte gib eine Startdestination an.\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("Ein unbekannter Fehler ist aufgetreten. Bitte ueberpruefe deine Eingabe.");  
}

/* Reset Map */

function resetMap() {
	document.getElementById("route").style.display = 'none';
	document.getElementById("route").innerHTML = '';
	load();
}

/* Open print-popup */

function printRoute() { 
	var wOption="toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=755,height=655,left=100,top=25";
	var innerHTML = document.getElementById('mapWrapper').innerHTML;
	var newWindow = window.open("","",wOption);
	newWindow.document.open();
	newWindow.document.write('<html><head><style type="text/css">body { font:normal 70%/1.6em Arial, Helvetica, sans-serif; } #routeform{ display:none; }</style></head><body onload="window.print();">');
	newWindow.document.write(innerHTML);
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	newWindow.focus();
}
