
//**********************************************************
//Any functions that can apply to the map, such as zoom in,
//zoom out, identifying, drill down capabilities.
//
//**********************************************************
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 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 cx=0;
var cy=0;
var previousState = "";
var mapX = 0; 
var mapY = 0; 
var zoomBoxWidth = 1;
var zooming = false;
var panning = false;
var measuring = false;
var armed = false;
var bottomBorderHeight = 0;
// 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;
var testX = "";
var defx1 = null;
var defy1 = null;
var cnter = 0;



if (isNav) {
	
	if (parseFloat(navigator.appVersion)<5) {
		isNav4=true;
		} else {
		is5up = true;
	}
} else {
	isIE4=true;
	isIE=true;
	if (navigator.appVersion.indexOf("MSIE 5")>0) {
		isIE4 = false;
		is5up = true;
	}
}	

if (navigator.userAgent.indexOf("Win") >= 0) 
	isWin = true;
else
	isMac = true;

//***********************************************	
//***************** FUNCTIONS *******************
//***********************************************

// check for mouseup
function chkMouseUp(e) { 
	if (zooming || panning) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>mapWidth)
			mouseX = mapWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>mapHeight)
			mouseY = mapHeight;
		mapTool(e);
	} else 
	{
		extendedChkMouseUp(e);
	}
}

function getImageXY(e) {
	//if (document.layers) {
	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else 
	{
		mouseX=e.clientX + top.map.document.body.scrollLeft;
		mouseY=e.clientY + top.map.document.body.scrollTop;
		
	}
	// subtract offsets from page left and right
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;
	
}	

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
	mouseX = xIn;
	var pixelX = (maxx-minx) / mapWidth;
	mapX = pixelX * mouseX + minx;
	mouseY = mapHeight - yIn;
	var pixelY = (maxy-miny) / mapHeight;
	mapY = pixelY * mouseY + miny;
}
function getTrueMapXY(xIn,yIn) {
	mouseX = xIn - hspc;
	var pixelX = (maxx-minx) / mapWidth;
	mapX = pixelX * mouseX + minx;
	mouseY = mapHeight - (yIn - vspc);
	var pixelY = (maxy-miny) / mapHeight;
	mapY = pixelY * mouseY + miny;
}

function getMapXY2(xIn,yIn) {
	mouseX = xIn;
	var pixelX = (maxx-minx) / mapWidth;
	cx = pixelX * mouseX + minx;
	mouseY = mapHeight - yIn;
	var pixelY = (maxy-miny) / mapHeight;
	cy = pixelY * mouseY + miny;
}

// get the coords at mouse position
function getMouse(e) {
	//document.writeln("getmouse");
	window.status="";
	getImageXY(e);
	if (zooming) 
	{
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>mapWidth)
			mouseX = mapWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>mapHeight-bottomBorderHeight)
			mouseY = mapHeight-bottomBorderHeight;
		x2=mouseX;
		y2=mouseY;
		getMapXY2(mouseX, mouseY);
		setClip();
		return false;
	} else if (panning) 
	{
	//top.bottomFrame.document.getElementById("message").innerText = "pan " + cnter;
		
        cnter++;
		x2=mouseX;
		y2=mouseY;
		panMouse();	
		return false;
    } else if (measuring) 
    {
		return false;
	} else 
	{    
	
		extendedGetMouse(e);
	
		//document.getElementById("msg").innerText = document.getElementById("msg").innerText + "something else " + cnter;
		cnter++;
		var pixelX = (maxx-minx) / mapWidth;
        x2=mapX;
        y2=mapY;
	   	mapX = pixelX * mouseX + minx;
	   	mouseY = mapHeight - mouseY;
	   	var pixelY = (maxy-miny) / mapHeight;
	   
	   	mapY = pixelY * mouseY + miny;
        if ( mouseX > (hspc-11) && mouseX < mapWidth && mouseY > (vspc-10) && mouseY < mapHeight ) 
        {
             mapX = Math.round(mapX*Math.pow(10,2))/Math.pow(10,2);
             mapY = Math.round(mapY*Math.pow(10,2))/Math.pow(10,2);
             var theCoords = "<b>X:</b> " + mapX + "	<b>Y:</b> " + mapY;
             theCoords += mapToLatLon(mapX, mapY);
             
            // top.bottomFrame.document.all.statusValue.innerHTML = theCoords;
           
         }
	}
           
    return true;
}

