/*
* Measure 
* @author pmichel
*/
function jwMeasure (oKaMap)
{
	kaTool.apply( this, [oKaMap] );
	this.kaMap = oKaMap;
	this.name = "jwMeasure";
	this.cursor = "crosshair";
	this.points_x = new Array();
	this.points_y = new Array();
	this.clicks = 0;
	this.startx= null;
	this.starty= null;
	this.endx= null;
	this.endy= null;
	this.dist = 0;
	 
	//this.kaMap.addCanvas(initCanvas("theInsideLayer"),"theInsideLayer");
	for (var p in kaTool.prototype) {
		if (!jwMeasure.prototype[p])
			jwMeasure.prototype[p]= kaTool.prototype[p];
	}
}

jwMeasure.prototype.getPosition = function(e) {
	var x = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
	var y = e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
	return this.adjustPixPosition( x,y );
}

jwMeasure.prototype.onmousedown = function (e) {
	getRawObject("theInsideLayer").style.zIndex = 101;
	getRawObject("theInsideLayer").style.backgroundColor = "#000000";
	if(this.clicks<=0)
	{
		this.clicks = 1;
		var aPixPos = this.getPosition(e);
		this.startx=-aPixPos[0];
		this.starty=-aPixPos[1];
		var pix_to_geo = this.kaMap.pixToGeo(this.startx,this.starty);
		this.points_x[0] = pix_to_geo[0];
		this.points_y[0] = pix_to_geo[1];
	}
}
jwMeasure.prototype.onmousemove = function (e) {
}
jwMeasure.prototype.onmouseup= function (e) {
	var aPixPos = this.getPosition(e);
	this.endx = -aPixPos[0];
	this.endy = -aPixPos[1];

	var pix_to_geo = this.kaMap.pixToGeo(this.endx,this.endy);
	this.points_x[this.clicks] = pix_to_geo[0];
	this.points_y[this.clicks] = pix_to_geo[1];

	if (this.clicks>0)
	{
		var pstartx = this.startx-this.kaMap.xOrigin;
		var pstarty = this.starty-this.kaMap.yOrigin;
		var pendx = this.endx-this.kaMap.xOrigin;
		var pendy = this.endy-this.kaMap.yOrigin;
		jg.drawLine(pstartx,pstarty,pendx,pendy);
		this.drawMapMarker(pendx,pendy);
		jg.paint();
		// neue Entfernung berechnen
		var dist_x = this.points_x[this.clicks] - this.points_x[this.clicks - 1];
		var dist_y = this.points_y[this.clicks] - this.points_y[this.clicks - 1];
		this.dist = this.dist + Math.sqrt(dist_x * dist_x + dist_y * dist_y);
		this.kaMap.triggerEvent( KAMAP_ERROR,
		'Distance:'+Math.round(this.dist)+"<br/>\n");
		this.clicks += 1;
		this.startx = this.endx;
		this.starty = this.endy;
	}
}

jwMeasure.prototype.drawMapMarker = function (x,y)
{
	jg.drawLine(x - 5, y, x + 5, y);
	jg.drawLine(x, y - 5, x, y + 5);
}

