// USGS_NATMAP_MapTools.js

nmMapToolsPresent = true;

var GNIS_NAMES_CLASS = "NAMES";
var GNIS_HYDRO_FLAG  = "-99999";

function zoomToPoint(xIn, yIn) {
	zoomToPointAtScale(xIn, yIn, DEFAULT_ZOOM_REL_SCALE);
}

// Update (global) map extent given a central point and deltas in the x and
// y directions from the zoom point.
function updateExtent(x, y, halfExtentX, halfExtentY) {
	saveLastExtent();
	var xFloat = parseFloat(x);
	var yFloat = parseFloat(y);
  reqExt.set(xFloat - halfExtentX, yFloat - halfExtentY, xFloat + halfExtentX, yFloat + halfExtentY);
}

// Updates global ClassVisible array. Used for updating
// request across scales in a single step.
function setClassVisible(className) {
  for (var i = 0; i < ClassName.length; i++) {
    if (ClassName[i] == className) {
      ClassVisible[i] = "true";
			return;
    }
  }
}

// Check if flag is set to use NHD layers for GNIS feature
// zoom.
function useGnisHydroLayers(s) {
	return (s == GNIS_HYDRO_FLAG ? true : false);
}

// Prepare NHD Layers for display. Currently NHD layers are 
// the only layers in the classes of interest, so we can
// just turn on the necessary layers. If other classes are
// added to "WATERBODIES" or "STREAM NETWORK" this function
// may need to be updated (has the side effect of turning on 
// other layers under the class). The "Stream Names" layer is currently 
// assigned to "STREAM NETWORK" in the catalog. There is a 
// "STREAM NAMES" category that it may eventually get re-assigned to.
function prepareNhdLayers(gnisLayerId) {
  for (var i = 0; i < GnisNhdClassNames.length; i++) {
		setClassVisible(GnisNhdClassNames[i]);
	}
}

// Set the classes and layers for GNIS feature zoom
function prepareGnisLayers(gnisLayerId) {
  var i = 0;
  var namesClassAlreadyVis = false;

  // Set class visibility. Note: this has the side effect
	// of turning on other layers under the Geographic Names|NAMES
	// class - eg. "BTS Names"
  for (i = 0; i < ClassName.length; i++) {
		if (ClassName[i] == GNIS_NAMES_CLASS) {
			if (ClassVisible[i]) {
				namesClassAlreadyVis = true;
      }
      ClassVisible[i] = "true";
      break;
  	}
  }

  // Set layers visibility
  if (namesClassAlreadyVis) {
    for (i = 0; i < WmsLayerId.length; i++) {
  	  // leave other layers alone; preserve their state
			if (WmsLayerId[i] == gnisLayerId) {
				WmsLayerVisible[i] = "true";
				break;
			}
		}
	}
	// Names class previously not visible. Select service
	// containing feature as visible, set all others to
	// not visible.
	else {
		AddVisibleLayers.push(gnisLayerId);
		for (idx in GnisIds) {
			if (GnisIds[idx] == gnisLayerId) {
				continue;
			}
			AddHiddenLayers.push(GnisIds[idx]);
		}
		// Set other layers under Geographic Names|CLASS
		// to not visible.
		for (idx in NamesClassHideIds) {
			AddHiddenLayers.push(NamesClassHideIds[idx]);
		}
	}
}

// Zoom to point, given lat-lon coords, relative 
// scale factor denominator and gnisLayerId. 
// 
// * for 1:12,000 scale use relScale arg of 12000
// * gnisLayerId is the catalog layer id for the GNIS layer to be
//   turned on.
function zoomToGnisLayerPoint(xIn, yIn, relScale, gnisLayerId) {
	// update extent
	handleRelScalePointZoom(xIn, yIn, relScale);
	// perform check here to see if we have a HYDRO feature type
	useGnisHydroLayers(gnisLayerId) ? prepareNhdLayers(gnisLayerId) 
																	: prepareGnisLayers(gnisLayerId); 
	tagIt(xIn,yIn);
	requestNewMainMap();
}


