extensions.push('measure');

		
var measurehtml    ='<div id="distanceDiv" style="cursor:url(images/righello.cur);position:absolute;top:90px;left:45px;width:660px;height:450px; z-index: 4000; visibility: hidden" onclick="beginMeasureDistance()">';
measurehtml       +='			<v:rect strokecolor="#A8A6A6" id="distancerect" style="position:absolute;top:0px;left:0px;width:660px;height:450px;z-index:3000">';
measurehtml       +='				<v:fill opacity="0" color="red"/>';
measurehtml       +='				<v:stroke opacity="0" color="red"/>';
measurehtml       +='			</v:rect>';
measurehtml       +='</div>';
measurehtml       +='<div id="distanceTextDiv" style="position:absolute;top:50px;left:100px;width:450;height:20; z-index: 4002;visibility:hidden">';
measurehtml       +='			<input style="border:black solid 1px;position:relative;top:-3px" name="distanceTextInput" value="0 m" disabled="true"/>';
measurehtml       +='			<img style="cursor:hand;border:white solid 0px" value="Nuova misura" src="images/ricomincia.gif" onClick="endLineDistance()"/>';
measurehtml       +='			<img style="cursor:hand;border:white solid 0px" value="Annulla misura" src="images/chiudi.gif" onClick="endMeasureDistance()"/>';
measurehtml       +='		</div>';

measurehtml       +='<div id="areaDiv" style="cursor:url(images/righello_area.cur);position:absolute;top:90px;left:45px;width:660px;height:450px; z-index: 4000; visibility: hidden" onclick="beginMeasureArea()">';
measurehtml       +='			<v:rect strokecolor="#A8A6A6" id="arearect" style="position:absolute;top:0px;left:0px;width:660px;height:450px;z-index:3000">';
measurehtml       +='				<v:fill opacity="0" color="red"/>';
measurehtml       +='				<v:stroke opacity="0" color="red"/>';
measurehtml       +='			</v:rect>';
measurehtml       +='</div>';
measurehtml       +='<div id="areaTextDiv" style="position:absolute;top:50px;left:100px;width:450;height:20; z-index: 4002;visibility:hidden">';
measurehtml       +='			<input style="border:black solid 1px;position:relative;top:-3px" name="areaTextInput" value="0 mq" disabled="true"/>';
measurehtml       +='			<img style="cursor:hand;border:white solid 0px" value="Nuova misura" src="images/ricomincia.gif" onClick="endLineArea()"/>';
measurehtml       +='			<img style="cursor:hand;border:white solid 0px" value="Annulla misura" src="images/chiudi.gif" onClick="endMeasureArea()"/>';
measurehtml       +='		</div>';



function measureBegin() {

	addExtensionHTML(measurehtml);
	commands['measureDistanceToggle']='measureDistanceTogglePrimary';
	commands['measureAreaToggle']='measureAreaTogglePrimary';

	/*otherMapGui.push('distanceDiv');
	otherMapGui.push('distanceTextDiv');

	otherMapGui.push('areaDiv');
	otherMapGui.push('areaTextDiv');*/


	eventHandlers.push('measureDistance');
	eventHandlers.push('measureArea');

	eventHandlersStatus['measureDistance']=false;
	eventHandlersStatus['measureArea']=false;
	
	var shape=document.getElementById('distanceDiv');
	if(shape) {

	//-------------------------------------------------------
		var html='<v:rect strokecolor="#A8A6A6" id="distancerect" style="position:absolute;top:0px;left:0px;width:'+mapWidth+'px;height:'+mapHeight+'px;z-index:3000">';
		html+='<v:fill opacity="0" color="red"/>';	
		html+='<v:stroke opacity="0" color="red"/>';
		html+='</v:rect>';
		shape.innerHTML=html;
	//-------------------------------------------------------

		mapInfo.setProperty('originalDistanceShape',shape.innerHTML);


	//-------------------------------------------------------
		shape=document.getElementById('areaDiv');
		html='<v:rect strokecolor="#A8A6A6" id="arearect" style="position:absolute;top:0px;left:0px;width:'+mapWidth+'px;height:'+mapHeight+'px;z-index:3000">';
		html+='<v:fill opacity="0" color="red"/>';
		html+='<v:stroke opacity="0" color="red"/>';
		html+='</v:rect>';
		shape.innerHTML=html;
	//-------------------------------------------------------

		mapInfo.setProperty('originalAreaShape',shape.innerHTML);
	}
	
	mapInfo.setProperty('lineCount',0);

	eventListeners.attachToEvent('measureDistanceToggle','measureDistanceTogglePrimary');
	eventListeners.attachToEvent('measureAreaToggle','measureAreaTogglePrimary');
	
}

