//*****************************************************************************
//
// USGS Map Interface Visual Identity code 
// Created By:  Chris Rusanowski
// Installation:
//   -Copy USGS_Vis_ID.js into the javascript directory of the HTML Viewer
//   -Include the javascript/USGS_Vis_ID.js in MapFrame.htm AND/OR any page that 
//    needs to include the Vis-ID, style sheet, footer, etc. with the code below:
//    <script type="text/javascript" language="JavaScript" src="javascript/USGS_Vis_ID.js"></script>
//   -Call document.writeln(Vis_ID_Style(parent.USGSBackgroundColor,parent.USGSTextColor,parent.USGSTextColor, parent.USGSBanner, parent.USGSBackgroundImage));
//    in the HEAD section of a HTML document to write out a style block
//   -Call document.writeln(Vis_ID_Header(parent.USGSTitle, parent.USGSBanner, parent.USGSalternative, parent.USGSUseNMAP));
//    Just below the BODY tag
//   -Call document.writeln(Vis_ID_Footer(parent.USGSLocalDepartmentURL,parent.USGSLocalDepartment,parent.USGSmaintainer,parent.USGSmdate,true));
//    to write out the footer
//   -NOTE:  All colors are passed in as strings similar to '#FFFFFF' and URL's may be relative or Full URL's
//   -NOTE:  Any parameters left out or blank will be set to default values.
//
//  Example:  A blank XHTML page with banner and footer using only default values:
//
//  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
//      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
//  
//  <html xmlns="http://www.w3.org/1999/xhtml">
//    <head>
//      <meta http-equiv="Pragma" content="no-cache" />
//      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
//  
//      <title>Map Interface Background</title>
//      <!-- USGS Visual Identity Functions -->
//      <script type="text/javascript" language="JavaScript" src="/JS_Library/ArcIMS_Template/USGS_Vis_ID.js"></script>
//      <script type="text/javascript" language="JavaScript">
//      //<![CDATA[
//          document.writeln(Vis_ID_Style());
//      //]]>
//      </script>
//    </head>
//    <body bgcolor="#FFFFFF">
//      <script type="text/javascript" language="JavaScript">
//      //<![CDATA[
//          document.writeln(Vis_ID_Header("Blank Example Page"));
//      //]]>
//      </script>
//      <script type="text/javascript" language="JavaScript">
//      //<![CDATA[
//          document.writeln(Vis_ID_Footer());
//      //]]>
//      </script>
//    </body>
//  </html>
//*****************************************************************************

//*****************************************************************************
// Function Vis_ID_Header
// This function will return a string that contains basic header display of the 
// USGS Logo and the page title.
//*****************************************************************************
function Vis_ID_Header(PageTitle,USGSBanner,PageAltLink,UseNMAP) {
  if (!Boolean(PageTitle)) { PageTitle=" "; }
  if (!Boolean(USGSBanner)) { USGSBanner="USGS_key_color_green"; }
  if (!Boolean(PageAltLink)) { PageAltLink=" "; }
  if (!Boolean(UseNMAP)) { UseNMAP=false; }
  var LocalString = '';
  //LocalString += ('<table border="0" bgcolor="' + rgb2hex(USGS_Banner_Color_RGB(USGSBanner)) + '" cellpadding="0" cellspacing="0" width="100%">');
  LocalString += ('<table border="0" bgcolor="' + rgb2hex(USGS_Banner_Color_RGB(USGSBanner)) + '" cellpadding="0" cellspacing="0">');
  LocalString += ('<tr><td>');
  LocalString += ('<map name="tnm"><area alt="Link to the USGS Home Page" href="http://www.usgs.gov" shape="rect" coords="5, 0, 89, 35">');
  LocalString += ('<area alt="The National Map is the product of a consortium of Federal, State, and local partners." href="http://nationalmap.usgs.gov" shape="rect" coords="106, 1, 305, 31">');
  LocalString += ('</map>');

  if (USGSBanner.indexOf("logo")>0) {
    LocalString += ("<img src=\"" + USGSBanner + "\" border=\"0\"  usemap='#tnm'/>");
  } else {
    LocalString += ("<img src=\"" + USGSBanner + "\" border=\"0\"  usemap='#tnm' />");
  }
  LocalString += ('</td></tr></table>');
  LocalString += ('<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td nowrap="nowrap">');
  if (PageTitle.length>2) {
    if (UseNMAP) {
      LocalString += ('<h2 class="title" style="width: 100%; background-image: url(usgs_images/nmap_logobar.gif);">');
    } else {
      LocalString += ('<h2 class="title">');
    }
    //LocalString += ('Geography - <b><i>The National Map</i></b></h2><h2 class="title">');
    LocalString += ('<b><i>The National Map</i></b> ');
    LocalString += (PageTitle);
    LocalString += ('</h2>');
  }
  if (PageAltLink.length>2) {
    LocalString += ('<a target="_top" href="' + PageAltLink + '" alt="Go to Alternative Content Page" >');
    LocalString += ('<img src="images/pixel.gif" border="0" width="1" height="1" alt="Link to Alternative Content Page at ' + PageAltLink + '"/>');
    LocalString += ('</a>');
  }
  LocalString += ('</td></tr></table>');
  return LocalString;
}

