
var zooming = false;
var panning = false;
var pasX1 = 0;
var pasY1 = 0;
var pasX2 = 0;
var pasY2 = 0;
var panX1 = 0;
var panY1 = 0;
var panX2 = 0;
var panY2 = 0;
var spazio = 1;

var ovIsVisible = false;


   //Create a DHTML layer
function createLayer1(name, inleft, intop, width, height, visible, content) {
	  var layer;
	  if (isNav4) {
	  	document.writeln('<layer name="' + name + '" left=' + inleft + ' top=' + intop + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
	    document.writeln(content);
	    document.writeln('</layer>');
	  } else {
	    document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;' + '; z-index:1; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
	    document.writeln(content);
	    document.writeln('</div>');
	  }
  } 

function mapTool(e) {
     var theButton= 0;
	if (isNav) {
		theButton = e.which;
	} else {
		theButton =window.event.button;
	}	
	if (theButton == 1) {
		getImageXY(e);
		if ((mouseX >= 0) && (mouseX < larghezza) && (mouseY >= 0) && (mouseY < altezza)) {
		   //alert("locWidth"+locWidth);
			//if ((mouseX >= 0) && (mouseX < locWidth) && (mouseY >= 0) && (mouseY < locHeight)) {
			//toolMode = 4;
		  //}
		    //alert(toolMode);
		   
				switch(toolMode) {
				case 0:
						getMapXY(mouseX,mouseY);
						X2 = mapX;
						Y2 = mapY;
						zoomin(1);
						return false;
						break
				case 1:
						getMapXY(mouseX,mouseY);
						X2 = mapX;
						Y2 = mapY;
						zoomin(2);
						return false;
						break
				case 2:
						startZoomBox(e);
						return false;
						break
				 		
				case 3:
				        getImageXY(e);
						pasX1 = mouseX;
						pasY1 = mouseY;
						point(pasX1,pasY1);
						return false;
						break
				case 4:
						startPan(e);
						return false;
						break
				}
		}
	}
}

// start pan.... image will move
function startPan(e) {
	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<larghezza) && (mouseY<altezza)) {
			panX1 = mouseX;
			panY1 = mouseY;
			panX2 = panX1+1;
			panY2 = panY1+1;
			panning=true;
	}
	return false;

}

// move map image with mouse
function panMouse() {
	var xMove = panX2-panX1;
	var yMove = panY2-panY1;
	var cLeft = -xMove;
	var cTop = -yMove;
	var cRight = larghezza;
	var cBottom = altezza;
	if (xMove>0) {
		cLeft = 0;
		cRight = larghezza - xMove;
	}
	if (yMove>0) {
		cTop = 0;
		cBottom = altezza - yMove;
	}
	moveLayer("theMap",xMove,yMove); 
	return false;
}



// stop moving image.... pan 
function stopPan(e) {
	window.scrollTo(0,0);
	panning = false;
	pan_move(panX1,panY1,panX2,panY2);
	return true;
}



// start zoom ... box 
function startZoomBox(e) {
	getImageXY(e);
	if ((mouseX<larghezza) && (mouseY<altezza)) {
			x1=mouseX;
			y1=mouseY;
			getMapXY(x1,y1);
			pasX1 = x1;
			pasY1 = y1;
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1
			boxIt(x1,y1,x2,y2);
			zooming=true;
	return false;
	}
}

// stop zoom  box. . . 
function stopZoomBox(e) {
        zooming=false;
		//if (isNav4) {
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
		getImageXY(e);
		x2=mouseX;
		y2=mouseY;
		getMapXY(x2,y2);
		pasX2 = x2;
		pasY2 = y2;
		var toolModeTemp = toolMode;
		if (pasX1==pasX2){
		  /*if (useZoomIn) {
		           toolMode = 0;
				   mapTool(e);
				   toolMode = toolModeTemp;
		  }
		  if (useZoomOut) {
		           toolMode = 1;
				   mapTool(e);
				    toolMode = toolModeTemp;
		  }*/
		}else{
		    if(useRett)	arrayrett(pasX1,pasY1,pasX2,pasY2);
			if(useZoomIn) zoomin_box(pasX1,pasY1,pasX2,pasY2);
			//if(useMultiplaRect) multipla(pasX1,pasY1,pasX2,pasY2);	
			if(useZoomOut) zoomout_box(pasX1,pasY1,pasX2,pasY2);
		}
	return true;
}