function measureDistanceToggle(secondary) {
	//if(secondary || confirm('Confermi l\'operazione richiesta?')) {
		var measdiv=document.getElementById('distanceDiv');
		var meastextdiv=document.getElementById('distanceTextDiv');
		
		var measrect=document.getElementById('distancerect');
		measdiv.style.pixelLeft=mapOffsetX-3;
		measdiv.style.pixelTop=mapOffsetY-3;

		meastextdiv.style.pixelLeft=mapOffsetX+10;
		meastextdiv.style.pixelTop=mapOffsetY+10;
		/*panrect.style.pixelLeft=mapOffsetX-1;
		panrect.style.pixelTop=mapOffsetY-1;*/
		measrect.style.pixelWidth=mapWidth+8;
		measrect.style.pixelHeight=mapHeight+7;
		measdiv.style.pixelWidth=mapWidth+8;
		measdiv.style.pixelHeight=mapHeight+7;
		
		if(measdiv.style.visibility=='hidden') {
			eventListeners.fireEvent('enableEvent','measureDistance');
			measdiv.style.visibility = 'visible';
			meastextdiv.style.visibility = 'visible';
			measdiv.onclick=beginMeasureDistance;
			enableEvent('measureDistance');
			measdiv.innerHTML=mapInfo.getProperty('originalDistanceShape');
			mapInfo.setProperty('lineCount',0);	
			mapInfo.setProperty('distance',0);	
			var text=document.getElementById('distanceTextInput');
			text.value="0 m";
		}
		else {
			eventListeners.fireEvent('disableEvent','measureDistance');
			
			disableEvent('measureDistance');
			measdiv.style.visibility = 'hidden';
			meastextdiv.style.visibility = 'hidden';
			measdiv.onclick=null;
			measdiv.onmousemove=null;
		}
		if(!secondary)
			actionEnd();
	/*} else
		actionEnd();*/
}

function measureDistanceTogglePrimary() {
	measureDistanceToggle(false);
	endToggle();
}



function beginMeasureDistance() {
	
	var x1d=event.offsetX;
	var y1d=event.offsetY;
			
	var shape=document.getElementById('distanceDiv');
	var lineCount=mapInfo.getProperty('lineCount');
	shape.innerHTML += '<v:line id="line'+lineCount+'" from="'+x1d+','+y1d+'" to="'+x1d+','+y1d+'" strokecolor="black" filled="false"/>';
	mapInfo.setProperty('x1',x1d);
	mapInfo.setProperty('y1',y1d);
	shape.onmousemove=measureDistanceMouseMove;
	//shape.ondblclick=endDraw;
	shape.onclick=nextMeasureDistance;
	
}
	
function measureDistanceMouseMove() {
	var x2d=event.offsetX;
	var y2d=event.offsetY;
	var line=document.getElementById('line'+mapInfo.getProperty('lineCount'));
	line.to=x2d+','+y2d;
}

function nextMeasureDistance() {
	
	var x2d=event.offsetX;
	var y2d=event.offsetY;
	var x1d=mapInfo.getProperty('x1');
	var y1d=mapInfo.getProperty('y1');

	var currDist=Math.pow((x2d-x1d)*(x2d-x1d)+(y2d-y1d)*(y2d-y1d),1/2);
	var distance=mapInfo.getProperty('distance');
	distance += currDist;
	mapInfo.setProperty('distance',distance);
	var scaleFactor=map.realRange.getDeltaX()/mapWidth;

	var text=document.getElementById('distanceTextInput');
		
	text.value=Math.round(distance*scaleFactor)+" m";
		
	var width=x2d-x1d;
	var height=y2d-y1d;
		
	var lineCount=mapInfo.getProperty('lineCount');
	lineCount++;
	mapInfo.setProperty('lineCount',lineCount);
	beginMeasureDistance();
	
}

