// 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
*/

aimsClickPresent=true;

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;

var totalMeasure=0;
var currentMeasure=0;
var lastTotMeasure=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;
}


/*  *****************************************************
*	Point click functions
* 	used by Measure and Select by Line/Polygon
*	*****************************************************
*/

// put a point at click and add to clickCount
function clickAddPoint() {
	var theX = mouseX;
	var theY = mouseY;
	getMapXY(theX,theY);
	clickPointX[clickCount]=mapX;
	clickPointY[clickCount]=mapY;
	clickCount += 1;
	selectCount=0;
	totalMeasure = totalMeasure + currentMeasure;
		//var u = Math.pow(10,numDecimals);
		//if (totalMeasure!=0) totalMeasure = parseInt(totalMeasure*u+0.5)/u;

	clickMeasure[clickCount]=totalMeasure;
	legendTemp=legendVisible;
	legendVisible=false;
	var theString = writeXML();
	var theNum = 99;
	sendToServer(imsURL,theString,theNum);

}

// zero out all clicks in clickCount
function resetClick() {
	var c1 = clickCount;
	clickCount=0;
	clickPointX.length=1;
	clickPointY.length=1;
	currentMeasure=0;
	totalMeasure=0;
	lastTotMeasure=0;
	clickMeasure.length=1;
	selectCount=0;
	
	legendTemp=legendVisible;
	legendVisible=false;
	var theString = writeXML();
	var theNum = 99;
		//showRetrieveMap();
	sendToServer(imsURL,theString,theNum);
		
	if (toolMode==20) updateMeasureBox();

}	

// remove last click from clickCount
function deleteClick() {
	var c1 = clickCount;
	clickCount=clickCount-1;
	selectCount=0;
	if (clickCount<0) clickCount=0;
	if (clickCount>0) {
		totalMeasure = clickMeasure[clickCount]
		clickPointX.length=clickCount;
		clickPointY.length=clickCount;
		clickMeasure.length=clickCount;
		
	} else {
		totalMeasure=0;
		clickMeasure[0]=0;
	}
	currentMeasure=0;
	if (c1>0) {
		legendTemp=legendVisible;
		legendVisible=false;
		var theString = writeXML();
		var theNum = 99;
		sendToServer(imsURL,theString,theNum);
	}
	
}


//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) {
	// Map tip function
	case "trailmaptips":
		toolMode = 2001;
		panning=false;
		selectBox=false;
		setCursor("theTop", "default");
		modeBlurb = "Trail Map tips"
		break;
	// Zooming functions
	case "zoomin":
		// zoom in mode
		toolMode = 1;
		panning=false;
		selectBox=false;
		if(isNav) setCursor("theTop", "crosshair");
		else setCursor("theTop", "url('" + websiteURL + "/images/zoomin.cur')");
		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(isNav) setCursor("theTop", "crosshair");
		else setCursor("theTop", "url('" + websiteURL + "/images/zoomout.cur')");
		modeBlurb = modeList[1];
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break;
	case "zoomlast":
		zoomBack();
		panning=false;
		zooming=false;
		selectBox=false;
		break;
	case "zoomactive":
		//alert(LayerExtent[ActiveLayerIndex]);
		var q = LayerExtent[ActiveLayerIndex].split("|");
		panning=false;
		zooming=false;
		selectBox=false;
		//zoomToEnvelope(parseFloat(q[0]),parseFloat(q[1]),parseFloat(q[2]),parseFloat(q[3]));
		
		var l = parseFloat(setDecimalString(q[0]));
		var b = parseFloat(setDecimalString(q[1]));
		var r = parseFloat(setDecimalString(q[2]));
		var t = parseFloat(setDecimalString(q[3]));
		var w = r-l;
		var h = t-b;
		// add a bit of a margin around the layer
		var wm = w * (5/100);
		var hm = h * (5/100);
		l = l - wm;
		r = r + wm;
		b = b - hm;
		t = t + hm;
		zoomToEnvelope(l,b,r,t);
		if(toolMode == 2001){
			setToolPic('Trail Maptips');
		}
		break;
	case "fullextent":
		fullExtent();
		break;
		
	// Pan functions
	case "pan":
		// pan mode
		toolMode = 3;
		
		zooming=false;
		selectBox=false;
		setCursor("theTop", "url('" + websiteURL + "/images/pan_hand.cur')");
		modeBlurb = modeList[2];
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break;

	// Measure-Unit function
	case "measure":
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		if (clickCount>0) {
			if (totalMeasure==0) resetClick();
		}
		toolMode = 20;
		setCursor("theTop", "crosshair");
		modeBlurb = modeList[12];
		if (clickType==1) {
			//if (useTextFrame) parent.TextFrame.location= appDir + "measure.htm";
				showLayer("measureBox");
				updateMeasureBox();
		}
		showGeocode=false;
		break;
		// Print function	
	case "print":
		 printIt();
		 break;	
	default:
		//alert(msgList[51]);
		break;
	}

	modeName=modeBlurb;
	if (useModeFrame) {
		writeModeFrame(modeBlurb);
	} else if ((drawFloatingMode) && (modeLayerOn)) {
		writeModeLayers(modeBlurb);
	} else if ((modeRefreshMap) && (drawModeOnMap)) {
		//var theString = writeXML();
		sendMapXML();
	}
}

