
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 typeOfZoom = "zoomInClick"

var circleDrag = false;

var mapClickAsRecenter = true;
var mapBoxAsZoom = true;
var allowRubberband = true;
var allowMapClick = true;
var state = "zoomIn";  // zoomOut, pan

// 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 rectangleselection=false; //-----------------------------------------------------------------
var polygon=false;
var bottomBorderHeight = 13;

// setup test for Nav 4.0
var isIE = false;
var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isNav4 = false;
var isIE4 = false;
var is5up = false;
var isMac = false;
var isWin = false;

//Added by David
//Used in getMapNewExtent(xIn,yIn)
var XToBeTurnedIntoExtent = 0;
var YToBeTurnedIntoExtent = 0;
var TemporaryMapX = 0;
var TemporaryMapY = 0;
//End of additions

if (isNav) {
	
	if (parseFloat(navigator.appVersion)<5) {
		isNav4=true;
		//alert("Netscape 4.x or older");
	} else {
		is5up = true;
	}
} else {
	isIE4=true;
	isIE=true;
	if (navigator.appVersion.indexOf("MSIE 5")>0) {
		isIE4 = false;
		is5up = true;
		//alert("IE5");
	}
}	

if (navigator.userAgent.indexOf("Win") >= 0) 
	isWin = true;
else
	isMac = true;

//***********************************************	
//***************** FUNCTIONS *******************
//***********************************************

// check for mouseup
function chkMouseUp(e) { 
	if (zooming || panning || rectangleselection) { //---------------------------------------------------------
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>iWidth)
			mouseX = iWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>iHeight)
			mouseY = iHeight;
		mapTool(e);
	}
}

function customMapBox(_newMinx, _newMiny, _newMaxx, _newMaxy) {
refreshMap()
}

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;
}

//Not in use//

