
	var markerpDistanza = [];		    var pathDistanza = [];
	var markerpSuperficie = [];		    var pathSuperficie = [];
	var markerpPercorso = [];		    var pathPercorso = [];

	var polylineDistanza;
	var polygonsSuperficie;
	var polylinePercorso;

	function trapezoide(Pto1,Pto2)	{	
		/////
			var bounds = omap.getBounds();
			var southWest = bounds.getSouthWest();
			var northEast = bounds.getNorthEast();
			//var altoSx = new GLatLng(northEast.lat(), southWest.lng());
			//var altoDx = new GLatLng(northEast.lat(), northEast.lng());
			var bassoSx = new GLatLng(southWest.lat(), southWest.lng());
			//var bassoDx = new GLatLng(southWest.lat(), northEast.lng());
		/////

		var Pto1 = new GLatLng(Pto1.lat(), Pto1.lng());
		var Pto11 = new GLatLng(Pto1.lat(), Pto2.lng());
		var AsseX = (Pto11.distanceFrom(Pto1));
		if (Pto2.lng() > Pto1.lng())
		{	AsseX = AsseX;	}
		else
		{	AsseX= 0-AsseX;	}
				
		var ya = new GLatLng(Pto1.lat(), Pto1.lng());
		var yb = new GLatLng(Pto2.lat(), Pto1.lng());
		var yrif = new GLatLng(bassoSx.lat(), Pto1.lng());
			
		var OrigYa = (yrif.distanceFrom(ya));
		var OrigYb = (yrif.distanceFrom(yb));
		AsseY=OrigYa+OrigYb;

		return (((AsseX)*(AsseY))/2);
	}

	Distanza = false;		
	function Distanza1_onclick() {		
		if (Distanza == false)
		{	pulisci_tasti();
			document.getElementById('Distanza1').src = 'images/ico_distanza_on.gif'; 
			Distanza = true;
		}	else	{	
			pulisci_tasti();
			document.getElementById('Distanza1').src = 'images/ico_distanza_off.gif'; 
			Distanza = false;
		}
		//Distanza_onclick();
	}

	Superficie = false;
	function Area1_onclick() {
		if (Superficie == false)
		{	pulisci_tasti();
			document.getElementById('Area1').src = 'images/ico_area_on.gif'; 
			Superficie = true;
		}	else	{
			pulisci_tasti();
			document.getElementById('Area1').src = 'images/ico_area_off.gif';
			Superficie = false;
		}
		//Superficie_onclick();
	}
	
	Percorso = false;

	function MappaClick(overlay, point)		{
		if (Distanza)
		{	if (point != undefined)
			{	
				markerpDistanza.push(new GMarker(point))
				omap.addOverlay(markerpDistanza[markerpDistanza.length -1 ]);
				pathDistanza.push(point);
				if (pathDistanza.length > 1)
				{	document.getElementById('sotto').innerHTML = '';
					omap.removeOverlay(polylineDistanza);
					distanza = 0;
					for (i=1; i < pathDistanza.length; i++)
					{ distanza = distanza + (pathDistanza[i-1].distanceFrom(pathDistanza[i]) );	}
					document.getElementById('sotto').innerHTML = '<center><b><span '+ styleSotto +'>mt: ' + distanza.toFixed(3) + '</span></b></center>';
				}
				polylineDistanza = new GPolyline(pathDistanza, "#FF0000", 10);
				omap.addOverlay(polylineDistanza);
			}
		}
		
		if (Superficie)
		{	if (point != undefined)
			{	markerpSuperficie.push(new GMarker(point))
				omap.addOverlay(markerpSuperficie[markerpSuperficie.length -1 ]);
				pathSuperficie.push(point);
				if (pathSuperficie.length > 2)
				{	document.getElementById('sotto').innerHTML = '';
					omap.removeOverlay(polygonsSuperficie);
					seudoArea = 0;
					area_Sel = 0;
					for (i=1; i < pathSuperficie.length; i++)
					{	seudoArea+=trapezoide(pathSuperficie[i-1],pathSuperficie[i]);	}
					area_Sel = Math.abs((seudoArea+trapezoide(pathSuperficie[pathSuperficie.length-1],pathSuperficie[0])));
					document.getElementById('sotto').innerHTML = '<center><b><span '+ styleSotto +'>mq:' + area_Sel.toFixed(3) + '</span></b></center>';
				}
				if (pathSuperficie.length > 1)
				{	polygonsSuperficie = new GPolygon(pathSuperficie, true, "#ff6b54", 0.40, true);
					omap.addOverlay(polygonsSuperficie);
				}
			}
		}
		
		if (Percorso)
		{	if (point != undefined)
			{	
				markerpPercorso.push(new GMarker(point))
				omap.addOverlay(markerpPercorso[markerpPercorso.length -1 ]);
				pathPercorso.push(point);
				if (pathPercorso.length > 1)
				{	document.getElementById('sotto').innerHTML = '';
					omap.removeOverlay(polylinePercorso);
					Percorso = 0;
					for (i=1; i < pathPercorso.length; i++)
					{ distanza = distanza + (pathPercorso[i-1].distanceFrom(pathPercorso[i]) );	}
					document.getElementById('sotto').innerHTML = '<center><b><span '+ styleSotto +'>mt: ' + distanza.toFixed(3) + '</span></b></center>';
				}
				polylinePercorso = new GPolyline(pathPercorso, "#FF0000", 10);
				omap.addOverlay(polylinePercorso);
			}
		}
	}
		
	function PulisciDistanza()	{
		if (polylineDistanza != undefined)	{
			omap.removeOverlay(polylineDistanza);
			lunghezza =  markerpDistanza.length -1;
			for (i= lunghezza; i >= 0 ; i--)
			{	omap.removeOverlay(markerpDistanza[i]);	
				markerpDistanza.pop();
				pathDistanza.pop();
			}
			markerpDistanza = [];
			pathDistanza = [];
			document.getElementById("sotto").innerHTML = '';
		}
	}

	function PulisciSuperficie()	{
		if (polygonsSuperficie != undefined)	{
			omap.removeOverlay(polygonsSuperficie);
			lunghezza =  markerpSuperficie.length -1;
			for (i= lunghezza; i >= 0 ; i--)	
			{	omap.removeOverlay(markerpSuperficie[i]);
				markerpSuperficie.pop();
				pathSuperficie.pop();
			}
			markerpSuperficie = [];
			pathSuperficie = [];
			document.getElementById("sotto").innerHTML = '';
			seudoArea = 0;
		}
	}
		
	function pulisci_tasti()
	{	
		Distanza = false;
		document.getElementById('Distanza1').src = 'images/ico_distanza_off.gif'; 
		Superficie = false;
		document.getElementById('Area1').src = 'images/ico_area_off.gif'; 
			
		PulisciDistanza();
		PulisciSuperficie();
	}
		