/* globale Variablen */
		var map;
    var Locations = new Array();
		var ViewMode = 0;
		var SHOW_MAP = 0;
		var SHOW_ROUTE = 1;
/* Übersichtsfunktionen */
/* ***************************************************** */
		function ChangeView(newMode){
			if( newMode == SHOW_MAP ) {
				document.getElementById("btn_map").style["display"] = 'none';
				document.getElementById("route").style["display"] = 'none';
				document.getElementById("btn_route").style["display"] = 'inline';
				document.getElementById("map").style["display"] = 'block';
			}else {
				document.getElementById("btn_route").style["display"] = 'none';
				document.getElementById("map").style["display"] = 'none';
				document.getElementById("btn_map").style["display"] = 'inline';
				document.getElementById("route").style["display"] = 'block';
			}
			ViewMode = newMode;
		}
/* ***************************************************** */
		function ChangePoint(point){
			map.setCenter(Locations[point]["point"], Zoom);
			var title = Locations[point]["headline"];
			var headline = title.replace(/<.*>/g, "&nbsp;");
			document.getElementById("sTitle").innerHTML = headline;
			document.getElementById("pHeadline").innerHTML = headline;
			document.getElementById("pDescription").innerHTML = Locations[point]["description"];
			SetDestination(point);
		}
/* ***************************************************** */
		function SetDestination(index){
			document.getElementById("f_target").selectedIndex = index;
		}
/* ***************************************************** */
		function createMarker(latlong, markerOptions, myHtml, point_index) {
			var marker = new GMarker(latlong, markerOptions);
			GEvent.addListener(marker, "click", function(lnglat) {
				map.openInfoWindowHtml(latlong, myHtml);
				SetDestination(point_index);
			});
			return marker;
		}
/* ***************************************************** */
    function InitMap(preset) {
      if (GBrowserIsCompatible()) {
      	var text;
				map = new GMap2(document.getElementById("map"), {mapTypes:[G_HYBRID_MAP, G_NORMAL_MAP, G_SATELLITE_MAP]});
				Locations[0]["point"] = new GLatLng(Locations[0]["latitude"],Locations[0]["longitude"]);
				map.setCenter(Locations[0]["point"], Zoom);

				map.addControl(new GLargeMapControl());
/*				map.addControl(new GSmallZoomControl()); */
				map.addControl(new GMapTypeControl());
				map.addControl(new GScaleControl());
// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
        var baseIcon = new GIcon();
				baseIcon.image = "./res/info1.png";
				baseIcon.shadow = "./res/info1_shadow.png";
        baseIcon.iconSize = new GSize(48, 24);
        baseIcon.shadowSize = new GSize(48, 24);
        baseIcon.iconAnchor = new GPoint(16, 24);
        baseIcon.infoWindowAnchor = new GPoint(15, 5);
// Set up our GMarkerOptions object literal
				markerOptions = { icon:baseIcon };
				for (var i = 0; i < Locations.length; i++) {
					if (i > 0) {
						Locations[i]["point"] = new GLatLng(Locations[i]["latitude"],Locations[i]["longitude"]);
					}
					text = Templ[0] + Locations[i]["headline"] + Templ[1] + Locations[i]["description"] + Templ[2];
					map.addOverlay(createMarker(Locations[i]["point"], markerOptions, text, i));
				}
				ChangePoint(preset);
      }
    }
/* ***************************************************** */
    var routemap;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function InitRoute() {
      if (GBrowserIsCompatible()) {
        routemap = new GMap2(document.getElementById("map_canvas"), {mapTypes:[G_NORMAL_MAP, G_HYBRID_MAP]});
        gdir = new GDirections(routemap, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
      }
    }
/* ***************************************************** */
    function setDirections(fromAddress, toAddress) {
			InitRoute();
			gdir.load("from: " + fromAddress + " to: " + toAddress );
    }
/* ***************************************************** */
    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Eine der angegebenen Adressen konnte nicht gefunden werden. Das kann bei relativ neuen Adressen vorkommen, oder aber war die Eingabe fehlerhaft. Benutzen sie möglichst folgende Syntax: [ PLZ Ort, Strasse Hausnummer ]");
	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("Eine Routinganfrage konnte nicht vollständig verarbeitet werden.");
	   else alert("Ein undefinierbarer Fehler ist aufgetreten.");
	}
/* ***************************************************** */
	function onGDirectionsLoad(){
      // Use this function to access information about the latest load()
    if (gdir.getStatus().code == G_GEO_SUCCESS){
			routemap.addControl(new GLargeMapControl());
			routemap.addControl(new GMapTypeControl());
		}
	}
/* ***************************************************** */
	function initialize(preset) {
		InitMap(preset);
		InitRoute();
	}
/* ***************************************************** */


