// 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;

/*
***************************************************************************************

Map Navigation functions - Zoom , Pan, etc.

***************************************************************************************
*/

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
		mouseX = xIn;
		pixelX = xDistance / iWidth;
		mapX = pixelX * mouseX + eLeft;
		mouseY = iHeight - yIn;
		pixelY = yDistance / iHeight;
		mapY = pixelY * mouseY + eBottom;
}

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;

}	

function getOVImageXY(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-ovHspc;
	mouseY = mouseY-ovVspc;
	
}


// get coordinates on ov map and reset display
function ovMapClick(x,y) {
	var ovWidth = i2Width;
	var ovHeight = i2Height;
	var ovXincre = fullOVWidth / ovWidth;
	var ovYincre = fullOVHeight / ovHeight;
	var ovX = x;
	var ovY = ovHeight - y;
	var ovmapX = ovX * ovXincre + fullOVLeft;
	var ovmapY = ovY * ovYincre + fullOVBottom;
	saveLastExtent();
	eLeft = ovmapX - xHalf;
	eRight = ovmapX + xHalf;
	eTop = ovmapY + yHalf;
	eBottom = ovmapY - yHalf;
	sendMapXML();
	
}

// get click on OVmap and move display there
function ovMap2Click(e) {
	getOVImageXY(e);
	mouseY = mouseY - ovBorderWidth;
	zooming=false;
	panning=false;
	selectBox=false;
	ovMapClick(mouseX,mouseY);
}


// zoom in around mouse click
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);
	
	sendMapXML();
}

// zoom out from mouse click
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);
	if (enforceFullExtent) {
		if ((eRight-eLeft)>fullWidth) {
			eLeft = fullLeft;
			eRight = fullRight;
			eTop = fullTop;
			eBottom = fullBottom;
			lastLeft=tempLeft;
			lastRight=tempRight;
			lastTop=tempTop;
			lastBottom=tempBottom;
		}
	}
	
	sendMapXML();
}	

// get the coords at mouse position
function getMouse(e) {
	self.status="";
	getImageXY(e);
	if (isIE) {
		if ((hasOVMap) && (ovIsVisible) && (ovMapIsLayer)) {
			
		 	if ((mouseX<i2Width+2) && (mouseY<i2Height)) {
		 	
				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<i2Width+ovBoxSize) && (mouseY<i2Height+ovBoxSize) && (ovMapIsLayer))) {
		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;
	
	if (toolMode==20) {
		calcDistance(mapX,mapY);	
	} 
	else if (showXYs) {
	//Dnyanada zoombox 2/7/2006
		var screenDPI = 96; 
		var inchesPerFoot = 12;
	//end Dnyanada 2/7/2006
	
		var u = Math.pow(10,numDecimals);
		var uX = parseInt(mapX * u + (5/10)) / u
		var uY= parseInt(mapY * u + (5/10)) / u
		
		//Dnyanada 2/7/2006 start
		//this for scale update in input box
		var scaleFactorDisplay = Math.round(mapScaleFactor * screenDPI * inchesPerFoot);
		parent.tbps.updateScale(scaleFactorDisplay);
			//end 2/7/2006 scale update
			
			
		var mouseString = msgList[52] + uX + " , " + uY + " -- " + msgList[53] + mouseX + " , " + mouseY;	
		//following line commented out by Dnyanada 1/7/2004
		//if (showScalePercent) mouseString = mouseString + " -- " + msgList[54] + mapScaleFactor;
		//added by Dnyanada 1/7/2004
		if (showScalePercent) mouseString = "Scale: " + parent.MapFrame.getMapScale(true) + " " + mouseString;
	
		//if (onOVArea) {mouseString += " -- On OV Map Area [" + i2Width + "x" + i2Height + "]"} else {mouseString += " -- Off OV Map Area [" + i2Width + "x" + i2Height + "]"}
		self.status = mouseString;
	}

}
// start zoom in.... box displayed
function startZoomBox(e) {
	moveLayer("theMap",hspc,vspc);
	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");
	
		//following line changed by Dnyanada to see a intermittent problem we had where it says "envelop not found" occures
	//think it is because of when a user zooms at a point but drags a mouse few pixels so either height or width is zero
	//it gives division by zero error 7/13/2004
	//if ((zright <zleft+2) && (zbottom < ztop+2)) {
	
	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);
	
		sendMapXML();
	}
	return true;

}

