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 

//*****************************************************************************
// Global vars to save mouse position
var mouseX=0;
var mouseY=0;

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 state;
var zooming = false;
var panning = false;
var enableDataOnMap;


var hspc=22;
var vspc=133;


	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('<div id="' + name + '" style="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);
	} 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);
	layer.clip = 'rect(' + cliptop + ' ' + clipright + ' ' + clipbottom + ' ' + clipleft +')';
}
//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);
	var newWidth = clipright - clipleft;
	var newHeight = clipbottom - cliptop;
	layer.height = newHeight;
	layer.width = newWidth; 
	layer.top = cliptop + "px";
	layer.left = clipleft + "px";
}


//function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	  //alert(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  + "px";
//			layer.left	= clipleft + "px";
//			
//		}

//}


function boxIt(theLeft,theTop,theRight,theBottom) {
		if (!isNav4) {
			//theTop = theTop + vspc;
			//theBottom = theBottom + vspc;
			//theLeft = theLeft + hspc;
			//theRight = theRight + hspc;
			theTop = theTop + 133;
			theBottom = theBottom + 133;
			theLeft = theLeft + 22;
			theRight = theRight + 22;
		}
	
	clipLayer("zoomBoxTop",theLeft,theTop,theRight,theBottom);
	showLayer("zoomBoxTop");

}
// end Dhtml.js 


// 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;
	mouseX = mouseX-22;
	mouseY = mouseY-133;
}	

// perform appropriate action with mapTool
function chkMouseDown (e) {
	if (!isLeftMouse(e)) return true;
	getImageXY(e);
	if (!inMapWindow()) return true;
	x1=mouseX;
	y1=mouseY;

	//state=document.frmSearch.state.value;
	switch (state){
		case "zoomin":
		  startZoomBox(e);
		  break;
		case "zoomout":
		  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
	} else {
		x2=mouseX;
		y2=mouseY;
		if (zooming) {
			setClip();
		} else if (panning) {
			panMouse();	
		} 
//		showStatus("["+x1+","+y1+" "+x2+","+y2+"]");
  	}
//	showStatus(enableDataOnMap);
	// next line needed for Mac
	return false;
}

//
function chkMouseUp(e) { 
	if (!isLeftMouse(e)) return false;
	getImageXY(e);
	if (!inMapWindow()) return false;
	switch (state) {
		case "zoomin":	
			if (zooming) {
				stopZoomBox(e);
				doCommand(state);
			}
			break;
		case "zoomout":
			if (zooming) {
				stopZoomBox(e);
				doCommand(state);
			}
			break;
		case "pan":
			if (panning) {
				stopPan(e);
				doCommand(state);
			}
			break;
		case "identify":
			//alert (state);
			doCommand(state);
			break;
		case "radius":
			//alert (state);
			doCommand(state);
			break;
		case "sampleproperties":
			//alert (state);
			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 {
			x2=x1+1;
			y2=y1+1;
			boxIt(x1,y1,x2,y2);
			zooming=true;

		}
	}
	return false;
}

function stopZoomBox(e) {
	zooming=false;
	hideLayer("zoomBoxTop");
}



// 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);
		//unsetTool();
	}	else if (panning) {
		stopPan(e);
		moveLayer("theMap",hspc,vspc);
		clipLayer2("theMap",0,0,440,450);
		//unsetTool();
	}
	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);
		} else {
			x2=x1+1;
			y2=y1+1;
			panning=true;
		}
	}
	return false;

}

function stopPan(e) {
	panning=false;
	//return true;
	
}

