// 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 FindPlaceWindow = window;
var newHelpWin = window;
var PrintWin = window;
var ExtractWin = window;

// 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;

// variables for interactive clicks
var clickCount = 0;
var	clickPointX = new Array();
var clickPointY = new Array();
var clickPointMouseX = new Array();
var clickPointMouseY = new Array();
var clickMeasure = new Array();
	// type - 1=Measure; 2=SelectLine ; 3=SelectPolygon
var clickType = 1;

var zooming=false;

var leftButton =1;

/*  *****************************************************
*	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;
	clickPointMouseX[clickCount]=theX;
	clickPointMouseY[clickCount]=theY;
	clickCount += 1;
	totalMeasure = totalMeasure + currentMeasure;
		//var u = Math.pow(10,numDecimals);
		//if (totalMeasure!=0) totalMeasure = parseInt(totalMeasure*u+0.5)/u;

	clickMeasure[clickCount]=totalMeasure;
	updateMeasureLine();
}

// zero out all clicks in clickCount
function resetClick() {
	var c1 = clickCount;
	clickCount=0;
	clickPointX.length=1;
	clickPointY.length=1;
	clickPointMouseX.length=1;
	clickPointMouseY.length=1;
	currentMeasure=0;
	totalMeasure=0;
	lastTotMeasure=0;
	clickMeasure.length=1;
	updateMeasureLine();
	if (toolMode == MAPTOOL_MEASURE) updateMeasureBox();

}	

// keep track of currently selected tool, and display it to user
// set the imsMap cursor tool
function clickFunction (toolName) {

  //check the state of any popup windows
  checkPopUps();
	
	//alert(toolName);
	if (hasLayer("measureBox")) {
		hideLayer("measureBox");
		if (toolMode == MAPTOOL_MEASURE) {
			// previous tool was measure so clear everything to get ready for new tool
			dismissMeasureBox();
		}
	}

	switch(toolName) {

		case "zoomin":

			toolMode = MAPTOOL_ZOOMIN;

			if (isIE)	{
				document.all.theTop.style.cursor = "default";
				theCursor = document.all.theTop.style.cursor;
			}
			break;

		case "zoomout":

			toolMode = MAPTOOL_ZOOMOUT;

			if (isIE)	{
				document.all.theTop.style.cursor = "default";
				theCursor = document.all.theTop.style.cursor;
			}
			break;

		case "zoomlast":

			zoomBack();
			zooming=false;
			break;

		case "recenter":
			
			toolMode = MAPTOOL_RECENTER; 

			zooming = false;

			if (isIE)	{
				document.all.theTop.style.cursor = "move";
				theCursor = document.all.theTop.style.cursor;
			}
			break;

		case "fullextent":

			fullExtent();
			break;
		
		case "identify":

			zooming  = false;
			toolMode = MAPTOOL_IDENTIFY;
				
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			hideLayer("measureBox");
			break;

		case "elevation":
			zooming  = false;
			toolMode = MAPTOOL_ELEVATION;
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			hideLayer("measureBox");
			break;

		case "measure":
			zooming=false;
			if (clickCount>0) {
				if (totalMeasure==0) resetClick();
			}
			toolMode = MAPTOOL_MEASURE;
			if (isIE)	{
				document.all.theTop.style.cursor = "crosshair";
				theCursor = document.all.theTop.style.cursor;
			}
			if (clickType==1) {
					showLayer("measureBox");
					updateMeasureBox();
			}
			break;
					
		case "print":
			 openPrintInfo();
			 break;
			 			
		case "extract":
			ExtractWin = window.open("Extract.htm","ExtractWin",
				"top=" + popUpScrnCoordTop + 
				",left=" + popUpScrnCoordLeft + 
				",width=" + extractWinWth + 
				",height=" + extractWinHt + ",scrollbars=yes,resizable=yes");
			ExtractWin.focus();
			break;
			 
		case "legend":
			legendVisible = !legendVisible;
			if (legendVisible) {
				parent.TOCFrame.document.location=appDir + "Legend.htm";
			} 
			else { 
				parent.TOCFrame.document.location = appDir + "toc.htm";
			} 
			break;
			
		case "findPlace":
			FindPlaceWindow = window.open("FindPlace.htm","FindPlaceWindow",
				"top=" + (popUpScrnCoordTop-50) + 
				",left=" + popUpScrnCoordLeft + 
				",width=" + findPlcWinWth + 
				",height=" + findPlcWinHt +
				",toolbar=yes,scrollbars=yes,resizable=yes");
			FindPlaceWindow.focus();
			break;
			
		case "bookmark":
			BookMarkWindow = window.open("BookMark.htm","BookMarkWindow",
				"top=" + (popUpScrnCoordTop-50) + 
				",left="   + popUpScrnCoordLeft + 
				",width="  + bookmarkWinWth + 
				",height=" + bookmarkWinHt +
				",toolbar=yes,scrollbars=no,resizable=yes");
			BookMarkWindow.focus();
			break;
				
		case "refresh":
			if (!legendVisible) {
				requestNewMainMap();
			}
			break;
		
		case "help":

			newHelpWin = window.open("docs/help.htm","helpWin",
				"top=" + popUpScrnCoordTop + 
				",left=" + popUpScrnCoordLeft + 
				",width=" + helpWinWth + 
				",height=" + helpWinHt +			
				",scrollbars=no,resizable=yes");
			newHelpWin.focus();
			break;
		
    // scale bar variables
   	case "minimum":
   		zoomScale(MIN_SCALE_FACTOR);
      break;

   	case "min1":
      zoomScale(MIN1_SCALE_FACTOR);
      break;

   	case "min2":
      zoomScale(MIN2_SCALE_FACTOR);
      break;

   	case "mid":
      zoomScale(MID_SCALE_FACTOR);
      break;

   	case "max2":
      zoomScale(MAX2_SCALE_FACTOR);
      break;

   	case "max1":
      zoomScale(MAX1_SCALE_FACTOR);
      break;

   	case "maximum":
      fullExtent();
      break;
      
    case "zoomScale0":
      zoomToMaxExt();
      break;
    
    case "zoomScale1":
    case "zoomScale2":
    case "zoomScale3":
    case "zoomScale4":
    case "zoomScale5":
    case "zoomScale6":
      var idx = (toolName.slice(-1)) - 0;
      zoomToScale(scaleToolZoomScales[idx], curExt);
      break;
			 
   	case "zoomPartner":      
		 	toolMode = MAPTOOL_ZOOMPARTNER;
		 	break;
			 
		case "options":
			 openOptionsWindow();
			 break;

		default:
			alert(msgList[5] + ' : ' + toolName);
	}

}

function checkPopUps(){
	if (!PrintWin.closed){
	  PrintWin.focus();
	}
	
	if (!FindPlaceWindow.closed){
	 	FindPlaceWindow.focus();
	}
  if (!newHelpWin.closed){
	  newHelpWin.focus();
	}  
}

// check for mouseup
function chkMouseUp(e) { 
	if ((toolMode == MAPTOOL_ZOOMIN) && (zooming)) {
		stopZoomBox(e);
	}
	if ((toolMode == MAPTOOL_ZOOMOUT) && (zooming)) {
		stopZoomOutBox(e);
	}
	if (toolMode == MAPTOOL_ELEVATION) {
		processElevation(e);
	}
	if (toolMode == MAPTOOL_IDENTIFY) {
		// trap mouse click immediately, store in global vars
		identifyX = mouseX;
		identifyY = mouseY;
		identify(identifyX, identifyY);
	}
	
	
	// return false; Comment require for safari compatibility
}

// 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 ((hasOVMap) && (ovIsVisible) && (mouseX < ovImgWidth+ovBoxSize) && 
			    (mouseY < ovImgHeight) ) {
				ovMapClick(mouseX,mouseY);
				window.status = "On OV Map Area";
			} 
			else {
				switch(toolMode) {

				case MAPTOOL_ZOOMIN:
						startZoomBox(e);
						return false;
						break;
					
				case MAPTOOL_ZOOMOUT:
						startZoomOutBox(e);
						return false;
						break;

				case MAPTOOL_RECENTER:
						doRecenter(e);
						return false;
						break;

				case MAPTOOL_IDENTIFY:
					// done in chkMouseUp(e)
					break;
					
				case MAPTOOL_MEASURE:
					// measure
					clickType=1;
					clickAddPoint();
					break;

				case MAPTOOL_ZOOMPARTNER:	  
					saveLastExtent();
					zoomToPartnerByClick(e);
					break;
					
				case MAPTOOL_ELEVATION:
					// done in chkMouseUp(e)
					break;

				default:
					alert("Unimplemented toolMode: " + toolMode);

				} // end switch
			} // end if-else (hasOVMap)
		} // end if (MouseX>=0)
	} // end if (theButton == leftButton)
} // end maptool function

// update measureBox layer
function updateMeasureBox() {
	if (isNav4) {
		var theForm = document.layers["measureBox"].document.forms[0];
	} 
	else {
		var theForm = document.forms[0];
	}
	var u = Math.pow(10,numDecimals);
	var tMeas = 0;
	if (totalMeasure!=0) {
		tMeas = parseInt(totalMeasure*u+0.5)/u;
	}
	theForm.theMeasTotal.value = tMeas + " " + measureBoxUnits;
	theForm.theMeasSegment.value = currentMeasure + " " + measureBoxUnits;
	showLayer("measureBox");
}