//Exactly the same as the next function getMaxXY(xIn,yIn)
function getMapNewExtent(xIn, yIn, tempminx, tempminy, tempmaxx, tempmaxy) 
{
//Added by David - Variables
//XToBeTurnedIntoExtent, YToBeTurnedIntoExtent
//NewExtentPixelX, NewExtentPixelY
//TemporaryMapX,TemporaryMapY
	XToBeTurnedIntoExtent = xIn;
	var NewExtentPixelX = (tempmaxx - tempminx) / iWidth;
	TemporaryMapX = NewExtentPixelX * XToBeTurnedIntoExtent + tempminx;
	YToBeTurnedIntoExtent = iHeight - yIn;
	var NewExtentPixelY = (tempmaxy - tempminy) / iHeight;
	TemporaryMapY = NewExtentPixelY * YToBeTurnedIntoExtent + tempminy;
}
// 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);
	displayCurrentCoords(mouseX,mouseY);
	if ((mouseY >= iHeight-26) || (disableMouseForLayers)){
	}
	else{ 
	if (zooming || rectangleselection) {
		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 (state == "circle" && circleDrag)
	{
	dragCircle(mouseX, mouseY);
	}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);
	
  if ((parent.navigation.variablestorage.overviewmapshown.value == "true") && (mouseX >= 0) && (mouseX <= iWidth) && (mouseY >= 0) && (mouseY <= iHeight - 26) && (mouseY >= iHeight - 127) && (mouseX >= iWidth - 172) && (disableMouseForLayers == false))
  {
	pan2MapX = (mouseX - (iWidth - 172));
	pan2MapY = (mouseY - (iHeight - 126));
	//alert(pan2MapX + "," + pan2MapY);
	panRecenter(pan2MapX,pan2MapY);
	//recenter2();
  }
	else
  {
  
	
	if (state == "polygon"){
	addPoint(mouseX, mouseY);
	}

	
	if (state == "measure"){
	
	//alert (mouseX);
	addPointForMeasure(mouseX, mouseY);
	}
	
	if (state == "circle"){
	
	centreCircle(mouseX, mouseY);
	}
	
	  // 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 <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight - 26) && (disableMouseForLayers == false)){
			getMapXY(mouseX,mouseY);
			sendIDENTIFY(mapX,mapY);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	
	if ((state == "pinp") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight - 26) && (disableMouseForLayers == false)){
			getMapXY(mouseX,mouseY);
			sendPINP(mapX,mapY);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	
	if ((state == "pointdrilldown") && 
	    (mouseX >= 0) && (mouseX <= iWidth) && 
	    (mouseY >= 0) && (mouseY <= iHeight - 26) && (disableMouseForLayers == false)){
			getMapXY(mouseX,mouseY);
			sendPOINTDRILLDOWN(mapX,mapY);
			return false; // <-- false assures that the popup
			              //     box stays in front.
	}
	if ((state == "measurearea"))
		{
			//alert(state);
			addPointForAreaMeasure(mouseX, mouseY);
		}
	if ((state == "redlining"))
		{
			
			if (parent.support.redlining.tool_type.value == 'Lines')
				{
					addPointForRedlining(mouseX, mouseY);
				}
			else
				{
					var tool = parent.support.redlining.tool_type.value;
					
					getMapXY(mouseX,mouseY);
					sendREDLINING(tool,mapX,mapY);
					return false; 
				}
		}
			
	if ((!zooming) && (!panning) && (!rectangleselection) &&
		(mouseX >= 0) && (mouseX <= iWidth) && 
		(mouseY >= 0) && (mouseY <= iHeight - 26) && (disableMouseForLayers == false)) {
		if (state == "pan")
			startPan(e);
		else if (state == "polygon")
			var tree = 0;
		else if (state == "measure")
			var tree = 0;
		else if (state == "measurearea")
			var tree = 0;
		else if (state == "circle")
			var tree = 0;
		else if (state == "redlining")
			var tree = 0;
		else 
			startZoomBox(e);	
		return false;
	} else if (zooming) {
		getMouse(e);
		stopZoomBox(e);
	} else if (rectangleselection) {
		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 = 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;
}

// recenter map is the default option
function recenter() {

	// 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 recenterZoom(zoomType) {

	// 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;

		refreshMapZoom(zoomType); 
	} else
		customMapClick(mapX,mapY);
		
}

function panRecenter(x,y) {
	
	mapClickAsRecenter = true;
	// otherwise we don't have these layers
	if (allowRubberband)
	hideZoomBox();
	
//	oldmaxx = maxx;
//	oldmaxy = maxy;
//	oldminx = minx;
//	oldminy = miny;
	var ovmax,ovminx,ovmaxy,ovminy;
	ovmaxx=parseFloat(document.Variables.OverviewInitialExtentXMax.value);
	ovminx=parseFloat(document.Variables.OverviewInitialExtentXMin.value);
	ovmaxy=parseFloat(document.Variables.OverviewInitialExtentYMax.value);
	ovminy=parseFloat(document.Variables.OverviewInitialExtentYMin.value);

	mouseX = x;
	var pixelX = (ovmaxx-ovminx) / 170;
	mapX = pixelX * mouseX + ovminx;
	mouseY = y;
	var pixelY = (ovmaxy-ovminy) / 100;
	mapY = ovmaxy - pixelY * mouseY;
	
		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;

		refreshMapThree(); 

		
}


function recenter2(e) {

	
	getMapXY(mouseX,mouseY);
	//newXVal = ((172 - (iWidth - 172) - mouseX)*iWidth;
	//newYVal = ((126 - (iHeight - 126) - mouseY)*iHeight;
	newXVal = (mouseX-(iWidth-172))/172
	newYVal	= (mouseY-(iHeight-126))/100
		
	fullxDiff=((document.Variables.maxX.value - document.Variables.minX.value)/2) + document.Variables.minX.value
	fullyDiff=((document.Variables.maxY.value - document.Variables.minY.value)/2) + document.Variables.minY.value
	
	oxDiff = ((document.Variables.MapExtentMaxX.value - document.Variables.MapExtentMinX.value)/2) + document.Variables.MapExtentMinX.value
	oyDiff = ((document.Variables.MapExtentMaxY.value - document.Variables.MapExtentMinY.value)/2) + document.Variables.MapExtentMinY.value

	 changeInExtentX = (parseInt(fullxDiff) - parseInt(oxDiff))
	 changeInExtentY = (parseInt(fullyDiff) - parseInt(oyDiff))
	 
	minx = parseFloat(document.Variables.MapExtentMinX.value);
    miny = parseFloat(document.Variables.MapExtentMinY.value);
    maxx = parseFloat(document.Variables.MapExtentMaxX.value);
    maxy = parseFloat(document.Variables.MapExtentMaxY.value);
	 
	newMinx = minx + changeInExtentX
	newMaxx = maxx + changeInExtentX
	newMaxy = maxy + changeInExtentY
	newMiny = miny + changeInExtentY
	
	//alert(parseInt(oxDiff))
	//alert(parseInt(oyDiff))
	
	//alert(parseInt(fullxDiff))
	//alert(parseInt(fullyDiff))
	//alert(changeInExtentX)
	//alert(changeInExtentY)
	
	//alert(minx)
	//alert(maxx)
	//alert(maxy)
	//alert(miny)
	
	//alert(newMinx)
	//alert(newMaxx)
	//alert(newMaxy)
	//alert(newMiny)
	alert("The pan tool for the overview map has been disabled")
	//refreshMap(); 
		
		
}


//------------------------------------------------------------
function refreshMap() {
  hideZoomBox();
  showLayer("loadLayer");
  document.location = sMapProcessor+"?Cmd="+state+
                      "&MinX="+newMinx.toString()+
                      "&MinY="+newMiny.toString()+
                      "&MaxX="+newMaxx.toString()+
                      "&MaxY="+newMaxy.toString()+
					// Added by David
					  "&WidthOfMap="+iWidth+
					  "&HeightOfMap="+iHeight
					//End of Additions
  //NOTE: calling hideLayer("loadLayer") causes
  //      the "Loading" image to disappear 
  //      immediately.
//------------------------------------------------------------
}

function refreshMapThree() {
  hideZoomBox();
  showLayer("loadLayer");
  document.location = sMapProcessor+"?Cmd="+state+
					  "&Command="+state+
                      "&MinX="+newMinx.toString()+
                      "&MinY="+newMiny.toString()+
                      "&MaxX="+newMaxx.toString()+
                      "&MaxY="+newMaxy.toString()+
					// Added by David
					  "&WidthOfMap="+iWidth+
					  "&HeightOfMap="+iHeight
					//End of Additions
  //NOTE: calling hideLayer("loadLayer") causes
  //      the "Loading" image to disappear 
  //      immediately.
//------------------------------------------------------------
}

function refreshMapZoom(zoomType) {
  hideZoomBox();
  showLayer("loadLayer");
  document.location = sMapProcessor+"?Cmd="+zoomType+
                      "&MinX="+newMinx.toString()+
                      "&MinY="+newMiny.toString()+
                      "&MaxX="+newMaxx.toString()+
                      "&MaxY="+newMaxy.toString()+
					// Added by David
					  "&WidthOfMap="+iWidth+
					  "&HeightOfMap="+iHeight
					//End of Additions
  //NOTE: calling hideLayer("loadLayer") causes
  //      the "Loading" image to disappear 
  //      immediately.
//------------------------------------------------------------
}

function sendPOINTDRILLDOWN(numMapX,numMapY) 
	{
		showLayer("loadLayer");
	
		var objWin;
		var sURL;
		
	    iWidth = document.Variables.MapWidth.value;
	    iHeight = document.Variables.MapHeight.value;
	      
	    minx = parseFloat(document.Variables.MapExtentMinX.value);
	    miny = parseFloat(document.Variables.MapExtentMinY.value);
	    maxx = parseFloat(document.Variables.MapExtentMaxX.value);
	    maxy = parseFloat(document.Variables.MapExtentMaxY.value);

		sURL = "map.asp?Cmd=CIRCLE&Command=POINTDRILLDOWN&X="+ numMapX.toString() +"&"+"Y="+numMapY.toString() + "&WidthOfMap="+ iWidth +"&HeightOfMap="+ iHeight +"&MinX="+ minx +"&MaxX="+ maxx +"&MinY="+ miny +"&MaxY="+ maxy;	parent.map.location.replace(sURL);
		parent.map.location.replace(sURL);
		
	}

function sendIDENTIFY(numMapX,numMapY) 
	{
		showLayer("loadLayer");
	
		var objWin;
		var sURL;

	    iWidth = document.Variables.MapWidth.value;
	    iHeight = document.Variables.MapHeight.value;
	      
	    minx = parseFloat(document.Variables.MapExtentMinX.value);
	    miny = parseFloat(document.Variables.MapExtentMinY.value);
	    maxx = parseFloat(document.Variables.MapExtentMaxX.value);
	    maxy = parseFloat(document.Variables.MapExtentMaxY.value);

		sURL = "map.asp?Cmd=CIRCLE&Command=IDENTIFY&X="+ numMapX.toString() +"&"+"Y="+numMapY.toString() + "&WidthOfMap="+ iWidth +"&HeightOfMap="+ iHeight +"&MinX="+ minx +"&MaxX="+ maxx +"&MinY="+ miny +"&MaxY="+ maxy;	parent.map.location.replace(sURL);
		parent.map.location.replace(sURL);
		
		
	}

function sendPINP(numMapX,numMapY) 
	{
		showLayer("loadLayer");
	
		var objWin;
		var sURL;

	    iWidth = document.Variables.MapWidth.value;
	    iHeight = document.Variables.MapHeight.value;
	      
	    minx = parseFloat(document.Variables.MapExtentMinX.value);
	    miny = parseFloat(document.Variables.MapExtentMinY.value);
	    maxx = parseFloat(document.Variables.MapExtentMaxX.value);
	    maxy = parseFloat(document.Variables.MapExtentMaxY.value);

		sURL = "map.asp?Cmd=CIRCLE&Command=PINP&X="+ numMapX.toString() +"&"+"Y="+numMapY.toString() + "&WidthOfMap="+ iWidth +"&HeightOfMap="+ iHeight +"&MinX="+ minx +"&MaxX="+ maxx +"&MinY="+ miny +"&MaxY="+ maxy;	parent.map.location.replace(sURL);
		parent.map.location.replace(sURL);
		
		
	}


function sendREDLINING(tool,numMapX,numMapY) 
	{
		showLayer("loadLayer");
		
		var objWin;
		var sURL;
		var toolInfo;
		var toolType;
		iWidth = document.Variables.MapWidth.value;
	    iHeight = document.Variables.MapHeight.value;
	      
	    minx = parseFloat(document.Variables.MapExtentMinX.value);
	    miny = parseFloat(document.Variables.MapExtentMinY.value);
	    maxx = parseFloat(document.Variables.MapExtentMaxX.value);
	    maxy = parseFloat(document.Variables.MapExtentMaxY.value);
		
		//alert (parent.support.redlining.tool_type.value);
		
		if (tool == 'Points')
			{
				//alert ('Redlining - Points');
				
				toolType = 'Point';
				
				var pointStyle;
				
				if (parent.support.form_point.square.checked == true)
					{
						pointStyle = 'square';
					}
				else if (parent.support.form_point.circle.checked == true)
					{
						pointStyle = 'circle';
					}
				else if (parent.support.form_point.triangle.checked == true)
					{
						pointStyle = 'triangle';
					}
				else if (parent.support.form_point.cross.checked == true)
					{
						pointStyle = 'cross';
					}
				else if (parent.support.form_point.star.checked == true)
					{
						pointStyle = 'star';
					}
				else
					{
						pointStyle = '';
					}
				
				if (pointStyle == '')
					{
						toolInfo = '';
					}
				else
					{
						toolInfo = parent.support.form_point.pointColour.value +','+ parent.support.form_point.pointSize.value + ',' + pointStyle;
					}
			}
		else if (tool == 'Lines')
			{
				//alert ('Redlining - Lines');
				
				toolType = 'Line';
				
				var lineStyle;
				
				lineStyle = -1;
				
				if (parent.support.form_line.solid.checked == true)
					{
						lineStyle = 0;
					}
				else if (parent.support.form_line.dash.checked == true)
					{
						lineStyle = 1;
					}
				else if (parent.support.form_line.dot.checked == true)
					{
						lineStyle = 2;
					}
				else if (parent.support.form_line.dash_dot.checked == true)
					{
						lineStyle = 3;
					}
				else if (parent.support.form_line.dash_dot_dot.checked == true)
					{
						lineStyle = 4;
					}
				else if (parent.support.form_line.arrow.checked == true)
					{
						lineStyle = 5;
					}
				else
					{
						lineStyle = -1;
					}
				
				var lineJoin;
				
				if (parent.support.form_line.Continue.checked == true)
					{
						lineJoin = 'Yes';
					}
				else if (parent.support.form_line.Close.checked == true)
					{
						lineJoin = 'Close';
					}
				else if (parent.support.form_line.End.checked == true)
					{
						lineJoin = 'End';
					}
				else
					{
						lineJoin = 'Yes';
					}
					
				if (lineStyle == -1)
					{
						toolInfo = '';
					}
				else
					{
						toolInfo = parent.support.form_line.lineColour.value + ',' + parent.support.form_line.lineWidth.value + ',' + lineStyle + ',' + lineJoin + ',' + parent.document.redliningCoordinates.coord.value;
					}
				
				//alert (toolInfo);
				
			}
		else if (tool == 'Text')
			{
				//alert ('Redlining - Text');
				
				toolType = 'Text';
				
				if (parent.support.form_text.textInfo.value == '')
					{
						toolInfo = '';
					}
				else
					{						
						toolInfo = parent.support.form_text.textColour.value +','+ parent.support.form_text.textBackground.value +','+ parent.support.form_text.textType.value +','+  parent.support.form_text.textSize.value +','+ parent.support.form_text.textStyle.value +','+  parent.support.form_text.textInfo.value;
					}
			}
		else
			{
				//alert ('Redlining - Failed');
				toolType = '';
				toolInfo = '';
			}
		
		//alert (toolInfo);
		
		sURL = "map.asp?Cmd=REDLINING&Command=REDLINING&X="+ numMapX.toString() +"&"+"Y="+numMapY.toString() + "&WidthOfMap="+ iWidth +"&HeightOfMap="+ iHeight +"&MinX="+ minx +"&MaxX="+ maxx +"&MinY="+ miny +"&MaxY="+ maxy +"&toolInfo="+ toolInfo +"&toolType="+ toolType;
		
		//sURL = "../redlining/redlining_function.asp?Cmd=REDLINING&Command=REDLINING&X="+ numMapX.toString() +"&"+"Y="+numMapY.toString() + "&WidthOfMap="+ iWidth +"&HeightOfMap="+ iHeight +"&MinX="+ minx +"&MaxX="+ maxx +"&MinY="+ miny +"&MaxY="+ maxy +"&toolInfo="+ toolInfo +"&toolType="+ toolType;
				
		if (toolInfo != '')
			{
				parent.map.location.replace(sURL);
				//parent.support.location.replace(sURL);
			}
		else
			{
				alert('Please make sure you have selected / entered all the information in for the ' + toolType + ' tool.');
			}
		
	}
	


// 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;
}