function hideZoomBox() {
	hideLayer("zoomBoxTop");
	hideLayer("zoomBoxLeft");
	hideLayer("zoomBoxRight");
	hideLayer("zoomBoxBottom");
}

// perform appropriate action with mapTool
//MouseDown Event Handler
function mapTool (e) {
//alert("in mt");
	getImageXY(e);
	
	if ( (state == "MEASURE") && (mouseX >= 0) && (mouseX <= mapWidth) && (mouseY >= 0) && (mouseY <= mapHeight) )
	{
    	getMouse(e);
        return false;      
	}  

	if (state != "ZOOMIN" && state != "ZOOMOUT" && state != "PAN")
	{
		extendedMapTool(e);
		return false;
	}
	
	  // If we made it to here, it's a navigational click 	
	if ((!zooming) && (!panning) && 
		(mouseX >= 0) && (mouseX <= mapWidth) && 
		(mouseY >= 0) && (mouseY <= mapHeight)) 
	{
		//alert("In pz");
		if (state == "PAN")
			startPan(e);
		else 
			startZoomBox(e);
			
		return false;
	} else if (zooming) 
	{
		getMouse(e);
		stopZoomBox(e);
	} else if (panning) 
	{
		getMouse(e);
		stopPan(e);
	}
	//alert("end");
	return true;
}

function setImage( data ) {
    if ( data != null ) {
    	top.setImage(data);
    }
    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 = mapWidth;
	var cBottom = mapHeight;
	if (xMove>0) {
		cLeft = 0;
		cRight = mapWidth - xMove;
	}
	if (yMove>0) {
		cTop = 0;
		cBottom = mapHeight - yMove;
	}
	clipLayer("theMap",cLeft,cTop,cRight,cBottom);
	moveLayer("theMap",xMove+hspc,yMove+vspc);

	return false;
}

// recenter map is the default option
function recenter(e) {

	// 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(); 
	} 
}


function printMap() {
  var printWin = window.open("print.aspx", "", "width=800,height=600,scrollbars=no,resizable=yes,status=yes,menubar=yes");
  setState("ZOOMIN");
}

function showloading(){
    showLayer("loadLayer");
}


function setCommand(data) {
    if ( data != null ) {
        document.all.cmd.value = data;
    }
}
// Write new map envelope to global min-max values
// HashMap returned from navigation bean
//Holds new map extents after zoom
function handleConfig(data) {
    var hash = data;
    if ( hash != null ) {
        minx = hash.minx;
        miny = hash.miny;
        maxx = hash.maxx;
        maxy = hash.maxy;
    }
    return;
}

// 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;
}

