aimsMapPresent=true;

var eLeft = -180.0;
var eRight = 180.0;
var eTop = 90.0;
var eBottom = -90.0;
var fullLeft = eLeft;
var fullRight = eRight;
var fullTop = eTop;
var fullBottom = eBottom;

var iWidth = 630;
var iHeight = 512;

var xDistance = Math.abs(eRight-eLeft);
var yDistance = Math.abs(eTop-eBottom);
var panX = xDistance * panFactor;
var panY = yDistance * panFactor;
var pixelX = xDistance/iWidth;
var pixelY = yDistance/iHeight;
var mapX = eLeft;
var mapY = eTop;
var lastLeft = eLeft;
var lastRight = eRight;
var lastTop = eTop;
var lastBottom = eBottom;
var theCursor = "crosshair";

var fullWidth = Math.abs(fullRight - fullLeft);
var fullHeight = Math.abs(fullTop - fullBottom);
var mapScaleFactor = xDistance / iWidth;

var dQuote = '"';

var searchTolerance=1/100;

function fullExtent() {
	if (aimsDHTMLPresent) moveLayer("theMap",hspc,vspc);
	window.scrollTo(0,0);
	submitData();
}

function saveLastExtent() {
	theForm.lastMinx.value=eLeft;
	theForm.lastMaxx.value=eRight;
	theForm.lastMiny.value=eBottom;
	theForm.lastMaxy.value=eTop;
 }


function showRetrieveData() {
	if (hasLayer("LoadData")) {
		showLayer("LoadData");
	}
}

function hideRetrieveData() {
	if (hasLayer("LoadData")) {
		hideLayer("LoadData");
	}
}

function showRetrieveMap() {
	if (hasLayer("LoadMap")) {
		showLayer("LoadMap");
	}
}

function hideRetrieveMap() {
	if (hasLayer("LoadMap")) {
		hideLayer("LoadMap");
	}
}

// restituisce la larghezza in pixel della mappa
function getMapWidth () {
	var mapFrameWidth = thePageWin.innerWidth;
	if (mapFrameWidth == null) {mapFrameWidth = thePageDoc.body.clientWidth;}
	return mapFrameWidth;
}

// restituisce l'altezza in pixel della mappa
function getMapHeight () {
	var mapFrameHeight = thePageWin.innerHeight;
	if (mapFrameHeight == null) {mapFrameHeight = thePageDoc.body.clientHeight;}
	return mapFrameHeight;
}

function getMapXY(xIn,yIn) {
		mouseX = xIn;
		pixelX = xDistance / iWidth;
		mapX = pixelX * mouseX + eLeft;
		mouseY = iHeight - yIn;
		pixelY = yDistance / iHeight;
		mapY = pixelY * mouseY + eBottom;
}


function setClip() {

	var tempX=x1;
	var tempY=y1;
	if (x1>x2) {
		zright=x1;
		zleft=x2;
	} else {
		zleft=x1;
		zright=x2;
	}
	if (y1>y2) {
		zbottom=y1;
		ztop=y2;
	} else {
		ztop=y1;
		zbottom=y2;
	}
	
	if ((x1 != x2) && (y1 != y2)) {
		boxIt(zleft,ztop,zright,zbottom);
	}
}


function zoomin(e) {
	getMapXY(mouseX,mouseY);
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	saveLastExtent();

	eLeft = mapX - (xHalf/zoomFactor);
	eRight = mapX + (xHalf/zoomFactor);
	eTop = mapY + (yHalf/zoomFactor);
	eBottom = mapY - (yHalf/zoomFactor);
	
	submitData();
}

function zoomout(e) {
	getMapXY(mouseX,mouseY);
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	saveLastExtent();

	eLeft = mapX - (xDistance*zoomFactor/2);
	eRight = mapX + (xDistance*zoomFactor/2);
	eTop = mapY + (yDistance*zoomFactor/2);
	eBottom = mapY - (yDistance*zoomFactor/2);
	submitData();
}	

function startZoomBox(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (zooming) {
			stopZoomBox(e);
		} else {
			x1=mouseX;
			y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1;
			boxIt(x1,y1,x2,y2);
			zooming=true;
		}
	}
	return false;
	
}

