var newMinx = 0;
var newMiny = 0;
var newMaxx = 0;
var newMaxy = 0;
var boxMinx = 0;
var boxMiny = 0;
var boxMaxx = 0;
var boxMaxy = 0;

var mapClickAsRecenter = true;
var mapBoxAsZoom = true;
var allowRubberband = true;
var allowMapClick = 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;

//SEARCHLITE var state = "zoom"; // "pan"

var zooming=false;
var panning=false;
var bottomBorderHeight = 13;


//***********************************************	
//***************** FUNCTIONS *******************
//***********************************************

// check for mouseup
function chkMouseUp(e) { 
	if (zooming || panning) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>mapWidth)
			mouseX = mapWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>mapHeight)
			mouseY = mapHeight;
		imgX2 = mouseX;
		imgY2 = mouseY;
		mapTool(e);
	}
}

function customMapBox(_newMinx, _newMiny, _newMaxx, _newMaxy) {

}

function customMapClick(_newX, _newY) {

}

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;
}	

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {

	mouseX = xIn;
	var pixelX = (document.getElementById('mapxmax').value-document.getElementById('mapxmin').value) / mapWidth;
	mapX = (pixelX * mouseX) + parseFloat(document.getElementById('mapxmin').value);
	mouseY = mapHeight - yIn;
	var pixelY = (document.getElementById('mapymax').value-document.getElementById('mapymin').value) / mapHeight;
	mapY = (pixelY * mouseY) + parseFloat(document.getElementById('mapymin').value);
}

// get the coords at mouse position
function getMouse(e) {
 
	window.status="";
	getImageXY(e);
	if (zooming) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>mapWidth)
			mouseX = mapWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>mapHeight-bottomBorderHeight)
			mouseY = mapHeight-bottomBorderHeight;
		x2=mouseX;
		y2=mouseY;
		setClip();
		return false;
	} else if (panning) {
		x2=mouseX;
		y2=mouseY;
		panMouse();	
		return false;
	} else 
    	return true;
	return true;
}

function hideZoomBox() {
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
}

// perform appropriate action with mapTool
function mapTool (e) {

	getImageXY(e);

	  // Deal with the possibility of an
	  // "identify" first, since it is a
	  // little different than the other
	  // states (doesn't require a mouse
	  // up event).  

	if ((state == "identify") && 
	    (mouseX >= 0) && (mouseX <= mapWidth) && 
	    (mouseY >= 0) && (mouseY <= mapHeight)){
			imgX1 = mouseX;
			imgY1 = mouseY;
			getMapXY(mouseX,mouseY);
			x1=imgX1;
			y1=imgY1
			x2=x1+1;
			y2=y1+1;
			refreshMap();
			return false; // <-- false makes the popup
			              //     box stays in front.
	}
	if ((state == "birdseye") && 
	    (mouseX >= 0) && (mouseX <= mapWidth) && 
	    (mouseY >= 0) && (mouseY <= mapHeight)){
			imgX1 = mouseX;
			imgY1 = mouseY;
			getMapXY(mouseX,mouseY);
			x1=imgX1;
			y1=imgY1
			x2=x1+1;
			y2=y1+1;
			var url='/birdseye/default.aspx?x='+mapX + '&y='+mapY;
			//window.open(url,"BirdsEyeWindow","height=575,width=612");
			loading_graphic();
			//alert(url);
			window.location.href=url;
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	  // If we made it to here, it's a 
	  // navigational click and not an
	  // identify.
	
	if ((!zooming) && (!panning) && 
		(mouseX >= 0) && (mouseX <= mapWidth) && 
		(mouseY >= 0) && (mouseY <= mapHeight)) {
		imgX1 = mouseX;
		imgY1 = mouseY;
		if (state == "pan")
			startPan(e);
		else 
			startZoomBox(e);
			
		return false;
	} else if (zooming) {
		getMouse(e);
		stopZoomBox(e);
	} else if (panning) {
		getMouse(e);
		stopPan(e);
	}
	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 = mapWidth;
	var cBottom = mapHeight;
	if (xMove>0) {
		cLeft = 0;
		cRight = mapWidth - xMove;
	}
	if (yMove>0) {
		cTop = 0;
		cBottom = mapHeight - yMove;
	}
	/*some code added by P.Nowlan to stop clipping error.  This error may only*/
	/*occur during debugging.*/
	//window.status=('xMove:' + xMove + ', yMove:' + yMove + ', cLeft:' + cLeft + ', cRight:' + cRight + ', cTop:' + cTop + ', cBottom:' + cBottom);  //*testing*
	if (cTop > -1 && cBottom > -1 && cLeft > -1 && cRight > -1){
		clipLayer_Test("mainmap",cLeft,cTop,cRight,cBottom);  //clipLayer_Test is a hack to get panning to work with ie and nav5up
	}
	moveLayer("mainmap",xMove+hspc,yMove+vspc);

	return false;
}

// 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;
		newMinx = mapX - widthHalf;
		newMaxx = mapX + widthHalf;
		newMaxy = mapY + heightHalf;
		newMiny = mapY - heightHalf;

		refreshMap();
	} else
		customMapClick(mapX,mapY);
}