// start zoom out... box displayed
function startZoomOutBox(e) {
	moveLayer("theMap",hspc,vspc);
	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 {	
		var tempLeft=eLeft;
		var tempRight=eRight;
		var tempTop=eTop;
		var tempBottom=eBottom;
		saveLastExtent();
		var zWidth = Math.abs(zright-zleft);
		var zHeight = Math.abs(ztop-zbottom);
		var xRatio = iWidth / zWidth;
		var yRatio = iHeight / zHeight;
		var xAdd = xRatio * xDistance / 2;
		var yAdd = yRatio * yDistance / 2;
		eLeft = eLeft - xAdd;
		eRight = eRight + xAdd;
		eTop = eTop + yAdd;
		eBottom = eBottom - yAdd;
		window.scrollTo(0,0);
		if (enforceFullExtent) {
			if ((eRight-eLeft)>fullWidth) {
				eLeft = fullLeft;
				eRight = fullRight;
				eTop = fullTop;
				eBottom = fullBottom;
				lastLeft=tempLeft;
				lastRight=tempRight;
				lastTop=tempTop;
				lastBottom=tempBottom;
			}
		}
		sendMapXML();
	}
	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);
	}
}

// start pan.... image will move
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;

}

// stop moving image.... pan 
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;
	if (enforceFullExtent) {
		if (eLeft < limitLeft) {
			eLeft = limitLeft;
			eRight = eLeft + xDistance;
		}
		if (eTop > limitTop) {
			eTop = limitTop;	
			eBottom = eTop - yDistance;
		}	
		if (eRight > limitRight) {
			eRight = limitRight;
			eLeft = eRight - xDistance;
		}
		if (eBottom < limitBottom) {
			eBottom = limitBottom;	
			eTop = eBottom + yDistance;
		}	
	}
	lastLeft = tempLeft;
	lastRight = tempRight;
	lastTop = tempTop;
	lastBottom = tempBottom;
	//hideLayer("theMap");
	if (hasLayer("theMapClicks")) {
		document.theClickImage.src = blankImage;

	}
	//document.theImage.onload = resetPanImage;
	sendMapXML();
	
	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;
	}
	clipLayer2("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+hspc,yMove+vspc);
	if (hasLayer("theMapClicks")) {
		clipLayer2("theMapClicks",cLeft,cTop,cRight,cBottom);
		moveLayer("theMapClicks",xMove+hspc,yMove+vspc);

	}
}

// pan to mouse click
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;
	sendMapXML();
}	


function setZoomColor() {
	setLayerBackgroundColor("zoomBoxTop", zoomBoxColor);
	setLayerBackgroundColor("zoomBoxLeft", zoomBoxColor);
	setLayerBackgroundColor("zoomBoxRight", zoomBoxColor);
	setLayerBackgroundColor("zoomBoxBottom", zoomBoxColor);
	
	setLayerBackgroundColor("zoomOVBoxTop", zoomBoxColor);
	setLayerBackgroundColor("zoomOVBoxLeft", zoomBoxColor);
	setLayerBackgroundColor("zoomOVBoxRight", zoomBoxColor);
	setLayerBackgroundColor("zoomOVBoxBottom", zoomBoxColor);
}

 // Returns map scale assuming 96 dpi
// If formated == true then it is returned as a string in the format "1:x,xxx"
// Otherwise an integer is returned
function getMapScale(formated)
{
	var tempScale = (parent.MapFrame.eRight - parent.MapFrame.eLeft) / parent.MapFrame.iWidth;
	var tempFactor = 96;
	//Dnyanada changed following to display scale in feet 2/27/2004
	//var inchPerMeter = 39.4;
	var inchPerFeet = 12;
	//end
	
	if (formated)
	{
		//Dnyanada changed following to display scale in feet 2/27/2004
		//var scaleValue = parseInt((tempScale * tempFactor * inchPerMeter) + 0.5);
		var scaleValue = parseInt((tempScale * tempFactor * inchPerFeet) + 0.5);
		//end
		
		var scale = scaleValue.toString(10);
		
		var formatedScale = "";
		var scaleLength = scale.length;
		
		if (scaleLength > 3)
		{
			var rightPos = scaleLength;

			// The first three numbers on the right
			formatedScale = scale.substring(rightPos - 3, rightPos);

			// Now all the bits in the middle
			for (rightPos -= 3; rightPos > 3; rightPos -= 3)
			{
				formatedScale = scale.substring(rightPos - 3, rightPos) + "," + formatedScale;				
			}

			// Finally the header
			formatedScale = scale.substring(0, rightPos) + "," + formatedScale;				
		}
		else
		{
			formatedScale = scale;
		}
		
		return "1:" + formatedScale;
	}
	else
	{	
		//Dnyanada 5/2/2006 changed following line
		return  parseInt((tempScale * tempFactor * inchPerFeet) + 0.5);
		//return parseInt((tempScale * tempFactor * inchPerMeter) + 0.5);
	}
}
  