function stopZoomBox(e) {
	zooming=false;
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	if ((zright <zleft+2) && (zbottom < ztop+2)) {
		zoomin(e);
	} else {
		var tempLeft=lastLeft;
		var tempRight=lastRight;
		var tempTop=lastTop;
		var tempBottom=lastBottom;
		saveLastExtent();
		pixelX = xDistance / iWidth;
		var theY = iHeight - ztop;
		pixelY = yDistance / iHeight;
		eTop = pixelY * theY + eBottom;
		eRight = pixelX * zright + eLeft;
		eLeft = pixelX * zleft + eLeft;
		theY = iHeight - zbottom;
		pixelY = yDistance / iHeight;
		eBottom = pixelY * theY + eBottom;
		window.scrollTo(0,0);
	
		submitData();
	}
	return true;

}

function startZoomOutBox(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		
		if (zooming) {
			stopZoomOutBox(e);
		} else {
			x1=mouseX;
			y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1;
			boxIt(x1,y1,x2,y2);
			zooming=true;
		}
	}
	return false;
}

function stopZoomOutBox(e) {
	
		saveLastExtent();
		zooming=false;
		var theForm = parent.PostFrame.document.theForm;
		var vx1= eLeft;
		var vy1= eBottom;
		var vx2= eRight;
		var vy2= eTop;
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
		var minX = theForm.minx.value;
		minX = parseInt(minX);
		var maxX = theForm.maxx.value;
		maxX = parseInt(maxX);
		var minY = theForm.miny.value;
		minY = parseInt(minY);
		var maxY = theForm.maxy.value;
		maxY = parseInt(maxY);

		var xDistance = Math.abs(maxX-minX);
		var yDistance = Math.abs(maxY-minY);
		var xo2 = (vx1 + vx2 )/2;
		var yo2 = (vy1 + vy2 )/2;
		var xo1 = (maxX + minX )/2;
		var yo1 = (maxY + minY )/2;
		var Ratio = 1.0;
		
	if ((zright <zleft+2) && (zbottom < ztop+2)) {
			Ratio = 1.333;
			getMapXY(mouseX,mouseY)
			xo2 = mapX;
			yo2 = mapY;
	} else {
			var zWidth = Math.abs(zright-zleft);
			var zHeight = Math.abs(ztop-zbottom);
			var xRatio = iWidth / zWidth;
			var yRatio = iHeight / zHeight;
			if (xRatio >= yRatio) 
				Ratio = xRatio;
			else
				Ratio = yRatio;
			}	
			
		var xAdd = Ratio * xDistance / 2;
		var yAdd = Ratio * yDistance / 2;
		vx1 = xo2 - xAdd;
		vx2 = xo2 + xAdd;
		vy1 = yo2 - yAdd;
		vy2 = yo2 + yAdd;
		eTop = vy2;
		eRight = vx2;
		eLeft = vx1;
		eBottom = vy1;
		submitData();
			
	return true;
}

function startPan(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (panning) {
			stopPan(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			panning=true;
			
		}
	}
	return false;

}

function stopPan(e) {
	window.scrollTo(0,0);
	panning=false;
	var tempLeft=eLeft;
	var tempRight=eRight;
	var tempTop=eTop;
	var tempBottom=eBottom;
	saveLastExtent();
	var ixOffset = x2-x1;
	var iyOffset = y1-y2;
	pixelX = xDistance / iWidth;
	var theY = iHeight - ztop;
	pixelY = yDistance / iHeight;
	var xOffset = pixelX * ixOffset;
	var yOffset = pixelY * iyOffset;
	eTop = eTop - yOffset;
	eRight = eRight - xOffset;
	eLeft = eLeft - xOffset;
	eBottom = eBottom - yOffset;
	lastLeft = tempLeft;
	lastRight = tempRight;
	lastTop = tempTop;
	lastBottom = tempBottom;
	hideLayer("theMap");
	submitData();
	
	return true;
	
}

function panMouse() {
	var xMove = x2-x1;
	var yMove = y2-y1;
	var cLeft = -xMove;
	var cTop = -yMove;
	var cRight = iWidth;
	var cBottom = iHeight;
	if (xMove>0) {
		cLeft = 0;
		cRight = iWidth - xMove;
	}
	if (yMove>0) {
		cTop = 0;
		cBottom = iHeight - yMove;
	}
	//alert(cLeft+","+cTop+","+cRight+","+cBottom);
	moveLayer("theMap",xMove,yMove);
	
}

