/*
***************************************************************************************

DHTML layer functions - used to create and manipulate style sheets
	These functions are used by the GAP data viewer

***************************************************************************************
*/

//These are the global variables that set parameters for the DHTML portions of the application.
var zoomBoxColor = "FF0000";	//Used in TEST BODY -- color of zoombox
var ovBoxSize = 2; 	//Used in SETCLIP -- Zoombox line width
var iHeight; //Used in TEST BODY and throughout -- image height in pixels
var iWidth;  //Used in TEST BODY and throughout -- image width in pixels
var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isNav4 = false;  //Used throughout -- these 5 variables keep track of what browser is running.
var isIE4 = false;
var isIE = false;
var is5up = false;
var hspc = 0; 		//Used in TEST BODY and throughout -- horizontal image offset
var vspc = 0; 		//Used in TEST BODY and throughout -- vertical image offset	
var newMaxy = 0;    //Used throughout -- A temp variable for storing the new map bounding coordinates.
var newMaxx = 0;
var newMiny = 0;
var newMiny = 0;
var maxy = 0;		//Used in TEST BODY and throughout -- Holds the current map bounding coordinates.
var maxx = 0;
var miny = 0;
var minx = 0;
var zmaxx = 0;   //Used throughout -- Temp variable to hold the zooming bounding coordinates.
var zmaxy = 0;
var zminx = 0;
var zminy = 0;
var pixelX = 0;  //Used throughout -- The pixel that was clicked on
var pixelY = 0;
var mapX = 0;   //Used throughout -- The map coordinate that was clicked on
var mapY = 0;
var mouseX=0;   //Used throughout -- Records the pixel position of the mouse
var mouseY=0;
var x1=0;   //Used throughout -- temporary transfer variables
var y1=0;
var x2=0;
var y2=0;
var zooming = false;  //Used throughout -- keep track of if the user is currently zooming or panning.
var panning = false;	
		/*	var zleft=0;       //These variables are not used in any program right now
		var zright=0;
		var ztop=0;
		var zbottom=0;  */

//This section tries to figure out which browser the user is using.
//alert(navigator.appVersion);
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");
	}
}	
	
// Used by test CONTROLS -- Sets tool tips at the bottom of the browser.
function showStatus( text){
	window.status = text;
	return true;
}

// Used in test LOADFUNCTION, BODY and REFRESHMAP -- Loads up the LoadMap gif and shows it.
function showRetrieveMap() {
	if (hasLayer("LoadMap")) {
		showLayer("LoadMap");
	}
}
		
// Used in the test BODY -- as far as I can tell, inserts HTML code into the map which creates a dynamic layer.
function createLayer(name, inleft, intop, width, height, visible, content) {
	  var layer;
	  if (isNav4) {
	    document.writeln('<layer name="' + name + '" left=' + inleft + ' top=' + intop + ' 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>');
	  }
}

// Used by SHOWRETRIEVEMAP -- Checks to see if a dynamic layer has been added to the map.		
function hasLayer(name) {
	var result = false;
	if (isNav4) {
		if (document.layers[name]!=null) result=true;
	}  else if (isIE) {
		if (eval('document.all.' + name)!=null) result=true;
	} else if (isNav) {
		var theElements = document.getElementsByTagName("DIV");
		var theObj;
		var j = -1;
		for (i=0;i<theElements.length;i++) {
			if (theElements[i].id==name) result=true;
		}
    }
	return result;
}
		
// Used in Test BODY -- Appears to set a background color for the dynamic layer.
function setLayerBackgroundColor(name, color) {		
  	var layer = getLayer(name);		
 	 if (isNav4)
    	layer.bgColor = color;
  	//else if (document.all)
	else
    	layer.backgroundColor = color;
}
		
// Used by SHOWRETRIEVEMAP, MAPTOOL, and SHOWZOOMBOX -- Just sets a dynamic layer to visible.	
function showLayer(name) {		
  	var layer = getLayer(name);		
  	if (isNav4)
    	layer.visibility = "show";
  	//if (document.all)
	else
   	 layer.visibility = "visible";
	 //layer.display="block";
}

// Used by STOPZOOMBOX -- toggles the layer to invisible
function hideLayer(name) {	
   var layer = getLayer(name);  			
  	if (isNav4)
    	layer.visibility = "hide";
  	//if (document.all)
	else
   		 layer.visibility = "hidden";
		 //layer.display="none";
}
	