function endLineDistance() {
	var shape=document.getElementById('distanceDiv');
	
	shape.innerHTML=mapInfo.getProperty('originalDistanceShape');
		
	shape.onclick=beginMeasureDistance;
	shape.onmousemove=null;
	
	var text=document.getElementById('distanceTextInput');
	
	text.value="0 m";
		
	/*var text=document.getElementById('distanceText');
	
	text.innerHTML=0;*/
	
	mapInfo.setProperty('lineCount',0);
	mapInfo.setProperty('distance',0);
}

function endMeasureDistance() {
	var shape=document.getElementById('distanceDiv');
	
	shape.innerHTML=mapInfo.getProperty('originalDistanceShape');
		
	shape.onclick=beginMeasureDistance;
	shape.onmousemove=null;
	
	var text=document.getElementById('distanceTextInput');
	
	text.value="0 m";
		
	/*var text=document.getElementById('distanceText');
	
	text.innerHTML=0;*/
	mapInfo.setProperty('lineCount',0)
	mapInfo.setProperty('distance',0)
	measureDistanceToggle(false);
	endToggle();
}


function measureAreaToggle(secondary) {
	//if(secondary || confirm('Confermi l\'operazione richiesta?')) {
		var measdiv=document.getElementById('areaDiv');
		var meastextdiv=document.getElementById('areaTextDiv');
		
		var measrect=document.getElementById('arearect');
		measdiv.style.pixelLeft=mapOffsetX-3;
		measdiv.style.pixelTop=mapOffsetY-3;

		meastextdiv.style.pixelLeft=mapOffsetX+10;
		meastextdiv.style.pixelTop=mapOffsetY+10;
		/*panrect.style.pixelLeft=mapOffsetX-1;
		panrect.style.pixelTop=mapOffsetY-1;*/
		measdiv.style.width=mapWidth+8;
		measdiv.style.height=mapHeight+7;
		measrect.style.width=mapWidth+8;
		measrect.style.height=mapHeight+7;
		if(measdiv.style.visibility=='hidden') {
			measdiv.innerHTML=mapInfo.getProperty('originalAreaShape');
			mapInfo.setProperty('lineCount',0);	
			mapInfo.setProperty('areaSum',0);	
			var text=document.getElementById('areaTextInput');
			text.value="0 mq";
			document.getElementById('puls_area').onmouseover=null;
			document.getElementById('puls_area').onmouseout=null;
			document.getElementById('puls_area').src='images/b_area_down.gif';
			measdiv.style.visibility = 'visible';
			meastextdiv.style.visibility = 'visible';
			measdiv.onclick=beginMeasureArea;
			enableEvent('measureArea');
			mapInfo.setProperty('drawFinalLine',false);
		}
		else {
			document.getElementById('puls_area').onmouseover=new Function("MM_nbGroup('over','puls_area','images/b_area_roll.gif','images/b_area_roll.gif',1);");
			document.getElementById('puls_area').onmouseout=new Function("MM_nbGroup('out');");
			document.getElementById('puls_area').src='images/b_area.gif';
			disableEvent('measureArea');
			measdiv.style.visibility = 'hidden';
			meastextdiv.style.visibility = 'hidden';
			measdiv.onclick=null;
			measdiv.onmousemove=null;
		}
		if(!secondary)
			actionEnd();
	/*} else
		actionEnd();*/
}

function measureAreaTogglePrimary() {
	measureAreaToggle(false);
	endToggle();
}


function beginMeasureArea() {
	
	var x1d=event.offsetX;
	var y1d=event.offsetY;

	mapInfo.setProperty('x1',x1d);
	mapInfo.setProperty('y1',y1d);
		
	var shape=document.getElementById('areaDiv');
	if(mapInfo.getProperty('lineCount')==0) {
		mapInfo.setProperty('xfa',x1d);
		mapInfo.setProperty('yfa',y1d);
		
	}
	var lineCount=mapInfo.getProperty('lineCount');
	shape.innerHTML += '<v:line id="lineArea'+lineCount+'" from="'+x1d+','+y1d+'" to="'+x1d+','+y1d+'" strokecolor="black" filled="false"/>';
	shape.onmousemove=measureAreaMouseMove;
	//shape.ondblclick=endDraw;
	shape.onclick=nextMeasureArea;
	
}
	