function pan(e) {
	getMapXY(mouseX,mouseY);
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	saveLastExtent();

	eLeft = mapX - xHalf;
	eRight = mapX + xHalf;
	eTop = mapY + yHalf;
	eBottom = mapY - yHalf;
	submitData();
}

function selectCircle(e){
	if (toolMode == 10){
		var x1,y1;
		
		x1 = mouseX;
		y1 = mouseY;

		//salvataggio coordinate
		var theForm = parent.PostFrame.document.theForm;
		theForm.x.value = mapX;
		theForm.y.value = mapY;
		
		x2=x1+3;
		y2=y1+3;
		zleft=x1;
		ztop=y1;
		zbottom=y1;
		zright=x1;
		
		//pulisci
		for(i=0; i<=lastLayer; i++)
			hideLayer("Layer"+i);
		
		var u = (theForm.map_width.value / (theForm.maxx.value - theForm.minx.value) );
		
		var raggio;
		var undefined;
		if (parent.SearchFrame.document.forms[0].raggio === undefined)
			raggio = theForm.raggio.value;			
		else raggio = parent.SearchFrame.document.forms[0].raggio.value;
		// valore del raggio troppo grande per l'envelope attuale
		if (raggio * u - 50 > theForm.map_width.value / 3) {
//			alert("Valore del raggio troppo grande! Ridurre la scala.");
			
			showRetrieveMap();
			refreshNoSubmit();
			theForm.raggio.value = raggio;
			theForm.service_name.value="NavigaMappa";
			theForm.moveaction.value="zoomoutcircle";
			//theForm.start.value="false";
			
			theForm.target = "MapFrame";
			theForm.submit();

			return null;
		}
		
		var points = calcolaPuntiCerchio(new point(x1,y1), (raggio * u));
		
		disegnaCerchio(new point(x1,y1), points);
		
	}
}

function selectNota(e){
	if (toolMode == 14){
		var x1,y1;
		
		x1 = mouseX;
		y1 = mouseY;

		//salvataggio coordinate
		var theForm = parent.PostFrame.document.theForm;
		theForm.x.value = mapX;
		theForm.y.value = mapY;
		
		x2=x1+3;
		y2=y1+3;
		zleft=x1;
		ztop=y1;
		zbottom=y1;
		zright=x1;
		
		//pulisci
		for(i=0; i<=lastLayer; i++)
			hideLayer("Layer"+i);
		
		var u = (theForm.map_width.value / (theForm.maxx.value - theForm.minx.value) );
		
		var raggio = 5000;
/*		var undefined;
		if (parent.SearchFrame.document.forms[0].raggio == undefined)
			raggio = theForm.raggio.value;
		else raggio = parent.SearchFrame.document.forms[0].raggio.value;*/
		var points = calcolaPuntiCerchio(new point(x1,y1), (raggio * u));
		
		disegnaCerchio(new point(x1,y1), points);
		
	}
}



function startSelectBox(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (selectBox) {
			stopSelectBox(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1
			boxIt(x1,y1,x2,y2);
			selectBox=true;
			showLayer("zoomBoxTop");
			showLayer("zoomBoxLeft");
			showLayer("zoomBoxRight");
			showLayer("zoomBoxBottom");
		}
	}
	return false;
}

function stopSelectBox(e) {
	selectBox=false;
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	if ((zright <zleft+2) && (zbottom < ztop+2)) {
		getMapXY(mouseX,mouseY);
		searchTolerance = (xDistance/iWidth) * pixelTolerance;
		tempLeft = mapX-searchTolerance;
		tempTop = mapY-searchTolerance;
		tempRight = mapX+searchTolerance;
		tempBottom = mapY+searchTolerance;
		
	} else {
		pixelX = xDistance / iWidth;
		var theY = iHeight - ztop;
		pixelY = yDistance / iHeight;
		tempTop = pixelY * theY + eBottom;
		tempRight = pixelX * zright + eLeft;
		tempLeft = pixelX * zleft + eLeft;
		theY = iHeight - zbottom;
		pixelY = yDistance / iHeight;
		tempBottom = pixelY * theY + eBottom;
	}
	window.scrollTo(0,0);
	submitData();	
	return true;

}

