// zoom box functionality

var zoomLoaded = true;
// 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 zminx=0;
var zmaxx=0;
var zmaxy=0;
var zminy=0;

var mapX = 0; 
var mapY = 0; 
var zoomBoxWidth = 1;

var zooming=false;
var panning=false;
var bottomBorderHeight = 0;



function setZoomBoxSettings() {

	// Set up event capture for mouse movement
	//if (document.layers) {
	if (isNav && is5up) {
		document.captureEvents(Event.MOUSEMOVE);
		document.captureEvents(Event.MOUSEDOWN);
		document.captureEvents(Event.MOUSEUP);
		document.onmousemove = getMouse;
		document.onmousedown = mapTool;
		document.onmouseup = chkMouseUp;
	} else if (isNav4) {
		// otherwise the buttons don't work
/*		getLayer("theLocTop").captureEvents(Event.MOUSEMOVE);
		getLayer("theLocTop").captureEvents(Event.MOUSEDOWN);
		getLayer("theLocTop").captureEvents(Event.MOUSEUP);
		getLayer("theLocTop").onmousemove = getMouse;
		getLayer("theLocTop").onmousedown = mapTool;
		getLayer("theLocTop").onmouseup = chkMouseUp;*/
		getLayer("theTop").captureEvents(Event.MOUSEMOVE);
		getLayer("theTop").captureEvents(Event.MOUSEDOWN);
		getLayer("theTop").captureEvents(Event.MOUSEUP);
		getLayer("theTop").onmousemove = getMouse;
		getLayer("theTop").onmousedown = mapTool;
		getLayer("theTop").onmouseup = chkMouseUp;
	} else {
		document.onmousemove = getMouse;
		document.onmousedown = mapTool;
		document.onmouseup = chkMouseUp;
	}
}

// check for mouseup
function chkMouseUp(e) { 
	if (zooming || panning) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight)
			mouseY = iHeight;
		mapTool(e);
	}
}

// perform appropriate action with mapTool
function mapTool (e) {
// get variables (tool types) to take action on map click - getvars is in data.asp
getvars();
	getImageXY(e);
	if ((!zooming) && (!panning) && (mouseX >= 0) && (mouseX <= iWidth) && (mouseY >= 0) && (mouseY <= iHeight)) {
		switch (parent.ToolFrame.state) {
			case "Pan":
				startPan(e);
				break;
			case "Measure":
				getMapXY(mouseX,mouseY)
				document.mapcontrol.measurex.value = mapX;
				document.mapcontrol.measurey.value = mapY;
				refreshMap();
				break;
			case "Select":
				startZoomBox(e);
				break;
			case "ZoomOut":
			case "ZoomIn":
			case "IdentifyRect":
				startZoomBox(e);
				break;
			case "IdentifyM":
				getMapXY(mouseX,mouseY)
				document.mapcontrol.selectxmin.value = mapX;
				document.mapcontrol.selectymin.value = mapY;
				document.mapcontrol.selectxmax.value = mapX;
				document.mapcontrol.selectymax.value = mapY;
				document.mapcontrol.action.value = "IdentifyM";
				showLayer("loadLayer");
				refreshMap();
				break;
					}
			
		return false;
	} 
	else 
		if ((!zooming) && (!panning) && (mouseX >= ovHspc-hspc) && (mouseX <= ovHspc-hspc+ovWidth) && (mouseY >= ovVspc-vspc) && (mouseY <= ovVspc-vspc+ovHeight)) {
			// user clicked ov map
			getOVImageXY(e);
			if (allowMapClick)
				ovMapClick(mouseX,mouseY);
		} 
		else 
			if (zooming) {
				getMouse(e);
				stopZoomBox(e);
			} 
			else 
				if (panning) {
					getMouse(e);
					stopPan(e);
				}

	return true;
}

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {

	mouseX = xIn;
	var pixelX = (maxx-minx) / iWidth;
	mapX = pixelX * mouseX + minx;
	mouseY = iHeight - yIn;
	var pixelY = (maxy-miny) / iHeight;
	mapY = pixelY * mouseY + miny;
}

function getImageXY(e) {
	//if (document.layers) {
	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 right
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;
}	


