var map;
var geocoder;
var baseIcon = new GIcon(G_DEFAULT_ICON);
var myColors = new Array("red", "yellow", "#00ff00");
var bounds = new GLatLngBounds();
var gindex = 0;
var markers = new Array();

function initialize(coordArray) {
	
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);

	map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(51, 10), 5);
	map.setUIToDefault();
	//map.removeControl(GMapTypeControl());
	//geocoder = new GClientGeocoder();
	
	for ( var i = 0; i < coordArray.length; ++i) {
		coords = coordArray[i];
		//geocoder.getLocations(address, addAddressToMap);
		addAddressToMap(coords);
	}
	//map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	
}

function set_bgColor(elm_id, clr) {
	f = document.getElementById(elm_id);
	if (f && (clr == "red")) {
		/*
		div = document.getElementById('map_canvas');
		$(div).innerHTML += '<p class="error_msg">Google Maps konnte die Adresse nicht eindeutig zuweisen, bitte überprüfen sie Ihre Eingabe</p>';
		*/
	}
}

function colorByAccuracyValue(v) {
	if (v < 6) {
		cv = 0
	} else if (v < 8) {
		cv = 1
	} else {
		cv = 2
	}
	return myColors[cv];
}

// addAddressToMap() is called when the geocoder returns an
// answer. It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(coords) {
	// map.clearOverlays();
	if (coords != "0;0") {
		c = coords.split(";");
		c = coords.split(";");
		var point = new GLatLng(c[0],c[1]);
		bounds.extend(point);
		
		var letter = String.fromCharCode("A".charCodeAt(0) + gindex);
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
		markerOptions = { icon:letteredIcon };
		var marker = new GMarker(point, markerOptions);
		// marker.openInfoWindowHtml(place.address);
		marker.gindex = gindex;
		// Infofenster mit Routenberechnung
		marker.openRouteInfoWindow = getRouteInfoWindow;
		// Hier die Adresse des Markers eintragen
		a = namesArray[gindex];
		marker.openRouteInfoWindow_targetName = a.substring(a.indexOf(', ')+2);
		// Hier HTML eintragen, das oben im InfoFenster erscheinen soll.
		marker.openRouteInfoWindow_html = namesArray[gindex].replace(/,/g,"<br />"); //+", "+place.address;
		GEvent.addListener(marker, "click", function() {
			marker.openRouteInfoWindow();
		});
		markers[gindex] = marker;
		map.addOverlay(marker);
		if (coordArray.length==1) {
			map.setCenter(bounds.getCenter(), 15);
		} else {
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		}
	}
	gindex += 1;
}

function getRouteInfoWindow(sAction) { // this ist marker
	var LatLong = this.getLatLng();

	var aDisplay = [ "block", "none", "none" ];
	if (sAction == "to")
		aDisplay = [ "none", "block", "none" ];
	else if (sAction == "from")
		aDisplay = [ "none", "none", "block" ];

	var sTargetName = this.openRouteInfoWindow_targetName;
	var sHtml = this.openRouteInfoWindow_html;

	// Info Window: Route
	sHtml += '<div style="display:'
			+ aDisplay[0]
			+ ';">'
			+ 'Route: <a href="javascript:ShowRoute(\'to\','+this.gindex+');">Hierher</a> - <a href="javascript:ShowRoute(\'from\','+this.gindex+');">Von hier</a>'
			+ '</div>';

	// Info Window: Hierher
	sHtml += '<div style="display:'
			+ aDisplay[1]
			+ ';">'
			+ 'Route: <b>Hierher</b> - <a href="javascript:ShowRoute(\'from\','+this.gindex+');">Von hier</a>'
			+ '<br />Start:<form action="http://maps.google.com/maps" method="get" target="_blank">'
			+ '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br />'
			+ '<input value="OK" TYPE="SUBMIT">';
	if (sTargetName != "")
		sHtml += '<input type="hidden" name="daddr" value="' + sTargetName + '" />';
	else
		sHtml += '<input type="hidden" name="daddr" value="' + LatLong.lat()
				+ ',' + LatLong.lng() + '" />';
	sHtml += '</form></div>';

	// Info Window: von hier
	sHtml += '<div style="display:'
			+ aDisplay[2]
			+ ';">'
			+ 'Route: <a href="javascript:ShowRoute(\'to\','+this.gindex+');">Hierher</a> - <b>Von hier</b>'
			+ '<br />Ziel:<form action="http://maps.google.com/maps" method="get" target="_blank">'
			+ '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br />'
			+ '<input value="OK" TYPE="SUBMIT">';
	if (sTargetName != "")
		sHtml += '<input type="hidden" name="saddr" value="' + sTargetName + '" />';
	else
		sHtml += '<input type="hidden" name="saddr" value="' + LatLong.lat()
				+ ',' + LatLong.lng() + '" />';
	sHtml += '</form></div>';

	this.closeInfoWindow();
	this.openInfoWindow(sHtml);
}

function ShowRoute(sAction,idx) {
	//marker.openRouteInfoWindow(sAction);
	markers[idx].openRouteInfoWindow(sAction);
}

// showLocation() is called when you click on the Search button
// in the form. It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
	var address = document.forms[0].q.value;
	geocoder.getLocations(address, addAddressToMap);
	// getLocations soll ein JSON Objekt zurÃ¼ckgeben, welches u.A.
	// Informationen
	// Ã¼ber die Genauigkeit der Geokodierung enthalten soll.
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
	document.forms[0].q.value = address;
	showLocation();
}