//*****************************************************************************
// Function Vis_ID_Style
// This function will return a string that has the basic styles used in all the
// pages for the Map Interface.  Writing this string out as part of the HEAD
// section for a page will give the page some basic colors and formatting.
//*****************************************************************************
function Vis_ID_Style(PageColor,TextColor,HoverColor,USGSBanner,BackgroundImage) {
  if (!Boolean(PageColor)) { PageColor="#ff0000"; }
  if (!Boolean(TextColor)) { TextColor="#000000"; }
  if (!Boolean(HoverColor)) { HoverColor="#00FF00"; }
  if (!Boolean(USGSBanner)) { USGSBanner="USGS_key_color_green"; }
  if (!Boolean(BackgroundImage)) { BackgroundImage=" "; }
  var LocalString = '';
  LocalString += ('<style type="text/css">');
  LocalString += ('/*<![CDATA[*/');
  //LocalString += ('  .title { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16pt; font-weight: bold; font-style: italic; }');
  LocalString += ('  body { font-family: Verdana, Arial, Helvetica, sans-serif; margin-left: 5px; margin-right: 5px; color: ' + TextColor + '; background-color: ' + PageColor + ';  font-size: 10pt; ');
  if (BackgroundImage.length>2) { LocalString += ('background-image: url(' + BackgroundImage + '); '); }
  LocalString += ('}');
  LocalString += ('  a { text-decoration: none; color: ' + TextColor + '; background-color: ' + PageColor + '; }');
  LocalString += ('  a:hover { text-decoration: underline; color: ' + HoverColor + '; background-color: ' + PageColor + '; }');
  LocalString += ('  a.visited { color: ' + TextColor + '; background-color: ' + PageColor + '; }');
  LocalString += ('  br { line-height: 50%; }');
  LocalString += ('  th {  background-color: ' + PageColor + '; }');
  LocalString += ('  td {  background-color: ' + PageColor + '; }');
  LocalString += ('  iframe { background-color: #FFFFFF; }');
  LocalString += ('  h2 { margin: 0px; border-top: #010455 solid thin; font-size: 14px; font-family: Verdana, Arial, Helvetica, sans-serif; }');
  LocalString += ('  .title_label { margin: 0px; border-top: #F7EADE solid thin; font-size: 14px; font-family: Verdana, Arial, Helvetica, sans-serif; border-bottom: #F7EADE solid thin; background-color: #663333; font-weight: bold; }');
  LocalString += ('  form { margin: 0px; }');
  LocalString += ('  address { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; }');
  LocalString += ('  hr {background-color: #FFFFFF; color: ' + rgb2hex(USGS_Banner_Color_RGB(USGSBanner)) + '; line-height: 4px; border-style: none; height: 4px; margin: 2px;}');
  LocalString += ('  .button { color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-SIZE: 10pt; text-decoration: none; background-color: #CCCCCC; border: outset; padding: 2px; }');
  LocalString += ('/*]]>*/');
  LocalString += ('</style>');
  return LocalString;
}