// 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)) {
					//alert(mouseX + ", " + mouseY);
					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 4:
					identify(e);
					break;
					
				case 5:
					// identify all
					identifyAll(e);
					break;
					
				// custom modes
				///*
				case 6:
					// route - requires custom route routine - not in default
						// requires aimsRoute.js, aimsGeocode.js
					//routeClick = routeClick + 1;
					//if (routeClick > 2) routeClick = 2;
					//setRouteXY()
					//writeRoutePage();
					address(e);
					break;
				//*/
				case 7:
					// address - requires custom reverseGeocode routine - not in default
						// requires aimsRoute.js, aimsGeocode.js
					address(e);
					break;
				//*/
				case 10:
					//select(e);
						startSelectBox(e);
						return false;
						break;
				case 11:
					//select point
					if (checkIfActiveLayerAvailable()) {
						select(e);
					}
					break;
				case 12:
					//select line
					if (checkIfActiveLayerAvailable()) {
						clickType=2;
						clickAddPoint();
						if (useTextFrame) {
							if (parent.TextFrame.document.title!==modeList[60]) {
								parent.TextFrame.document.location= appDir + "selectline.htm";
							}
						}
					}
					break;
				case 13:
					//select polygon
					if (checkIfActiveLayerAvailable()) {
						clickType=3;
						clickAddPoint();
						if (useTextFrame) {
							if (parent.TextFrame.document.title!==modeList[7]) {
								parent.TextFrame.document.location= appDir + "selectpoly.htm";
							}
						}
					}
					break;
				case 15:
					// hyperlink
					hyperLink(e);
					break;
				case 16:
					//select shape
					if (checkIfActiveLayerAvailable()) {
						clickType=2;
						clickAddPoint();
						if (useTextFrame) {
							if (parent.TextFrame.document.title!==modeList[8]) {
								parent.TextFrame.document.location= appDir + "select.htm";
							}
						}
					}
					break;
					///* 
				case 17:
					//buffer shape - 
					if (checkIfActiveLayerAvailable()) {
						clickType=2;
						clickAddPoint();
						if (useTextFrame) {
							if (parent.TextFrame.document.title!=modeList[11]) {
								parent.TextFrame.document.location= appDir + "shapeBuffer.htm";
							}
						}
					}
					break;
					//*/
				case 20:
					// measure
					clickType=1;
					clickAddPoint();
					break;
				case 21:
					// shape
					clickType=4;
					clickAddPoint();
					break;
				case 30:
					// hyperlink
					hyperLinkAny(e);
					break;
				case 40:
					// db identify - requires custom db query - not in default
					if (aimsDBPresent) {
						matchDBLinkLayer(dbLinkLayer);
						dbIdentify(e);
					}
					break;
				default:
					if (toolMode>=1000) {
						customMapTool(e);
					}
				}
			}
		}
	}
}

// update measureBox layer
function updateMeasureBox() {
	if (isNav4) {
		var theForm = document.layers["measureBox"].document.forms[0];
	} else {
		//var theForm = document.measureBox.forms[0];
		var theForm = document.forms[0];
	}
	var j = 1;
	for (var i=0;i<sUnitList.length;i++) {
		if (ScaleBarUnits==sUnitList[i]) j=i;
	}
		var u = Math.pow(10,numDecimals);
		var tMeas = 0;
		if (totalMeasure!=0) tMeas = parseInt(totalMeasure*u+0.5)/u;
	theForm.theMeasTotal.value = tMeas + " " + unitList[j];
	theForm.theMeasSegment.value = currentMeasure + " " + unitList[j];
	showLayer("measureBox");

}

// set the cursor of the specified layer
function setCursor(divLayer, cursor) {
	var dObj = getLayer(divLayer);
	if (dObj!=null) {
		dObj.cursor = cursor;
		theCursor = dObj.cursor;
	}
}