function measureAreaMouseMove() {
	var x2d=event.offsetX;
	var y2d=event.offsetY;

	var lineCount=mapInfo.getProperty('lineCount');

	var line=document.getElementById('lineArea'+lineCount);
	line.to=x2d+','+y2d;

	/*var areaSum=mapInfo.getProperty('areaSum');
	areaSum += x1d*y2d-y1d*x2d;
	mapInfo.setProperty('areaSum',areaSum);*/

	var lineCount=mapInfo.getProperty('lineCount');
	var drawFinalLine=mapInfo.getProperty('drawFinalLine');
	var xfa=mapInfo.getProperty('xfa');
	var yfa=mapInfo.getProperty('yfa');

	if(lineCount==1 && !drawFinalLine) {
		var shape=document.getElementById('areaDiv');
		shape.innerHTML += '<v:line id="lineAreaFinal" from="'+x2d+','+y2d+'" to="'+xfa+','+yfa+'" strokecolor="black" filled="false"/>';
		mapInfo.setProperty('drawFinalLine',true);
	} else if(lineCount>=1) {
		var finalLine=document.getElementById('lineAreaFinal');
		finalLine.from=x2d+','+y2d;
	}
}

function nextMeasureArea() {
	var lineCount=mapInfo.getProperty('lineCount');
	var xfa=mapInfo.getProperty('xfa');
	var yfa=mapInfo.getProperty('yfa');

	var x1d=mapInfo.getProperty('x1');
	var y1d=mapInfo.getProperty('y1');

	var line=document.getElementById('lineArea'+lineCount);
	line.strokecolor='red';
	
	lineCount++;
	mapInfo.setProperty('lineCount',lineCount);

	var x2d=event.offsetX;
	var y2d=event.offsetY;
	
	mapInfo.setProperty('x1',x1d);
	mapInfo.setProperty('y1',y1d);
	
	/*this.distance +=  (this.x2-this.x1)*(this.y1+this.y2);
		var scaleFactor=this.toolbar.map.realRange.getDeltaX()/this.canvas.getWidth();

		$('measureOutput'+this.name).value=Math.abs(Math.round((this.distance+((this.x0-this.x2)*(this.y2+this.x0)))/2*scaleFactor*scaleFactor))+" mq";*/
	
	var areaSum=mapInfo.getProperty('areaSum');
		
	//areaSum += x1d*y2d-y1d*x2d;
	areaSum += (x2d-x1d)*(y1d+y2d);
	mapInfo.setProperty('areaSum',areaSum);
	
	
	var scaleFactor=map.realRange.getDeltaX()/mapWidth;

	if(lineCount>=2) {
		var text=document.getElementById('areaTextInput');
		//text.value=Math.abs(Math.round((areaSum+(x2d*yfa-y2d*xfa))/2*scaleFactor*scaleFactor))+" mq";
		text.value=Math.abs(Math.round((areaSum+((xfa-x2d)*(y2d+yfa)))/2*scaleFactor*scaleFactor))+" mq";
	}
		
	var width=x2d-x1d;
	var height=y2d-y1d;
		
	beginMeasureArea();
	

}

function endLineArea() {
	var shape=document.getElementById('areaDiv');
	
	shape.innerHTML=mapInfo.getProperty('originalAreaShape');
		
	shape.onclick=beginMeasureArea;
	shape.onmousemove=null;
	
	var text=document.getElementById('areaTextInput');
	
	text.value="0 mq";
		
	/*var text=document.getElementById('distanceText');
	
	text.innerHTML=0;*/
	mapInfo.setProperty('lineCount',0);
	mapInfo.setProperty('areaSum',0);
	mapInfo.setProperty('drawFinalLine',false);
}

function endMeasureArea() {
	var shape=document.getElementById('areaDiv');
	
	shape.innerHTML=mapInfo.getProperty('originalAreaShape');
		
	shape.onclick=beginMeasureArea;
	shape.onmousemove=null;
	
	var text=document.getElementById('areaTextInput');
	
	text.value="0 mq";
		
	/*var text=document.getElementById('distanceText');
	
	text.innerHTML=0;*/
	
	mapInfo.setProperty('lineCount',0);
	mapInfo.setProperty('areaSum',0);
	measureAreaToggle(false);
	endToggle();
	
	
}
