/*
    How to create values for a GLatLng point for "mapDestinationArray" and "targetDestinationArray":
    Go to GoogleMaps and search your location, e.g. "Balanstr. 73, 81541 München".
    Right-click on the spot you want to be the destination and choose "was ist hier?" from the pulldown.
    Find the geolocation latitude and longitude values in the search field, e.g. "48.11924,11.600593".
*/
// values to change start
var zoomArray = new Array('13'); // 0-19 in normal. 0-20 in satelite.
var maptypeArray = new Array(G_NORMAL_MAP); // G_NORMAL_MAP, G_SATELLITE_MAP or G_HYBRID_MAP
var mapDestinationArray = new Array(new GLatLng(48.11659,11.580745)); // center of the map.
var targetDestinationArray = new Array(new GLatLng(48.111767,11.590188)); // target marker, can be same as mapDestination.
var targetTextArray = new Array('<div style="text-align:left;"><h6>Rainer Lonau:</h6><br />Deisenhofener Str. 77<br />81539 München<br />089 17929904<br /><br /><a href=\'http://www.rainerlonau.com\'>www.rainerlonau.com</a></div>'); // target marker text (html).
var invalidStartAddressTextArray = new Array('Die Startadresse konnte nicht gefunden werden'); // text if no start address was found.
var routePlanerLanguageArray = new Array('de'); // 'de', 'en', 'fr', 'it', etc.
// values to change end

// the number of elements in the arrays must be the same as the the number of elements from the above values.
// for two maps, you need to set: "var map = new Array(null, null);"
var mapArray = new Array(null);
var gdirArray = new Array(null);
var geocoderArray = new Array(null);
var markerArray = new Array(null);
var startLatLngArray = new Array(new GLatLng(null, null));

// Creates and loads the google map.
function load() {
    if (GBrowserIsCompatible()) {
        for (var i = 0; i < mapArray.length; i++) {
            var map = new GMap2(document.getElementById('map_'+(i+1)));
            
            var gdir = new GDirections(map, document.getElementById('directions_'+(i+1)));
            GEvent.addListener(gdir, 'load', onGDirectionsLoad);
            GEvent.addListener(gdir, 'error', handleErrors);
            
            var geocoder = new GClientGeocoder();
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.setMapType(maptypeArray[i]);
            map.setCenter(mapDestinationArray[i], parseInt(zoomArray[i]));
            mapArray[i] = map;
            gdirArray[i] = gdir;
            geocoderArray[i] = geocoder;
            // marker
            var marker = new GMarker(targetDestinationArray[i]);
            map.addOverlay(marker);
            marker.openInfoWindowHtml(targetTextArray[i]); //use "bindInfoWindowHtml" if marker text should be hidden first.
            markerArray[i] = marker;
        }
    }
}

var savedIndex = 0;

// Is called when the user submits a start address.
function setDirections(tempIndex, fromAddress, locale) {
    savedIndex = tempIndex-1;
    if (locale != '') {
        routePlanerLanguageArray[savedIndex] = locale;
    }
	geocoderArray[savedIndex].getLocations(fromAddress, addAddressToMap);
}

// Callback function for the getLocations function.
// Will load the directions.
function addAddressToMap(response) {
	if (!response || response.Status.code != 200) {
		alert(invalidStartAddressTextArray[savedIndex]);
	} else {
		place = response.Placemark[0];
		startLatLngArray[savedIndex] = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        gdirArray[savedIndex].load('from: ' + startLatLngArray[savedIndex] + ' to: ' + targetDestinationArray[savedIndex], { 'locale': routePlanerLanguageArray[savedIndex] });
	}
}

function handleErrors(){
}

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