// aimsClick.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsNavigation.js
*/

var modeBlurb = "Zoom In";

var modeList = new Array();
modeList[0] = "Zoom In";
modeList[1] = "Zoom Out";
modeList[2] = "Pan";
modeList[3] = "Identify";
modeList[4] = "ID Visible Features";
modeList[5] = "Select by Point";
modeList[8] = "Select by Line/Polygon";

var onOVArea = false;

// 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 zleft=0;
var zright=0;
var ztop=0;
var zbottom=0;

// variables for interactive clicks
var clickCount = 0;
var	clickPointX = new Array();
var clickPointY = new Array();
var clickMeasure = new Array();
	// type - 1=Measure; 2=SelectLine ; 3=SelectPolygon
var clickType = 1;

var shapeSelectBuffer = false;

var panning=false;
var zooming=false;
var selectBox=false;
//var blankImage = "images/map.gif";

var leftButton =1;
var rightButton = 2;
if (isNav) {
	leftButton = 1;
	rightButton = 3;
}

//keep track of currently selected tool, and display it to user
// set the imsMap cursor tool
function clickFunction (toolName) {
	if (hasLayer("measureBox"))
		hideLayer("measureBox");
	switch(toolName) {
	// Zooming functions
	case "zoomin":
		// zoom in mode
		toolMode = 1;
		panning=false;
		selectBox=false;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[0];
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break
	case "zoomout":
		// zoom out mode
		toolMode = 2;
		panning=false;
		selectBox=false;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[1];
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break
	case "zoomlast":
		zoomBack();
		panning=false;
		zooming=false;
		selectBox=false;
		break

	case "fullextent":
		fullExtent();
		break
		
	// Pan functions
	case "pan":
		// pan mode
		toolMode = 3;
		
		zooming=false;
		selectBox=false;
		if (isIE)	{
			document.all.theTop.style.cursor = "move";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[2];
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break

	case "identifyall":
		// identify drill mode
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		toolMode = 5;
		if (isIE)	{
			document.all.theTop.style.cursor = "hand";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[4]; // identify visible features
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break
		
	case "dbidentify":
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		//BB: bypassing aimsIdentify.js
		toolMode = 40;
		if (isIE)	{
			document.all.theTop.style.cursor = "hand";
			theCursor = document.all.theTop.style.cursor;
		}
	
		modeBlurb = modeList[3];
		
		if (useTextFrame) {
			if (parent.QueryFrame != null) {
				parent.QueryFrame.document.location = "../../menu.asp"; //appDir + "menu.asp";
			}
			parent.TextFrame.document.location= appDir + "blank.htm";
		}
		break
		
	case "dbselect": 
		panning=false;
		zooming=false;
		shapeSelectBuffer = false;
		toolMode = 11;
		clickCount=0;
		//resetClick();
		if (isIE)	{
			document.all.theTop.style.cursor = "hand";
			theCursor = document.all.theTop.style.cursor;
		}
	
		modeBlurb = modeList[5];
		if (useTextFrame) {
			parent.QueryFrame.document.location = "../../menu.asp"; //appDir + "menu.asp";
			parent.TextFrame.document.location= appDir + "blank.htm";
		}
		break
		
	case "selectshape":
		panning=false;
		zooming=false;

		toolMode = 16;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		// clear out any existing APNs in buffer highlight list
		parent.PostFrame.document.forms[0].BuffIDs.value = "";
	
		//if (useTextFrame) {
			parent.QueryFrame.document.location= appDir + "../../select.htm";
			parent.TextFrame.document.location = appDir + "../../select_info.htm";
		//} else {
		//	Win1 = open("select.htm","QueryWindow","width=575,height=150,scrollbars=yes,resizable=yes");
		//}
		modeBlurb = modeList[8];
		break

	case "clearHighlights":
		 clearSelection();
		 break
		 		 
	default:
		alert( "Function not enabled.");
	}
	modeName=modeBlurb;
	if (useModeFrame) {
		parent.ModeFrame.document.location= appDir + "ModeFrame.htm";
	}
}

// check for mouseup
function chkMouseUp(e) { 
	if ((toolMode == 1) && (zooming)) {
			stopZoomBox(e);
		
	}
	if ((toolMode == 2) && (zooming)) {
			stopZoomOutBox(e);
	}
	if ((toolMode == 3) && (panning)) {
			stopPan(e);

	}
	if ((toolMode == 10) && (selectBox)) {
			stopSelectBox(e);
	}
		
	return false;
	
}

// perform appropriate action with mapTool
function mapTool (e) {
	var theButton= 0;
	// get the button pushed... if right, ignore... let browser do the popup... it will anyway
	if (isNav) {
		theButton = e.which;
	} else {
		theButton =window.event.button;
	}	
	if (theButton==leftButton) {
		getImageXY(e);
		if ((mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
			//if ((!isNav) || (!is5up)) {
			if ((hasOVMap) && (ovIsVisible) && (mouseX<i2Width+ovBoxSize) && (mouseY<i2Height) && (ovMapIsLayer)) {
					ovMapClick(mouseX,mouseY);
					window.status = "On OV Map Area";
			} else {
				//alert(mouseX + "," + mouseY);
				
				switch(toolMode) {
				case 1:
						startZoomBox(e);
						return false;
						break
					
				case 2:
						startZoomOutBox(e);
						return false;
						break
				case 3:
						startPan(e);
						return false;
						break

				case 5:
					// identify all
					sendIDSelect(e, "IdentifyAll");
					break
				
				case 10:
					//select(e);
						startSelectBox(e);
						return false;
						break
				case 11:
					//select point
					//if (checkIfActiveLayerAvailable()) {
						sendIDSelect(e, "BufferPoint");
					//}
					break
				case 16:
					//select shape
					//if (checkIfActiveLayerAvailable()) {
						clickType=2;
						addPointToLine(e);
						//if (useTextFrame) {
							if (parent.TextFrame.document.title!==modeList[8]) {
								parent.QueryFrame.document.location= appDir + "../../select.htm";
							}
						//}
					//}
					break

				case 40:
					// db identify features for database
					sendIDSelect(e, "Identify");
					break
				default:
					if (toolMode>=1000) {
						customMapTool(e);
					}
				}
			}
		}
	}
}