//*****************************************************************************
// Function Vis_ID_Footer
// This function will output a string containing the HTML formatted footer 
// used by the USGS Map Interface Template.  The variables used for the dynamic
// parts of the footer must be passed in.  ModifyURL tells this function 
// to modify the URL that is displayed to point to default.htm rather than the
// page that builds this string.
//*****************************************************************************
function Vis_ID_Footer(USGSLocalDepartmentURL,USGSLocalDepartment,USGSmaintainer,USGSmdate,ModifyURL) {
  if (!Boolean(USGSLocalDepartmentURL) || !Boolean(USGSLocalDepartment)) {
    USGSLocalDepartmentURL=" ";
    USGSLocalDepartment=" ";
  }
  if (!Boolean(USGSmaintainer)) { USGSmaintainer="webmapping@usgs.gov"; }
  if (!Boolean(USGSmdate)) { USGSmdate=document.lastModified; }
  if (!Boolean(ModifyURL)) { ModifyURL=true; }
  var LocalString = '';
  var tempStr = '';
  if ( ModifyURL ) {
    tempStr = 'http://' + location.host + location.pathname.substring(0, location.pathname.lastIndexOf("/")) + "/";
  } else {
    tempStr = 'http://' + location.host + location.pathname;
  }
  LocalString += ('  <small><address>');
  LocalString += ('    <a target="_top" href="http:\/\/www.doi.gov">U.S. Department of the Interior</a>');
  LocalString += ('    &nbsp;&nbsp;&nbsp || &nbsp;&nbsp;&nbsp;');
  LocalString += ('    <a target="_top" href="http:\/\/www.usgs.gov">U.S. Geological Survey</a>');
  if (USGSLocalDepartmentURL.length>2) {
      LocalString += ('    &nbsp;&nbsp;&nbsp || &nbsp;&nbsp;&nbsp;');
      LocalString += ('    <a target="_top" href="' + USGSLocalDepartmentURL + '">' + USGSLocalDepartment + '</a>');
  }
  LocalString += ('    <br />');
  LocalString += ('    URL: ' + tempStr + '&nbsp;&nbsp;&nbsp || &nbsp;&nbsp;&nbsp;');
  LocalString += ('    Maintainer: ');
  LocalString += ('    <a href="mailto:' + USGSmaintainer + '">');
  LocalString += (USGSmaintainer);
  LocalString += ('    </a>');
  LocalString += ('    &nbsp;&nbsp;&nbsp || &nbsp;&nbsp;&nbsp;');
  LocalString += ('    Modified: ' + USGSmdate);
  LocalString += ('  </address></small>');
  return LocalString;
}

//*****************************************************************************
// Function USGS_Banner_Color_RGB
// This looks at a string and searches for the USGS banner images filename to 
// try and determine the actual RGB color of that image.  This is used by the 
// Vis_ID_Header function to put a background behind the banner image using
// a table, so the it stretches the width of the screen.
// This is a copy of the function in USGS001.js
//*****************************************************************************
function USGS_Banner_Color_RGB(USGSBannerString) {
  if (!Boolean(USGSBannerString)) { USGSBannerString=" "; }
  var BannerRGB = "255,255,255";
  if (USGSBannerString.indexOf("key_color_green")>0) {
    BannerRGB = "0,102,51";
  } else if (USGSBannerString.indexOf("dark_blue_green")>0) {
    BannerRGB = "0,51,51";
  } else if (USGSBannerString.indexOf("blue_green")>0) {
    BannerRGB = "0,102,102";
  } else if (USGSBannerString.indexOf("blue_grey")>0) {
    BannerRGB = "153,204,204";
  } else if (USGSBannerString.indexOf("brown")>0) {
    BannerRGB = "153,51,0";
  } else if (USGSBannerString.indexOf("burgundy")>0) {
    BannerRGB = "153,0,51";
  } else if (USGSBannerString.indexOf("dark_blue")>0) {
    BannerRGB = "0,51,102";
  } else if (USGSBannerString.indexOf("dark_green")>0) {
    BannerRGB = "51,102,51";
  } else if (USGSBannerString.indexOf("dark_purple")>0) {
    BannerRGB = "51,0,102";
  } else if (USGSBannerString.indexOf("dark_red_violet")>0) {
    BannerRGB = "102,51,102";
  } else if (USGSBannerString.indexOf("green_grey")>0) {
    BannerRGB = "102,153,153";
  } else if (USGSBannerString.indexOf("light_blue")>0) {
    BannerRGB = "102,204,255";
  } else if (USGSBannerString.indexOf("light_grey")>0) {
    BannerRGB = "204,204,204";
  } else if (USGSBannerString.indexOf("purple")>0) {
    BannerRGB = "51,51,153";
  } else if (USGSBannerString.indexOf("red_violet")>0) {
    BannerRGB = "153,0,153";
  } else if (USGSBannerString.indexOf("red")>0) {
    BannerRGB = "255,0,51";
  } else if (USGSBannerString.indexOf("tan")>0) {
    BannerRGB = "204,153,102";
  } else if (USGSBannerString.indexOf("warm_grey")>0) {
    BannerRGB = "153,153,153";
  } else if (USGSBannerString.indexOf("yellow_orange")>0) {
    BannerRGB = "255,153,0";
  } else if (USGSBannerString.indexOf("yellow")>0) {
    BannerRGB = "255,204,0";
  } else if (USGSBannerString.indexOf("blueusgs")>0) {
    BannerRGB = "1,4,85";
  } else if (USGSBannerString.indexOf("blue")>0) {
    BannerRGB = "0,102,153";
  } else if (USGSBannerString.indexOf("NMAP")>0) {
    BannerRGB = "0,153,153";  
  } else {
    BannerRGB = "255,255,255";
  }
  return BannerRGB;
}