// move layer to x,y
function moveLayer(name, x, y) {		
  	var layer = getLayer1(name);		
  	if (isNav4)
    	layer.moveTo(x, y);
  	//if (document.all) 
	 else {
    	layer.left = x + "px";
   		layer.top  = y + "px";
  	}
}



function boxIt(theLeft,theTop,theRight,theBottom) {
		var vspc = 0;
		var ovBoxSize = 1;
		if (!isNav4) {
			theTop = theTop + vspc;
			theBottom = theBottom + vspc;
			theLeft = theLeft + vspc;
			theRight = theRight + vspc;
		}
		clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+ovBoxSize);
		clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+ovBoxSize,theBottom);
		clipLayer("zoomBoxRight",theRight-ovBoxSize,theTop,theRight,theBottom);
		clipLayer("zoomBoxBottom",theLeft,theBottom-ovBoxSize,theRight,theBottom);
		showLayer("zoomBoxTop");
		showLayer("zoomBoxLeft");
		showLayer("zoomBoxRight");
		showLayer("zoomBoxBottom");

}

function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {	
	  var layer = getLayer1(name);
	  if (isNav4) {
		    layer.clip.left   = clipleft;
		    layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
		    layer.clip.bottom = clipbottom;
	  }	  else {
			var newWidth = clipright - clipleft;
			var newHeight = clipbottom - cliptop;
			layer.height = newHeight;
			layer.width	= newWidth;
			layer.top	= cliptop  + "px";
			layer.left	= clipleft + "px";
		}

}

//get the layer object called "name"
  function getLayer1(name) {
	  if (isNav4)
	    return(document.layers[name]);
	  else if (isIE4) {
	    layer = eval('document.all.' + name + '.style');
	    return(layer);
	  } else if (is5up) {
		var theObj = document.getElementById(name);
		return theObj.style;
	  }
	  else
	    return(null);
  }

// toggle layer to visibleh
function showLayer(name) {		
  	var layer = getLayer1(name);		
  	if (isNav4)
    	layer.visibility = "show";
  	else
   	 layer.visibility = "visible";
}




// toggle layer to invisible
function hideLayer(name) {	
	var layer = getLayer1(name);
		if (isNav4)
    	layer.visibility = "hide";
  else
	 		 layer.visibility = "hidden";
}

// check for mouseup
function chkMouseUp(e) { 
	
	if (toolMode == 2){
			stopZoomBox(e);
	}

	if (toolMode == 4) {
			stopPan(e);

	}
	zooming = false;
	panning = false;
	return false;
	
}

// get the coords at mouse position
function getMouse(e) {
	window.status="";
	getImageXY(e);
	mouseStuff();
	return false;
} 



function mouseStuff() {
	if ((zooming)) {
		x2=mouseX;
		y2=mouseY;
		setClip();
	} else if (panning) {
		panX2=mouseX;
		panY2=mouseY;
		panMouse();	
	} 	
	if (TipsX.length > 0) {
		//alert(TipsX.length);
		x2=mouseX;
		y2=mouseY;
		getMapXY(x2,y2);
		pasX2 = x2;
		pasY2 = y2;
		
		// searchTipsByXY defined in aimsCustom.js
		
		s = searchTipsByXY(mapX, mapY);
		//window.status=s +" " +mouseX;				
		visualizzaCivicoLabel(s,mapX,mapY);
	}
}


