// aimsNavigation.js
aimsNavigationPresent=true;

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
		mouseX = xIn;
		pixelX = xDistance / iWidth;
		mapX = pixelX * mouseX + left;
		mouseY = iHeight - yIn;
		pixelY = yDistance / iHeight;
		mapY = pixelY * mouseY + bottom;
}

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();
	left = ovmapX - xHalf;
	right = ovmapX + xHalf;
	_top = ovmapY + yHalf;
	bottom = 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();

	left = mapX - (xHalf/zoomFactor);
	right = mapX + (xHalf/zoomFactor);
	_top = mapY + (yHalf/zoomFactor);
	bottom = mapY - (yHalf/zoomFactor);
	if(right-left < zoomInLimitHor && _top-bottom < zoomInLimitVer){
		zoomToChoiceMeasure(zoomLimitHor, zoomLimitVer);
	}
	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();

	left = mapX - (xDistance*zoomFactor/2);
	right = mapX + (xDistance*zoomFactor/2);
	_top = mapY + (yDistance*zoomFactor/2);
	bottom = mapY - (yDistance*zoomFactor/2);

	sendMapXML();
}

// get the coords at mouse position
function getMouse(e) {
	window.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 {

		if ((zooming) || (selectBox)) {
			x2=mouseX;
			y2=mouseY;
			setClip();
		} else if (panning) {
			x2=mouseX;
			y2=mouseY;
			panMouse();
		}
		pixelX = xDistance / iWidth;
		mapX = pixelX * mouseX + left;
		var theY = iHeight - mouseY;
		pixelY = yDistance / iHeight;
		mapY = pixelY * theY + bottom;

		if (toolMode==20) {
			calcDistance(mapX,mapY);
		}
		else if (showXYs) {
			var u = Math.pow(10,numDecimals);
			var uX = parseInt(mapX * u + 0.5) / u
			var uY= parseInt(mapY * u + 0.5) / u
			var mouseString = "Map: " + uX + " , " + uY + " -- Image: " + mouseX + " , " + mouseY;
			if(showScalePercent) mouseString = mouseString + " -- ScaleFactor: " + mapScaleFactor;
			if(windowstatus_ts != "")
				window.status = mouseString + " -- " + windowstatus_ts;
			else
				window.status = mouseString;
		}

	}
        return false;
}

// 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;
			showLayer("zoomBoxTop");
			showLayer("zoomBoxLeft");
			showLayer("zoomBoxRight");
			showLayer("zoomBoxBottom");

		}
	}
	return false;

}

// stop zoom box display... zoom in
function stopZoomBox(e) {
	zooming=false;
	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;
		_top = pixelY * theY + bottom;
		right = pixelX * zright + left;
		left = pixelX * zleft + left;
		theY = iHeight - zbottom;
		pixelY = yDistance / iHeight;
		bottom = pixelY * theY + bottom;
		window.scrollTo(0,0);
	      if(right-left < zoomInLimitHor && _top-bottom < zoomInLimitVer){
			zoomToChoiceMeasure(zoomLimitHor, zoomLimitVer);
		}
		sendMapXML();
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
	}
	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;
			showLayer("zoomBoxTop");
			showLayer("zoomBoxLeft");
			showLayer("zoomBoxRight");
			showLayer("zoomBoxBottom");
		}
	}
	return false;
}

// stop zoom out box. . . zoom out
function stopZoomOutBox(e) {
	zooming=false;
	if ((zright <zleft+2) && (zbottom < ztop+2)) {
		zoomout(e);
	} else {
		var tempLeft=lastLeft;
		var tempRight=lastRight;
		var tempTop=lastTop;
		var tempBottom=lastBottom;
		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;
		left = left - xAdd;
		right = right + xAdd;
		_top = _top + yAdd;
		bottom = bottom - yAdd;
		window.scrollTo(0,0);
		if (enforceFullExtent) {
			if ((right-left)>fullWidth) {
				left = fullLeft;
				right = fullRight;
				_top = fullTop;
				bottom = fullBottom;
				lastLeft=tempLeft;
				lastRight=tempRight;
				lastTop=tempTop;
				lastBottom=tempBottom;
			}
		}
		sendMapXML();
		hideLayer("zoomBoxTop");
		hideLayer("zoomBoxLeft");
		hideLayer("zoomBoxRight");
		hideLayer("zoomBoxBottom");
	}
	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)) {
		//clipLayer("zoomBox",zleft,ztop,zright,zbottom);
		boxIt(zleft,ztop,zright,zbottom);
	}
}

// start pan.... image will move
function startPan(e) {
  //console.debug('startPan()');
	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) {
  //console.debug('stopPan()');
	window.scrollTo(0,0);
	panning=false;
	var tempLeft=lastLeft;
	var tempRight=lastRight;
	var tempTop=lastTop;
	var tempBottom=lastBottom;
	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;
	_top = _top - yOffset;
	right = right - xOffset;
	left = left - xOffset;
	bottom = bottom - yOffset;

	checkFullExtent();

	hideLayer("theMap");
	if(hasLayer("theMapClicks")) {
		document.theClickImage.src = blankImage;
	}
	sendMapXML();
	return true;
}

// move map image with mouse
function panMouse() {
  //console.debug('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;
	}
	clipLayer("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+hspc,yMove+vspc);
	if (hasLayer("theMapClicks")) {
		clipLayer("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();

	left = mapX - xHalf;
	right = mapX + xHalf;
	_top = mapY + yHalf;
	bottom = mapY - yHalf;
	//var theString = writeXML();
	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);
}

function zoomToChoiceMeasure(xTravel, yTravel) {
	var tmpLeft = parseInt(left);
	var tmpRight = parseInt(right);
	var tmpTop = parseInt(_top);
	var tmpBottom = parseInt(bottom);

	var count_xDistance = parseInt(xDistance);
	var count_yDistance = parseInt(yDistance);

	while(count_xDistance != xTravel) {
		if(count_xDistance > xTravel) {
			tmpLeft += 0.5;
			tmpRight -= 0.5;
		}
		else {
			tmpLeft -= 0.5;
			tmpRight += 0.5;
		}
		count_xDistance = tmpRight - tmpLeft;
	}
	while(count_yDistance != yTravel) {
		if(count_yDistance > yTravel) {
			tmpTop -= 0.5;
			tmpBottom += 0.5;
		}
		else {
			tmpTop += 0.5;
			tmpBottom -= 0.5;
		}
		count_yDistance = tmpTop - tmpBottom;
	}

	left = tmpLeft;
	right = tmpRight;
	_top = tmpTop;
	bottom = tmpBottom;
}

