
var hspc = 0;


var newMinx = 0;
var newMiny = 0;
var newMaxx = 0;
var newMaxy = 0;

// 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 zooming=false;
var panning=false;

// setup test for Nav
var isNav = (navigator.appName.indexOf("Netscape")>=0);

//***********************************************	
//***************** FUNCTIONS *******************
//***********************************************

// set zoom box settings
function setZoomBoxSettings() {
	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);
	}
}


// get Image X Y
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;
}	


// check scrollbar click
function checkScrollbarClick(e){
	if (isNav) {
		cX=e.pageX;
		cY=e.pageY;
	} else {
		cX=event.clientX;
		cY=event.clientY;
	}
	if (isNav) {
		pW=window.innerWidth-16;
		pH=window.innerHeight-16;
	} else {
		pW=document.body.offsetWidth-20;
		pH=document.body.offsetHeight-20;
	}
	if((cX > pW)||(cY > pH)){
		return false;
	} else{
		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;
}


// get the coords at mouse position
function getMouse(e) {
 	window.status="";
	getImageXY(e);
	curTool = document.frmMain.tool.value;
	if(curTool == "MEASURE"){
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight)
			mouseY = iHeight;
		getMapXY(mouseX, mouseY);
		calcDistanceSegment(mapX,mapY);
	}
	if (zooming) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight)
			mouseY = iHeight;
		x2=mouseX;
		y2=mouseY;
		window.status= x1 + "," + x1 + ":" + x2 + "," + y2;
		setClip();
		return false;
	} else if (panning) {
		x2=mouseX;
		y2=mouseY;
		panMouse();	
		return false;
	} else 
    	return true;
	return true;
}

// perform appropriate action with mapTool
function mapTool (e) {
	if((document.frmMain.searchpanel.value == "SUBDIVISION") || (document.frmMain.searchpanel.value == "CONDO")){
		return false;
	}
	if(checkScrollbarClick(e)){
		curTool = document.frmMain.tool.value;
		getImageXY(e);
		// Deal with the possibility of a
		// select, identify or area tool first, since it is a little different than the other states
		// (doesn't require a mouse up event)
		//IDENTIFY
		if ((curTool == "IDENTIFY") && 
			(mouseX >= 0) && (mouseX <= iWidth) && 
			(mouseY >= 0) && (mouseY <= iHeight)){
				getMapXY(mouseX,mouseY);
				sendIdentify(mapX,mapY);
				return false;	             
		}
		//MEASURE
		if ((curTool == "MEASURE") && 
			(mouseX >= 0) && (mouseX <= iWidth) && 
			(mouseY >= 0) && (mouseY <= iHeight)){
				//if(clickCount >= 2){
				//	resetMeasureTool();
				//}
				getMapXY(mouseX,mouseY);
				addMeasurePixel(mouseX,iHeight-mouseY);
				addMeasurePoint(mapX,mapY);
				return false;
		}
		//ZOOMIN, ZOOMOUT, PAN, SELECT
		if ((!zooming) && (!panning) && 
			(mouseX >= 0) && (mouseX <= iWidth) && 
			(mouseY >= 0) && (mouseY <= iHeight)) {
			if (curTool == "PAN")
				startPan(e);
			else 
				startZoomBox(e);
			return false;
		} else if (zooming) {
			getMouse(e);
			stopZoomBox(e);
		} else if (panning) {
			getMouse(e);
			stopPan(e);
		}
		return true;
	}
}


// start zoom in.... box displayed
function startZoomBox(e) {
	getImageXY(e);	
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (!zooming) {
			x1=mouseX;
			y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			zooming=true;
			moveLayer("MapZoomBox",hspc+x1,vspc+y1);
			resizeLayer("MapZoomBox", 1, 1);
			//clipLayer("MapZoomBox",x1,y1,x2,y2);
			showLayer("MapZoomBox");
			
		} else {
			if (zooming) {
				stopZoomBox(e);
			}
		}
	}
	return false;	
}

