
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;

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;
// end testing browser 

// * start Click.js

//*****************************************************************************
// Global vars to save mouse position
var mouseX=0;
var mouseY=0;

var mapX=0.0;// real coord for mouse point;
var mapY=0.0; // used for draw lines, etc

var x1=0; // hold first mouse down x
var y1=0; // first mouse down y
var x2=0; // moving mouse x
var y2=0; // moving mouse y
//var zleft=0;  // used for clip
//var zright=0; 
//var ztop=0;
//var zbottom=0;

var zoomBoxColor = "#FF0000";	// color of zoombox

var dragBoxLineSize = 1; 	// Zoombox line width;
		
var zooming = false;
var panning = false;

// end Click.js */

var hspc=0;
var vspc=0;

function init(){
	if (isNav4) {
		document.captureEvents(Event.MOUSEMOVE|Event.MOUSEDOWN|Event.MOUSEUP);
	}
	document.onmousemove = getMouse;
	document.onmousedown = chkMouseDown;
	document.onmouseup = chkMouseUp;
	window.onresize=chkResize;
}

// start DHTML.js Create a DHTML layer
function createLayer(name, inleft, intop, width, height, visible, content) {
	 
	  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>');
	  }
}

// get the layer object called "name"
function getLayer(name) {
 	  if (isNav4){
	    return(document.layers[name]);
	  } else if (isIE4) {
	    var 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);
  	//if (document.all) {
	} 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 if (document.all)
	else
    	layer.backgroundColor = color;
}

// toggle 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";
}

// toggle 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";
}

// 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 +')';
	  }
}

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 {
		    //layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
			var newWidth = clipright - clipleft;
			var newHeight = clipbottom - cliptop;
			layer.height = newHeight;
			layer.width	= newWidth;
			//var theTop = parseInt((parseFloat(cliptop) * 10 + 0.5)/10);
			//var theLeft = parseInt((parseFloat(clipleft) * 10 + 0.5)/10);
			//alert(cliptop + " " + clipleft);
			layer.top	= cliptop  + "px";
			layer.left	= clipleft + "px";
			//layer.display= "none";
		}

}


// replace layer's content with new content
	// not working with Mozilla Milestone 12 (Nav5)
function replaceLayerContent(name, content) {
	  if (isNav4) {
		    var layer = getLayer(name);
		    layer.document.open();
		    layer.document.writeln(content);
		    layer.document.close();
	  }  else if (isIE) {
		    var str = "document.all." + name + ".innerHTML = '" + content + "'";
		    eval(str);
	  }
}
function boxIt(theLeft,theTop,theRight,theBottom) {
		if (!isNav4) {
			theTop = theTop + vspc;
			theBottom = theBottom + vspc;
			theLeft = theLeft + hspc;
			theRight = theRight + hspc;
		}
		clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+dragBoxLineSize);
		clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+dragBoxLineSize,theBottom);
		clipLayer("zoomBoxRight",theRight-dragBoxLineSize,theTop,theRight,theBottom);
		clipLayer("zoomBoxBottom",theLeft,theBottom-dragBoxLineSize,theRight,theBottom);	
		showLayer("zoomBoxTop");
		showLayer("zoomBoxLeft");
		showLayer("zoomBoxRight");
		showLayer("zoomBoxBottom");
}
// end Dhtml.js 

// /* start Navigation
// convert mouse click xy's into map coordinates
// NL: modified from orig with minx, miny, etc
function getMapXY(xIn,yIn) {
	var pixelX = (maxx-minx) / iWidth;
	mapX = pixelX * xIn + minx;
	var pixelY = (maxy-miny) / iHeight;
	mapY = pixelY * (iHeight - yIn) + miny;
}
// get cursor location, save image x&y in globals
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;
}	

// perform appropriate action with mapTool
function chkMouseDown (e) {
	if (!isLeftMouse(e)) return false;
	getImageXY(e);
	if (!inMapWindow()) return false;
	x1=mouseX;
	y1=mouseY;
	switch (state){
		case "identify":
		case "data":
		 // chkMouseUp(e);
		 break;
		case "zoom_in":
		case "zoom_out":
		  startZoomBox(e);
		  break;
		case "pan":
		  startPan(e);
		  break;
	}
	
	return false;
}

//*****************************************************************************
// getMouse() 

