/**
 * To use this script insert a script tag like so:
         <SCRIPT language="JavaScript" src="scripts/navbar.js">
         </SCRIPT>
         <NOSCRIPT>
         <p>Place link to site map here</p>
         </NOSCRIPT>

 * also insure that the style sheet is inserted on the page like so:
 
         <html>
         <head>
            <link REL="stylesheet" TYPE="text/css" HREF="theme/main.css">
         </head>
         <body bgcolor="#FFFFFF">
         .
         .
         .
   
 * The nav elements will not function nicely if the style sheet
 * is excluded
 *
 */

/**
 * Set your root directory of website here
* var m_rootDir  = "/parent/daughter/psu/";
* var m_rootDir  = "/faculty/derek/Derek/My Documents on U/www/projects/calipso/";
* var m_rootDir  = "/~elsworth/projects/calipso/";
*/
var m_rootDir  = "/~elsworth/projects/calipso/";


/**
 * Table generated can have a background color, assign this
 * to m_bgColor
 */
var m_bgColor  = "";

/**
 * Each row will have a line under it if m_insertHR is true
 * set m_insertHR to false to disable this option
 */
var m_insertHR = false;

/**
 * Use maxWidth to control width of navbar. This value can
 * be pixels (150) or percent (50%) or nothing "";
 */
var m_maxWidth = "145";

/**
 * The links tree is below. Each 'record' in the array, has three
 * entries:
 *    1) Link Text
 *    2) Link Url
 *    3) Link Children
 *
 * The link text can be any valid text for display as HTML. The link
 * url can be a relative or absolute url. Relative url's do not begin
 * with an http:// or a /. A relative url will have m_rootDir inserted
 * at the beginning of the URL. Absolute url's are not altered.
 *
 * Link chilren can be null or an array, in the same format as the parent
 * array. The children can also have children.
 *
 * Always end the array with an "eof" element.
 */

var m_navArray = new Array( 
/* Begin Links */

   "NEWS",              
   "index.html",
   null,

   "Instrumentation Sites",              
   "InstrumentationSites/InstrumentationSites.html",
   null,

   "3D Model",                  
   "3DModel/3DModel.html", 
   null,

   "Objectives",                
   "Objectives/Objectives.html",
   null,


   "Scientists",         
   "Scientists/Scientists.html",
   null,

   "References",  
   "References/References.html",
   null,

   "Internal Data",               
   "InternalData/InternalData.html",
   null,


   "Public Data",                     
   "PublicData/PublicData.html",
   null,

   "Links",                 
   "Links/Links.html",
   null,

   "Drilling Reports",                       
   "DrillingReports/DrillingReports.html",
   null,

   "Gallery",                 
   "gallery/Gallery.html",
   null,

   "eof" /* must be last */
/* End Links */
);

/**
 * Link over behavior
 */
function linkOver(element) {
   element.className = "navLinkOver"
}
/**
 * Link out behavior
 */
function linkOut(element) {
   element.className = "navLink"
}
/**
 * Builds a url from an absolute or
 * relative value.
 */
function build_href(urlValue) {
   if (urlValue==null) return "null";
   if (urlValue.search(/http:\/\/|^\//)<0) {
      return encodeURI(m_rootDir+urlValue);
   }
   return urlValue;
}
/**
 * Returns true if urlValue is found in
 * the current browsers url (browserHref).
 */
function isCurrentUrl(urlValue,browserHref) {
   //alert("Url :"+urlValue+"\n"+"Browser :"+browserHref);
   if (browserHref.indexOf(urlValue)>0) {
      return true;
   }
   return false;
}
/**
 * Builds the span markup.
 */
function build_SpanTag(strContent) {
   return "<SPAN class='navLink' onmouseover='linkOver(this)' onmouseout='linkOut(this)'>"
          +strContent
          +"</SPAN>";
}
/**
 * Builds the a markup.
 */
function build_ATag(strHref,strContent) {
   return "<A href='"
          +strHref
          +"'>"
          +strContent
          +"</A>";
}
/**
 * Builds the TABLE start tag.
 */
function build_TABLEStartTag(bgColor) {
   var str = "<TABLE ";
   if (m_maxWidth!=null && m_maxWidth.length>0) {
      str+= "width='"+m_maxWidth+"' ";
   }
   if (bgColor.length>0) {
      str+= "bgcolor='"+bgColor+"' ";
   }
   str += ">";
   return str;
}
/**
 * Recursively searchs through the children to find
 * a child that references the browsers url (browserHref).
 */
function searchPath(children,browserHref) {
   if (children==null) return;
   var i=0, n=0;
   n = children.length/3;
   for (i=0;i<n;i++) {
      var linkChild = children[i*3+2];
      var urlChild  = children[i*3+1];
      if (isCurrentUrl(urlChild,browserHref)==true) {
         return true;
      }
      else if (linkChild!=null) {
         var stat=searchPath(linkChild,browserHref);
         if (stat==true)
            // found it! 
            return stat;
         // else, keep going
      }
   }
   return false;
}
/**
 * This function does all the work, it builds the
 * navbar. Function is recursively called, to render
 * children of array. navArray is the root array.
 * If bInsertHR is true, <HR> tags are inserted under
 * links. iOffset is an positive integer. It controls
 * the level of indention. This value is increamented
 * with each recursive call.
 */
function buildNavBar(navArray, bInsertHR, iOffset) {
   if (navArray==null) return;
   var i=0, n=0;
   var browserHref = location.href;
   n = Math.floor(navArray.length/3);
   document.writeln(build_TABLEStartTag(m_bgColor));
   for (i=0;i<n;i++) {
      var linkChildren = navArray[i*3+2];
      var urlValue     = build_href(navArray[i*3+1]);
      var link         = navArray[i*3];
      if (link==null) link="null";
      document.writeln("<TR>");
      if (iOffset>0) {
         var j=iOffset;
         while (0<(j--)) {
            document.writeln("<TD>&nbsp;&nbsp;</TD>");
         }
      }
      document.writeln("<TD>");
      document.writeln(build_ATag(urlValue,build_SpanTag(link)));
      if (linkChildren!=null) {
         if (isCurrentUrl(urlValue,browserHref)==true) {
            buildNavBar(linkChildren,false,iOffset+1);
         }
         else if (searchPath(linkChildren,browserHref)==true) {
            buildNavBar(linkChildren,false,iOffset+1);
         }
      }
      if (bInsertHR) {
         document.writeln("<HR NOSHADE COLOR='#000000'>");
      }
      document.writeln("</TD>");
      document.writeln("</TR>");
   }
   document.writeln("</TABLE>");
}

// lets build it!
buildNavBar(m_navArray,m_insertHR,0);