function stopZoomBox(e) {
	hideLayer("MapZoomBox");
	zooming=false;
	curTool = document.frmMain.tool.value;
	var width = Math.abs(maxx - minx);
	var height = Math.abs(maxy - miny);
	var pixelX = width / iWidth;
	var pixelY = height / iHeight;
	var theminY = iHeight - zminy;
	var themaxY = iHeight - zmaxy;
	newMinx = pixelX * zminx + minx;
	newMiny = pixelY * theminY + miny;
	newMaxx = pixelX * zmaxx + minx;
	newMaxy = pixelY * themaxY + miny;
	// if the zoom box is too small
	if ((zmaxx <zminx+2) && (zmaxy < zminy+2)) {
		if (curTool == "ZOOMOUT") {
			newMinx = newMinx - width;
			newMiny = newMiny - height;
			newMaxx = newMaxx + width;
			newMaxy = newMaxy + height;
		} else if (curTool == "SELECT") {
			newMaxx = newMinx + 2;
			newMaxy = newMiny  + 2;
		} else {
			newMinx = newMinx - width / 4;
			newMiny = newMiny - height / 4;
			newMaxx = newMaxx + width / 4;
			newMaxy = newMaxy + height / 4;
		}
		refreshMap();
	// if the zoom box is not too small
	} else {
		if (curTool == "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();
	}
	return true;
}


// 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;
	if ((Math.abs(x2-x1) < 2) && (Math.abs(y2-y1) < 2)) {
		// the move is too small
		getMapXY(mouseX,mouseY);
		var widthHalf = Math.abs(maxx - minx) / 2;
		var heightHalf = Math.abs(maxy - miny) / 2;
		newMinx = mapX - widthHalf;
		newMaxx = mapX + widthHalf;
		newMiny = mapY - heightHalf;
		newMaxy = mapY + heightHalf;
	} else  {
		var width = Math.abs(maxx - minx);
		var height = Math.abs(maxy - miny);
		var ixOffset = x2-x1;
		var iyOffset = y1-y2;
		pixelX = width / iWidth;
		pixelY = height / iHeight;
		var xOffset = pixelX * ixOffset;
		var yOffset = pixelY * iyOffset;
		newMinx = minx - xOffset;
		newMiny = miny - yOffset;
		newMaxx = maxx - xOffset;
		newMaxy = maxy - 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;
}


// refresh map
function refreshMap() {
  if (curTool == "SELECT") {
   document.frmMain.cmd.value = "SELECT" 
   document.frmMain.selminX.value = newMinx.toString();
   document.frmMain.selminY.value = newMiny.toString();
   document.frmMain.selmaxX.value = newMaxx.toString();
   document.frmMain.selmaxY.value = newMaxy.toString();  
  } else {
    document.frmMain.cmd.value = "REFRESH";
    document.frmMain.minX.value = newMinx.toString();
    document.frmMain.minY.value = newMiny.toString();
    document.frmMain.maxX.value = newMaxx.toString();
    document.frmMain.maxY.value = newMaxy.toString();
  }
  document.frmMain.submit();
  showLayer("Splash");
}


// send select
function sendSelect(numMapX,numMapY) {
  document.frmMain.cmd.value = "SELECT";
  document.frmMain.selminX.value = numMapX.toString();
  document.frmMain.selminY.value = numMapY.toString();
  //document.getElementById("imgMap").click();
  document.frmMain.submit();
  showLayer("Splash");
}


// send identify
function sendIdentify(numMapX,numMapY) {
  document.frmMain.cmd.value = "IDENTIFY";
  document.frmMain.selminX.value = numMapX.toString();
  document.frmMain.selminY.value = numMapY.toString();
  //document.getElementById("imgMap").click();
  document.frmMain.submit();
  showLayer("Splash");
}


// 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 {
		zminy=y2;
		zmaxy=y1;
	}
	if ((x1 != x2) && (y1 != y2)) {
		moveLayer("MapZoomBox", hspc+zminx, vspc+zmaxy);
		//moveLayer("MapZoomBox", hspc+zminx, vspc+zminy);
		resizeLayer("MapZoomBox", Math.abs(zmaxx-zminx), Math.abs(zmaxy-zminy));
		//clipLayer("MapZoomBox",zminx,zminy,zmaxx,zmaxy);
		showLayer("MapZoomBox");
	}
}


//-------- LAYER SUPPORT FUNCTIONS --------------

// get the layer object called "name"
function getLayer(name) {
	var theObj = document.getElementById(name);
	if(theObj != null){
		return(theObj.style);
	} else {
		if ( eval('document.all.' + name) != null) {
		    layer = eval('document.all.' + name + '.style');
		    return(layer);
		} else {
			return(null);
		}
	
	}
}

// clip layer display to clipleft, cliptip, clipright, clipbottom
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);		
	if (layer != null) {
  		layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	}
}

// toggle layer to invisible
function hideLayer(name) {		
  	var layer = getLayer(name);		
	if (layer != null) {
		layer.visibility = "hidden";
		layer.overflow = "hidden";
	}
}

// toggle layer to visible
function showLayer(name) {		
  	var layer = getLayer(name);		
	if (layer != null) {
	  	layer.visibility = "visible";
	  	layer.overflow = "visible";
	}
}

// move layer to x,y
function moveLayer(name, x, y) {
	var layer = getLayer(name);		
	if (layer != null) {
	  	layer.left = x + "px";
   		layer.top  = y + "px";
	}
}

// resize layer to wd,ht
function resizeLayer(name, wd, ht) {
	var layer = getLayer(name);		
	if (layer != null) {
	  	layer.width = wd + "px";
   		layer.height  = ht + "px";
	}
}


// replace layer content
function replaceLayerContent(name, content) {
	if (document.getElementById(name) != null) {
		document.getElementById(name).innerHTML = content;
	}
}
