//************CONTROL REFERENCES FOR THE "ZOOM TO PREVIOUS"****************
var m_prevLayerList = "";
var m_prevURL = "";
var m_prevScale = "";
var m_prevMaxx = "";
var m_prevMaxy = "";
var m_prevMinx = "";
var m_prevMiny = "";
var m_prevIsArmed = false;

//VARIABLE FOR MAP CREATION
var layerList;

//add the elseif statements for the correct actions on clicking on submenu of drawing tool
//this statement made sure that the click on the submenu is not going to the map, which will 
//most likely bring up the loading image
var selecting = false;
var newSelectMinx;
var newSelectMiny;
var newSelectMaxx;
var newSelectMaxy;

//VARIABLES FOR IDENTIFY (ACTIVE LAYER)
var activeLayerID;
var activeLayerName;
var strQueryResponse;

var pointClickX;
var pointClickY;

function refreshMap() 
{
	showLayer("loadLayer");
	hideZoomBox();
	
		if ( state == "CLEAR" || state == "INIT") {
            setToolImage('zoomin');
            top.ajaxFrame.setState( "ZOOMIN" );
           
        } else if ( state == "ZOOMIN" || state == "ZOOMOUT" ) {

            if ( state == "ZOOMIN" ) {
                setToolImage('zoomin');
                top.ajaxFrame.setState( "ZOOMIN" );
            }
        } else if ( state == "PAN" || state == "LASTEXTENT" ) {
            if ( state == "LASTEXTENT" ) {
                setToolImage('zoomin');
                top.ajaxFrame.setState( "ZOOMIN" );
                handleLastExtent();
                return;  
            }
        } else if ( state == "ZOOMEXTENT" 
                    || state == "PANNE" 
                    || state == "PANSE" 
                    || state == "PANNW" 
                    || state == "PANSW" 
                    || state == "PANNORTH" 
                    || state == "PANSOUTH" 
                    || state == "PANEAST"   
                    || state == "PANWEST"
                    || state == "MAPREFRESH" ) {

			handleExtMapNav(state);
            setToolImage('zoomin');
            top.ajaxFrame.setState( "ZOOMIN" );
        } else if (state == "DRAWPOINT")
		{		
			
			//if (pointClickX == null && pointClickY == null)
			//{
				pointClickX = top.ajaxFrame.mapX.toString();
				pointClickY = top.ajaxFrame.mapY.toString();
			//}			
        } else if (state == "GEOCODEPOINT")
		{		
			//--Fixed by AEE
			//VALUES ARE ALREADY SET FROM OTHER FORM
		}
       
		layerList = "";
	

		var postString = "WIDTH=";
		postString += mapWidth;
		postString += "&HEIGHT=";
		postString += mapHeight;
		postString += "&MAXX=";
		postString += newMaxx;
		postString += "&MAXY=";
		postString += newMaxy;
		postString += "&MINX=";
		postString += newMinx;
		postString += "&MINY=";
		postString += newMiny;
		postString += "&LAYERLIST=";
		postString += layerList;
		if (pointClickX != null && pointClickY != null){
			postString += "&POINTCLICKX=";
			postString += pointClickX;
			postString += "&POINTCLICKY=";
			postString += pointClickY;
			
		}
		ajaxState = "MAP";
		
		top.ajaxFrame.initRequest(postString);
       
}

//*****************************************************
//*****************ORIGINAL FUNCTIONALITY**************
//*****************************************************
function extendedMapTool(e)
{
	if ( state == "IDENTIFY")
	{
		startSelectBox(e);
	}else if ( state == "DRAWPOINT")
	{
		getTrueMapXY(e.clientX, e.clientY);
		refreshMap();
	}
}
function extendedSetState(newstate)
{
	if (newstate == "IDENTIFY") {
        mapBoxAsZoom = true;
        setZoomBoxColor("#66cc00");
        setZoomBoxWidth(3);
    } 
}

// check for mouseup
function extendedChkMouseUp(e) { 
	if (selecting) {
		if (mouseX<0)
		 	mouseX = 0;
		if (mouseX>mapWidth)
			mouseX = mapWidth;
		if (mouseY<0)
			mouseY = 0;
		if (mouseY>mapHeight)
			mouseY = mapHeight;
		stopSelectBox(e);
	}
}