//Added by David
function setMapSize(iWidth, iHeight)
{	
	widthofmap = iWidth;
	heightofmap = iHeight;
}	
//End of Additions

function setState(newState) {
	hideLayer("areameasureWin");
	hideLayer("measureWin");
	hideLayer("redliningWin");
	state = newState;
	//alert(state);
    if (isIE && document.all.theTop) 
		{
	    if (state == "pan")
  		    document.all.theTop.style.cursor = "move";
		else
  		    document.all.theTop.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 == "rectangleselection") 
		{
		mapBoxAsZoom = false;
		setZoomBoxColor("#ff0000");
		setZoomBoxWidth(1);
		}
	else if (state == "polygon")
		{
		}
	else if (state == "measure")
		{
		}
	else if (state == "circle")
		{
		}
	else if (state == "pan")
		{
		}
	else if (state == "redlining")
		{
		}
	else if (state == "measurearea")
		{
		}
}

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("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;
}

// start zoom in.... box displayed
function startZoomBox(e) {

	getImageXY(e);	

	if (!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 || rectangleselection) {
				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 / 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;
		
		refreshMap();
	}
		
	return true;
	
}

// stop zoom box display... zoom in
function stopZoomBox(e) {
	var testspot;
	testspot = state;
	zooming=false;
	rectangleselection=false;
	if ((zmaxx < zminx + 2) && (zmaxy < zminy + 2)) {
		// if the zoom box is too small
		if ((testspot == "zoomIn") || (testspot == "zoomOut"))
		recenterZoom(testspot + "Click");
		else if (testspot == "pan")
		recenter(e);
		
		//else if (testspot == "zoomOut"){
		//	typeOfZoom = "zoomInClick"
		//	recenterThenZoom(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;
			}
			refreshMap();
		} else			
			customMapBox(newMinx, newMiny, newMaxx, newMaxy);
			
	}
	return true;
}

