//*****************************************************************************
//
// USGS National Map Interface Code additions 
// Created By:  Chris Rusanowski
// Installation:
//   -Copy USGS_NMP.js into the javascript directory of the HTML Viewer
//   -Include the javascript/USGS_NMP.js in MapFrame.htm 
//   -Add a var USGSUseNMAP=true; and ShowLabels=true to USGS001.js (if using 
//    the USGS Template) or to arcimsparams.js if using a standard viewer
//   -Add code to viewer.htm for differently aligned frames and pages
//   -Modify toolbar.htm to use NMP tools
//   -Copy buttons folder into sub-directory so NMP toolbar icons work
//   -Modify the top.htm to show correct header
//   -Modify the bottom frame (disclaimer.htm) to show correct footer
//   -If not using the USGS Template, then copy usgs_images into the directory
//    to get the correct banner and USGS logos...
//   -NMP template was designed to work with the USGS OGC background code
//    so you need to install that, or test and modify viewer to work without it
//
//*****************************************************************************
//USGS_NMP_Loaded is used to check if this is loaded before trying to make calls
//to this in the mapframe.htm.  This variable is defined in mapframe.htm to 
//keep error messages from showing if there is a problem retrieving this file.
USGS_NMP_Loaded=true;

//*****************************************************************************
// Function displayMapServicesInfo
// This function displays a window that shows information about the map services
// used in the current map display
//*****************************************************************************
function displayMapServicesInfo() {
  if (USGS_OGC_Loaded) {
    OGCLayerMetadata();
  } else {
    var WindowContent='';
    WindowContent += ('<ul>');
    WindowContent += ('Current Extent = ' + eLeft + ', ' + eBottom + ', ' + eRight + ', ' + eTop + '<br />');
    WindowContent += ('Map Image Size = ' + iWidth + ', ' + iHeight + '<br />');
    var CenterAdjust = Math.abs(eRight - eLeft);
    var ApproxScale = HaversineScale((eLeft + CenterAdjust), eBottom, (eLeft + CenterAdjust), eTop, iHeight, ScaleToolDPI);
    WindowContent += ('The current Vertical Map Scale (at center) is 1 inch = ' + ApproxScale + ' ');
    if ( MapUnits.toUpperCase() == "CENTIMETERS" ) {
      WindowContent += ('Centimeters');
    } else if ( MapUnits.toUpperCase() == "FEET" ) {
      WindowContent += ('Feet');
    } else if ( MapUnits.toUpperCase() == "KILOMETERS" ) {
      WindowContent += ('Kilometers');
    } else if ( MapUnits.toUpperCase() == "METERS" ) {
      WindowContent += ('Meters');
    } else if ( MapUnits.toUpperCase() == "MILES" ) {
      WindowContent += ('Miles');
    } else {
      WindowContent += ('Inches');
    }
    WindowContent += ('</ul>');
    WindowContent += ('<h2>Map Services Information:</h2><ul>');
    // get service name
    var mainSvc = getSvcName(parent.USGSMapService);
    // get host name
    var mainHost = getSvcHostName(parent.USGSMapService);
    WindowContent += ('Main Map Service: ' + mainSvc + ' Server: ' + mainHost + '<br />');
    if (parent.USGSUseOverview) {
      var ovSvc = getSvcName(parent.USGSOVService);
      var ovHost = getSvcHostName(parent.USGSOVService);
      WindowContent += ('Overview Map Service: ' + ovSvc + ' Server: ' + ovHost);
    }
    WindowContent += ('</ul>');
    WindowContent += ('<br /><br /><br />');
    WindowContent += ('Alternate Content URL: <a href="' + parent.USGSalternative + '" target="_blank">' + parent.USGSalternative + '</a><br />');
    Create_Pop_Up_Window('Current Map Information', '', WindowContent, 'MapInfo', 'width=500,height=500,scrollbars=yes,resizable=yes',true);
  }
}

//*****************************************************************************
// Function displayUserInstructions
// This function displays a window that explains the user tools in the toolbar
// and how to interact with the map interface.
//*****************************************************************************
function displayUserInstructions() {
	hideLayer("measureBox");
  var Win1 = open("User_Instructions.php","UserInstructionsWindow","width=575,height=300,scrollbars=yes,resizable=yes");
}

//*****************************************************************************
// Function displayMetadata
// This function displays a window that shows information about the data layers
// used in the map interface
//*****************************************************************************
function displayMetadata() {
	hideLayer("measureBox");
	var Win1 = open("docs/layers.htm","LayerMetadataWindow","width=575,height=300,scrollbars=yes,resizable=yes");
}

//*****************************************************************************
// Function AddUSGSLogoLayer
// This function creates a new layer with the USGS Logo
// Made to be called from the MapFrame.htm eg: "AddUSGSLogoLayer(5,mHeight-28);"
// Place the call to this before the "Glass" layer so it does not interrupt drawing the zoom box...
//*****************************************************************************
function AddUSGSLogoLayer(XPosition, YPosition) {
  if (parent.USGScopyright.length>0) {
    var content = '<img name="USGScopyright" src="' + parent.USGScopyright + '" width="83" height="23" hspace="0" vspace="0" border="0" alt="USGS Logo" />';
    createLayer("USGSLogo",XPosition,YPosition,83,23,true,content);
  }
}

//*****************************************************************************
// Function getSvcName
// Parse out the Name of the Service in the Request String
//*****************************************************************************
function getSvcName(str) {
  var svc;
  var svcIndex = str.indexOf("Name=");
  if (svcIndex == -1) {
    svc = "Not Available";
  } else {
    svc = str.substring(svcIndex+5);
  }
  return(svc);
}
  
//*****************************************************************************
// Function getSvcHostName
// Parse out the Host of the service in the Request String
//*****************************************************************************
function getSvcHostName(str) {
  var svcHost;
  var begIndex = str.indexOf(":");
  var endIndex = str.indexOf("/", begIndex+3);
  if ( (begIndex == -1) || (endIndex == -1) ) {
    svcHost = "Not Available";
  } else {
    svcHost = str.substring(begIndex+3, endIndex);
  }
  return(svcHost);
}
