var map;
var bounds = new GLatLngBounds();
var centerLatitude;
var centerLongitude;

var directionsPanel;
var directions;

function generateMarkerHTML(a, originalInfoWindow, result) {
  // Triggers when a map marker is clicked
  // Must return the DOM node to be displayed in the search result
  var div = document.createElement('div');
  div.innerHTML = 'Suchergebnis:<br>' + result.titleNoFormatting;
  return div;
}


function addMarker(latitude, longitude, description, symbol) {
var nakIcon = new GIcon();
nakIcon.image = "http://neu.nak-mitteldeutschland.de/fileadmin/googlemaps/" + symbol;
nakIcon.iconSize = new GSize(20, 34);
nakIcon.iconAnchor = new GPoint(10,34);
nakIcon.infoWindowAnchor = new GPoint(5, 1);
markerOptions = { icon:nakIcon };
if (description !="") {
var marker = new GMarker(
		new GLatLng(latitude, longitude), markerOptions);
                     
	GEvent.addListener(marker, 'click',
		function() {
			marker.openInfoWindowHtml(description);
		
		}
	);
}
else {
var marker = new GMarker(
		new GLatLng(latitude, longitude), markerOptions);

}


	map.addOverlay(marker);
bounds.extend(new GLatLng(latitude, longitude));
}

function init()
{
	if (GBrowserIsCompatible()) {
if (typeof(markers)!="undefined") {
                             map = new GMap2(document.getElementById("map"));
		for(id in markers) {
			addMarker(markers[id].latitude,
				markers[id].longitude, markers[id].name, markers[id].symbol);


		}
centerLatitude = ((bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2);
centerLongitude = ((bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2);

if (typeof(getcenterlat)!="undefined") {
	centerLatitude = getcenterlat;
}
if (typeof(getcenterlong)!="undefined") {
	centerLongitude = getcenterlong;
}

   		map.addControl(new GSmallMapControl());
   		if ((typeof(noscrollwithmouse)!="undefined")=="0"){   		
   		   map.enableScrollWheelZoom();
}


  		
		map.setCenter(new GLatLng(centerLatitude,
			centerLongitude));
var korrektur = "0";
if (id =="0") {korrektur = "4"};
map.setZoom((map.getBoundsZoomLevel(bounds)-korrektur));
if (typeof(getzoom)!="undefined") {
	map.setZoom(getzoom);
}
	}

// zeige Suchfeld an wenn Variable suche definiert ist
if (typeof(suche)!="undefined"){
var options = {
            searchFormHint : "Ort in Mitteldeutschland ...", 
suppressInitialResultSelection : true,
onGenerateMarkerHtmlCallback: generateMarkerHTML
};

          map.addControl(new google.maps.LocalSearch(options), new GControlPosition
(G_ANCHOR_BOTTOM_LEFT, new GSize(0,0)));
}
}

if (typeof(start)!="undefined"){

var bounds2 = new GLatLngBounds();
bounds2.extend(new GLatLng(start[0].latitude, start[0].longitude));
bounds2.extend(new GLatLng(ziel[0].latitude, ziel[0].longitude));
centerLatitude_route = ((bounds2.getNorthEast().lat() + bounds2.getSouthWest().lat()) /2);
centerLongitude_route = ((bounds2.getNorthEast().lng() + bounds2.getSouthWest().lng()) /2);
map = new GMap2(document.getElementById("map_canvas"));
map.addControl(new GSmallMapControl());
map.enableScrollWheelZoom();

map.setCenter(new GLatLng(centerLatitude_route , centerLongitude_route), map.getBoundsZoomLevel(bounds2));

directionsPanel = document.getElementById("route");
directions = new GDirections(map, directionsPanel);
directions.load("from:"+start[0].latitude+","+start[0].longitude+" to:"+ ziel[0].latitude+", "+ziel[0].longitude);
}
}
// GSearch.setOnLoadCallback(init);

window.onload = init;
window.onunload = GUnload;