/*
 * $Id: aimsCustom.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:
 *  - Added custom tools: "Driving Times" and "Re-center Map"
 *  - Added dynamic map layers to show driving time points and routes.
 *  - During "identify," suppress requests for layers with no relevant features.
 */

// aimsCustom.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js, aimsNavigation.js,
*/

// global variables
	aimsCustomPresent=true;
	// change these to send XML response to custom function.
	// use numbers >= 1000 and match in useCustomFunction()
	// defaults are defined in aimsXML.js and use standard functions
	
	// xml response mode for selection
	selectXMLMode = 6;
	// xml response mode for identify
	identifyXMLMode = 7;
	// xml response mode for query
	queryXMLMode = 8;
	// xml response mode for find
	findXMLMode = 14;
	// xml response mode hyperlink
	hyperlinkXMLMode = 15;
	
	// LH - Variable added for user defined refresh rate - default is 3 min.
	var refreshRate = 3;


function customVisible(layer) {
	return true;
}

function writeCustomLayersXML() {
	var theString = '';
	theString += writeTravelTimeXML();
	return theString;
}


// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
	if (toolMode == 1001) {
		recenter(e);
		return false;
	}
	else if (toolMode == 1002) {
		travelTimeClick(e);
		return false;
	}
}

function endTool(oldToolMode) {
	if (oldToolMode == 1002 && toolMode != 1002)
		stopNodeTool();
}

// re-center map
function recenter(e) {
	getImageXY(e);	
	getMapXY(mouseX,mouseY);
	saveLastExtent();

	eLeft = mapX - xHalf;
	eRight = mapX + xHalf;
	eTop = mapY + yHalf;
	eBottom = mapY - yHalf;

	sendMapXML();
}

/******* TRAVEL TIME CUSTOM TOOL ********/

var selectedOriginNode = -1;
var selectedDestinationNode = -1;
var travelTimeRoutes = new Array();
var selectedTravelTimeRoute = 1;

travelTimeRouteColor = '0,255,255';

// called when the traveltimes tool is selected
function startNodeTool() {
	if (selectedDestinationNode != -1)
		selectedOriginNode = -1;
	selectedDestinationNode = -1;
	travelTimeRoutes = new Array();
	selectedTravelTimeRoute = 1;
	if (useTextFrame) {
		var thePage = "traffic_map_frameTravelTimes1.asp";
		if (selectedOriginNode != -1)
			 thePage = "traffic_map_frameTravelTimes2.asp?origin=" + selectedOriginNode;
		parent.TextFrame.document.location= appDir + thePage;
	}
	if (selectedOriginNode == -1)
		showTripNodesLayer(true);
}

function stopNodeTool() {
}

function cancelNodeTool() {
	selectedOriginNode = -1;
	selectedDestinationNode = -1;
	travelTimeRoutes = new Array();
	showTripNodesLayer(false);

	if (useTextFrame)
		parent.TextFrame.document.location= appDir + "traffic_map_frameInfo.asp";

	if (toolMode == 1002) {
		parent.frameToolbar.setToolPic("Identify");
		clickFunction("identifyall");
	}
	return false;
}

function showTripNodesLayer(visible) {
	LayerVisible[findTripNodesLayer()] = visible;
	refreshImage();
}

function selectTripNodesLayer() {
	ActiveLayerIndex = findTripNodesLayer();
	if (ActiveLayerIndex >= 0) {
		ActiveLayer=LayerID[ActiveLayerIndex];
		ActiveLayerType=LayerType[ActiveLayerIndex];
	}
}

function findTripNodesLayer() {
	for (i in LayerName) {
		if (LayerName[i] == "TripNodes")
			return i;
	}
	return -1;
}

function travelTimeClick(e) {
	selectTripNodesLayer();
	canSelectInvisible = true;
	if (useTextFrame) parent.TextFrame.document.location= appDir + "text_loading.htm";
	identify(e);
}

function tripNodeSelected(id) {
	if (selectedOriginNode == -1) {
		selectedOriginNode = id;
		showTripNodesLayer(false);
		if (useTextFrame) parent.TextFrame.document.location= appDir + "traffic_map_frameTravelTimes2.asp?origin=" + selectedOriginNode;
	}
	else if (selectedDestinationNode == -1) {
		selectedDestinationNode = id;
		if (useTextFrame) parent.TextFrame.document.location= appDir
			+ "traffic_map_frameTravelTimes3.asp?route=" + selectedTravelTimeRoute + "&origin=" + selectedOriginNode + "&destination=" + selectedDestinationNode;
		clickFunction('identifyall');
		parent.frameToolbar.setToolPic('Identify');
	}
}

function clearTravelTimeRoutes() {
	travelTimeRoutes = new Array();
}

function addTravelTimeRoute(routeID, linkList) {
	travelTimeRoutes[parseInt(routeID)-1] = linkList;
}

