/*
Script for Navigation Bar.  Intended to be embedded in the head of a frame-document (called hereafter the "Navigation Bar") that is to manage a parent's "display-frame".  Links in "A" statements of the class "TPGnavLink" in the Navigation Bar are managed by this script and coordinated with the document loading of the display-frame.

NOTE:  This script requires MSIE 5.5+ or NN6+.  It should be inserted into the Navigation Bar page by means of a SCRIPT tag that specifies LANGUAGE = "JavaScript1.5".

The managed display-frame must have its Name attribute set to "TPGnavDisplayFrame" and be a descendant of the "top" window of the Navigation Bar.  For efficiency when accessed via MSIE 5.5+, the <FRAME> tag associated with the TPGnavDisplayFrame should contain the onLoad attribute:  ONLOAD="if (this.trigger) { this.trigger(); }".  This is ignored by NN6+ and is not required for MSIE 5.5+.

There may optionally be a call to TPGnavResetColors(newBase, newSelect, newOver) in the onOpen attribute of the <Body> tag of the Navigation Bar.  This resets the default colors used for the link text on that bar [defaults are baseColor = "black", selectColor = "red", and overColor = "blue"]. The new colors are given as character strings (i.e. enclosed in quotes) and may either be in hex form or color-descriptor form.

The general flow of this script (simplified) is:

- the initial parsing of the script inserts an onload event-handler into the 'top' window, which in turn calls TPGnavInit().

-  TPGnavInit first finds the display-frame and then constructs an array pointing to the TPGnavLink links in its own document.  To each such link it attaches properties to enable the various pertinent mouse events, including an onclick event-handler which bypasses the default linking mechanism.  Finally, it sets into operation two distinct mechanisms for tracking the arrival of a new page into the display-frame.

-  the first mechanism is the insertion in the display-frame's <FRAME> element (n.b. not its frame-window) of a pointer to the TPGnavTrigger routine reached via the <FRAME> onload event-handler.  This onload event-handler is non-standard and only recognized by MSIE 5.5+. In addition, this technique will only be operational if the <FRAME> tag has the optional onload attribute set.

-  the second mechanism is a timed polling process that invokes TPGnavArrival() every half-second.

-  if TPGnavTrigger is invoked, it shuts down the timed polling process as unneeded and then invokes TPGnavArrival().

-  TPGnavArrival attempts to read the document location in the display frame.  If this operation fails (because the location is from a different domain), the page is marked as an alien.  Otherwise, the location is compared to the array of TPGnavLink links and the appropriate one identified.  This identification is then used to set the colors on the NavBar.

*/

var isMSIE55 = false;
var isNN6 = false;
if (window.createPopup)
{ isMSIE55 = true; }
if (window.getComputedStyle)
{ isNN6 = true; }
if (isMSIE55 || isNN6)
{
   var TPGnavPoller = NaN;
   var TPGnavLinks = new Array();
   var TPGnavIndex = -1;
   var TPGnavDisplayFrame = null;
   var TPGnavDisplayFrameElement = null;
   var TPGnavBaseColor = "#000000";
   var TPGnavSelectColor = "#FF0000";
   var TPGnavOverColor = "#0000FF";
   if (isMSIE55)
   { top.document.body.onload = TPGnavInit; }
   else
   { top.onload = TPGnavInit; }
}

function TPGnavResetColors(newBase, newSelect, newOver)
{       // optionally called from onload of Navigator Bar
   if (newBase) { TPGnavBaseColor = newBase; }
   if (newSelect) { TPGnavSelectColor = newSelect; }
   if (newOver) { TPGnavOverColor = newOver; }
}

function TPGnavLocFileName(xlocpath)
{      // normalizes format of a pathname
   var frags = xlocpath.split('\\');
   var x = frags.join('/');
   if (x.substring(0,1) != '/')
   {  x = '/' + x;  }
   return x;
}

function TPGnavMatchLink(xlocpath)
{
  var thisFileName = TPGnavLocFileName(xlocpath);
  var thisLinkIndex = -1;
  for (var i = 0; i < TPGnavLinks.length; i++)
  {
    var thatFileName = TPGnavLinks[i].basePath;
    if (thisFileName == thatFileName)
    {
       thisLinkIndex = i;
       break;
    }
  }
  return thisLinkIndex;
}

function TPGnavResetLinks(j)
{
  if (TPGnavIndex != j)
  {
    TPGnavIndex = j;
    for (var i = 0; i < TPGnavLinks.length; i++)
    {
      TPGnavLinks[i].style.color = TPGnavBaseColor;
    }
    if (j >= 0)
    { 
      TPGnavLinks[j].style.color = TPGnavSelectColor;
    }
  }
}

function TPGnavArrival()
{   // TPGnavDisplayFrame has a new document present
  try
  {   // error implies alien page
     var xloc = TPGnavDisplayFrame.location;
     TPGnavResetLinks(TPGnavMatchLink(xloc.pathname));
  }
  catch(e)
  {
     TPGnavResetLinks(-1);
  }
}

function TPGnavTrigger()
{
  if (!isNaN(TPGnavPoller))
  {
     window.clearInterval(TPGnavPoller);
     TPGnavPoller = NaN;
  }
  TPGnavArrival();
}

function TPGnavInit()
{
  var possibles = top.document.getElementsByName("TPGnavDisplayFrame");
  if (possibles.length > 0)
  {
     TPGnavDisplayFrameElement = possibles[0];
     if (isMSIE55)     
     {
       TPGnavDisplayFrame = TPGnavDisplayFrameElement.contentWindow;
     }
     else
     {
       TPGnavDisplayFrame = TPGnavDisplayFrameElement.contentDocument.defaultView;
     }
  }
  if (TPGnavDisplayFrame == null)
  {
     alert("TPGnav SET-UP ERROR: No frame has name 'TPGnavDisplayFrame'");
  }
  else
  {
     for (var i = 0; i < document.links.length; i++)
     {  // add links in TPGnavLinks and modify their properties
       var thisLink = document.links[i];
       if (thisLink.className == "TPGnavLink")
       {  
          var nextJ = TPGnavLinks.length;
          TPGnavLinks[nextJ] = thisLink;
          thisLink.myIndex = nextJ;
          thisLink.baseHref = thisLink.href;
          thisLink.basePath = TPGnavLocFileName(thisLink.pathname);
          thisLink.onclick = TPGnavClickMouse;
          thisLink.style.color = TPGnavBaseColor;
          thisLink.onmouseover = TPGnavOverMouse;
          thisLink.onmouseout = TPGnavOutMouse;
       }
     }

     TPGnavDisplayFrameElement.trigger = TPGnavTrigger;
     TPGnavPoller = window.setInterval(TPGnavArrival, 500);
     TPGnavArrival();  // handles initial load
   }
}

function TPGnavOverMouse()
{   // note that 'this' will refer to link-element
  if (this.myIndex != TPGnavIndex)
  {
    this.style.color = TPGnavOverColor;
  }
}

function TPGnavOutMouse()
{   // note that 'this' will refer to link-element
  if (this.myIndex == TPGnavIndex)
  {
    this.style.color = TPGnavSelectColor;
  }
  else
  {
    this.style.color = TPGnavBaseColor;
  }
}

function TPGnavClickMouse()
{   // note that 'this' will refer to link-element
  TPGnavDisplayFrame.location = this.baseHref;
  TPGnavResetLinks(-1);
  return false;
}