// recenter map is the default option
function recenter(e) {

	// otherwise we don't have these layers
	if (allowRubberband)
		hideZoomBox();
		
	getMapXY(mouseX,mouseY);
	if (mapClickAsRecenter) {
		var widthHalf = Math.abs(maxx - minx) / 2;
		var heightHalf = Math.abs(maxy - miny) / 2;

		switch(parent.ToolFrame.state) {
			case "ZoomOut": 
				newMinx = mapX - widthHalf * 2;
				newMaxx = mapX + widthHalf * 2;
				newMaxy = mapY + heightHalf * 2;
				newMiny = mapY - heightHalf * 2;

				/*percentX = 2 //(maxx-minx)/(newMaxx-newMinx);
				percentY = 2 //(maxy-miny)/(newMaxy-newMiny);
				percent = (percentX+percentY)/2;
				
				widthH = (maxx-minx)/2;
				heightH = (maxy-miny)/2;
				cx = newMinx + widthH;
				cy = newMiny + heightH;
				
				newMinx = cx - percent * widthH;
				newMiny = cy - percent * heightH;
				newMaxx = cx + percent * widthH;
				newMaxy = cy + percent * heightH;*/

				document.mapcontrol.xmin.value = newMinx;
				document.mapcontrol.ymin.value = newMiny;
				document.mapcontrol.xmax.value = newMaxx;
				document.mapcontrol.ymax.value = newMaxy;
				//document.mapcontrol.action.value = "ZoomOut";
				showLayer("loadLayer");
				refreshMap();
				break;
			case "ZoomIn":
				newMinx = mapX - widthHalf / 2;
				newMaxx = mapX + widthHalf / 2;
				newMaxy = mapY + heightHalf / 2;
				newMiny = mapY - heightHalf / 2;
				document.mapcontrol.xmin.value = newMinx;
				document.mapcontrol.ymin.value = newMiny;
				document.mapcontrol.xmax.value = newMaxx;
				document.mapcontrol.ymax.value = newMaxy;
				//document.mapcontrol.action.value = "ZoomIn";
				showLayer("loadLayer");
				refreshMap();
				break;
			case "IdentifyRect":
				// Generate a map with the selected area highlighted
				document.mapcontrol.selectxmin.value = mapX;
				document.mapcontrol.selectymin.value = mapY;
				document.mapcontrol.selectxmax.value = mapX;
				document.mapcontrol.selectymax.value = mapY;
				document.mapcontrol.action.value = "IdentifyM";
				showLayer("loadLayer");
				refreshMap();
				break;
			case "Select":
				document.mapcontrol.selectxmin.value = mapX;
				document.mapcontrol.selectymin.value = mapY;
				document.mapcontrol.selectxmax.value = mapX;
				document.mapcontrol.selectymax.value = mapY;
				document.mapcontrol.action.value = "Select";
				showLayer("loadLayer");
				refreshMap();
				break;
		}
		refreshMap(); 
	} else
		customMapClick(mapX,mapY);
}

// get the coords at mouse position
function getMouse(e) {
 

	window.status="";
	getImageXY(e);
	//if (!document.layers && document.all.theTop) 
//	if (isIE && document.all.theTop) 
//		document.all.theTop.style.cursor = "crosshair";

	if (zooming) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight-bottomBorderHeight)
			mouseY = iHeight-bottomBorderHeight;
		x2=mouseX;
		y2=mouseY;
		setClip();
		return false;
	} else if (panning) {
		x2=mouseX;
		y2=mouseY;
		panMouse();	
		return false;
	} else 
    	return true;
	return true;
}

// start zoom in.... box displayed
function startZoomBox(e) {

	getImageXY(e);	

	if (!mapBoxAsZoom) { // allowRubberband) {
		stopZoomBox(e);
	} else {	
		// keep it within the MapImage
		if ((mouseX<iWidth) && (mouseY<iHeight-bottomBorderHeight)) {
			if (!zooming) {
				x1=mouseX;
				y1=mouseY;
				x2=x1+1;
				y2=y1+1;
				zooming=true;
				clipLayer("zoomBoxTop",x1,y1,x2,y2);
				clipLayer("zoomBoxLeft",x1,y1,x2,y2);
				clipLayer("zoomBoxRight",x1,y1,x2,y2);
				clipLayer("zoomBoxBottom",x1,y1,x2,y2);
				showZoomBox();
			}
		} else {
			if (zooming) {
				stopZoomBox(e);
			}
		}
	}
	return false;	
}

