var map = null;
var icon = null;

// laedt eine GoogleMap und speichert sie in das angegebene Element und setzt das
// Kartenzentrum
// divElementName = id des <div< Elementes, in das die Map gespeichert werden soll
// zentrum = das Zentrum der Karte, anzugeben in geographischen Koorinaten oder der
// Form (Strasse Hausnummer, PLZ, Stadt)
// geocoding = 0/1 (zentrum (liegt/liegt nicht) als geographische Koordinate vor
function load(divElementNameMap, divElementNameMapBox, width, height, zentrum, geocoding) {
	map = loadAddControl(divElementNameMap, divElementNameMapBox, width, height, zentrum, geocoding,new google.maps.SmallMapControl());
}

function loadAddControl(divElementNameMap, divElementNameMapBox, width, height, zentrum, geocoding,control) {
	if (google.maps.BrowserIsCompatible()) {
		var box = document.getElementById(divElementNameMapBox);
		var options;

		if (width && height) {
			options = new Object();
			options.size = new google.maps.Size(width, height);
		}

		iMap = new google.maps.Map2(document.getElementById(divElementNameMap), options);

		if (null != box){
			google.maps.Event.addListener(iMap, "load", function() {
				box.style.visibility = "visible";
			});
		}
	
		iMap.addControl(control);

		if (geocoding == 0) {
			iMap.setCenter(new GLatLng(zentrum.slice(0, zentrum.indexOf(",")),
					zentrum.slice(zentrum.indexOf(",") + 1, zentrum.length)), 13);
		} else if (geocoding == 1) {
			var Geocoder = new google.maps.ClientGeocoder();
			Geocoder.getLatLng(zentrum,
				function(point) {
					iMap.setCenter(point, 13);
				});
		}
		return iMap;
	}	
}

// fuegt einen Marker der Karte hinzu
// adresse = adresse des Markers
// geocoding = 0/1 (adresse (liegt/liegt nicht) als geographische Koordinate vor
// infoContent = Inhalt des InfoWindow des Markers
// tooltippContent = Inhalt des Tooltips des Markers
function addMarker(adresse, geocoding) {
  var marker = null;
  // Generiere das Icon fuer die Marker
  //generateIcon();
  // Liegt die Adresse in GeoKoordinaten vor: ja-> direkt verwenden, nein->mittels GClientGeocoder() umwandeln
  if (geocoding == 0) {
      marker = new google.maps.Marker(new google.maps.LatLng(adresse.slice(0, adresse.indexOf(",")),
      adresse.slice(adresse.indexOf(",") + 1, adresse.length)), icon);
      map.addOverlay(marker);
  } else if (geocoding == 1) {
    var Geocoder = new google.maps.ClientGeocoder();
    Geocoder.getLatLng(adresse,
      function(point) {
        marker = new google.maps.Marker(point, icon);
        map.addOverlay(marker);
      });
  }
}


function addMarkerAtPoint(point, icon) {
  var newMarker = new google.maps.Marker(point, icon);
  map.addOverlay(newMarker);
  return newMarker;
}


function generateIcon(imageURL, shadowURL) {
  icon = new google.maps.Icon();
  icon.image = imageURL;
  icon.shadow = shadowURL;
  icon.iconSize = new google.maps.Size(30, 38);
  icon.shadowSize = new google.maps.Size(30, 38);
  icon.iconAnchor = new google.maps.Point(4, 36);
  icon.infoWindowAnchor = new google.maps.Point(6, 24);
}


function generateNumberedMarkerIcon(imageURL, shadowURL) {
  var markerIcon = new google.maps.Icon();
  markerIcon.image = imageURL;
  markerIcon.shadow = shadowURL;
  markerIcon.iconSize = new google.maps.Size(20, 34);
  markerIcon.shadowSize = new google.maps.Size(37, 34);
  markerIcon.iconAnchor = new google.maps.Point(10, 34);
  markerIcon.infoWindowAnchor = new google.maps.Point(10, 34);
  return markerIcon;
}


function showInMap(lat, long) {
  map.setZoom(15);
  map.setCenter(new google.maps.LatLng(lat, long));
}

