/*
 * $Id: aimsClick.js,v 1.2 2008/05/30 18:43:29 mripplin Exp $
 *
 * +------+   Copyright 2008, ICx Transportation Group
 * | ICx  |                   All Rights Reserved
 * +------+----------------------------------------------------------
 * This file is property of ICx Transportation Group.  Any unauthorized use or
 * duplication of this file, or removal of the header, constitutes
 * theft of intellectual property.
 *
 * This file is based on the ESRI ArcIMS HTMLViewer source code.
 * It has been customized for the 511 Traffic web site.
 *
 * Modifications for the 511 Traffic site:
 *  - Removed unused code.
 *  - Added custom cursors for some tools.
 *  - Added custom "Driving Times" and "Center" tools.
 */

// 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);
	}
	
}

function setCursor(cursor, image) {
	if (document.all) {
			if (image && document.body.getAttributeNode) 
				document.all.theTop.style.cursor = 'url("'+image+'"), '+cursor;
			else
				document.all.theTop.style.cursor = cursor;
			theCursor = document.all.theTop.style.cursor;
	}
}


//keep track of currently selected tool, and display it to user
// set the imsMap cursor tool
function clickFunction (toolName) {
	var oldToolMode = toolMode;

	if (hasLayer("measureBox"))
		hideLayer("measureBox");
	switch(toolName) {
	// Zooming functions
	case "zoomin":
		// zoom in mode
		toolMode = 1;
		panning=false;
		selectBox=false;
		setCursor("crosshair", "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;
		setCursor("crosshair", "images/zoomout.cur");
		modeBlurb = modeList[1];
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		break;
	case "fullextent":
		fullExtent();
		break;
		
	// Custom function: re-center map
	case "center":
		toolMode = 1001;
		zooming=false;
		selectBox=false;
		setCursor("crosshair", "");
		modeBlurb = "";
		break;

	// Custom function: Travel-time tool
	case "traveltimes":
		toolMode = 1002;
		zooming=false;
		selectBox=false;
		setCursor("help", "");
		modeBlurb = "";
		startNodeTool();
		break;

	// Pan functions
	case "pan":
		// pan mode
		toolMode = 3;
		
		zooming=false;
		selectBox=false;
		setCursor("move", "images/hmove.cur");
		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 (canQuery) {
			setCursor("help", "");
			//modeBlurb = modeList[19]; // identify all
			modeBlurb = modeList[20]; // identify visible features
			//modeBlurb = modeList[3]; // identify
		} else {
			alert(msgList[46]);
		}
		//alert("Function Not Implemented");
		showGeocode=false;
		drawSelectBoundary=false;
		//if (useTextFrame) parent.TextFrame.document.location= appDir + "text.htm";
		hideLayer("measureBox");
		break;
		
	default:
		alert(msgList[51]);
	}
	endTool(oldToolMode);
	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+ovYOffset) && (ovMapIsLayer)) {
					//alert(mouseX + ", " + mouseY);
					ovMapClick(mouseX,mouseY-ovYOffset);
					
					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
					if (useTextFrame) parent.TextFrame.document.location= appDir + "text_loading.htm";
					identifyAll(e);
					break;
				default:
					if (toolMode>=1000) {
						customMapTool(e);
					}
				}
			}
		}
	}
}
