// values to change start
var zoom = 13; // 0-19 in normal. 0-20 in satelite.
var maptype = G_NORMAL_MAP; // G_NORMAL_MAP, G_SATELLITE_MAP or G_HYBRID_MAP
var mapDestination = 'Dorfstraße 32, 85445 Oberding'; // center of the map.
var targetDestination = 'Dorfstraße 32, 85445 Oberding'; // target marker, can be same as mapDestination.
var targetText = '<div style="text-align:left;"><h3>Perl Notstromsysteme GmbH:</h3><br />Dorfstraße 32<br />85445 Oberding<br />Tel. 08122 / 842 39 <br /><a href=\'http://www.perl-notstrom.de\'>www.perl-notstrom.de</a></div>'; // target marker text (html).
var invalidStartAddressText = 'Die Startadresse konnte nicht gefunden werden'; // text if no start address was found.
var routePlanerLanguage = 'de'; // 'de', 'en', 'fr', 'it', etc.
// values to change end

var map;
var gdir;
var geocoder = null;
var startLatLng = new GLatLng(null, null);
var targetLatLng = new GLatLng(null, null);

// Creates and loads the google map.
function load() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById('map'));
        
        gdir = new GDirections(map, document.getElementById('directions'));
        GEvent.addListener(gdir, 'load', onGDirectionsLoad);
        GEvent.addListener(gdir, 'error', handleErrors);
        
        geocoder = new GClientGeocoder();
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setMapType(maptype);
        showCenterMap(mapDestination);
    }
}

// Is called when the user submits a start address.
function setDirections(fromAddress, locale) {
    if (locale != '') {
        routePlanerLanguage = locale;
    }
	geocoder.getLocations(fromAddress, addAddressToMap);
}

// Callback function for the getLocations function.
// Will load the directions.
function addAddressToMap(response) {
	if (!response || response.Status.code != 200) {
		alert(invalidStartAddressText);
	} else {
		place = response.Placemark[0];
		startLatLng = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        gdir.load('from: ' + startLatLng + ' to: ' + targetLatLng, { 'locale': routePlanerLanguage });
	}
}

// Shows the marker.
function showAddress(address) {
    if (geocoder) {
        geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
                alert(address + ' konnte nicht gefunden werden');
            } else {
                targetLatLng = point;
                var marker = new GMarker(point);
                map.addOverlay(marker);
                marker.openInfoWindowHtml(targetText);
            }
        }
        );
    }
}

// Sets the center of the map.
function showCenterMap(addressMap) {
    if (geocoder) {
        geocoder.getLatLng(
        addressMap,
        function(point) {
            if (!point) {
                alert(addressMap + ' konnte nicht gefunden werden');
            } else {
                map.setCenter(point, zoom);
                showAddress(targetDestination);
            }
        }
        );
    }
}

function handleErrors(){
}

function onGDirectionsLoad(){
    // Use this function to access information about the latest load()
    // results.
    // e.g.
    // document.getElementById('getStatus').innerHTML = gdir.getStatus().code;
}
