//*****************************************************************************
//
// USGS Zoom to Point Tool 
// Created By:  Chris Rusanowski
// Installation:
//   -Copy USGS_Zoom2Point_Tool.js into the javascript directory of the HTML Viewer
//   -Include the javascript/USGS_Zoom2Point_Tool.js in MapFrame.htm 
//   -Add a button to the toolbar.htm that calls parent.MapFrame.zoomToPoint().
//   -Add a call to zoomToPoint_Add_XML in addCustomToMap2 in aimsCustom.js
//        EG: 	customString += zoomToPoint_Add_XML();
//   -Add a call to zoomToPoint_Clear in the clear tool, or clearSelection
//    function in aimsSelect.js.
//
//*****************************************************************************
// useZoomRegion determines if the toolbar should show this button or not.
var useZoom2Point = true;

//showPointLocation determines if the X,Y point entered should be marked on the map
//Zoom2Point_On determines if the X,Y marker is on or off.
var showPointLocation = true;
var Zoom2Point_On = false;

//Zoom2Point_X and Zoom2Point_Y hold the X, and Y values for display on the map
var Zoom2Point_X = "-97.5"
var Zoom2Point_Y = "43.5"
var Zoom2Point_Count = 0;

//Define color and size of marker
var Zoom2PointColor = "255,0,0";
var Zoom2PointSize = "8";
var Zoom2PointFontSize = "10";
var Zoom2PointLabel = true;

//*****************************************************************************
// Function zoomToPoint
// This function prompts the user for a point and then zooms to that point.
//*****************************************************************************
function zoom2Point() {
    Zoom2Point_On = false;
    var TempPointXArray = Zoom2Point_X.split(',');
    var TempPointYArray = Zoom2Point_Y.split(',');
    var searchString = prompt("Please Enter the X, Y (Longitude, Latitude) in decimal degrees of a point to zoom to:", TempPointXArray[TempPointXArray.length-1] + ", " + TempPointYArray[TempPointYArray.length-1]);
    if (searchString!=null) {
      var tempArray = searchString.split(",")
      if (tempArray.length==2) {
        Zoom2Point_On = true;
        if (Zoom2Point_Count==0) {
          Zoom2Point_X = tempArray[0];
          Zoom2Point_Y = tempArray[1];
        } else {
          Zoom2Point_X = Zoom2Point_X + "," + tempArray[0];
          Zoom2Point_Y = Zoom2Point_Y + "," + tempArray[1];
        }
        Zoom2Point_Count +=1;
        eval('zoom2Point2(' + searchString + ')');
      } else {
        alert('Unable to Parse input (' + searchString + ').  Please try again. [' + tempArray.length + ']');
      }
    }
}

//*****************************************************************************
// Function zoomToPoint_Add_XML
// This function creates the XML that will add the X,Y point to the map.
// A call to this function should be added to addCustomToMap2 in aimsCustom.js.
//  EG: 	customString += zoomToPoint_Add_XML();
//*****************************************************************************
function zoomToPoint_Add_XML() {
  var theString = "";
	if (showPointLocation) {
    if (Zoom2Point_On) {
      var TempPointXArray = Zoom2Point_X.split(',');
      var TempPointYArray = Zoom2Point_Y.split(',');
      for (var i=0;i<Zoom2Point_Count;i++) {
  		  // draw the point . . . also used to display any point with a label on map
  		  theString += '<LAYER type="acetate" name="Zoom2Point">\n';
  		  theString += '<OBJECT units="database">\n<POINT coords="' + TempPointXArray[i] + ' ' + TempPointYArray[i] + '">\n';
  		  theString += '<SIMPLEMARKERSYMBOL  type="cross"  color="' + Zoom2PointColor + '" width="' + Zoom2PointSize +'" overlap="false" />\n</POINT></OBJECT>\n';
  		  if (Zoom2PointLabel) {
    			theString += '<OBJECT units="database">\n<TEXT coords="' + TempPointXArray[i] + ' ' + TempPointYArray[i] + '" label="' + TempPointXArray[i] + ', ' + TempPointYArray[i] + '">\n';
    			theString += '<TEXTMARKERSYMBOL fontcolor="' + Zoom2PointColor + '" fontsize="' + Zoom2PointFontSize + '" interval="2" halignment="right" valignment="top" overlap="false" /></TEXT></OBJECT>\n';
    		}
    		theString += '</LAYER>\n';
      }
    }
	}
  return theString;
}

//*****************************************************************************
// Function zoomToPoint_Clear
// This function sets Zoom2Point_On to false so the marker is not shown.
// A call to this function should be added to the clear tool, or the 
// clearSelection function in aimsSelect.
//*****************************************************************************
function zoomToPoint_Clear() {
  Zoom2Point_On = false;
  Zoom2Point_Count = 0;
  
  //Also clear the Measure information - so we do not need to call clearSelection...
	clickCount=0;
	totalMeasure=0;
	currentMeasure=0;
  refreshMap();
}

//*****************************************************************************
// Function zoom2Point2
// This function replaces the zoomToPoint tool that is in aimsMap.js.  This one
// will zoom in to the selectPointMargin if they are currently zoomed out 
// beyond that point, but if they are already zoomed in, then it will stay
// at the current scale, but recenter.
//*****************************************************************************
function zoom2Point2(xIn, yIn) {
	var mWMargin = 0;
	var mHMargin = 0;
	mWMargin = Math.abs(limitRight-limitLeft) * selectPointMargin;
	mHMargin = Math.abs(limitTop-limitBottom) * selectPointMargin;
  if ( (mWMargin > Math.abs(eRight - eLeft)) || (mHMargin > Math.abs(eTop - eBottom)) ) {
    mWMargin = Math.abs(eRight - eLeft)/2;
    mHMargin = Math.abs(eTop - eBottom)/2;
  }
	saveLastExtent();
	eLeft = xIn - mWMargin;
	eRight = xIn + mWMargin;
	eTop = yIn + mHMargin;
	eBottom = yIn - mHMargin;
	sendMapXML();
}