function selectTravelTimeRoute(i) {
	selectedTravelTimeRoute = i;
	refreshImage();
}

function writeTravelTimeXML() {
	var theString = '';
	if (selectedOriginNode >= 0) {
		if (selectedDestinationNode == -1) {
			theString += '<LAYER type="featureclass" name="DestinationNodes" visible = "true">';
			theString += '   <DATASET fromlayer="25" />';
			theString += '   <SPATIALQUERY jointables="GlimpseDB.dbo.vwTrip" where="GlimpseDB.dbo.vwTrip.StartingNode = ' + selectedOriginNode + ' AND SDE.SDE.GHS_TRIPNODES.NodeID = GlimpseDB.dbo.vwTrip.EndingNode" />';
			theString += '   <SIMPLERENDERER>';
			theString += '      <SIMPLEMARKERSYMBOL type="circle" color="255,0,192" outline="0,0,0" width="12" />';
			theString += '   </SIMPLERENDERER>';
			theString += '</LAYER>';
		}
		else {
			theString += '<LAYER type="featureclass" name="DestinationNodes" visible = "true">';
			theString += '   <DATASET fromlayer="25" />';
			theString += '   <SPATIALQUERY where="SDE.SDE.GHS_TRIPNODES.NodeID = ' + selectedDestinationNode + '" />';
			theString += '   <SIMPLERENDERER>';
			theString += '      <SIMPLEMARKERSYMBOL type="circle" color="255,0,192" outline="0,0,0" width="12" />';
			theString += '   </SIMPLERENDERER>';
			theString += '</LAYER>';
		}
		theString += '<LAYER type="featureclass" name="OriginNodes" visible = "true">';
		theString += '   <DATASET fromlayer="25" />';
		theString += '   <SPATIALQUERY where="SDE.SDE.GHS_TRIPNODES.NodeID = ' + selectedOriginNode + '" />';
		theString += '   <SIMPLERENDERER>';
		theString += '      <SIMPLEMARKERSYMBOL type="circle" color="204,255,255" outline="0,0,0" width="16" />';
		theString += '   </SIMPLERENDERER>';
		theString += '</LAYER>';
	}
	if (selectedTravelTimeRoute <= travelTimeRoutes.length) {
		var i = selectedTravelTimeRoute;
		theString += writeRouteLayerXML(i, 19, '02', 0,        0.000445);
		theString += writeRouteLayerXML(i, 17, '03', 0.000445, 0.000900);
		theString += writeRouteLayerXML(i, 15, '05', 0.000900, 0.001400);
		theString += writeRouteLayerXML(i, 13, '07', 0.001400, 0);
	}
	return theString;
}

function writeRouteLayerXML(routeNumber, fromLayer, linkbufTable, minscale, maxscale) {
	var theString = '';
	
	theString += '<LAYER type="featureclass" name="Route' + routeNumber + '"';
	if (minscale > 0)
		theString += ' minscale="' + minscale + '"';
	if (maxscale > 0)
		theString += ' maxscale="' + maxscale + '"';
	theString += '>';
	theString += '<DATASET fromlayer = "' + fromLayer + '" />';
	theString += '<SPATIALQUERY where="SDE.SDE.GHS_LINKBUF' + linkbufTable + '.LinkID IN (';
	theString +=                travelTimeRoutes[routeNumber-1] + ')" />';
	theString += '<SIMPLERENDERER>';
	theString += '	<SIMPLEPOLYGONSYMBOL fillcolor="' + travelTimeRouteColor + '" boundary="false" />';
	theString += '</SIMPLERENDERER>';
	theString += '</LAYER>';
	
	theString += '<LAYER type="featureclass" name="LinkAuxDynamic">';
	theString += '<DATASET fromlayer = "125" />';
	theString += '<SPATIALQUERY where="SDE.SDE.GHS_LINKAUX.LinkID IN (';
	theString +=                travelTimeRoutes[routeNumber-1] + ')" />';
	theString += '<SIMPLERENDERER>';
	theString += '	<SIMPLELINESYMBOL color="' + travelTimeRouteColor + '" width="4" captype="round" />';
	theString += '</SIMPLERENDERER>';
	
	theString += '</LAYER>';
	return theString;
}

/*****************************************/

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		// insert code here
	} else if (XMLMode==1002) {
		// insert code here
	} else {
		alert(msgList[55] + XMLMode + msgList[56]);
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";
	
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
	var customString = "";
	return customString;
}

// add custom stuff to Map XML request. . . on top of everything
function addCustomToMap4(){
	var customString = "";
	return customString;
}

// extract layers to download
function extractIt() {
	hideLayer("measureBox");
	alert(msgList[51]);
}

function suppressIdentifyLayer(name) {
	re = /^(Streets|Counties|Cities|((Hwy|Water|CityNames|Edges|HighwayMask).*))$/;
	return re.test(name);
}
