// Global vars to save mouse position
var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var zleft=0;
var zright=0;
var ztop=0;
var zbottom=0;
var panning = false;
var measuring = false;
var clickCount = 0;
var clickType = 1;


function mapTool (e) {
   mapwidth = window.innerWidth;
		mapheight = window.innerHeight;			
		
		if (mapwidth == null) {				
			mapwidth = document.body.clientWidth;
		}

		document.Form1.FrameMapWidth.value = mapwidth;

		if(mapheight == null){				
			mapheight = document.body.clientHeight;
		}

		document.Form1.FrameMapHeight.value = mapheight;
			
   getImageXY(e);
   
   if ((mouseX<iWidth) && (mouseY<iHeight) && (mouseX>0) && (mouseY>0)){	
	if (document.Form1.toolaction.value == "ZoomIn"){				
		startZoomBox(e);		
	}
	else if(document.Form1.toolaction.value == "ZoomOut" || document.Form1.toolaction.value == "Center"){
	   		showLayer("theTop");
			x1=mouseX;
			y1=mouseY;
			document.Form1.xcoord.value = x1-7;
			document.Form1.ycoord.value = y1-7;			
			hideLayer("theMap");
			showRetrieveMap();
			document.Form1.submit();		
	}
	else if(document.Form1.toolaction.value == "Identify"){
			x1=mouseX;
			y1=mouseY;
			document.Form1.xcoord.value = x1-7;
			document.Form1.ycoord.value = y1-7;			
			//hideLayer("theMap");
			showRetrieveMap();
			document.Form1.submit();	
			
	}
	else if(document.Form1.toolaction.value == "Pan"){
		//x1=mouseX;
		//y1=mouseY;
		//document.Form1.xcoord.value = x1-7;
		//document.Form1.ycoord.value = y1-7;			
		//hideLayer("theMap");			 	
	 	startPan(e);
		//return false;
		//showRetrieveMap();
		//document.Form1.submit();
				
	}
			
	else if((document.Form1.toolaction.value == "MeasFeet")||(document.Form1.toolaction.value == "MeasMile")){
			startMeasuring(e);
	}
		
	
    }
	
}

// start zoom in.... box displayed
function startZoomBox(e) {
	getImageXY(e);	
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight) && (mouseX>0) && (mouseY>0)){
		if (zooming) {
			stopZoomBox(e);
			showRetrieveMap();
			document.Form1.submit();			
		} else {
			showLayer("theTop");
			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;
			document.Form1.zxmax.value = x2;
			document.Form1.zxmin.value = x1;
			document.Form1.zymax.value = y2;
			document.Form1.zymin.value = y1;
		}
	return false;
	}
	else{
	return true;
	}
}

// get cursor location
function getImageXY(e) {
	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else {
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop;
	}
	// subtract offsets from page left and top
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;
	return true;	
}	


function boxIt(theLeft,theTop,theRight,theBottom) {
	if(isIE){
	theLeft = theLeft + hspc;
	theRight = theRight + hspc;
	theBottom = theBottom + vspc;
	theTop = theTop + 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");

	//just added
	//return false
}
		


// Create a DHTML layer
function createLayer(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 hideLayer(name) {	
   var layer = getLayer(name);  			
	if (isNav4){
	layer.visibility = "hide";
	}
	else
		layer.visibility = "hidden";
		 //layer.display="none";
}

// toggle layer to visible

function showLayer(name) {		
	var layer = getLayer(name);		
	if (isNav4){
	 layer.visibility = "show";		    	 
	}
	else {
	 layer.visibility = "visible";
	}
	//layer.display="block";
}

// clip layer display to clipleft, cliptip, clipright, clipbottom
// Not working with Mozilla Milestone 12 (Nav5)

function clipLayer2(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);		
	  if (isNav4) {
		    //layer.clip.left   = clipleft;
		    //layer.clip.top    = cliptop;
		    //layer.clip.right  = clipright;
		    //layer.clip.bottom = clipbottom;

		    document.layers[name].clip.left   = clipleft;
		    document.layers[name].clip.top    = cliptop;
		    document.layers[name].clip.right  = clipright;
		    document.layers[name].clip.bottom = clipbottom;
	  }
	  //if (document.all)
	  else if (isIE) {
		    layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	  }
}


function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);			 

	  if (isNav4) {
	    //layer.clip.left   = clipleft;
	    //layer.clip.top    = cliptop;
	    //layer.clip.right  = clipright;
	    //layer.clip.bottom = clipbottom;

	    document.layers[name].clip.left   = clipleft;
	    document.layers[name].clip.top    = cliptop;
	    document.layers[name].clip.right  = clipright;
	    document.layers[name].clip.bottom = clipbottom;
	  }	 
	  else {
	    //layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
		var newWidth = clipright - clipleft;
		var newHeight = clipbottom - cliptop;
		layer.height = newHeight;
		layer.width	= newWidth;
		//var theTop = parseInt((parseFloat(cliptop) * 10 + 0.5)/10);
		//var theLeft = parseInt((parseFloat(clipleft) * 10 + 0.5)/10);
		//alert(cliptop + " " + clipleft);
		layer.top	= cliptop  + "px";
		layer.left	= clipleft + "px";
		//layer.display= "none";
	 }

}



// stop zoom box display... 
function stopZoomBox(e) {
	zooming=false;
	
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	window.scrollTo(0,0);	
	return true;
}	

