/*
***************************************************************************************

Click/Mouse functions - used to catch mouse button events
	Extended versions of these functions are used by the ArcIMS HTML Viewer - found in aimsClick.js

***************************************************************************************
*/
function startUp() 
{
	if (isNav) 
	{
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
	}
	document.onmousedown = mapTool;
	document.onmouseup = chkMouseUp;
	document.onmousemove = getMouse;
}

/*
***************************************************************************************

DHTML layer functions - used to create and manipulate style sheets
These functions are used by the ArcIMS HTML Viewer - found in aimsDHTML.js

***************************************************************************************
*/

// Create a DHTML layer
function createLayer(name, inleft, intop, width, height, visible, content) 
{
	var layer;
	if (isNav4) 
	{
		document.writeln('<layer name="' + name + '" left=' + hspc + ' top=' + vspc + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
		document.writeln(content);
		document.writeln('</layer>');
	} 
	else 
	{
		document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;' + '; z-index:1; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
		document.writeln(content);
		document.writeln('</div>');
	}
	
	
	
}

// get the layer object called "name"
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);
	}
}

function isVisible(name) 
{
	var layer = getLayer(name);
	if (isNav && layer.visibility == "show")
	{
		return(true);
	}
	if (isIE && layer.visibility == "visible")
	{
		return(true);
		return(false);
	}
}


// move layer to x,y
function moveLayer(name, x, y) 
{		
	var layer = getLayer(name);		
	if (isNav4)
	{
		layer.moveTo(x, y);
	}
	else 
	{
		layer.left = x + "px";
		layer.top  = y + "px";
	}
}

// set layer background color
function setLayerBackgroundColor(name, color) 
{		
	var layer = getLayer(name);		
	if (isNav4)
	{
		layer.bgColor = color;
	}
	else
	{
		layer.backgroundColor = color;
	}
}

// toggle layer to invisible
function hideLayer(name) 
{		
	var layer = getLayer(name);		
	if (isNav4)
	{
		layer.visibility = "hide";
	}
	else
	{
		layer.visibility = "hidden";
	}
}

// toggle layer to visible
function showLayer(name) 
{		
	var layer = getLayer(name);		
	if (isNav4)
	{
	layer.visibility = "show";
	}
	else
	{
	layer.visibility = "visible";
	}
}

// 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;
	}
	else if (isIE) 
	{
		layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	}
}

function clipLayer(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
	{
		var newWidth = clipright - clipleft;
		var newHeight = clipbottom - cliptop;
		layer.height = newHeight;
		layer.width	= newWidth;
		layer.top	= cliptop + vspc + "px";
		layer.left	= clipleft + hspc + "px"; 
	}
}

function boxIt(theLeft,theTop,theRight,theBottom) 
{
	clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+ovBoxSize);
	clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+ovBoxSize,theBottom);
	clipLayer("zoomBoxRight",theRight-ovBoxSize,theTop,theRight,theBottom);
	clipLayer("zoomBoxBottom",theLeft,theBottom-ovBoxSize,theRight,theBottom);	
	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
}


/*
***************************************************************************************

Navigation functions - used to resize zoom box
Extended versions of these functions are used by the ArcIMS HTML Viewer - found in aimsNavigation.js

***************************************************************************************
*/

// get cursor location
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;		
}	

// start zoom in.... box displayed
function startZoomBox(e) 
{
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	// keep it within the MapImage
	// this test to see if mouse is over the map
	// assign map tool value only once the mouse is over the map and on mouseDown			
	if ((mouseX<iWidth) && (mouseY<iHeight) && (mouseX >= 0) && (mouseY >= 0)) 
	{
	// initialy start with "map.tool.value=pointclick". and once the mouse has been dragged more than 
	// 3 pixels change the value of "map.tool.value=zoombox".
	document.map.tool.value = "pointclick";	
		if (zooming) 
		{
			stopZoomBox(e);
		} 
		else 
		{
			x1=mouseX;
			y1=mouseY
			x2=x1+1;
			y2=y1+1;
			zleft=x1;
			ztop=y1;
			zbottom=y1;
			zright=x1
			boxIt(x1,y1,x2,y2);							
			zooming=true;
		}
	}
	return false;
}

// stop zoom box display... 
function stopZoomBox(e) 
{
	zooming=false;
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	// nice thought but was causing problems
	//window.scrollTo(0,0);
	return true;
}

// get the coords at mouse position
function getMouse(e) 
{
	getImageXY(e);

	if (( mouseX >iWidth ) || (mouseY>iHeight) || (mouseX <=0) ||(mouseY<=0)) 
	{
		chkMouseUp(e);
	} 
	else 
	{
		if (zooming)
	{
	x2=mouseX;
	y2=mouseY;
	setClip();

	if (x1 > x2)
	{			
		xtemp = x2;
		xMax = x1;
		xMin = xtemp;
	} 
	else
	{
		xMax = x2;
		xMin = x1;					
	}

	if (y1 > y2)
	{			
		ytemp = y2;
		yMax = y1;
		yMin = ytemp;
	} 
	else
	{
		yMax = y2;
		yMin = y1;					
	}

	document.map.x1.value = xMin;
	document.map.x2.value = xMax;
	document.map.y1.value = yMin;
	document.map.y2.value = yMax;
	document.map['click.x'].value = xMin + ((xMax-xMin)/2);
	document.map['click.y'].value = yMin + ((yMax-yMin)/2)

	if (( xMax - xMin ) >= 3 && (yMax - yMin) >=3)
	{
		document.map.tool.value = "zoombox";		       
	}			
}
else
{      
	document.map['click.x'].value = mouseX;
	document.map['click.y'].value = mouseY;					
	document.map.x1.value = mouseX;
	document.map.y1.value = mouseY;
}


var mouseString = "";
if (zooming) mouseString += "ZoomBox: [" + x1 + "," + y1 + " x " + x2 + "," + y2 + "]    ";
mouseString += "Coords: " + mouseX + " , " + mouseY;	

}
// next line needed for Mac
return false;
}

// clip zoom box layer to mouse coords
function setClip() 
{	
	var tempX=x1;
	var tempY=y1;
	if (x1>x2) 
	{
		zright=x1;
		zleft=x2;
	} 
	else 
	{
		zleft=x1;
		zright=x2;
	}
	if (y1>y2) 
	{
		zbottom=y1;
		ztop=y2;
	} 
	else 
	{
		ztop=y1;
		zbottom=y2;
	}

	if ((x1 != x2) && (y1 != y2)) 
	{
		boxIt(zleft,ztop,zright,zbottom);
	}
}


// check for mouseup
function chkMouseUp(e) 
{ 
// we are only demo'ing the zoombox	. . . so only one thing to do
//if ((toolMode == 1) && (zooming)) {
	if (zooming == true) 
	{							
		zooming = false;
		stopZoomBox(e);
		document.map.submit();				
		// show "wait" cursor on submit
		theMap.style.cursor = "wait" ;				
	}
	//if ((toolMode == 3) && (panning)) {
	//	stopPan(e);		
	//}
	// next line needed for Mac	
	return false;
}

// perform appropriate action with mapTool
function mapTool (e) 
{
	// we are only demo'ing the zoombox. . . so only one thing to do
	startZoomBox(e);
	submitFlag = true;
	return false;
}