// 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;

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;
}	

// 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
      checkZoomExtents();
      aspectRatio();	
		refreshMap(); 
	} else
		customMapClick(mapX,mapY);
}

// get the coords at mouse position
function getMouse(e) {
 
	window.status="";
	getImageXY(e);

//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;
	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) {
			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;
			}
			checkZoomExtents();
         //aspectRatio();	
			//refreshMap();
         aspectRatio();	
			refreshMap();
			hideZoomBox();
		} else
			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 = 1;
		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) {
	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 ixOffset = x2-x1;
		var iyOffset = y1-y2;
		pixelX = width / iWidth;
		//var dY = 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;
      
      checkExtents();
		refreshMap();
	}
		
	return true;
	
}

function resetMapPosn()
{
	   moveLayer("theMap",hspc,vspc);
      clipLayer("theMap",0,0,iWidth,iHeight);
}

// 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");
}

// check if extent is too big or too small
function reaspect() {
	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 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;
	}
}

// Fix the aspect ratio for zommin/zoomout
function aspectRatio() {
	width = newMaxx - newMinx;
	height = newMaxy - newMiny;
	if (height == 0) height = 0.0001;

	// stretch the new selection to the correct aspect ratio
	ratio = iWidth / iHeight;
	if (ratio == 0) ratio = 0.0001; 
	if (width/height < ratio) {
		// stretch x 
		mid = newMinx + width/2;
		newMinx = mid - (height * ratio)/2;
		newMaxx = mid + (height * ratio)/2;
		width = newMaxx - newMinx;
	} else {
		// stretch y
		mid = newMiny + height/2;
		newMiny = mid - (width / ratio)/2;
		newMaxy = mid + (width / ratio)/2;
		height = newMaxy - newMiny;
	}

}

// check if extent of the new image is within the full extent bounds
function checkExtents() {
   // x < minimum x
	if (newMinx < fullMinx) {
	   var over = fullMinx - newMinx;
		newMinx = fullMinx;
		newMaxx = newMaxx + over;
	}
	// x > maximum x
	if (newMaxx > fullMaxx) {
	   var over = newMaxx - fullMaxx;
		newMinx = newMinx - over;
		newMaxx = fullMaxx;
	}
   // y < minimum y
	if (newMiny < fullMiny) {
	   var over = fullMiny - newMiny;
		newMiny = fullMiny;
		newMaxy = newMaxy + over;
	}
	// y > maximum y
	if (newMaxy > fullMaxy) {
	   var over = newMaxy - fullMaxy;
		newMiny = newMiny - over;
		newMaxy = fullMaxy;
	}
}

// check if extent of the new image is within the full extent bounds
function checkZoomExtents() {
   // x < minimum x
	if (newMinx < fullMinx) {
		newMinx = fullMinx;
	}
	// x > maximum x
	if (newMaxx > fullMaxx) {
		newMaxx = fullMaxx;
	}
   // y < minimum y
	if (newMiny < fullMiny) {
		newMiny = fullMiny;
	}
	// y > maximum y
	if (newMaxy > fullMaxy) {
		newMaxy = fullMaxy;
	}
}