// get the coords at mouse position (getMouse event)
		function getMouse(e) {
			
			if (document.Form1.toolaction.value != "zoomin"){
			hideLayer("theTop");
			}
			window.status="";
			getImageXY(e);
			
			if (panning) {
				x2=mouseX;
				y2=mouseY;
				
				if ((mouseX<iWidth) && (mouseY<iHeight)){
				 	panMouse();	
				}
			
			}
			
			if ((mouseX>iWidth) || (mouseY>iHeight) || (mouseX<=0) ||(mouseY<=0)) {
				stopZoomBox(e);	
			} else {
				if (zooming) {
					x2=mouseX;
					y2=mouseY;
					setClip();
				}
				
				var mouseString = "";
				if (zooming) mouseString += "ZoomBox: [" + x1 + "," + y1 + " x " + x2 + "," + y2 + "]    ";
				mouseString += "Coords: " + mouseX + " , " + mouseY;	
				window.status = mouseString;
			  
			}
			// next line needed for Mac
			return false;
		}

// clip zoom box layer to mouse coords
function setClip() {	
	var tempX=x1;
	var tempY=y1;
	var tempY2=y2;
	var tempX2=x2;

	if (x1>x2) {
		zright=x1;
		zleft=x2;
		tempX=x2;
		tempX2=x1;
	} else {
		zleft=x1;
		zright=x2;
	}
	if (y1>y2) {
		zbottom=y1;
		ztop=y2;
		tempY=y2
		tempY2=y1
	} else {
		ztop=y1;
		zbottom=y2;
	}

	if ((x1 != x2) && (y1 != y2)) {
		boxIt(zleft,ztop,zright,zbottom);
		document.Form1.zxmax.value = tempX2;
		document.Form1.zxmin.value = tempX;
		document.Form1.zymax.value = tempY2;
		document.Form1.zymin.value = tempY;
	}
	return false;
}
/*
***************************************************************************************
Click/Mouse functions - used to catch mouse button events
***************************************************************************************
*/

// check for mouseup (onMouseUp event)
function chkMouseUp(e) { 
	if (zooming){						
		document.Form1.submit();				
		stopZoomBox(e);
		hideLayer("theMap");
		showRetrieveMap();
	}
	
	if (panning) {			
		stopPan(e);
		panning=false;
		//hideLayer("theMap");
		//showRetrieveMap();
	}
	
	if (measuring) {		
		stopMeasuring(e);
		measuring=false;
		hideLayer("theMap");
		showRetrieveMap();
	}
	
	return true;

}

function showRetrieveMap() {
	if (hasLayer("LoadMap")) {		
		showLayer("LoadMap");
	}
}
		
// hide the "RetrieveMap" image
function hideRetrieveMap() {
	if (hasLayer("LoadMap")) {
		hideLayer("LoadMap");
	}
}

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;
}


	// move layer to x,y
function moveLayer(name, x, y) {		
  	var layer = getLayer(name);		
  	if (isNav4)
    		document.layers[name].moveTo(x, y);
	else {
	
    	layer.left = x + "px";
   		layer.top = y + "px";
   		
   	}
}

// set layer background color
function setLayerBackgroundColor(name, color) {		
	var layer = getLayer(name);		
	 if (isNav4)
	layer.bgColor = color;
	//else if (document.all)
	else
	layer.backgroundColor = color;
}
	
// get the layer object called "name"
function getLayer(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);
}	
function startPan(e) {
	
	moveLayer("theMap",hspc,vspc);
	
	getImageXY(e);		
	
	// keep it within the MapImage
	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 startMeasuring(e) {
	
	getImageXY(e);
	//moveLayer("theMap",hspc,vspc);	
	
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (measuring) {
			stopMeasuring(e);
		} else {
			x1=mouseX;
			y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			measuring=true;
		}
	}
	return false;

}


function stopMeasuring(e) {
		
	window.scrollTo(0,0);
	measuring=false;

	//hideLayer("theMap");
	
	//document.writeln (x1 + "," + y1 + "<br>")
	//document.writeln (x2 + "," + y2)
	
	x2=mouseX;
	y2=mouseY;	
	
	
	document.Form1.zxmax.value = x2;
	document.Form1.zxmin.value = x1;
	document.Form1.zymax.value = y2;
	document.Form1.zymin.value = y1;
	//showRetrieveMap();
	document.Form1.submit();

	return true;
	
}


// stop moving image.... pan 
function stopPan(e) {
	window.scrollTo(0,0);
	panning=false;

	hideLayer("theMap");
	
	x2=mouseX;
	y2=mouseY;	
	
	
	//document.Form1.zxmax.value = x2;
	//document.Form1.zxmin.value = x1;
	//document.Form1.zymax.value = y2;
	//document.Form1.zymin.value = y1;
	var xPanOffset = x2 - x1;
	var yPanOffset = y2 - y1;
	document.Form1.xcoord.value = (iWidth / 2) - xPanOffset;
	document.Form1.ycoord.value = (iHeight / 2) - yPanOffset
	//showRetrieveMap();
	document.Form1.submit();

	return true;
	
}
// move map image with mouse
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;
	}
	
	
	if (xMove<0) {
	//	cLeft = ;
		cRight = iWidth;
		}
		
	if (yMove<0) {
	//	cTop = 4;
		cBottom = iHeight;
	}
	
	clipLayer("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+hspc,yMove+vspc);	
	
	/*if (isNav4 != true){
		if (hasLayer("theMapClicks")) {
			clipLayer("theMapClicks",cLeft,cTop,cRight,cBottom);
			moveLayer("theMapClicks",xMove+hspc,yMove+vspc);

		}
	}*/
	//return false;
}
