var map = null;
var gdir = null;


// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index,lcText) {

// shadow, icon dimensions, etc.
var baseIcon = new GIcon();
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);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

  // Create a lettered icon for this point using our icon class
  var letteredIcon = new GIcon(baseIcon);
  letteredIcon.image = options.basepath + "/contact/marker/cnt_" + index + ".htm";

  // Set up our GMarkerOptions object
  markerOptions = { icon:letteredIcon };
  var marker = new GMarker(point, markerOptions);

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(lcText);
  });
  return marker;
}



function onLoad() {
	var mapObj = $('map');
	if (mapObj != "undefined" && mapObj != null) {
		map = new GMap2(mapObj);

		var maptype = G_NORMAL_MAP;
		switch (options.maptype) {
			case 'hybrid':
				maptype = G_HYBRID_MAP;
				break;
			case 'satellite':
				maptype = G_SATELLITE_MAP;
				break;
		}
		map.setCenter(new GLatLng(options.startlat, options.startlon), options.startzoom, maptype);

		// var point = new GLatLng(southWest.lat() + latSpan * Math.random(),        southWest.lng() + lngSpan * Math.random());    map.addOverlay(new GMarker(point

		// Create a base icon for all of our markers that specifies the



		lnMarkers = 0;
		if(loMapMark.length > 0)
		{
			for(lnCount=0;lnCount<loMapMark.length;lnCount++)
			{
				loLon = loMapMark[lnCount]["lon"];
				loLat = loMapMark[lnCount]["lat"];
				lcDest =loMapMark[lnCount]["des"];
				lcAdr = loMapMark[lnCount]["adr"];

				if(loMapMark[lnCount]["desc"] == "")
					lcDesc = "<b>" + lcDest + "</b><br /> " + lcAdr
				else
					lcDesc = loMapMark[lnCount]["desc"];

				if(loLon != 0 && loLat != 0)
				{
					// Add marker
					var point = new GLatLng(loLat,loLon);

					map.addOverlay(createMarker(point, lnCount, lcDesc));
				}
			}

		}

		map.addControl(new GLargeMapControl());
		if (options.maptypecontrol) map.addControl(new GMapTypeControl());
		if (options.scalecontrol) map.addControl(new GScaleControl());
		if (options.overviewmap) map.addControl(new GOverviewMapControl());
		if (options.doubleclickzoom) {
			map.enableDoubleClickZoom();
			map.enableContinuousZoom();
		}

		gdir = new GDirections(map, $("directions"));
		GEvent.addListener(gdir, "load", function() {
			$('gdirstatus').style.display = 'block';
			$('gdirstatus').firstChild.innerHTML = 'Die Route wurde berechnet, bitte klicken Sie <a href="#directions">hier</a>';
		});
		GEvent.addListener(gdir, "error", function() {
			$('gdirstatus').style.display = 'block';
			$('gdirstatus').firstChild.innerHTML = 'FEHLER: Die Route konnte nicht berechnet werden.';
		});



		if(lcStartDestination && lcStartDestination != "")
		{
		setDirections(lcStartDestination);
		}

		//setDirections("Berlin", "Traunstein", "de_DE");
	}
}


function setDirections(fromAddress) {
	$('gdirstatus').firstChild.innerHTML = 'Die Route wird berechnet...';
	gdir.load("from: " + fromAddress + " to: " + options.toaddress, { "locale": options.locale });

}