// Used by SETCLIP and PANMOUSE -- As the layer moves around the image is clipped at a certain point on the screen.	
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";
		}
	}
}

		// These functions are not currently used by any part of the test program.
		// hide the "RetriveMap" image
		function hideRetrieveMap() {
			if (hasLayer("LoadMap")) {
				hideLayer("LoadMap");
			}
		}
		
		// put up the "RetriveData" image
		function showRetrieveData() {
			if (hasLayer("LoadData")) {
				showLayer("LoadData");
			}
		}
		
		// hide the "RetriveData" image
		function hideRetrieveData() {
			if (hasLayer("LoadData")) {
				hideLayer("LoadData");
			}
		}
		
		// 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);
		}
		
		function boxIt(theLeft,theTop,theRight,theBottom) {
			if(isIE){
				theLeft = theLeft + hspc;
				theRight = theRight + hspc;
				theBottom = theBottom + vspc;
				theTop = theTop + vspc;
			}
			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
		
		***************************************************************************************
		*/
		
// Used by PANMOUSE -- moves layer to x,y
function moveLayer(name, x, y) {		
  	var layer = getLayer(name);		
  	if (isNav4)
    	layer.moveTo(x, y);
	 else {
    	layer.left = x;
   		 layer.top  = y;
  	}
}
		
// Used in Test LOADFUNCTION -- Used to return the map coordinates to the browser's status bar.	
function getMouse(e) {
	getImageXY(e);
	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;
		setClip();
		return false;
	} else if (panning) {
		x2=mouseX;
		y2=mouseY;
		panMouse();
		return false;	
	}
	if ((mouseX<iWidth) && (mouseY<iHeight) && (mouseX>0) && (mouseY>0)){
		var mouseString = "";
		
	   	window.status="";		
		if (zooming) mouseString += "ZoomBox: [" + x1 + "," + y1 + " x " + x2 + "," + y2 + "]    ";
		getMapXY(mouseX,mouseY)
		mouseString += "Coords: " + mapX + " , " + mapY;	
		window.status = mouseString;
	}
	return true;
}
		
// Used by Test LOADFUNCTION -- Based on what tool is selected when the mouse is pushed does certain things
function mapTool (e) {
	maxy = document.mapparams.top.value;
 	maxx = document.mapparams.right.value;
	miny = document.mapparams.bottom.value;
	minx = document.mapparams.left.value;
	if (document.mapparams.cmd.value == "zoomin"){
		startZoomBox(e);
	}else if(document.mapparams.cmd.value == "zoomout"){
    	if ((mouseX<=iWidth) && (mouseY<=iHeight) && (mouseX>=0) && (mouseY>=0)){
			clickZoom(e, "out");
			refreshMap(); 
		}
	}else if(document.mapparams.cmd.value == "pan"){
		if ((mouseX<=iWidth) && (mouseY<=iHeight) && (mouseX>=0) && (mouseY>=0)){
			startPan(e);
		}
	}else if(document.mapparams.cmd.value == "query"){
	 	if ((mouseX<iWidth) && (mouseY<iHeight) && (mouseX>0) && (mouseY>0)){
			document.mapparams.clickx.value = mouseX;
			document.mapparams.clicky.value = mouseY;
			showRetrieveMap();
			document.mapparams.submit();
		}
/*	}else if(document.mapparams.cmd.value == "redraw"){
		if ((mouseX<=iWidth) && (mouseY<=iHeight) && (mouseX>=0) && (mouseY>=0)){
			showRetrieveMap();
			document.mapparams.submit();
		}*/
	}
}

// Used by Test LOADFUNCTION -- If the mouse is unclicked, some things need to be stopped.
function chkMouseUp(e) { 
	if (zooming){
		stopZoomBox(e);
		zooming=false;
	}
	if (panning) {
		//stopZoomBox(e);	
		stopPan(e);
		panning=false;
	}
	// next line needed for Mac
	//here we deal with netscape not releasing the mousedown event.
	//go ahead and set the x and y coordinates manually to the xccord and ycoord,
	//and trigger a submit.	
	return true;
}

// Used by GETMOUSE, STARTZOOMBOX and STARTPAN -- gets the pixel position of the mouse on the image.
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;
}	

// Used by GETMOUSE and STARTZOOMBOX -- Stops the mouse from registering at the image limits.	
// 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+ovBoxSize);
		clipLayer("zoomBoxLeft",zminx,zmaxy,zminx+ovBoxSize,zminy);
		clipLayer("zoomBoxRight",zmaxx-ovBoxSize,zmaxy,zmaxx,zminy);
		clipLayer("zoomBoxBottom",zminx,zminy-ovBoxSize,zmaxx,zminy);
	}
}	