function chkMouseUp(e) {
	if ((toolMode == 1) && (zooming)) {
		stopZoomBox(e);
	}	
	if ((toolMode == 2) && (zooming)) {
		stopZoomOutBox(e);
	}
	if ((toolMode == 3) && (panning)) {
		stopPan(e);
	}
	if ((toolMode == 8) && (selectBox)) {
		stopSelectBox(e);
	}
	return false;
}


function getImageXY(e) {
	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else {
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop
	}
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;

}	

function getMouse(e) {
	window.status="";
	getImageXY(e);
	if ((mouseX>iWidth) || (mouseY>iHeight) || (mouseX<=0) ||(mouseY<=0) ) {
		chkMouseUp(e);
	} else {
		mouseStuff();
	}
	return false;
}

function mouseStuff() {
	if ((zooming) || (selectBox)) {
		x2=mouseX;
		y2=mouseY;
		setClip();
	} else if (panning) {
		x2=mouseX;
		y2=mouseY;
		panMouse();	
	}
	pixelX = xDistance / iWidth;
	mapX = pixelX * mouseX + eLeft;
	var theY = iHeight - mouseY;
	pixelY = yDistance / iHeight;
	mapY = pixelY * theY + eBottom;
	var u = Math.pow(10,numDecimals);
	var uX = parseInt(mapX * u + (5/10)) / u;
	var uY= parseInt(mapY * u + (5/10)) / u;
	var mouseString = "Mappa: " + uX + " , " + uY + " -- " + "Immagine: " + mouseX + " , " + mouseY;	
	window.status = mouseString;

}