// clip zoom box layer to mouse coords
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);
	}
	//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;
		
	}
	
}


//Calcola i valori X e Y immagina delle coordinate passate
  function getImageOvXY(xIn,yIn) {
    var t = parent.MapFrame;
	var xDistance = Math.abs(t.ovStartLeft-t.ovStartRight);  //difference in x coords (map units)
	var iWidth = locWidth;
	var eLeft = 0; //left coord of map extent (map units)

	var yDistance = Math.abs(t.ovStartBottom-t.ovStartTop); //difference in y coords (map units)
	var iHeight = locHeight;
	var eBottom = 0; ////left coord of map extent (map units)
	
	var	mouseX = xIn - t.ovStartLeft;
	var	mouseY = t.ovStartTop - yIn;
	
	var	pixelX = iWidth / xDistance;  
	imageX = pixelX * mouseX + eLeft; 
		
	var	pixelY = iHeight / yDistance;  
	imageY = pixelY * mouseY + eBottom;
	
	//alert(imageY);
			
  }
  
//Aggiorna le variabili globali per disegnare il rettangolo
  function cambia1(primoX,primoY,secondoX,secondoY) {
    getImageOvXY(primoX,primoY);
	var t_prX = imageX;
	var t_prY = imageY;
	getImageOvXY(secondoX,secondoY);
	var t_secX = imageX;
	var t_secY = imageY;
	distanzaX = Math.abs(t_prX-t_secX);
	distanzaY = Math.abs(t_prY-t_secY);
	var partenzaX = Math.min(t_prX,t_secX);
	var partenzaY = Math.min(t_prY,t_secY);
	if (partenzaX <= 0) {
	        distanzaX = distanzaX + partenzaX;
			if(distanzaX < 0) distanzaX=0;
	        partenzaX=spazio;
	}
	if (partenzaY <= 0) {
	        distanzaY = distanzaY + partenzaY;
			if(distanzaY < 0) distanzaY=0; 
			partenzaY=spazio;
	}
	
	if ((partenzaX+distanzaX) >= locWidth) {
	        barraRight = locWidth-spazio;
			distanzaX = barraRight - partenzaX;
			if(distanzaX < 0) distanzaX=0; 
			if(partenzaX >= locWidth) partenzaX = locWidth+spazio; 
	}else{
	        if((partenzaX+distanzaX)<0){
	            distanzaX = spazio; 
				barraRight = 0;
		    }else{
	            barraRight = partenzaX+distanzaX;
			}
	}
	
	if ((partenzaY+distanzaY) >= locHeight) {
	       barraBottom = locHeight-spazio;
			distanzaY = barraBottom - partenzaY;
			if(distanzaY < 0) distanzaY=0; 
			if(partenzaY >= locHeight) partenzaY = locHeight+spazio;
	}else{
	        if((partenzaY+distanzaY)<0){
	            distanzaY = spazio; 
				barraBottom = 0;
		    }else{
	            barraBottom = partenzaY+distanzaY;
			}
	        
	}
	
	
	disegna_ret1(partenzaX,partenzaY);
  }

  
  
  //Ridisegna il rettangolo rosso passando come parametri il punto iniziale
  
  function disegna_ret1(partenzaX,partenzaY) {
     var layer = getLayer1("zoomOVBoxTop");
		layer.top	= partenzaY - spazio  ;
		layer.left	= partenzaX - spazio  ;
		layer.width	= distanzaX + 2*spazio;
		layer.height = spazio ;
	layer = getLayer1("zoomOVBoxLeft") ;
		layer = getLayer1("zoomOVBoxLeft") ;	
		layer.top	= partenzaY  - spazio ;
		layer.left	= partenzaX - spazio  ;
		layer.width	= spazio;
		layer.height = distanzaY + 2*spazio ;
	layer = getLayer1("zoomOVBoxRight");	
		layer.top	= partenzaY  - spazio;
		layer.left	= barraRight;
		layer.width	= spazio;
		layer.height = distanzaY+ 2*spazio;
	layer = getLayer1("zoomOVBoxBottom");
		layer.top	= barraBottom;
		layer.left	= partenzaX - spazio;
		layer.width	= distanzaX + 2*spazio;
		layer.height = spazio; 
	}

 