function refreshMap() {
  hideZoomBox();
  document.Form1.MouseDownX.value=x1;
  document.Form1.MouseUpX.value=x2;
  document.Form1.MouseDownY.value=y1;
  document.Form1.MouseUpY.value=y2;
  document.Form1.mapcmd.value=state;
  loading_graphic();  //call the "loading" function
  document.Form1.submit();
}

function sendIdentify(numMapX,numMapY) {

var objWin;
var sURL;

  sURL = sIDProcessor+"?"+
		 "X="+numMapX.toString()+"&"+
		 "Y="+numMapY.toString();
		 
  objWin = window.open(sURL, "Identify", "height=480,width=240,scrollbars=yes,resizeable=yes");
  if (parseInt(navigator.appVersion) >=4) objWin.window.focus();
  
}

// 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);
	}
}

function setExtent(_minx,_miny,_maxx,_maxy) {
	minx = _minx;
	miny = _miny;
	maxx = _maxx;
	maxy = _maxy;
}

function setState(newState) {
	state = newState;
    if (state == "pan") {
		if (document.getElementById){   // DOM3 = IE5, NS6
				document.getElementById('theTop').style.cursor = "move";
				document.getElementById('mainmap').style.cursor = "move";
		}else if (document.layers){   // Netscape 4
				document.theTop.style.cursor = "move";
				document.mainmap.style.cursor = "move";
		}else {  // IE 4
				document.all.theTop.style.cursor = "move";
				document.all.mainmap.style.cursor = "move";
		}}
	else {
			if (document.getElementById){   // DOM3 = IE5, NS6
				document.getElementById('theTop').style.cursor = "crosshair";
				document.getElementById('mainmap').style.cursor = "crosshair";
		}else 	if (document.layers) {  // Netscape 4
				document.theTop.style.cursor = "crosshair";
				document.mainmap.style.cursor = "crosshair";
		}else  { // IE 4
				document.all.theTop.style.cursor = "crosshair";
				document.all.mainmap.style.cursor = "crosshair";
		}}

	if (state == "zoomIn") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	} else if (state == "zoomOut") {
		mapBoxAsZoom = true;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
	} else if (state == "pan") {
	}
}

function setStateOnLoad() {
	if (document.getElementById('radioID').checked)
		setState('identify');
	else if (document.getElementById('radioBirdsEye').checked)
		setState('birdseye');
	else if (document.getElementById('radioZI').checked)
		setState('zoomIn');
	else if (document.getElementById('radioZO').checked)
		setState('zoomOut');
	else if (document.getElementById('radioRC').checked)
		setState('pan');
}

function setZoomBoxColor(color) {
	setLayerBackgroundColor("zoomBoxTop", color);
	setLayerBackgroundColor("zoomBoxLeft", color);
	setLayerBackgroundColor("zoomBoxRight", color);
	setLayerBackgroundColor("zoomBoxBottom", color);
}

function setZoomBoxSettings() {

	// Set up event capture for mouse movement
	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;
	}
}

function setZoomBoxWidth(size) {
	zoomBoxWidth = size;
}

function showZoomBox() {
	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
}

// start pan.... image will move
function startPan(e) {

	moveLayer("mainmap",hspc,vspc);

	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<mapWidth) && (mouseY<mapHeight)) {
		if (panning) {
			stopPan(e);
		} else {
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			panning=true;
		}
	}
	return false;

}

// start zoom in.... box displayed
function startZoomBox(e) {

	getImageXY(e);	

	if (!allowRubberband) {
		stopZoomBox(e);
	} else {	
		// keep it within the MapImage
		if ((mouseX<mapWidth) && (mouseY<mapHeight-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 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 / mapWidth;
		var theY = mapHeight - zmaxy;
		pixelY = height / mapHeight;
		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;
		
		refreshMap();
	}
		
	return true;
	
}

// 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 / mapWidth;
		var theY = mapHeight - zmaxy;
		var pixelY = height / mapHeight;
		newMaxy = pixelY * theY + miny;
		newMaxx = pixelX * zmaxx + minx;
		newMinx = pixelX * zminx + minx;
		theY = mapHeight - zminy;
		pixelY = height / mapHeight;
		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;
			}
			refreshMap();
		} else
			customMapBox(newMinx, newMiny, newMaxx, newMaxy);
			
	}
	return true;
}

//-------- LAYER SUPPORT FUNCTIONS --------------

// atlas_common.js

// clip layer display to clipleft, cliptip, clipright, clipbottom
	// Not working with Mozilla Milestone 12 (Nav5)
function clipLayer2(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);		
	  if (isNav4) {
		    layer.clip.left   = clipleft;
		    layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
		    layer.clip.bottom = clipbottom;
	  }
	  //if (document.all)
	  else if (isIE) {
		    layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	  }
}