// Used by GETMOUSE -- Moves the layer as the mouse moves
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;
}
	
// Used by MAPTOOL, GETMOUSE, CLICKZOOM, and RECENTER -- convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
	var mouseX;
	var mouseY;
	mouseX = xIn;
	var pixelX = (maxx-minx) / iWidth;
	mapX = parseFloat(pixelX) * parseFloat(mouseX) + parseFloat(minx);
	mouseY = iHeight - yIn;
	var pixelY = (maxy-miny) / iHeight;
	mapY = parseFloat(pixelY) * parseFloat(mouseY) + parseFloat(miny);
}
	
// Used by MAPTOOL -- start zoom in.... box displayed
function startZoomBox(e) {
	getImageXY(e);	
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)  && ((mouseX+hspc)>hspc) && ((mouseY+vspc)>vspc)){
		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;	
}
	
// Used by STARTZOOMBOX -- Shows the layers that apparently draw the lines on the map.
function showZoomBox() {
	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
}
		
// Used by CHKMOUSEUP and STARTZOOMBOX -- figure out new map area, hide the lines, draw the map. 
function stopZoomBox(e) {
	if ((zmaxx <zminx+2) && (zmaxy < zminy+2)) {
		// if the zoom box is too small
		clickZoom(e, "in")
	} 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 = parseFloat(pixelY) * parseFloat(theY) + parseFloat(miny);
		newMaxx = parseFloat(pixelX) * parseFloat(zmaxx) + parseFloat(minx);
		newMinx = parseFloat(pixelX) * parseFloat(zminx) + parseFloat(minx);
		theY = iHeight - zminy;
		pixelY = height / iHeight;
		newMiny = parseFloat(pixelY) * theY + parseFloat(miny);
	}
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
	window.scrollTo(0,0);
	refreshMap();
	return true;
}
	
// Used by MAPTOOL and STOPZOOMBOX -- Zooms in or out by 40% on the click
function clickZoom(e, direction) {	
	getMapXY(mouseX,mouseY);
	if (direction == "out"){
		var widthHalf = (Math.abs(maxx - minx) / 2) + (Math.abs(maxx - minx)/ 5);
		var heightHalf = (Math.abs(maxy - miny) / 2) + (Math.abs(maxy - miny)/5);
	}
	else if (direction == "in"){
		var widthHalf = (Math.abs(maxx - minx)/ 2) - (Math.abs(maxx - minx)/ 5);
		var heightHalf = (Math.abs(maxy - miny)/ 2) - (Math.abs(maxy - miny)/5);
	}
	newMinx = mapX - widthHalf;
	newMaxx = mapX + widthHalf;
	newMaxy = mapY + heightHalf;
	newMiny = mapY - heightHalf;
}

// Used by MAPTOOL, STOPZOOMBOX, and STOPPAN -- Redraws the map.
function refreshMap() {
	//set the last extent so that the user can return to it
	//document.mapparams.LMaxY.value = document.mapparams.MaxY.value
  	//document.mapparams.LMaxX.value = document.mapparams.MaxX.value
	//document.mapparams.LMinY.value = document.mapparams.MinY.value
	//document.mapparams.LMinX.value = document.mapparams.MinX.value
	//set the new extent
  	document.mapparams.top.value = newMaxy;
  	document.mapparams.right.value = newMaxx;
	document.mapparams.bottom.value = newMiny;
	document.mapparams.left.value = newMinx;
	showRetrieveMap();
	document.mapparams.submit();
}		
		
// Used by MAPTOOL -- start pan.... image will move (it all starts here)
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;
}
		
// Used by STARTPAN and CHKMOUSEUP -- stop moving image.... pan, redraws the map
function stopPan(e) {
	if ((Math.abs(x2-x1) < 2) && (Math.abs(y2-y1) < 2)) {
		// the move is too small
		recenter(e);
		refreshMap(); 
	} 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 - ztop;
		pixelY = height / iHeight;
		var xOffset = pixelX * ixOffset;
		var yOffset = pixelY * iyOffset;
		
		newMaxy = maxy - yOffset;
		newMaxx = maxx - xOffset;
		newMinx = minx - xOffset;
		newMiny = miny - yOffset;
		//alert(newMaxy + " " + newMaxx + " " + newMinx + " " + newMiny);
		refreshMap();
	}
	return true;
}

// Used by STOPPAN -- Determines the new center of the map from the mouse position	
function recenter(e) {
	getMapXY(mouseX,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;
}