function panMouse() {
	var xMove = x2-x1;
	var yMove = y2-y1;
	var cLeft = -xMove;
	var cTop = -yMove;
	var cRight = 440;
	var cBottom = 450;
	if (xMove>0) {
		cLeft = 22;
		cRight = 440 - xMove;
	}
	if (yMove>0) {
		cTop = 133;
		cBottom = 450 - yMove;
	}
	clipLayer2("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+22,yMove+133);
}


//*****************************************************************************
// inMapWindow() 
// returns true if current mouseX and mouseY are inside map window
// accounts for scrolling of document

function inMapWindow() {
	return (mouseX>=0) && (mouseX<440) && (mouseY>=0) && (mouseY<450);
}


//*****************************************************************************

//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;
	//alert("called "+content);
	  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, 22,133, width + 22, height + 133);
}

// NL: return the layer's X pixel 
function getLayerX(name){
//alert("called getLayerX"+name);
	if (document.layers) { 
		//alert("isNav4");
	    return(document.layers[name].pageX);
	} else if (document.getElementById) { 
		//alert("is5up");
	  	//return eval('document.all.' + name + '.offsetLeft');
	  	var theObj = document.getElementById(name);
		return theObj.offsetLeft;
	} else if (document.all) { 
		//alert("isIE4");
		//var theObj = document.getElementById(name);
		//return theObj.offsetLeft;
		return eval('document.all.' + name + '.offsetLeft');
	} else
		//alert("Else");
		return 100;
}