function getMouse(e) {
	getImageXY(e);	//save image x&y in global mouseX and mouseY
	// keep it within the MapImage or MapWindow
	if (!inMapWindow()) {
		killDrag(e);
		return true; // enable select text outside map
		//chkMouseUp(e);
	} else {
		x2=mouseX;
		y2=mouseY;
		if (zooming) {
			setClip();
		} else if (panning) {
			panMouse();	
		} 
		getMapXY(mouseX, mouseY);
		showStatus("["+parseInt(mapX)+","+""+parseInt(mapY)+"]");
		//showStatus("["+x1+","+y1+" "+x2+","+y2+"]");
  	}
	// next line needed for Mac
	return false;
}

//
function chkMouseUp(e) { 
	if (!isLeftMouse(e)) return false;
	getImageXY(e);
	if (!inMapWindow()) return false;
	document.forms[0].x1.value=x1;
	document.forms[0].y1.value=y1;
	document.forms[0].x2.value=mouseX;
	document.forms[0].y2.value=mouseY;
	switch (state) {
		case "zoom_in":	
		case "zoom_out":
			if (zooming) {
				stopZoomBox(e);
				doCommand(state);
			}
			break;
		case "pan":
			if (panning) {
				stopPan(e);
				doCommand(state);
			}
			break;
		case "identify":
		case "data":
		 doCommand(state);
		 break;
 		default:
			break;
	}
	// next line needed for Mac	
	return false;
}

function startZoomBox(e) {

	moveLayer("theMap",hspc,vspc);
	
	getImageXY(e);	
	// keep it within the MapImage or MapWindow
	if (inMapWindow()) {
		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;
}

function stopZoomBox(e) {
	zooming=false;
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
}



// clip zoom box layer to mouse coords
function setClip() {	
	boxIt(Math.min(x1,x2),Math.min(y1,y2),Math.max(x1,x2),Math.max(y1,y2));
	//return false;
}



function killDrag(e) {
	if (zooming) {
		stopZoomBox(e);
	}	else if (panning) {
		stopPan(e);
		moveLayer("theMap",hspc,vspc);
		clipLayer2("theMap",0,0,iWidth,iHeight);
	}
	return true;
}

//*****************************************************************************
// startPan() 
// starts the pan, movement of image

function startPan(e) {
	moveLayer("theMap",hspc,vspc);
	getImageXY(e);
	// keep it within the MapImage or MapWindow
	if (inMapWindow()) {
		if (panning) {
			stopPan(e);
			//killDrag(e);
		} else {
			//x1=mouseX;
			//y1=mouseY;
			x2=x1+1;
			y2=y1+1;
			panning=true;
		}
	}
	return false;

}

function stopPan(e) {
	//window.scrollTo(0,0);
	panning=false;
	//return true;
	
}

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;
	}
	clipLayer2("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+hspc,yMove+vspc);
}


//*****************************************************************************
// inMapWindow() 
// returns true if current mouseX and mouseY are inside map window
// accounts for scrolling of document

function inMapWindow() {
	return (mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight);
}


//*****************************************************************************

//function used to determine whether left mouse button was clicked
function isLeftMouse(e) {
	if (isNav) {
		if (e.which == 1) return true;
	} else if (isIE) {
		if (window.event.button == 1) return true;
	}
	return false;
}

function showStatus(stat){
	window.status = stat;
}

// NL: create a place holder for map
function createRelativeLayer(name, width, height, visible, content) {
	  var layer;
	  if (isNav4) {
	    document.writeln('<layer name="' + name + '" 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:relative; width:' + width + 'px; height:' + height + 'px;' + ' visibility:' + (visible ? 'visible;' : 'hidden;') + '">');
	    document.writeln(content);
	    document.writeln('</div>');
	  }
	  clipLayer(name, 0, 0, width, height);
}

// NL: return the layer's X pixel 
function getLayerX(name){
	if (document.layers){//isNav4){
	    return(document.layers[name].pageX);
	} else if (document.all){//isIE4) {
	  	return eval('document.all.' + name + '.offsetLeft');
	} else if (document.getElementById){//is5up) {
		var theObj = document.getElementById(name);
		return theObj.offsetLeft;
	} else
	    return 0;
}
// NL: return the layer's Y pixel 
function getLayerY(name){
	if (document.layers){//isNav4){
	    return(document.layers[name].pageY);
	} else if (document.all){//isIE4) {
	  	return eval('document.all.' + name + '.offsetTop');
	} else if (document.getElementById){//is5up) {
		var theObj = document.getElementById(name);
		return theObj.offsetTop;
	} else
	    return 0;
}

function chkResize(){
	hspc=getLayerX("thePlaceHolder");
	vspc=getLayerY("thePlaceHolder");
	moveLayer("theMap",hspc,vspc);
}

