// aimsNavigation.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js
*/

aimsNavigationPresent=true;

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
		mouseX = xIn;
		pixelX = curExt.xSize / iWidth;
		mapX = pixelX * mouseX + curExt.minX ;
		mouseY = iHeight - yIn;
		pixelY = curExt.ySize / iHeight;
		mapY = pixelY * mouseY + curExt.minY;
}

function getImageXY(e) {
	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else {
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop;
	}
}	

// get coordinates on ov map and reset display
function ovMapClick(x,y) {
	var ovXincre = (ovBbMaxX - ovBbMinX) / ovImgWidth;
	var ovYincre = (ovBbMaxY - ovBbMinY) / ovImgHeight;
	var ovX = x;
	var ovY = ovImgHeight - y;
	var ovmapX = ovX * ovXincre + ovBbMinX;
	var ovmapY = ovY * ovYincre + ovBbMinY;
	var xHalf = curExt.xSize / 2;
	var yHalf = curExt.ySize / 2;
	saveLastExtent();

	reqExt.set(ovmapX - xHalf,ovmapY - yHalf,ovmapX + xHalf,ovmapY + yHalf);
	requestNewMainMap();
}

// zoom in around mouse click
function zoomin(e) {
	getMapXY(mouseX,mouseY);
	var xHalf = curExt.xSize / 2;
	var yHalf = curExt.ySize / 2;
	saveLastExtent();
  reqExt.set(mapX - (xHalf/zoomFactor),mapY - (yHalf/zoomFactor),mapX + (xHalf/zoomFactor),mapY + (yHalf/zoomFactor));
	requestNewMainMap();
}

// zoom out from mouse click
function zoomout(e) {
	getMapXY(mouseX,mouseY);
	saveLastExtent();
	reqExt.set(mapX - (curExt.xSize*zoomFactor/2),mapY - (curExt.ySize*zoomFactor/2),mapX + (curExt.xSize*zoomFactor/2),mapY + (curExt.ySize*zoomFactor/2));
	requestNewMainMap();
}	

// get the coords at mouse position
function getMouse(e) {
	window.status="";
	getImageXY(e);
	if (isIE) {
		if ((hasOVMap) && (ovIsVisible) ) {
			
		 	if ((mouseX<ovImgWidth+2) && (mouseY<ovImgHeight)) {
		 	
				document.all.theTop.style.cursor = "default";
		 	} else {
		 		document.all.theTop.style.cursor = theCursor;
		 	}
		}
	}
	if ((mouseX>iWidth) || (mouseY>iHeight) || (mouseX<=0) ||(mouseY<=0) || ((hasOVMap) && (ovIsVisible) && (mouseX<ovImgWidth+ovBoxSize) && (mouseY<ovImgHeight+ovBoxSize) )) {
		if (zooming)
			chkMouseUp(e);
		} 		
		
	 else {
		mouseStuff();
	}
	return false;
}