function setState( newState ) {
    state = newState;
    armed = false;
   
    if (document.all.cmd != undefined)
	    document.all.cmd.value = newState;
    if (document.all.state != undefined)
    	document.all.state.value = newState;

    if ( state == "DRAW" ) {
        state = "";
    } else if ( state == "CLEAR" ) {
        document.all.cmd.value = "ZOOMIN";
        refreshMap(); 
    } else if ( state == "MEASURE" ) {
      	doArm();
    } else if ( state == "PRINT" ) {
       state = "ZOOMIN";
    } else if ( state == "IDENTIFY") {
    	top.setMapCursor("crosshair");
    } else if ( state == "FIND" ) {
       find();
       state = "ZOOMIN";
    } else if ( state == "MAPREFRESH" ) {
       refreshMap();
    } else if ( state == "LASTEXTENT" ) {
       refreshMap();
    } else if ( state == "PANNE" ) {
       document.all.cmd.value = "ZOOMIN";
       refreshMap();
    } else if ( state == "PANSE" ) {
       document.all.cmd.value = "ZOOMIN";
       refreshMap();
    } else if ( state == "PANNW" ) {
       document.all.cmd.value = "ZOOMIN";
       refreshMap();
    } else if ( state == "PANSW" ) {
       document.all.cmd.value = "ZOOMIN";
       refreshMap();
    } else if ( state == "PANNORTH" ) {
       document.all.cmd.value = "ZOOMIN";
       refreshMap();
    } else if ( state == "PANSOUTH" ) {
       document.all.cmd.value = "ZOOMIN";
       refreshMap();
    } else if ( state == "PANWEST" ) {
       document.all.cmd.value = "ZOOMIN";
       refreshMap();
    } else if ( state == "PANEAST" ) {
       document.all.cmd.value = "ZOOMIN";
       refreshMap();
    } else if ( state == "ZOOMEXTENT" ) {
       document.all.cmd.value="ZOOMIN";
       refreshMap();
    }else if ( state == "DRAWPOINT" ) {
		top.setMapCursor("crosshair");
		state = "DRAWPOINT";
       document.all.cmd.value="DRAWPOINT";
    }else if ( state == "GEOCODEPOINT" ) {
		top.setMapCursor("crosshair");
		state = "GEOCODEPOINT";
       document.all.cmd.value="GEOCODEPOINT";
    }
   
    if ( state == "PAN" ) 
    {
        top.setMapCursor("move");
    } else 
    {
		if ( state == "DRAWPOINT" )
		{
			try{
				top.setMapCursor("images/place-location.ico");
			}catch(e){
				top.setMapCursor("crosshair");
			}
		} else
		{
			top.setMapCursor("default");
		}
    }

    if ( state == "ZOOMIN") 
    {
        mapBoxAsZoom = true;
		setZoomBoxColor("#ff6600");
		setZoomBoxWidth(3);
    } else if (state == "ZOOMOUT") 
    {
        mapBoxAsZoom = true;
        setZoomBoxColor("#ff6600");
		setZoomBoxWidth(3);
    } else if (state == "QUERY") 
    {
        mapBoxAsZoom = true;
        setZoomBoxColor("#66cc00");
        setZoomBoxWidth(3);
    }  
    
}

function setZoomBoxColor(color) {
    setLayerBackgroundColor("zoomBoxTop", color);
    setLayerBackgroundColor("zoomBoxLeft", color);
    setLayerBackgroundColor("zoomBoxRight", color);
    setLayerBackgroundColor("zoomBoxBottom", color);
}


	
function setZoomBoxWidth(size) {
    zoomBoxWidth = size;
}

function showZoomBox() {
    if ( state != "" ) {
        showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
    }
}

