var MWMap = {
	map: null,
	geocoder: null,
	directions: null,
	marker: null,
	routeGeo: false,

	infoHtmlPre: '',
	infoHtmlPost: '',
	infoHtml: '',
	
	initialize: function () {
		this.infoHtmlPre = '<div id="googlemapInfo">';
		this.infoHtmlPost = '<hr size="1" noshade="noshade" align="left" />';
		this.infoHtmlPost += 'Route berechnen von:<form action="javascript:;" onsubmit="MWMap.directionSet(this.elements[0].value)">';
		this.infoHtmlPost += '<input type="text" size="30" value="Strasse, PLZ Ort" onfocus="if (this.value == this.defaultValue) this.value = \'\';" />';
		this.infoHtmlPost += '</form></div>';

		if (GBrowserIsCompatible()) {
			this.map = new GMap2(document.getElementById("map_canvas"));
			
			this.geocoder = new GClientGeocoder();
			this.geocoder.setBaseCountryCode('de');
			
			this.map.addControl(new GSmallMapControl());
			this.map.addControl(new GMapTypeControl());

		if (MWtoLat > 0 && MWtoLng > 0) {
			this.routeGeo = true;
		}

            if (this.routeGeo) {
                // verwenden von Geokoordinaten
           		this.setMarkerCenterMap(new GLatLng(MWtoLat, MWtoLng), 13);
            } else {
                // verwenden der Adresse
	       		this.geocoder.getLatLng(MWtoAddress, this.geocoderPoint);
            }
			
			var directionsPanel = document.getElementById("route_canvas");
			this.directions = new GDirections(this.map, directionsPanel);

            GEvent.addListener(this.directions, "error", this.directionError);
		}
	},
	
	directionSet: function (fromAddress) {
		this.map.closeInfoWindow();
		this.marker.hide();

		var e = document.getElementById("route_canvas");
		e.style.display = '';

        if (this.routeGeo) {
            // verwenden von Geokoordinaten
    		this.directions.load('from: ' + fromAddress + ' to: ' + MWtoName + ' @' + MWtoLat + ',' + MWtoLng, { 'locale': 'de' });
        } else {
            // verwenden der Adresse
    		this.directions.load('from: ' + fromAddress + ' to: ' + MWtoAddress, { 'locale': 'de' });
        }

		return false;
	},
	
	directionError: function () {
		alert('Ihre Adresse konnte nicht gefunden werden!');

		var e = document.getElementById("route_canvas");
		e.style.display = 'none';

		MWMap.marker.show();
		MWMap.marker.openInfoWindowHtml(MWMap.infoHtml);
	},
	
	geocoderPoint: function (point) {
		if (!point) {
			alert('Adresse: ' + MWtoAddress + ' nicht gefunden!');
		} else {
            MWMap.setMarkerCenterMap(point);
		}
	},

	setMarkerCenterMap: function (point) {
		MWMap.map.setCenter(new GLatLng(point.lat() + 0.01, point.lng()), 13);

		MWMap.marker = new GMarker(point);
		MWMap.marker.setLatLng(point);
		MWMap.map.addOverlay(MWMap.marker);

		MWMap.infoHtml = MWMap.infoHtmlPre + MWaddressHtml + MWMap.infoHtmlPost;
		MWMap.marker.bindInfoWindowHtml(MWMap.infoHtml);
		MWMap.marker.openInfoWindowHtml(MWMap.infoHtml);
	}
}

