// zoom box functionality

// 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 state = "zoom"; // "pan"

var zooming=false;
var panning=false;
var bottomBorderHeight = 13;
var showSelectBox=false;

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("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) {

	getImageXY(e);
	if ((!zooming) && (!panning) && (mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
		if (state == "pan") {
			//hideZoomBox();
			startPan(e);
		} else
			startZoomBox(e);
		return false;
	} else if ((!zooming) && (!panning) && (mouseX < 0) &&
		(mouseX >= ovHspc-hspc) && (mouseX <= ovHspc-hspc+ovWidth) &&
		(mouseY >= ovVspc-vspc) && (mouseY <= ovVspc-vspc+ovHeight)) {
		// user clicked ov map
		getOVImageXY(e);
		if ((mouseX>=0) && (mouseX<ovWidth) && (mouseY>=0) && (mouseY<ovHeight)) {
			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) {

	// if we're completely zoomed out, the values might not be correct otherwise
	reaspect();

	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;
}

function getOVImageXY(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 minx and maxy
	mouseX = mouseX-ovHspc;
	mouseY = mouseY-ovVspc;
}

// get coordinates on ov map and reset display
function ovMapClick(x,y) {

	var fullWidth = Math.abs(fullMaxx - fullMinx);
	var fullHeight = Math.abs(fullMaxy - fullMiny);
	var ovXincre = fullWidth / ovWidth;
	var ovYincre = fullHeight / ovHeight;
	var ovX = x;
	var ovY = ovHeight - y;
	var ovmapX = ovX * ovXincre + fullMinx;
	var ovmapY = ovY * ovYincre + fullMiny;
	var width = maxx - minx;
	var height = maxy - miny;
	newMinx = ovmapX - width / 2 ;
	newMaxx = ovmapX + width / 2;
	newMaxy = ovmapY +  height / 2;
	newMiny = ovmapY - height / 2;

	// keep zoom factor
	refreshMap();
}

// get click on OVmap and move display there
function ovMap2Click(e) {
	getOVImageXY(e);
	zooming=false;
	ovMapClick(mouseX,mouseY-2);
}

// recenter map is the default option
function recenter(e) {

	hideZoomBox();
	getMapXY(mouseX,mouseY);
	if (mapClickAsRecenter) {
		var widthHalf = Math.abs(maxx - minx) / 2;
		var heightHalf = Math.abs(maxy - miny) / 2;
		newMinx = mapX - widthHalf;
		newMaxx = mapX + widthHalf;
		newMaxy = mapY + heightHalf;
		newMiny = mapY - heightHalf;

		if (state == "zoomIn") {
			addPercent(-50);
		} else if (state == "zoomOut") {
			addPercent(75);
		} //else pan
		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";

//alert("move");
	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);
	// 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) {
	zooming=false;
	hideZoomBox();
	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;
		//alert(iWidth + " x " + iHeight + " : " + newMinx + ", " + newMiny + ", " + newMaxx + ", " + newMaxy);
		if (mapBoxAsZoom) {
			if (state == "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;
			}

			refreshMap();
		} else {
			showSelectBox=true;
			if (document.forms.length != 0) {
				if (document.forms[0].place) {
					document.forms[0].place.value = "";
				}
			}
			customMapBox(newMinx, newMiny, newMaxx, newMaxy);
		}

	}
	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)) {
		var ovBoxSize = 2;
		clipLayer("zoomBoxTop",zminx,zmaxy,zmaxx,zmaxy+ovBoxSize);
		clipLayer("zoomBoxLeft",zminx,zmaxy,zminx+ovBoxSize,zminy);
		clipLayer("zoomBoxRight",zmaxx-ovBoxSize,zmaxy,zmaxx,zminy);
		clipLayer("zoomBoxBottom",zminx,zminy-ovBoxSize,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;

		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 addPercent(value) {

	// add %
	value = 1 + value/100;

	var width = Math.abs(newMaxx-newMinx);
	var height = Math.abs(newMaxy-newMiny);
	var cx = newMinx + width/2;
	var cy = newMiny + height/2;

	newMinx = cx - width*value/2;
	newMiny = cy - height*value/2;
	newMaxx = cx + width*value/2;
	newMaxy = cy + height*value/2;
}

function showZoomBox() {
	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
}

function reaspect() {

	// check if extent is too big or too small
	width = maxx - minx;
	height = maxy - miny;
	if (height == 0) height = 0.0001;
	fullWidth = fullMaxx - fullMinx;
	fullHeight = fullMaxy - fullMiny;
	if (fullHeight == 0) fullHeight = 0.0001;

	if (width/fullWidth > height/fullHeight)
		percent = width/fullWidth;
	else
		percent = height/fullHeight;

	if (percent < minScale) {
		midX = minx + ((maxx-minx) / 2);
		midY = miny + ((maxy - miny) / 2);
		minx = midX - (fullWidth / 2 * minScale);
		maxy = midY + (fullHeight / 2 * minScale);
		maxx = midX + (fullWidth / 2 * minScale);
		miny = midY - (fullHeight / 2 * minScale);
		width = maxx - minx;
		height = maxy - miny;
	} else if (percent > maxScale) {
		midX = minx + ((maxx-minx) / 2);
		midY = miny + ((maxy - miny) / 2);
		minx = midX - (fullWidth / 2 * maxScale);
		maxy = midY + (fullHeight / 2 * maxScale);
		maxx = midX + (fullWidth / 2 * maxScale);
		miny = midY - (fullHeight / 2 * maxScale);
		width = maxx - minx;
		height = maxy - miny;
	}

	// if we move or change the extent of the map
	// and the map is somewhere at the side
	// place it inside full extent

	// stretch the image to the maxx size
	ratio = iWidth / iHeight;
	if (ratio == 0) ratio = 0.0001;
	if (width/height < ratio) {
		// stretch x
		mid = minx + width/2;
		minx = mid - (height * ratio)/2;
		maxx = mid + (height * ratio)/2;
		width = maxx - minx;
	} else {
		// stretch y
		mid = miny + height/2;
		miny = mid - (width / ratio)/2;
		maxy = mid + (width / ratio)/2;
		height = maxy - miny;
	}

	// now position the image maxx
	if ((minx + width > fullMaxx) && (maxx - width >= fullMinx)) {
		minx = fullMaxx - width;
		maxx = fullMaxx;
	}
	if ((maxx - width < fullMinx) && (minx + width <= fullMaxx)) {
		minx = fullMinx;
		maxx = fullMinx + width;
	}
	if ((minx + width >= fullMaxx) && (maxx - width <= fullMinx)) {
		minx = (fullMaxx+fullMinx)/2 - width/2;
		maxx = (fullMaxx+fullMinx)/2 + width/2;
	}

	if ((miny + height > fullMaxy) && (maxy - height >= fullMiny)) {
		miny = fullMaxy - height;
		maxy = fullMaxy;
	}
	if ((maxy - height < fullMiny) && (miny + height <= fullMaxy)) {
		miny = fullMiny;
		maxy = fullMiny + height;
	}
	if ((miny + height >= fullMaxy) && (maxy - height <= fullMiny)) {
		miny = (fullMaxy+fullMiny)/2 - height/2;
		maxy = (fullMaxy+fullMiny)/2 + height/2;
	}
}

// zoom using button.
//		zoomType: 1=in; 2=out
function zoomButton(zoomType) {
	var xHalf = (maxx-minx) / 2;
	var yHalf = (maxy-miny) / 2;

	if (zoomType == 1) {
		// zoom in
		newMinx = minx + (xHalf/2);
		newMaxx = maxx - (xHalf/2);
		newMaxy = maxy - (yHalf/2);
		newMiny = miny + (yHalf/2);
	}
	else {
		// zoom out
		newMinx = minx - xHalf;
		newMaxx = maxx + xHalf;
		newMaxy = maxy + yHalf;
		newMiny = miny - yHalf;
	}
	refreshMap();
}