function testActiveLayer()
{


}

//-------- LAYER SUPPORT FUNCTIONS --------------

// atlas_common.js

// clip layer display to clipleft, cliptip, clipright, clipbottom
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);		
	if (layer != null) {
  		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 {
     		layer.height = clipbottom - cliptop;
			layer.width	= clipright - clipleft;
			layer.top	= (cliptop+vspc) + "px";
			layer.left	= (clipleft+hspc) + "px";
		}
	}
}

// Create a DHTML layer
function createLayer() {
  // parse arguments
  var args = createLayer.arguments;
  var name = args[0];
  var left = args[1];
  var top = args[2];
  var width = args[3];
  var height = args[4];
  var visible = args[5];
  var content = args[6];
  var zindex = (args[7]!=null) ? args[7] : null;
  
  // other variables
  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;' + ((zindex==null) ? '' : (' z-index:' + zindex + ';')) + ' left:' + left + '; top:' + top + '; width:' + width + '; height:' + height + ';' + ' visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
    document.writeln(content);
    document.writeln('</div>');
  }
  clipLayer(name, 0, 0, width, height);	  
}

// get the layer object called "name"
function getLayer(name) {
	  if (isNav4)
	    return(document.layers[name]);
	  else if (isIE4 || is5up) {
	  	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).style;
		//return theObj;
	  } else
	    return(null);
}

// 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 = iHeight - y;
			layer.width	= iWidth - 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";
	}
}