// NL: return the layer's Y pixel 
function getLayerY(name){
	if (isNav4){
	    return(document.layers[name].pageY);
	} else if (isIE4) {
	  	return eval('document.all.' + name + '.offsetTop');
	} else if (is5up) {
		var theObj = document.getElementById(name);
		return theObj.offsetTop;
	} else
	    return 0;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function chkResize(){
	hspc=getLayerX("thePlaceHolder");
	vspc=getLayerY("thePlaceHolder");
	moveLayer("theMap",hspc,vspc);
}


//unset the tool value
function unsetTool(){
	var theCursor="default";
	enableDataOnMap=true;
	resetImage();
	state='none';
	if (document.all) getLayer("theMap").cursor=theCursor;
}

function resetImage(){
		// reset images 
	document.images["zoomin"].src="images/zoomin1.gif";
	document.images["zoomout"].src="images/zoomout1.gif";
	document.images["pan"].src="images/pan1.gif";
//	document.images["identify"].src="images/identify1.gif";
}
function setTool(tool){
	//alert("set tool = " + tool);
	var theCursor="default";
	enableDataOnMap=false;
	//store tool value	
	state=tool;	
	switch (tool){
		case "zoomin":
			theCursor = "crosshair";
			document.images["identify"].src="images/identify1.gif";
			document.images["pan"].src="images/pan1.gif";
			document.images["zoomout"].src="images/zoomoutbtn1.gif";
			document.images["zoomin"].src="images/zoominbtn2.gif";
			document.cookie='maptool='+tool;
			break;
		case "zoomout":
			theCursor = "crosshair";
			document.images["identify"].src="images/identify1.gif";
			document.images["pan"].src="images/pan1.gif";
			document.images["zoomin"].src="images/zoominbtn1.gif";
			document.images["zoomout"].src="images/zoomoutbtn2.gif";
			document.cookie='maptool='+tool;
			break;
		case "pan":
			theCursor = "move";
			document.images["identify"].src="images/identify1.gif";
			document.images["zoomout"].src="images/zoomoutbtn1.gif";
			document.images["zoomin"].src="images/zoominbtn1.gif";
			document.images["pan"].src="images/pan2.gif";
			document.cookie='maptool='+tool;
			break;
		case "identify":
			theCursor = "crosshair";
			document.images["pan"].src="images/pan1.gif";
			document.images["zoomout"].src="images/zoomoutbtn1.gif";
			document.images["zoomin"].src="images/zoominbtn1.gif";
			document.images["identify"].src="images/identify2.gif";
			document.cookie='maptool='+tool;
			break;
		case "radius":
			theCursor = "crosshair";
			document.images["identify"].name="disabled";
			document.images["pan"].name="disabled";
			document.images["zoomin"].name="disabled";
			document.images["zoomout"].name="disabled";
			document.images["identify"].src="images/daidentify1.gif";
			document.images["pan"].src="images/dapan1.gif";
			document.images["zoomin"].src="images/dazoominbtn1.gif";
			document.images["zoomout"].src="images/dazoomoutbtn1.gif";
			break;
		case "sampleproperties":
			theCursor = "crosshair";
			document.images["identify"].name="disabled";
			document.images["pan"].name="disabled";
			document.images["zoomin"].name="disabled";
			document.images["zoomout"].name="disabled";
			document.images["identify"].src="images/daidentify1.gif";
			document.images["pan"].src="images/dapan1.gif";
			document.images["zoomin"].src="images/dazoominbtn1.gif";
			document.images["zoomout"].src="images/dazoomoutbtn1.gif";
			break;
		default:
			;
	}
	if (document.all) getLayer("theMap").cursor=theCursor;
}
function checkEnabled(tool){
	if (document.images[tool].name!="disabled"){
		//alert(document.images[tool].name);
		setState(tool);
		}
	}	
// sets the tool value. A tool is a mode that needs futher click on map.
function setState(tool){
//alert(tool)
	//if(state==tool){
	//	unsetTool();
	//}else{
		//resetImage();
		setTool(tool);
	//}
}

function doCommand(cmd){	
	// replace this will real stuff such as submit form
//alert("tool="+state+"\nx1="+x1+"\ny1="+y1+"\nx2="+x2+"\ny2="+y2);
 //   showLayer("loadLayer");
//	setTimeout("window.location.reload()",2000);
	//document.frmSearch.action="search.aspx?action="+state+"&x1="+x1+"&y1="+y1+"&x2="+x2+"&y2="+y2;
	document.cookie='nx1='+x1;
	document.cookie='ny1='+y1;
	document.cookie='nx2='+x2;
	document.cookie='ny2='+y2;
	document.images['imgwait'].src = wait.src;
	document.images['imgwaittext'].src = waittext.src;
	document.cookie='mapfunction='+state;
	document.form1.submit();
}

function callAction0(action)
{
	document.frmSearch.action="search.aspx?action="+ action;
	document.frmSearch.submit();
}

function callAction1(action,param1){
	document.frmSearch.action="search.aspx?action="+action+"&p1="+ param1;
	document.frmSearch.submit();
}

function callAction2(action,param1,param2){
	document.frmSearch.action="search.aspx?action="+action+"&p1="+ param1+"&p2="+ param2;
	document.frmSearch.submit();
}

function callAction4(action,p1,p2,p3,p4){
	document.frmSearch.action="search.aspx?action="+action+"&p1="+ p1+"&p2="+ p2+"&p3="+p3 +"&p4="+ p4;
	document.frmSearch.submit();
}

function viewDataOnMap(name,address,bioid,srn) {
	if(enableDataOnMap){
		//alert('viewdataonmap');
		var mousex = window.event.x;        // mouse location capture event
		var mousey = window.event.y;        // mouse location capture event
//		showStatus("mousex="+mousex+"   mousey="+mousey);
		SODataOnMapPhoto.src='photo.aspx?bioid='+bioid;
		SODataOnMapName.innerText=name;
		SODataOnMapAddress.innerText=address;
		var h="javascript:callAction2('getdetail','"+bioid+"','"+srn+"');";
		//alert(h);
		SODataOnMapDetail.href=h;
		SODataOnMap.style.visibility = 'visible';  // show or hide respective Example
		SODataOnMap.style.left = mousex;       // place popup at the mouse X (left) location
		SODataOnMap.style.top = mousey;            // place popup at the mouse Y (top) location
		// too right
		if(mousex>hspc+440-160){
		  SODataOnMap.style.left=mousex-160;
		}
		//too low
		if(mousey>vspc+450-90){
		  SODataOnMap.style.top = mousey-90;
		}
		
	}
}

function submitSearch(){
	if (typeof(Page_ClientValidate) == 'function'){
		if(Page_ClientValidate()){
			callAction0('search');
		}
	}
}


