function MMMeasureTool(){	
	this.map;
	
	this.polyline = null;	
	this.textbox;
	
	// GLatLngs
	this.startpoint = null;
	this.endpoint = null;

	this.listener = new Array();
	
	this.menuicon = new MMMenuIcon();	
	this.menuicon.setImage(MMRegistry.urlPortal + "dateien/icon_distance.gif");	
	this.menuicon.setTooltipText("Messwerkzeug zur Bestimmung der Entfernung zwischen 2 Punkten");	
	this.menuicon.setSize(32,32);
	this.menuicon.setPosition( new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(20,270)) );
}
MMMeasureTool.prototype = new GOverlay();


/**
 * Called by the map after the overlay is added to the map using GMap2.addOverlay(). The overlay object 
 * can draw itself into the different panes of the map that can be obtained using GMap2.getPane().
 */
MMMeasureTool.prototype.initialize = function(map){
	this.map = map;	

	this.map.addControl(this.menuicon);
	this.menuicon.show();
	
	GEvent.bind(this.menuicon, 'click', this, function(){
		this.menuicon.menuImage.style.cursor = "crosshair";
		this.listener.push( GEvent.bind(this.map, "click", this, this.onMapClick) );
	});
}


MMMeasureTool.prototype.onMapClick = function(overlay, latlng, overlaylatlng){
					

	if(this.startpoint != null)
	{			
		if(this.polyline) this.map.removeOverlay(this.polyline);
		if(this.textbox) this.map.removeControl(this.textbox);
		
		for(var i=0; i<this.listener.length; i++){
			GEvent.removeListener(this.listener[i]);
		}
		this.listener = new Array();		
		this.startpoint = null;
		this.endpoint = null;
		this.menuicon.menuImage.style.cursor = "pointer";
	}
	else
	{
		this.startpoint = latlng;
		
		this.listener.push(GEvent.bind(this.map, "mousemove", this, function(latlng){
			
			// um "Flackern" im IE zu vermeiden
			//if(latlng.lat() <= 80 && latlng.lat() >= -80)			
			if(this.endpoint)
			{
				//var projection = G_NORMAL_MAP.getProjection();
				var oldPos = this.map.fromLatLngToDivPixel(this.endpoint);
				var newPos = this.map.fromLatLngToDivPixel(latlng);
				
				var dx = Math.abs(oldPos.x - newPos.x);
				var dy = Math.abs(oldPos.y - newPos.y);
				
				if( Math.sqrt(dx*dx + dy*dy) < 150)
				{
					this.endpoint = latlng;			
				}
			}
			else
			{
				this.endpoint = latlng;
			}
			
			
		}));
		
		this.listener.push(GEvent.bindDom(this.map.getContainer(), "mousemove", this, function(e){				
			
			// Linie erstellen
			if(this.polyline) this.map.removeOverlay(this.polyline);			
			this.polyline = new GPolyline([this.startpoint, this.endpoint], "#000000", 2, 1);
			this.map.addOverlay(this.polyline);
			
			// Entfernung formatieren			
			var meter = this.polyline.getLength();			
			if(meter < 1000) var text = Math.round(meter) + " m";
			else if(meter < 100000)var text = Math.round(meter/100)/10 + " km";
			else var text = Math.round(meter/1000) + " km";
			
			// Box positionieren
			if(this.textbox)this.map.removeControl(this.textbox);		
			this.textbox = new MMTooltip(text);
			this.map.addControl(this.textbox);
			this.textbox.show({x:e.clientX, y:e.clientY});
			
		}));
	}		

}

/**
 * Called by the map after the overlay is removed from the map using GMap2.removeOverlay() or GMap2.clearOverlays(). 
 * The overlay must remove itself from the map panes here.
 */
MMMeasureTool.prototype.remove = function(){
	if(this.polyline) this.map.removeOverlay(this.polyline);
	if(this.textbox) this.map.removeControl(this.textbox);
	
	for(var i=0; i<this.listener.length; i++){
		GEvent.removeListener(this.listener[i]);
	}
	this.listener = new Array();
	this.map = null;	
}

/**
 * Called by the map when the map display has changed. The argument force will be true if the zoom level or 
 * the pixel offset of the map view has changed, so that the pixel coordinates need to be recomputed.
 */
MMMeasureTool.prototype.redraw = function(force)
{		
	if(this.polyline)
	{

	}	
}

/**
 * Returns an uninitialized copy of itself that can be added to the map.
 */
MMMeasureTool.prototype.copy = function(map){ return new MMMeasureTool(); }