function extendedGetMouse(e)
{
	if (selecting)
	{
		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;
}

// start zoom in.... box displayed
function startSelectBox(e) {
	//alert("insz");
    getImageXY(e);	

    if (!allowRubberband) 
    { 
        stopSelectBox(e);
    } else 
    {	
        // keep it within the MapImage
		if ((mouseX<mapWidth) && (mouseY<mapHeight-bottomBorderHeight)) 
		{
            if (!selecting) 
            {
                x1 = mouseX;
				y1 = mouseY;
				x2 = x1 + 1;
				y2 = y1 + 1;
				selecting = 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 (selecting) 
           	{
               	stopSelectBox(e);
	        }
		}
    }   
    return false;	
}

// stop select box display... show identify
function stopSelectBox(e) {
    selecting=false;
    if ((zmaxx <zminx+2) && (zmaxy < zminy+2)) 
    {
        // if the select box is too small
		return false;
    } 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;
		newSelectMaxy = pixelY * theY + miny;
		newSelectMaxx = pixelX * zmaxx + minx;
		newSelectMinx = pixelX * zminx + minx;
		theY = mapHeight - zminy;
		pixelY = height / mapHeight;
		newSelectMiny = pixelY * theY +miny;
		hideZoomBox();
		queryAllLayers();
    }
    return true;
}
//*****************************************************
//*************END ORIGINAL FUNCTIONALITY**************
//*****************************************************





function queryAllLayers( ) {
	
	var layerList = getActiveLayerID();
	if (layerList == null){
		alert("Please activate a layer in the Table of Contents");
		return;
	}else{
		ajaxState = "QUERY";
		var postString = "MINX="+newSelectMinx.toString()+
						"&MINY="+newSelectMiny.toString()+
						"&MAXX="+newSelectMaxx.toString()+
						"&MAXY="+newSelectMaxy.toString()+
						"&CLICKX="+cx.toString()+
						"&CLICKY="+cy.toString()+
						"&LAYER="+layerList;
						  
		top.ajaxFrame.initRequest(postString);
    }              
     
}

//PARSES VALUES FROM THE XMLHTTPREQUEST RESPONSETEXT
function handleQueryUpdate(responseString)
{
	setQueryResponse(responseString);
	theDispWin = window.open("displayinfo.htm","displayinfo","height=300,resize=no,width=280,alwaysRaised=true,location=no,menubar=no,toolbar=no,<SPAN class=clsLiteral>titlebar=no");
	
}

//PARSES VALUES FROM THE XMLHTTPREQUEST RESPONSETEXT
function handleMapUpdate(responseString)
{
	m_prevURL = top.map.getImage();
	m_prevMaxx = maxx;
	m_prevMaxy = maxy;
	m_prevMinx = minx;
	m_prevMiny = miny;
	
    var mapArray = new Array();
   // alert(responseString);
    mapArray = responseString.split("|");
    mapURL = mapArray[0];
    if (mapURL == "NOFEATURES"){
    
		//NO MAPABLE FEATURES EXIST, CLEAR VARIABLES
		alert("no mapable features exist, please try again");
		top.tabFrame.setMapInProgress(false);		
    }else{
		//ELSE DISPLAY MAP
		maxx = mapArray[1] * 1;
		maxy = mapArray[2] * 1;
		minx = mapArray[3] * 1;
		miny = mapArray[4] * 1;
		layerList = mapArray[5];
		//SET VALUES, SWAP FRAMES
		top.setImage(mapURL);
		
    }  
}


//HANDLER FOR ZOOM TO LAST EXTENT
function handleLastExtent()
{
	maxx = m_prevMaxx;
	maxy = m_prevMaxy;
	minx = m_prevMinx;
	miny = m_prevMiny;
    top.setImage(m_prevURL);
}
function nullExtent()
{
	newMaxx = 0;
	newMaxy = 0;
	newMinx = 0;
	newMiny = 0;
}

//HANDLER FOR MAP NAV INCLUDING ZOOMEXTENT AND BORDER PANS
//THIS DOESNT WORK CURRENTLY
/*function handleExtMapNav(state)
{
	if (state == "ZOOMEXTENT"){
		newMaxx = 0;
		newMaxy = 0;
		newMinx = 0;
		newMiny = 0;
	}else{
		
	}
}*/
function handleExtMapNav(state)
{
	switch(state)
		{
		case "PANSOUTH": 
			newMaxy = newMaxy - ((newMaxy - newMiny) * .15);
			newMiny = newMiny - ((newMaxy - newMiny) * .15);
			break;
		case "PANNORTH": 
			newMaxy = newMaxy + ((newMaxy - newMiny) * .15);
			newMiny = newMiny + ((newMaxy - newMiny) * .15);
			break;
		case "PANEAST": 
			newMaxx = newMaxx + ((newMaxx - newMinx) * .15);
			newMinx = newMinx + ((newMaxx - newMinx) * .15);
			break;
		case "PANWEST": 
			newMaxx = newMaxx - ((newMaxx - newMinx) * .15);
			newMinx = newMinx - ((newMaxx - newMinx) * .15);
			break;
		case "PANNW":
			newMaxy = newMaxy + ((newMaxy - newMiny) * .07);
			newMiny = newMiny + ((newMaxy - newMiny) * .07);
			newMaxx = newMaxx - ((newMaxx - newMinx) * .07);
			newMinx = newMinx - ((newMaxx - newMinx) * .07);
			break;
		case "PANNE": 
			newMaxy = newMaxy + ((newMaxy - newMiny) * .07);
			newMiny = newMiny + ((newMaxy - newMiny) * .07);
			newMaxx = newMaxx + ((newMaxx - newMinx) * .07);
			newMinx = newMinx + ((newMaxx - newMinx) * .07);
			break;
		case "PANSE": 
			newMaxy = newMaxy - ((newMaxy - newMiny) * .07);
			newMiny = newMiny - ((newMaxy - newMiny) * .07);
			newMaxx = newMaxx + ((newMaxx - newMinx) * .07);
			newMinx = newMinx + ((newMaxx - newMinx) * .07);
			break;
		case "PANSW": 
			newMaxy = newMaxy - ((newMaxy - newMiny) * .07);
			newMiny = newMiny - ((newMaxy - newMiny) * .07);
			newMaxx = newMaxx - ((newMaxx - newMinx) * .07);
			newMinx = newMinx - ((newMaxx - newMinx) * .07);
			break;
		case "ZOOMEXTENT": 
			newMaxx = 0;
			newMaxy = 0;
			newMinx = 0;
			newMiny = 0;
			break;
		}
}
function setActiveLayerID(value)
{
	activeLayerID = value;
}
function getActiveLayerID()
{
	return activeLayerID;
}
function setActiveLayerName(value)
{
	activeLayerName = value;
}
function getActiveLayerName()
{
	return activeLayerName;
}

function setQueryResponse(value)
{
	strQueryResponse = value;
}
function getQueryResponse()
{
	return strQueryResponse;
}