 
    
  var map;
  var geocoder;
  
  function load() {
    if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
      map.enableScrollWheelZoom();
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      
      
    if(document.getElementById('lat'). value != '' && document.getElementById('lon'). value != '') {
      	lat = document.getElementById('lat'). value;
      	lon = document.getElementById('lon'). value;
      	createMarker(lat, lon);
      	map.setCenter(new GLatLng(lat, lon), 10);
      }else{
	      map.setCenter(new GLatLng(50.4419, 9.1419), 5);
      }
      geocoder = new GClientGeocoder();
    }
  }
  
  function loadShowImage(lat, lon) {
  	 if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"), {size:new GSize(170,170)});
      
      map.enableScrollWheelZoom();
      map.addControl(new GSmallMapControl());
      //map.addControl(new GMapTypeControl());
      createMarker(lat, lon);
      map.setCenter(new GLatLng(lat, lon), 5);
     }
     geocoder = new GClientGeocoder();
  }
  
  function addAddressToMap(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
      alert("Sorry, wir konnten den Standort nicht finden. Versuchs nochmal mit einer anderen Anfrage.");
    } else {
    	place = response.Placemark[0];
		document.getElementById('lat'). value =  place.Point.coordinates[1];       
    	document.getElementById('lon'). value =  place.Point.coordinates[0];  
    	createMarker(place.Point.coordinates[1], place.Point.coordinates[0]);  
		marker.openInfoWindowHtml(place.address + '<br>');
    }
  }
  
  function createMarker(lat, lon) {
  	point = new GLatLng(lat, lon);
  	map.setCenter(point,10);
    var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(50, 21);
	baseIcon.iconAnchor = new GPoint(23, 21);
	baseIcon.infoWindowAnchor = new GPoint(9, 0);
      
    var myIcon = new GIcon(baseIcon);
	myIcon.image = "/gfx/gmapMarker.png";
    markerOptions = { icon:myIcon, draggable: true};
      
      marker = new GMarker(point, markerOptions);
      GEvent.addListener(marker, "dragstart", function() {
  		map.closeInfoWindow();
  		});

	GEvent.addListener(marker, "dragend", function() {
	  //marker.openInfoWindowHtml(place.address + '<br>');
	  if(document.getElementById('lat')) {
		  document.getElementById('lat').value =  marker.getLatLng().lat();       
		  document.getElementById('lon').value =  marker.getLatLng().lng();
	  }
	});
      
    GEvent.addListener(marker, "click", function(){ window.location.href='/map.html?lat=' +lat +'&lon=' +lon } );
      
    map.addOverlay(marker);
  }
  
  function showAddress(address) {
    if (geocoder) {
      geocoder.getLocations(address, addAddressToMap);
    }
  }
  
  function resetMap () {
  	 map.clearOverlays();
  	 document.getElementById('lat'). value =  '';       
      document.getElementById('lon'). value =  '';
  	 
  }
  
  function addNewMarker() {
  	map.clearOverlays();
  	var latLon = map.getCenter();
	document.getElementById('lat').value =  latLon.lat();       
	document.getElementById('lon').value =  latLon.lng();  	
  	createMarker(latLon.lat(), latLon.lng());
  }
   