function mapTool(e) {
	var theButton= 0;
	if (isNav) {
		theButton = e.which;
	} else {
		theButton =window.event.button;
	}	
	if (theButton==leftButton) {
		getImageXY(e);
		if ((mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {	
			//alert(toolMode);	
			switch(toolMode) {
				case 1:
					startZoomBox(e);
					return false;
					break
				case 2:
					startZoomOutBox(e);
					return false;
					break
				case 3:
					startPan(e);
					return false;
					break
				case 4:
					info(e);
					break
				case 8:
					startSelectBox(e);
					top.frames["PostFrame"].document.theForm.service_action.value="";
					return false;
					break
				case 9:
					if (!isSelect) {
						setSearchFrame("polygon.htm");
						clickFunction("select line/polygon");
					}
					getRetta();
					break
				case 10:
					if (!isSelect) {
						setSearchFrame("circle.htm");
						clickFunction("selectcircle");
					}
					selectCircle(e);
					break
				case 11:
					if (!isSelect) {
						setSearchFrame("measuredist.htm");
						clickFunction("measuredist");
					}
					getRetta();
					break
				case 12:
					if (!isSelect) {
						setSearchFrame("measurearea.htm");
						clickFunction("measurearea");
					}
					getRetta();
					break
				case 13:
					identifyPlugin(e);
					break
				case 14:
					if (!isSelect) {
						setSearchFrame("inseriscinota.htm");
						clickFunction("inseriscinota");
					}
					theForm.x.value = mapX;
					theForm.y.value = mapY;
					p = new point(mouseX, mouseY);
					clipLayer("notaLayer",p.x,p.y,p.x+16,p.y+16);
					showLayer('notaLayer');
					
					break
				case 15:
					if (!isSelect) {
						setSearchFrame("cancellanota.htm");
						clickFunction("cancellanota");
					}
					
					deleteNota(e);
					break
                                        
					
			}
		}
	}
}

// funzione richiamata dal plug in 
function identifyPlugin(e) {

	var theForm = parent.PostFrame.document.theForm;
	if((theForm.old_url_plugin.value != "") && (theForm.url_plugin.value == "")) theForm.url_plugin.value = theForm.old_url_plugin.value;
	var newWin=new Object();
	newWin=window.open('emptyframe.htm','pluginwindow','resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,width=850,height=650,top=50,left=100');
	newWin.focus();
	theForm.target="pluginwindow";
	theForm.infoaction.value="point";
	theForm.service_name.value="plugin";
	theForm.x.value = (document.all?window.event.clientX:e.pageX);
	theForm.y.value = (document.all?window.event.clientY:e.pageY);
	theForm.coord_x.value = mapX;
	theForm.coord_y.value = mapY;
	
	theForm.submit();
	
	// senza queste istruzioni, le prossime operazioni non trovano il PostFrame
	if(isNav4 || isMoz){window.setTimeout("theForm.target=\"MapFrame\"",50);}	
	else{theForm.target="MapFrame";}
	

}

function info(e) {
	showRetrieveData();
	
	var theForm = parent.PostFrame.document.theForm;
	var xml=creaXml(parent.PostFrame.document.gruppi);
	
	theForm.gruppi.value=xml;
	theForm.infoaction.value="point";
	theForm.service_name.value="InfoMappaLayers";
	theForm.infominx.value=theForm.minx.value;
	theForm.infominy.value=theForm.miny.value;
	theForm.infomaxx.value=theForm.maxx.value;
	theForm.infomaxy.value=theForm.maxy.value;

	if (!document.all){window.captureEvents(Event.MOUSEMOVE)};
	theForm.x.value = (document.all?window.event.clientX:e.pageX);
	theForm.y.value = (document.all?window.event.clientY:e.pageY);

	// coordinate mappa, servono per lo stato 
	theForm.coord_x.value = mapX;
	theForm.coord_y.value = mapY;
	
	if (theForm.infoNewWindow.value=="false"){					
		theForm.target="SearchFrame";
		parent.PostFrame.document.theForm.ricerca.value="false";
	
		if (parent.OptionFrame.arBt[1][0]==1)
			parent.OptionFrame.arBt[1][0]=0;
		FB_swapImage("img1",parent.OptionFrame.arBt[1][1]);
		parent.PostFrame.document.theForm.ricerca.value=false;
	
		theForm.submit();
		// chiude un eventuale finestra aperta precedentemente
		if (theForm.oldSearch.value=="NewWin")  {
			window.open('','Risultati','').close();
		}

		parent.PostFrame.document.theForm.oldSearch.value="SearchFrame";
		
		if(isNav4 || isMoz){window.setTimeout("theForm.target=\"MapFrame\"",50);}	
		else{theForm.target="MapFrame";}
		
	}
	else { 									
	
		var newWin=new Object();
		newWin=window.open('about:blank','Risultati','resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,width=800,height=600,top=50,left=150');
		//newWin=window.open('about:blank','Risultati','');
		theForm.target="Risultati";
		parent.SearchFrame.document.location.URL="text.htm";
		newWin.focus();

		theForm.ricerca.value="false";
		theForm.ricerca.value=false;
		theForm.submit();
		
		if(isNav4 || isMoz){window.setTimeout("theForm.target=\"MapFrame\"",50);}	
		else{theForm.target="MapFrame";}
		
		// ricreazione del search frame e impostazione del pulsante "attiva frame ricerca"
		if (theForm.oldSearch.value=="SearchFrame")  {
			parent.PostFrame.document.frame.target="SearchFrame";
			parent.PostFrame.document.frame.service_name.value="Search";
			parent.PostFrame.document.frame.submit();
			FB_swapImage("img1",parent.OptionFrame.arBt[1][3]);
		}
										
		theForm.oldSearch.value="NewWin";
		
	}
			
}

function deleteNota(e) {
	showRetrieveData();
	
	var theForm = parent.PostFrame.document.theForm;

	theForm.service_name.value="selezionanota";
	
	theForm.target="SearchFrame";
	parent.PostFrame.document.theForm.ricerca.value="false";
	
	if (parent.OptionFrame.arBt[1][0]==1)
		parent.OptionFrame.arBt[1][0]=0;
	FB_swapImage("img1",parent.OptionFrame.arBt[1][1]);
	parent.PostFrame.document.theForm.ricerca.value=false;

	theForm.infominx.value=theForm.minx.value;
	theForm.infominy.value=theForm.miny.value;
	theForm.infomaxx.value=theForm.maxx.value;
	theForm.infomaxy.value=theForm.maxy.value;
	theForm.infoaction.value="point";
	theForm.x.value = (document.all?window.event.clientX:e.pageX);
	theForm.y.value = (document.all?window.event.clientY:e.pageY);
	
	theForm.submit();

	// chiude un eventuale finestra aperta precedentemente
	if (theForm.oldSearch.value=="NewWin")  {
		window.open('','Risultati','resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no').close();
	}

	parent.PostFrame.document.theForm.oldSearch.value="SearchFrame";
		
	if(isNav4 || isMoz){window.setTimeout("theForm.target=\"MapFrame\"",50);}	
	else{theForm.target="MapFrame";}
		
	
}

