// 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
*/


function viewMisc(url)
{
	var w = (screen.width)*.70;
	var h = (screen.height)*.70;
	//var winll = (screen.width-w)/2;
	//var wintt = (screen.height-h)/2;
	var setting  ='height='+h+',';
	setting +='width='+w+',';
	setting +='top=20,';
	setting +='left=20,';
	setting +='scrollbars=yes,';
	setting +='resizable=yes,';
	setting +='status=no,';
	setting +='menubar=no,';
	setting +='toolbar=no,';
	setting +='location=no';
	winnew = window.open(url,"viewMisc",setting);
	if (winnew != null) {
		winnew.focus();
	}
}


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";
modeList[9] = "ID Active Layer";
modeList[12] = "Measure"

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 updateLegend;
var legendPath = "";
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 "search":
		//single data frame
		parent.QueryFrame.document.location = "locate.htm";
		break
	
//	case "bufferform":
//		//single data frame
//		//parent.TextFrame.document.location = "viewer/lac/bottom.htm";
//		parent.QueryFrame.document.location = "../../viewer/lac/bottom.htm";
//		break
		
	case "legend":
		//alert("mapscalkefactor: " + mapScaleFactor);
		var ur
		ur = "legend.asp?act=" + document.MapForm.actLayer.value + "&sFactor=" + mapScaleFactor;
		//alert("url: " + ur);
		//single data frame
		//parent.TextFrame.document.location = ur;
		parent.QueryFrame.document.location = ur;
		break
		
	case "find":
		////single data frame
		//parent.TextFrame.document.location = "menu.asp";
		parent.QueryFrame.document.location = "menu.asp";
		break
		
	case "fullextent":
		fullExtent();
		parent.QueryFrame.document.location = "menu.asp";
		break
		
	// Pan functions
	case "pan":
		// pan mode
		toolMode = 3;
		
		zooming=false;
		selectBox=false;
		if (isIE)	{
			document.all.theTop.style.cursor = "Hand";
			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;
		//single data frame
//		parent.TextFrame.document.location = "identifyall.htm";
		parent.QueryFrame.document.location = "identifyall.htm";
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[4]; // identify visible features
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break
	
	case "IdentifySingle":
		// identify drill mode
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		toolMode = 6;
		//single data frame
		//parent.TextFrame.document.location = "identify.htm";
	//	parent.QueryFrame.document.location = "identifypcl.htm";
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[9]; // identify visible features
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break

	case "Identify":
		// identify drill mode
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		toolMode = 7;
		//single data frame
		//parent.TextFrame.document.location = "identify.htm";
		//parent.QueryFrame.document.location = "identify.htm";
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[9]; // identify visible features
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break
	
	//add2 add complete case measure
	case "measure":
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		//single data frame
//		parent.TextFrame.document.location = "measure.htm";
		parent.QueryFrame.document.location = "measure.htm";
		if (clickCount>0) {
			if (totalMeasure==0) resetClick();
		}
		toolMode = 20;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[12];
		if (clickType==1) {
			//if (useTextFrame) parent.TextFrame.location= appDir + "measure.htm";
				showLayer("measureBox");
				updateMeasureBox();
		}
		showGeocode=false;
		break
	
	//ML1 add this case
	case "measureline":
		panning=false;
		zooming=false;
		selectBox=false;
		shapeSelectBuffer = false;
		parent.TextFrame.document.location = "measure.htm";
		//if (clickCount>0) {
		//	if (totalMeasure==0) resetClick();
		//}
		toolMode = 21;
		if (isIE)	{
			document.all.theTop.style.cursor = "crosshair";
			theCursor = document.all.theTop.style.cursor;
		}
		modeBlurb = modeList[12];
		/*if (clickType==1) {
			//if (useTextFrame) parent.TextFrame.location= appDir + "measure.htm";
				showLayer("measureBox");
				updateMeasureBox();
		}*/
		showGeocode=false;
		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";
			}
			//single data frame
			//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";
		}
		
		sendToASP("BufferAPN");
		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
		document.MapForm.BuffIDs.value = "";
	
		//if (useTextFrame) {
			//parent.QueryFrame.document.location= appDir + "select.htm";
			//single data frame
			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":
		 parent.QueryFrame.document.location= "clear.htm";
		 clearSelection();
		 break
		 
	case "viewHelpFAQ":
		 viewMisc('http://maps.assessor.lacounty.gov/mapping/help.asp')
		 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;
	
}

//add3 --- complete function updateMeasureBox
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 + " " + sUnitList[j].toLowerCase();
	theForm.theMeasSegment.value = currentMeasure + " " + sUnitList[j].toLowerCase();
	showLayer("measureBox");

}


// perform appropriate action with mapTool
function mapTool (e) {
	document.MapForm.ToolMode.value = toolMode;
	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("mapTool  toolMode: "+toolMode);
				
				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 6:
					// identify all
					sendIDSelect(e, "IdentifySingle");
					break
					
				case 7:
					// identify active
					sendIDSelect(e, "Identify");
					break
					
				case 10:
					//select(e);
						startSelectBox(e);
						return false;
						break
				case 11:
					//select point
					//if (checkIfActiveLayerAvailable()) {
						sendIDSelect(e, "BufferAPN");
					//}
					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 20: //add4
					// measure
					clickType=1;
					//clickAddPoint();
					addPointToLine(e);
					break
				
				//ML2 add this case
				case 21:
					// measure
					clickType=1;
					//clickAddPoint();
					addPointToLine(e);
					break

				case 40:
					// db identify features for database
					sendIDSelect(e, "Identify");
					break
				default:
					if (toolMode>=1000) {
						customMapTool(e);
					}
				}
			}
		}
	}
}