//funzione attivata quando si vuole spostare la mappa
  function cliccato1(e) {
   if(navigator.appName == "Netscape"){
	     var cliccatoX = e.pageX;  // image coords of the click
	     var cliccatoY = e.pageY;  // image coords of the click
	}else{
	     var cliccatoX = e.offsetX;  // image coords of the click
	     var cliccatoY = e.offsetY;  // image coords of the click
	}
	var t = parent.MapFrame;
	var xDistance = Math.abs(t.ovStartLeft-t.ovStartRight);
	var iWidth = locWidth; 
	var eLeft = parseFloat(t.ovStartLeft);
	var yDistance = Math.abs(t.ovStartBottom-t.ovStartTop);
	var iHeight = locHeight;
	var eBottom = parseFloat(t.ovStartBottom);
	
	var mouseX = cliccatoX;
	var mouseY = iHeight - cliccatoY; //operazione necessaria per invertire il senso delle y
	
	var pixelX = xDistance / iWidth; // scale ratio for Gauss Boaga(how many meters corresponds to a pixel)
	var mgX = pixelX * mouseX + eLeft;
	
	var pixelY = yDistance / iHeight;
	var mgY = pixelY * mouseY + eBottom;
	t.ov_pan(mgX,mgY);
	
  }
  
  
   // toggle Overview Map display
function toggleOVMap() {
    if (imsURL!="") {
		if (ovIsVisible) {
			showLayer("ovLayer");
			showLayer("zoomOVBoxTop");
			showLayer("zoomOVBoxLeft");
			showLayer("zoomOVBoxRight");
			showLayer("zoomOVBoxBottom");
			showLayer("zoomOVBoxTopDown");
			showLayer("zoomOVBoxLeftDown");
			showLayer("zoomOVBoxRightDown");
			showLayer("zoomOVBoxBottomDown");
			parent.MapFrame.ovIsVisible = false;
			
			
		} else {
			hideLayer("ovLayer");
			hideLayer("zoomOVBoxTop");
			hideLayer("zoomOVBoxLeft");
			hideLayer("zoomOVBoxRight");
			hideLayer("zoomOVBoxBottom");
			hideLayer("zoomOVBoxTopDown");
			hideLayer("zoomOVBoxLeftDown");
			hideLayer("zoomOVBoxRightDown");
			hideLayer("zoomOVBoxBottomDown");
			parent.MapFrame.ovIsVisible = true;
			
		}
		if (isIE) {
			//document.all.theTop.style.cursor = theCursor;
		}
	} else {
		alert("A MapService must be loaded");
	}
	
}
 
 // caricamento immagine attesa
function showRetrieveData() {
    if (hasLayer("LoadData")) {
	    showLayer("LoadData");
	}
}

// fine caricamento immagine attesa
function hideRetrieveData() {
    if (hasLayer("LoadData")) {
		hideLayer("LoadData");
	}
}
 
// toggle layer to invisible
function hideLayer(name) {	
    var layer = getLayer1(name); 
	if (isNav4)
    	layer.visibility = "hide";
  	else
		 layer.visibility = "hidden";
		
}


// controlla l'esistenza del layer 
function hasLayer(name) {
	var result = false;
	if (isNav4) {
		if (document.layers[name]!=null) result=true;
	}  else if (isIE) {
	   if (eval('document.all.' + name)!=null) result=true;
	} else if (isNav) {
		var theElements = document.getElementsByTagName("DIV");
		var theObj;
		var j = -1;
		for (i=0;i<theElements.length;i++) {
			if (theElements[i].id==name) result=true;
		}
    }
	return result;
}