// start pan.... image will move
function startPan(e) {
    //moveLayer("theLrs",hspc,vspc);
    getImageXY(e);
    // keep it within the MapImage
    if ((mouseX<mapWidth) && (mouseY<mapHeight)) {
        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) {
	//alert("insz");
    getImageXY(e);	

    if (!allowRubberband) 
    { 
        stopZoomBox(e);
    } else 
    {	
        // keep it within the MapImage
		if ((mouseX<mapWidth) && (mouseY<mapHeight-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) 
           	{
               	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 / mapWidth;
	var theY = mapHeight - zmaxy;
	pixelY = height / mapHeight;
	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();
	clipLayer("theMap",0,0,mapWidth, mapHeight);
	moveLayer("theMap",hspc,vspc);
    }
		
return true;
}

// stop zoom box display... zoom in
function stopZoomBox(e) {
    zooming=false;
    if ((zmaxx <zminx+2) && (zmaxy < zminy+2)) {
        // if the zoom box is too small
	recenter(e);
    } else {
        var width = Math.abs(maxx - minx);
	var height = Math.abs(maxy - miny);
	var pixelX = width / mapWidth;
	var theY = mapHeight - zmaxy;
	var pixelY = height / mapHeight;
	newMaxy = pixelY * theY + miny;
	newMaxx = pixelX * zmaxx + minx;
	newMinx = pixelX * zminx + minx;
	theY = mapHeight - zminy;
	pixelY = height / mapHeight;
	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 LoadScale() {
   var selScale = window.document.forms.frmNavigation.cboScales;
   var selScaleVal = selScale.options[selScale.selectedIndex].value;
   if ( selScaleVal != 0 ) {
      var centerX = ((maxx+minx)/2);
      var centerY = ((maxy+miny)/2);
      var mapScale = (selScaleVal * 0.0254);
      var mapWidth = (mapWidth * mapScale);
      var mapHeight = (mapHeight * mapScale);
      var minpX = (centerX-(mapWidth/2));
      var maxpX = (centerX+(mapWidth/2));
      var minpY = (centerY-(mapHeight/2));
      var maxpY = (centerY+(mapHeight/2));

      document.location = sMapProcessor+"?cmd=SCALE"+
           "&MinX="+minpX.toString()+
           "&MinY="+minpY.toString()+
           "&MaxX="+maxpX.toString()+
           "&MaxY="+maxpY.toString()+checkfordrawing();
    }
}*/

function setMeasuring() {
   measuring=true;
   state="MEASURE";
}

function clearMeasuring(theState) {
   	measuring=false;
   	state=theState;
   	armed=false;
   	top.map.resetEvents();
}

//Not needed 
function captureCmd(){}

//***************These functions came from functions.js****************************


function setToolImage(tool) {
	//Used by Graphical Button Clients.
	//alert("TOp of settoolimage");
	var tool= tool.toLowerCase();
	//alert("Next");
	// Return all images to first state.
	
	top.topFrame.document.getElementById("zoomin").src = 'images/maptools/firm-zoomin.gif';
	top.topFrame.document.getElementById("zoomout").src = 'images/maptools/firm-zoomout.gif';
    top.topFrame.document.getElementById("zoomextent").src = 'images/maptools/firm-fitmap.gif';
	top.topFrame.document.getElementById("pan").src = 'images/maptools/firm-pan.gif';
	top.topFrame.document.getElementById("lastmap").src = 'images/maptools/firm-lastmap.gif';
	top.topFrame.document.getElementById("drawpoint").src = 'images/maptools/firm-placelocation.gif';

    

        //Then brighten the passed tool.
		if (tool == "zoomin") {
            top.topFrame.document.getElementById("zoomin").src = 'images/maptools/firm-zoomin-down.gif';
        } else if (tool == "zoomout") {
			top.topFrame.document.getElementById("zoomout").src = 'images/maptools/firm-zoomout-down.gif';
        } else if (tool == "zoomextent") {
            top.topFrame.document.getElementById("zoomin").src = 'images/maptools/firm-zoomin-down.gif';
        } else if (tool == "pan") {
			top.topFrame.document.getElementById("pan").src = 'images/maptools/firm-pan-down.gif';
        } else if (tool == "lastmap") {
            top.topFrame.document.getElementById("zoomin").src = 'images/maptools/firm-zoomin-down.gif';
        } else if (tool == "drawpoly") {
            top.topFrame.document.getElementById("drawpoint").src = 'images/maptools/firm-placelocation-down.gif';
		}
		/*if (tool == "zoomin") {
            top.topFrame.document.getElementById("zoomin").src = 'images/maptools/zoomin_down.gif';
        } else if (tool == "zoomextent") {
            top.topFrame.document.getElementById("zoomin").src = 'images/maptools/zoomin_down.gif';
        } else if (tool == "zoomout") {
			top.topFrame.document.getElementById("zoomout").src = 'images/maptools/zoomout_down.gif';
        } else if (tool == "print") {
			top.topFrame.document.getElementById("print").src = 'images/maptools/print_down.gif';
        } else if (tool == "pan") {
			top.topFrame.document.getElementById("pan").src = 'images/maptools/pan_down.gif';
        }  else if (tool == "measure") {
			top.topFrame.document.getElementById("measure").src = 'images/maptools/measure_down.gif';
		}  else if (tool == "identify") {
			top.topFrame.document.getElementById("identify").src = 'images/maptools/identify_down.gif';
        }  else if (tool == "lrs") {
			//top.topFrame.document.getElementById("lrs").src = 'images/tools/lrs_down.gif';
        } 
       */

}

function pause(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return;
        }
    }

function ProcessLink(sURL) {
		document.body.style.cursor = "wait";
		document.location = sURL;
}