// stop zoom box display... zoom in
function stopZoomBox(e) {
	//alert(parent.ToolFrame.state);
	zooming=false;
	if ((zmaxx <zminx+2) && (zmaxy < zminy+2)) {
		// if the zoom box is too small
		recenter(e);
	} else {
		var width = Math.abs(maxx - minx);
		var height = Math.abs(maxy - miny);
		var pixelX = width / iWidth;
		var theY = iHeight - zmaxy;
		var pixelY = height / iHeight;
		newMaxy = pixelY * theY + miny;
		newMaxx = pixelX * zmaxx + minx;
		newMinx = pixelX * zminx + minx;
		theY = iHeight - zminy;
		pixelY = height / iHeight;
		newMiny = pixelY * theY + miny;

		if (mapBoxAsZoom) {
			switch(parent.ToolFrame.state) {
				case "ZoomOut": 
					percentX = (maxx-minx)/(newMaxx-newMinx);
					percentY = (maxy-miny)/(newMaxy-newMiny);
					percent = (percentX+percentY)/2;
				
					widthH = (maxx-minx)/2;
					heightH = (maxy-miny)/2;
					cx = newMinx + widthH;
					cy = newMiny + heightH;
				
					newMinx = cx - percent * widthH;
					newMiny = cy - percent * heightH;
					newMaxx = cx + percent * widthH;
					newMaxy = cy + percent * heightH;
				
				case "ZoomIn":
					document.mapcontrol.xmin.value = newMinx;
					document.mapcontrol.ymin.value = newMiny;
					document.mapcontrol.xmax.value = newMaxx;
					document.mapcontrol.ymax.value = newMaxy;
					//document.mapcontrol.action.value = "ZoomIn";
					showLayer("loadLayer");
					refreshMap();
					break;
				case "IdentifyRect":
					// Generate a map with the selected area highlighted
					document.mapcontrol.selectxmin.value = newMinx;
					document.mapcontrol.selectymin.value = newMiny;
					document.mapcontrol.selectxmax.value = newMaxx;
					document.mapcontrol.selectymax.value = newMaxy;
					document.mapcontrol.action.value = "IdentifyM";
					showLayer("loadLayer");
					refreshMap();
					break;
				case "Select":
					// Generate a map with the selected area highlighted
					document.mapcontrol.selectxmin.value = newMinx;
					document.mapcontrol.selectymin.value = newMiny;
					document.mapcontrol.selectxmax.value = newMaxx;
					document.mapcontrol.selectymax.value = newMaxy;
					document.mapcontrol.action.value = "Select";
					showLayer("loadLayer");
					refreshMap();
					break;
			}
		} else
			refreshMap();
	}
	return true;
}

// clip zoom box layer to mouse coords
function setClip() {	

	if (x1>x2) {
		zmaxx=x1;
		zminx=x2;
	} else {
		zminx=x1;
		zmaxx=x2;
	}
	if (y1>y2) {
		zminy=y1;
		zmaxy=y2;
	} else {
		zmaxy=y1;
		zminy=y2;
	}
	
	if ((x1 != x2) && (y1 != y2)) {
		clipLayer("zoomBoxTop",zminx,zmaxy,zmaxx,zmaxy+zoomBoxWidth);
		clipLayer("zoomBoxLeft",zminx,zmaxy,zminx+zoomBoxWidth,zminy);
		clipLayer("zoomBoxRight",zmaxx-zoomBoxWidth,zmaxy,zmaxx,zminy);
		clipLayer("zoomBoxBottom",zminx,zminy-zoomBoxWidth,zmaxx,zminy);
	}
}

// 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) {

	if ((Math.abs(x2-x1) < 2) && (Math.abs(y2-y1) < 2)) {
		// the move is too small
		recenter(e);
	} else  {
		window.scrollTo(0,0);
		panning=false;
		var width = Math.abs(maxx - minx);
		var height = Math.abs(maxy - miny);
		var tempLeft=minx;
		var tempRight=maxx;
		var tempTop=maxy;
		var tempBottom=miny;
		var ixOffset = x2-x1;
		var iyOffset = y1-y2;
		pixelX = width / iWidth;
		var theY = iHeight - zmaxy;
		pixelY = height / iHeight;
		var xOffset = pixelX * ixOffset;
		var yOffset = pixelY * iyOffset;
		newMaxy = maxy - yOffset;
		newMaxx = maxx - xOffset;
		newMinx = minx - xOffset;
		newMiny = miny - yOffset;

		// AP specific setting
		attributeUpdate = false;
		
		document.mapcontrol.xmin.value = newMinx;
		document.mapcontrol.ymin.value = newMiny;
		document.mapcontrol.xmax.value = newMaxx;
		document.mapcontrol.ymax.value = newMaxy;
		document.mapcontrol.action.value = "Pan";
		
		showLayer("loadLayer");
		refreshMap();
	}
		
	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;
	}
	clipLayer("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+hspc,yMove+vspc);

	return false;
}

function hideZoomBox() {
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
}

function showZoomBox() {
	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
}

function setZoomBoxColor(color) {
	setLayerBackgroundColor("zoomBoxTop", color);
	setLayerBackgroundColor("zoomBoxLeft", color);
	setLayerBackgroundColor("zoomBoxRight", color);
	setLayerBackgroundColor("zoomBoxBottom", color);
}

function setZoomBoxWidth(size) {
	zoomBoxWidth = size;
}