// Zoom to point, given lat-lon coords, relative 
// scale factor denominator. Turns on all GNIS map
// service layers, and other layers that happen
// to be under the Geographic Names|NAMES class.
// 
// * for 1:12,000 scale use relScale arg of 12000
function zoomToGnisPoint(xIn, yIn, relScale) {
	// update extent
	handleRelScalePointZoom(xIn, yIn, relScale);
	// update NAMES class
	setClassVisible(GNIS_NAMES_CLASS);
	if (tagAddress)
	   tagIt(xIn,yIn);
	requestNewMainMap();
}

// Recenter on GNIS feature and turn appropriate Gnis layer on
function doRecenterGnisLayer(x, y, gnisLayerId) {
	// perform check here to see if we have a HYDRO feature type
	useGnisHydroLayers(gnisLayerId) ? prepareNhdLayers(gnisLayerId) 
																	: prepareGnisLayers(gnisLayerId); 
	tagIt(x,y);																
	doRecenterXY(x, y);
}

// Recenter on GNIS feature
function doRecenterGnis(x, y) {
	setClassVisible(GNIS_NAMES_CLASS);
	doRecenterXY(x, y);
}

// Recenter on click event
function doRecenter(e) {
	// sets global vars mapX, mapY (absolute map coords)
	// from event
	getMapXY(mouseX,mouseY);
	doRecenterXY(mapX, mapY);
}

// Recenter on x,y point 
function doRecenterXY(x, y) {
	var xHalf = curExt.xSize /2;
	var yHalf = curExt.ySize /2;
	saveLastExtent();
	updateExtent(x, y, xHalf, yHalf);
	requestNewMainMap();
}

function zeroMeasureBox() {
	clickCount=0;
	clickPointX.length=0;
	clickPointY.length=0;
	clickPointMouseX.length=0;
	clickPointMouseY.length=0;
	clickMeasure.length=0;
	currentMeasure=0;
	totalMeasure=0;
	if (toolMode == MAPTOOL_MEASURE) updateMeasureBox();
}

function dismissMeasureBox() {
	zeroMeasureBox();
	for (var i = 0; i < clickPointLayer.length; i++) {
		hideLayer(clickPointLayer[i]);
	}
	hideLayer("measureBox");
}

function dismissTag() {
	tagX=-1;
	tagY=-1;
	hideLayer("thePlaceLayer");
}

function revertToDefaultTool () {
	clickFunction('zoomin');
	parent.ToolFrame.setToolPic('Zoom In');
}

/**
 * draws measure line segement on map if it exists
 * clears the line segement if no longer pertinent
 */
function updateMeasureLine () {
	if (clickPointToUse >= clickPointLayer.length) {
		// wrap back to the beginging
		clickPointToUse = 0;
	}
	// the "radius" of the clickPoint even though its a square
	var r = parseInt(clickPointSize / 2);
	clipLayer(clickPointLayer[clickPointToUse],(clickPointMouseX[clickCount - 1] - r) ,(clickPointMouseY[clickCount - 1] - r), (clickPointMouseX[clickCount - 1] + r), (clickPointMouseY[clickCount -1] + r));
	showLayer(clickPointLayer[clickPointToUse]);
	clickPointToUse += 1;
}

// array for holding name of DHTML layers for showing the clicks from the measure tool
var clickPointLayer = new Array();
// which clickPoint in the array is the one available to use
var clickPointToUse = 0;

function processElevation (e) {
	getMapXY(mouseX, mouseY);
	window.open(elevLookupStub + 'X=' + mapX + '&Y=' + mapY + '&RETURNURL=' 
		+ escape(elevLookupResultUrl),
		'Elevation_results', 
		'top=' + popUpScrnCoordTop + ',left=' + popUpScrnCoordLeft +
		',width=' + elevWinWth + ',height=' + elevWinHt + 
		',scrollbars=yes,resizable=yes');
	return false;
}

function openUserInstructions(theHash) {
	var theUrl = webAppRoot + "/userInstructions.jsp?mode=brief";
	if (theHash != null && theHash != "") {
		theUrl += '#' + theHash;
	}
	InstrWin=window.open(theUrl , "InstrWin","toolbar=no,location=no,directories=no,status=no,resizable=yes,scrollbars=yes,width=" + instrWinWth + ",height=" + instrWinHt);
	InstrWin.focus();
}

function zoomToMaxExt() {
  window.scrollTo(0,0);
  saveLastExtent();
  reqExt.setFrom(maxExt);
  requestNewMainMap();
}
