if (GBrowserIsCompatible()) {
			
	var gmarkers = [];
	var gpolys = [];
	var lastindex = 0;
	var pointset = false;
	var gdir;

	function load() {
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(51.154586,7.254066),12);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		
		gdir = new GDirections(map, document.getElementById("directions"));
		
		// == use different GDirections for adding and dragging, it is just simpler that way ==
		var dirn1 = new GDirections();
		var dirn2 = new GDirections();
		var dirn3 = new GDirections();
		var dirnhome = new GDirections();
		
		var logo = new GIcon(G_DEFAULT_ICON);
		logo.image = "http://www.mw-reifenservice.de/assets/img/site/logo_google.png";
		logo.iconSize = new GSize(180, 46);
		logo.shadow = "http://www.mw-reifenservice.de/assets/img/site/logo_google_shadow.png";
		logo.shadowSize = new GSize(2, 2);
		markerOptions = { icon:logo };
		
		var latlng = new GLatLng(51.152748,7.272606);
		map.addOverlay(new GMarker(latlng, markerOptions));
		
		GEvent.addListener(map, "click", function(overlay,point) {
			if(pointset == false){
				if (!overlay) {
					//klick des users
					dirn1.loadFromWaypoints([point.toUrlValue(6),point.toUrlValue(6)],{getPolyline:true});
				}
			}
		});
						
		// == when the load event completes, plot the point on the street ==
		GEvent.addListener(dirn1,"load", function() {
			// snap to last vertex in the polyline
			var n = dirn1.getPolyline().getVertexCount();
			var p=dirn1.getPolyline().getVertex(n-1);
			var marker=new GMarker(p,{draggable:true});
			GEvent.addListener(marker, "dragend", function() {
				lastIndex = marker.MyIndex;
				var point = marker.getPoint();
				if (lastIndex>0) {
					// recalculate the polyline preceding this point
					dirn2.loadFromWaypoints([gmarkers[lastIndex-1].getPoint(),point.toUrlValue(6)],{getPolyline:true});
				}
				if (lastIndex<gmarkers.length-1) {
					// recalculate the polyline following this point
					dirn3.loadFromWaypoints([point.toUrlValue(6),gmarkers[lastIndex+1].getPoint()],{getPolyline:true});
				}
		
			});
			map.addOverlay(marker);
			// store the details
			marker.MyIndex=0;
			gmarkers.push(marker);
			pointset = true;
			
			//ziel hinzufügen
			dirnhome.loadFromWaypoints([gmarkers[0].getPoint(),"51.152748,7.272606"],{getPolyline:true});
		});
		
		GEvent.addListener(dirnhome,"load", function() {
			// snap to last vertex in the polyline
			var n = dirnhome.getPolyline().getVertexCount();
			var p=dirnhome.getPolyline().getVertex(n-1);
			var marker=new GMarker(p,{draggable:false});
			
			map.addOverlay(marker);
			marker.hide();
			// store the details
			marker.MyIndex=1;
			gmarkers.push(marker);
			map.addOverlay(dirnhome.getPolyline());
			gpolys.push(dirnhome.getPolyline());
			calculateDistance();
			
			document.getElementById("link").style.display="";
		});

		// == move the polyline preceding this point ==
		GEvent.addListener(dirn2,"load", function() {
			// snap to last vertex in the polyline
			var n = dirn2.getPolyline().getVertexCount();
			var p=dirn2.getPolyline().getVertex(n-1);
			gmarkers[lastIndex].setPoint(p);
			// remove the old polyline
			map.removeOverlay(gpolys[lastIndex-1]);
			// add the new polyline
			map.addOverlay(dirn2.getPolyline());
			gpolys[lastIndex-1] = (dirn2.getPolyline());
			calculateDistance();
		});

		// == move the polyline following this point ==
		GEvent.addListener(dirn3,"load", function() {
			// snap to first vertex in the polyline
			var p=dirn3.getPolyline().getVertex(0);
			gmarkers[lastIndex].setPoint(p);
			// remove the old polyline
			map.removeOverlay(gpolys[lastIndex]);
			// add the new polyline
			map.addOverlay(dirn3.getPolyline());
			gpolys[lastIndex] = (dirn3.getPolyline());
			calculateDistance();
		});

		GEvent.addListener(dirn1,"error", function() {
			GLog.write("Failed: "+dirn1.getStatus().code);
		});
		GEvent.addListener(dirn2,"error", function() {
			GLog.write("Failed: "+dirn2.getStatus().code);
		});
		GEvent.addListener(dirn3,"error", function() {
			GLog.write("Failed: "+dirn3.getStatus().code);
		});
		
	}

	function calculateDistance() {
		var dist = 0;
		for (var i=0; i<gpolys.length; i++) {
			dist+=gpolys[i].Distance();
		}
		document.getElementById("distance").innerHTML="<p>Entfernung: "+(dist/1000).toFixed(2)+" km.<\/p>";
	}

	

	function linkToGoogle() {
		gdir.load("from: " + gmarkers[0].getPoint().toUrlValue(5) + " to: " + gmarkers[1].getPoint().toUrlValue(5), { "locale": "de" });
	}

}else {
	alert("Leider ist Google Maps nicht mit Ihrem Browser kompatibel.");
}