function mouseStuff() {

	var ngCoordStr = "";
	var mouseStr   = "";
	var showScalePercentStr = "";
	// make local copies of mapX and MapY
	var mapX=mapX;
	var mapY=mapY;
	
	if (zooming) {
		x2=mouseX;
		y2=mouseY;
		setClip();
	} 

	pixelX = curExt.xSize / iWidth;
	mapX = pixelX * mouseX + curExt.minX;
	var theY = iHeight - mouseY;
	pixelY = curExt.ySize / iHeight;
	mapY = pixelY * theY + curExt.minY;
	
	if (toolMode == MAPTOOL_MEASURE) {
		calcDistance(mapX,mapY);	
	} 
	/*   Commented to remove from status bar 
	else if (showXYs) {

		// truncation multiplier
		var u = Math.pow(10, pageDisplayCoordPrecision);

		// truncated coords (for display)
		var uX = parseInt(mapX * u + (5/10)) / u
		var uY = parseInt(mapY * u + (5/10)) / u

		// check lat coords - block values outside of [-90 .. 90] range

		var mouseStr;
		if (mapY > 90.0 || mapY < -90.0) {
			mouseStr = msgList[9] + " coords undefined. ";	
		}
		else {
			if (useDmsDisplayCoords) {
				mouseStr = msgList[9] + ConvertToDDMMSS(mapX)
					+ msgList[14] + ConvertToDDMMSS(mapY);
				// display image coordinates: sometimes useful for debugging
		    // + msgList[10] + mouseX + msgList[14] + mouseY;	
			}
			else {
				mouseStr = msgList[9] + uX + msgList[14] + uY;	
			}
		}

		if (showNationalGrid) {
			// use raw coord values (non-rounded) as input
			var ngCoords = LLtoUSNG(mapY, mapX);
			ngCoordStr = msgList[13] + msgList[12] + ngCoords;
		}

		if (showScalePercent) {

			var v = Math.pow(10,4);
			var DDMapScaleFactor=curExt.xSize/iWidth;

			vMapScaleFactor = parseInt(DDMapScaleFactor * v +(5/10))/v;

			showScalePercentStr = msgList[13] + msgList[11] 
							+ vMapScaleFactor + msgList[15];
		}

		window.status = mouseStr + ngCoordStr + showScalePercentStr;
                */  // comment removes position information from status bar
	
		// truncation multiplier
		var u = Math.pow(10, pageDisplayCoordPrecision);

		// truncated coords (for display)
		var uX = parseInt(mapX * u + (5/10)) / u
		var uY = parseInt(mapY * u + (5/10)) / u
		// display vars on footer frame
		var displayX = top.BottomFrame.document.getElementById("coordx");
		var displayY = top.BottomFrame.document.getElementById("coordy");
		var displayUSNG = top.BottomFrame.document.getElementById("usng");
		var ngCoords = LLtoUSNG(mapY, mapX);
		if(displayX !=null){
			if (useDmsDisplayCoords) {
				displayX.value = ConvertToDDMMSS(mapX);
				displayY.value = ConvertToDDMMSS(mapY);
			}		
			else{
				displayX.value = uX+ "\260";
				displayY.value = uY+ "\260";
			}
			displayUSNG.value = ngCoords;
		}	
}

function clearCoords(e){
		var displayX = top.BottomFrame.document.getElementById("coordx");
		var displayY = top.BottomFrame.document.getElementById("coordy");
		var displayUSNG = top.BottomFrame.document.getElementById("usng");
		var ngCoords = LLtoUSNG(mapY, mapX);
		if(displayX !=null){
			displayX.value ="";
			displayY.value ="";
			displayUSNG.value ="";
		}	
}

// start zoom in.... box displayed
function startZoomBox(e) {
	getImageXY(e);	
	// keep it within the MapImage
	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;
	
}

// stop zoom box display... zoom in
function stopZoomBox(e) {
	zooming=false;
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	if ((zright <zleft+2) && (zbottom < ztop+2)) {
		zoomin(e);
	} else {
		saveLastExtent();
		var xDistance = curExt.xSize;
		var yDistance = curExt.ySize;
		pixelX = curExt.xSize / iWidth;
		var theY = iHeight - ztop;
		pixelY = curExt.ySize / iHeight;
		reqExt.maxY = pixelY * theY + curExt.minY;
		reqExt.maxX = pixelX * zright + curExt.minX;
		reqExt.minX = pixelX * zleft + curExt.minX;
		theY = iHeight - zbottom;
		reqExt.minY = pixelY * theY + curExt.minY;
		window.scrollTo(0,0);
		requestNewMainMap();
	}
	return true;
}

// start zoom out... box displayed
function startZoomOutBox(e) {
	getImageXY(e);
	// keep it within the MapImage
	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;	
}

// stop zoom out box. . . zoom out
function stopZoomOutBox(e) {
	zooming=false;
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	if ((zright <zleft+2) && (zbottom < ztop+2)) {
		zoomout(e);
	} else {	
		saveLastExtent();
		var zWidth = Math.abs(zright-zleft);
		var zHeight = Math.abs(ztop-zbottom);
		var xRatio = iWidth / zWidth;
		var yRatio = iHeight / zHeight;
		var xAdd = xRatio * curExt.xSize / 2;
		var yAdd = yRatio * curExt.ySize  / 2;
		reqExt.set(curExt.minX - xAdd,curExt.minY - yAdd,curExt.maxX + xAdd,curExt.maxY + yAdd);
		window.scrollTo(0,0);
		requestNewMainMap();
	}
	return true;
}

// 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);
	}
}