// clip layer display to clipleft, cliptip, clipright, clipbottom
// original active.js clipLayer function
// this one let's zoom work in nav and ie.  recenter in okay in ie, fails in nav.
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);		
	if (layer != null) {
		//alert('layer is not null' + name);
  		if (isNav4) {
			layer.clip.left   = clipleft;
			layer.clip.top    = cliptop;
			layer.clip.right  = clipright;
			layer.clip.bottom = clipbottom;
		} else if (isIE) {
			layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	    } else {
			//alert('layer is null' + name);
     			layer.height = clipbottom - cliptop;
			layer.width	= clipright - clipleft;
			layer.top	= (cliptop+vspc) + "px";
			layer.left	= (clipleft+hspc) + "px";
		}
	}
}

// clipLayer function from aimsDHTML version 4.0.1
// modified by p.nowlan
// this one recenters in ie and nav.  zoom in neither
function clipLayer_Test(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);
	  if (isNav4) {
		    layer.clip.left   = clipleft;
		    layer.clip.top    = cliptop;
		    layer.clip.right  = clipright;
		    layer.clip.bottom = clipbottom;
	  }	  else {
		    //esri commented//layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
			var newWidth = clipright - clipleft;
			var newHeight = clipbottom - cliptop;
			layer.height = newHeight;
			layer.width	= newWidth;
			//esri commented//var theTop = parseInt((parseFloat(cliptop) * 10 + 0.5)/10);
			//esri commented//var theLeft = parseInt((parseFloat(clipleft) * 10 + 0.5)/10);
			//esri commented//alert(cliptop + " " + clipleft);
			var newtop=cliptop+vspc;
			var newleft=clipleft+hspc;
			layer.top = newtop  + "px";
			layer.left = newleft + "px";
			//esri commented//layer.display= "none";
			// added below by p.nowlan (uncommented and repositioned really)
			//this makes recenter work but zoom doesn't work with this
			layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
		}
}


// Create a DHTML layer
function createLayer(name, left, top, width, height, visible, content) {
	  var layer;
	  if (isNav4) {
	    document.writeln('<layer name="' + name + '" left=' + left + ' top=' + top + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
	    document.writeln(content);
	    document.writeln('</layer>');
	    layer = getLayer(name);
	    layer.width = width;
	    layer.height = height;
	  } else {
	    document.writeln('<div id="' + name + '" style="position:absolute; overflow:none; left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px;' + ' visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
	    document.writeln(content);
	    document.writeln('</div>');
	  }
  	  clipLayer(name, 0, 0, width, height);
}

// get the layer object called "name"
// active.js original script
//function getLayer(name) {
//	  if (isNav4)
//	    return(document.layers[name]);
//	  else if (isIE4) {
//	  	if ( eval('document.all.' + name) != null) {
//		    layer = eval('document.all.' + name + '.style');
//		    return(layer);
//		} else
//			return(null);
//	  } else if (is5up) {
//		var theObj = document.getElementById(name);
//		return theObj.style
//	  } else
//	    return(null);
//}


// get the layer object called "name"
// copied from aimsDHTML v4.01
function getLayer(name) {
	  if (isNav4)
	    return(document.layers[name]);
	  else if (isIE4) {
	    layer = eval('document.all.' + name + '.style');
	    return(layer);
	  } else if (is5up) {
		var theObj = document.getElementById(name);
		return theObj.style
	  }
	  else
	    return(null);
}

// get a DHTML element/layer
function getObj(name)
{
  if (document.getElementById)
  {
	//alert('here');
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

// toggle layer to invisible
function hideLayer(name) {		
  	var layer = getLayer(name);		
	if (layer != null) {
	 	if (isNav4)
    		layer.visibility = "hide";
		else
   			 layer.visibility = "hidden";
	}
}

// move layer to x,y
function moveLayer(name, x, y) {		
  	var layer = getLayer(name);		
	if (layer != null) {
	  	if (isNav4)
    		layer.moveTo(x, y);
	 	else if (isIE) {
    		layer.left = x + "px";
   			layer.top  = y + "px";
		} else {
			layer.height = mapHeight - y;
			layer.width	= mapWidth - x;
    		layer.left = x + "px";
   			layer.top  = y + "px";
	  	}
	}
}

// replace layer's content with new content
function replaceLayerContent(name, content) {
	  if (isNav4) {
		    var layer = getLayer(name);
			if (layer != null) {
			    layer.document.open();
			    layer.document.writeln(content);
			    layer.document.close();
			}
	  }  else if (isIE) {
			if (eval("document.all." + name) != null) {
		  		content = content.replace(/\'/g,"\\'");
			    var str = "document.all." + name + ".innerHTML = '" + content + "'";
			    eval(str);
			}
	  }
}

// set layer background color
function setLayerBackgroundColor(name, color) {		
  	var layer = getLayer(name);		
	if (layer != null) {
	    if (isNav4) 
    		layer.bgColor = color;
		else 
    		layer.backgroundColor = color;
	}
}

// toggle layer to visible
function showLayer(name) {		
  	var layer = getLayer(name);		
	if (layer != null) {
	  	if (isNav4)
    		layer.visibility = "show";
		else
   		 	layer.visibility = "visible";
	}
}