//*****************************************************************************
// Function rgb2hex
// This will parse a comma separated string of R,G,B values into a hexadecimal
// string for that color combination.
// This is a copy of the function in USGS001.js
//*****************************************************************************
function rgb2hex(rgb) {
  if (!Boolean(rgb)) { rgb="0,0,0"; }
  var comma1 = rgb.indexOf(",");
  var comma2 = rgb.lastIndexOf(",");
  var redStr = rgb.substring(0, comma1);
  var grnStr = rgb.substring(comma1 + 1, comma2);
  var bluStr = rgb.substring(comma2 + 1, rgb.length);
  var hex = "#" + dec2hex(redStr) + dec2hex(grnStr) + dec2hex(bluStr);
  return(hex);
}

//*****************************************************************************
// Function dec2hex
// Pass in an Integer variable and receive back a string of that variable
// in Hexadecimal.  Designed for converting 0-255 to Hex for rgb2hex()
// This is a copy of the function in USGS001.js
//*****************************************************************************
function dec2hex(dec) {
  if (!Boolean(dec)) { dec=0; }
  var digit1 = Math.floor(dec / 16);
  var digit2 = dec - (digit1 * 16);
  var hex = "";
  switch(digit1) {
      case 10:
          hex += "A";
          break;
      case 11:
          hex += "B";
          break;
      case 12:
          hex += "C";
          break;
      case 13:
          hex += "D";
          break;
      case 14:
          hex += "E";
          break;
      case 15:
          hex += "F";
          break;
      default:
          hex += digit1;
          break;
  }
  switch(digit2) {
      case 10:
          hex += "A";
          break;
      case 11:
          hex += "B";
          break;
      case 12:
          hex += "C";
          break;
      case 13:
          hex += "D";
          break;
      case 14:
          hex += "E";
          break;
      case 15:
          hex += "F";
          break;
      default:
          hex += digit2;
          break;
  }
  return hex;
}

//*****************************************************************************
// Function Create_Pop_Up_Window
// Gets a Page Title, Header Content, and Body Content and uses the Vis-ID 
// functions to create a window in the text frame, or in a new pop up window
// with the appropriate Vis-ID. 
//*****************************************************************************
function Create_Pop_Up_Window(PopUpTitle, HeaderContent, BodyContent, WindowName, Window_Def_String, Force_External) {
  if (!Boolean(PopUpTitle)) { PopUpTitle=" "; }
  if (!Boolean(HeaderContent)) { HeaderContent=" "; }
  if (!Boolean(BodyContent)) { BodyContent=" "; }
  if (!Boolean(WindowName)) { WindowName=" "; }
  if (!Boolean(Window_Def_String)) { Window_Def_String=" "; }
  if (!Boolean(Force_External)) { Force_External=false; }
	var theFrame = "parent.MapFrame";
  var Win1;
	if ((useExternalWindow) || (!useTextFrame) || (Force_External)) {
    Win1 = window.open("",WindowName,Window_Def_String);
    theFrame = "opener";
    if (parent.MapFrame!=null) { theFrame = "opener.parent.MapFrame"; }
	} else {
    Win1 = parent.TextFrame;
    //Win1.document.close();
	}
  Win1.document.close();
	Win1.document.open();
	Win1.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');
	Win1.document.writeln('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');

	Win1.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml">');
	Win1.document.writeln('  <head>');
	Win1.document.writeln('    <meta http-equiv="Pragma" content="no-cache" />');
	Win1.document.writeln('    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />');
	Win1.document.writeln('    <meta http-equiv="imagetoolbar" content="no" />');
	Win1.document.writeln('    <title>' + PopUpTitle + '</title>');
  Win1.document.writeln(Vis_ID_Style(parent.USGSBackgroundColor,parent.USGSTextColor,parent.USGSTextColor,parent.USGSBanner));
  Win1.document.writeln(HeaderContent);
  Win1.document.writeln('</head><body bgcolor="#FFFFFF">');
  var CompleteBannerURL = parent.USGSThisURL + "/" + parent.USGScopyright;
  if ((useExternalWindow) || (!useTextFrame) || (Force_External)) { Win1.document.writeln(Vis_ID_Header(PopUpTitle, CompleteBannerURL, parent.USGSalternative, parent.USGSUseNMAP)); }
  if ((useExternalWindow) || (!useTextFrame) || (Force_External)) { Win1.document.writeln('<hr />'); }
  Win1.document.writeln(BodyContent);
	if ((useExternalWindow) || (!useTextFrame) || (Force_External)) { 
    Win1.document.writeln('<hr />');
    Win1.document.writeln(Vis_ID_Footer(parent.USGSLocalDepartmentURL,parent.USGSLocalDepartment,parent.USGSmaintainer,Date().toString(),true)); 
  }
  Win1.document.writeln('</body></html>');
}
