/* Arraid Web Site
Script file for the navigation system for the main part of the web site.

Web Site Version 04
Modification History
Date        By          		Modification
********************************************************************************************
10/01/2004	Tom Swales			Update to Site Version 04. Includes all of the current classes for the nav system.
            SitesByTom, LLC		Added notes for tracking and maintenance.
10/07/2004	Tom Swales			Add scripts to support popup 'help' boxes.
10/16/2004	Tom Swales			Add scripts to support footer placement.
11/17/2004	Tom Swales			Add script to support expansion selections that can be expanded further.
11/20/2004	Tom Swales			Complete expansion scripts. Add calculation for selection indentation
								based on rank and content.
*/

// Define the object variables required by the nav bar and expanding drop menu scripts.
var objBarSelHover = null;  // The nav bar selection with the mouse over it.  Whole Object.
var objBarSelOut = null;  // The nav bar selection with the mouse leaving.  Whole Object.
var objBarSelActive = null;  // The nav bar selection that has been clicked.  Whole Object.
var objPrevBarSelActive = null;  // The nav bar selection that is active when has been clicked.  Whole Object.
var objBarSelCurrent = null;  // When a page is loaded, this is the nav bar selection to which the page belongs.
var objDropMenu = null;  // The drop menu made visible by the nav bar selection.  Whole Object.
var objPrevDropMenu = null;  // The drop menu that was visible before the new one is made visible.  Whole Object.
var objDropSelHover = null;  // The drop menu selection with the mouse over it.  Whole Object.
var objDropSelOut = null;  // The drop menu selection with the mouse leaving.  Whole Object.
var objDropSelActive = null;  // The drop menu selection that has been clicked.  Whole Object.
  
// Nav Bar Selections.
//  CSS Classes are used to define three different appearances. Changing the value of object.className changes the appearance.
var clBarSelOff = 'navBarSelOff';
var clBarSelOn = 'navBarSelOn';
var clBarSelActive = 'navBarSelActive';

// Nav Drop Menu Selections
var clDropSelOff = 'navDropSelOff';
var clDropSelOn = 'navDropSelOn';
var clDropSelActive = 'navDropSelActive';

// Nav Exp Drop Menu Selections
var clDropExpSelNone = 'navDropExpSelNone'; //Class used when collapsed.
var clDropExpSelOff = 'navDropExpSelOff';
var clDropExpSelOn = 'navDropExpSelOn';
var clDropExpSelActive = 'navDropExpSelActive';

// Indent for Menus
var numAllPad = 5; // Sets a left margin for the drop menu.
var numSelChar = 9; // Width of the +- character for primary selections.
var numExpChar = 9; // Width of the +- character for expansion selections.
var numExpIndent = 12; // Amount that each expansion level is indented.


var strBrowser = null;
/*  alert(navigator.userAgent);
  alert(navigator.appName);
  alert(navigator.appVersion); */
if (navigator.userAgent.indexOf('MSIE') != -1) strBrowser = "IE";
else if (navigator.userAgent.indexOf('Netscape') != -1) strBrowser = "NS";
else if (navigator.userAgent.indexOf('Firefox') != -1) strBrowser = "FF";
else if (navigator.userAgent.indexOf('Opera') != -1) strBrowser = "OP";
else if (navigator.userAgent.indexOf('Mac') != -1) strBrowser = "MC";
else strBrowser = "MO";

/* Called on page load. */
function initPage() {
  document.onclick = hideDropMenu;
  moveFooter();
}

/*  This routine sets up, but does not display the drop menu appropriate to the current page.
  Each page has a four character identification, passed in the call to initDropMenu.
  This identification matches the ID of the nav menu selection that called the page.
  Using the page ID, the nav menu is set up so that it will look the same as when the page was selected.
  The proper drop expansion is expanded. */
function initDropMenu (strWhoAmI) {
  // Parse the string, strWhoAmI
  var numWhoAmIPtr = -1;
  var strWhoAmIPart = new Array("", "", "", "");
  for (i=0; i<strWhoAmI.length; i++) {
	if (strWhoAmI.charAt(i) == '-') numWhoAmIPtr++;
	else strWhoAmIPart[numWhoAmIPtr] = strWhoAmIPart[numWhoAmIPtr] + strWhoAmI.charAt(i);
  }
  // Make the appropriate nav bar selection active.
  strBarSelCurrent = 'navBarChoice' + '-' + strWhoAmIPart[0];
  objBarSelCurrent = document.getElementById(strBarSelCurrent);
  objBarSelCurrent.className = clBarSelActive;
  // If the page is called from an expansion, make sure that menu section is expanded.
  if (strWhoAmIPart[2] > "") {
	// Determine the drop menu selection calling the expansion and change + to -
    strDropSelNameSuff = '-' + strWhoAmIPart[0] + '-' + strWhoAmIPart[1];
    strDropSelName = 'dropChoice' + strDropSelNameSuff;
    objDropSelActive = document.getElementById(strDropSelName);
    strDropTitle = objDropSelActive.innerHTML;
    strDropTitle = "&#8211;" + strDropTitle.substr(1);
    objDropSelActive.innerHTML = strDropTitle;
    objDropSelActive = null;
    numDropExpNum = 1; // Set up the first expansion element
    strDropExpName = strDropSelName + '-' + numDropExpNum;
    while (document.getElementById(strDropExpName)) {  // Set all expansion elements at this level to be revealed.
	  document.getElementById(strDropExpName).className = clDropExpSelOff;
	  // Calculate the appropriate indent
	  numDropExpPos = 1;
	  if (document.getElementById(strDropExpName).innerHTML.charAt(0) != '+') {
		numDropExpIndent = numAllPad + numExpChar + (numDropExpPos * numExpIndent);
	  }
	  else numDropExpIndent = numAllPad + (numDropExpPos * numExpIndent);
	  document.getElementById(strDropExpName).style.paddingLeft = numDropExpIndent + 'px';
	  numDropExpNum += 1;
      strDropExpName = strDropSelName + '-' + numDropExpNum;
    }
  }
  if (strWhoAmIPart[3] > "") {
	// Determine the drop menu selection calling the expansion and change + to -
    strDropSelNameSuff = strDropSelNameSuff + '-' + strWhoAmIPart[2];
    strDropSelName = 'dropChoice' + strDropSelNameSuff;
    objDropSelActive = document.getElementById(strDropSelName);
    strDropTitle = objDropSelActive.innerHTML;
    strDropTitle = "&#8211;" + strDropTitle.substr(1);
    objDropSelActive.innerHTML = strDropTitle;
    objDropSelActive = null;
    numDropExpNum = 1; // Set up the first expansion element
    strDropExpName = strDropSelName + '-' + numDropExpNum;
    while (document.getElementById(strDropExpName)) {  // Set all expansion elements at this level to be revealed.
	  document.getElementById(strDropExpName).className = clDropExpSelOff;
	  // Calculate the appropriate indent
	  numDropExpPos = 2;
	  if (document.getElementById(strDropExpName).innerHTML.charAt(0) != '+') {
		numDropExpIndent = numAllPad + numExpChar + (numDropExpPos * numExpIndent);
	  }
	  else numDropExpIndent = numAllPad + (numDropExpPos * numExpIndent);
	  document.getElementById(strDropExpName).style.paddingLeft = numDropExpIndent + 'px';
	  numDropExpNum += 1;
      strDropExpName = strDropSelName + '-' + numDropExpNum;
    }
  }
}

/* This portion contains routines to manage the horizontal (primary) nav bar.
  This bar is a division.  Within this are other divisions, each containing aselection.
  A selection may take a user to a new page or display a drop down menu. */

// Called when mouse is over a nav bar selection.  'objSelection' is the nav bar item object (this).
function  barSelHover(objSelection) {
  document.onclick = null;
  objBarSelHover = objSelection;
  if (objBarSelHover == objBarSelCurrent) return;
  if (objBarSelHover != objBarSelActive) {
	objBarSelHover.className = clBarSelOn;  
  }
}

// Called when mouse leaves a nav bar selection.  'objSelection' is the nav bar item object (this).
function barSelOut(objSelection) {
  document.onclick = hideDropMenu;
  objBarSelOut = objSelection;
  if (objBarSelHover == objBarSelCurrent) return;
  if (objBarSelOut != objBarSelActive) {
	objBarSelHover.className = clBarSelOff;
  }
}

// Called by clicking on a nav bar selection, which directs the user to a new page.
// Takes the place of using a link, allowing consistent CSS control of appearance.
function barSelActGoto(whereto) {
  window.open(whereto, "_self", "junk");
}

/* This portion contains routines to manage the drop menus.
  The drop menu is a division, absolutely positioned based on the calling nav bar selection.
  The drop menu contains drop menu selections encased in block divisions so that the drop menu is built downward.
  All drop menus start out initially hidden.  Only one drop menu may be visible at a time.
  Calling a drop menu hides the previous drop menu.
  A drop menu selection may take a user to a new page or expand the drop down menu, revealing a submenu. */

// Called when mouse is clicked on a nav bar selection that produces a drop down menu of other selections.
function barSelActDrop(menu, menuLOffset, objSelection) {
  // Get the menu element
  objDropMenu = document.getElementById(menu);
  // If already visible, hide it.
  if (objPrevDropMenu == objDropMenu) {
    hideDropMenu();
	// The following correct the assumption made in hideDropMenu
	if (objPrevBarSelActive != objBarSelectCurrent) objPrevBarSelActive.className = clBarSelOn;  
    return;
  }
  // If a different menu is visible, hide it.
  if (objPrevDropMenu != null) hideDropMenu();
  objBarSelActive = objSelection;
  // This portion works backward through the positioning reference to calculate the xy position of the calling element.
  xPos = 0;
  yPos = 0;
  objOffsetPointer = objSelection;
  while (objOffsetPointer) {
	xPos += objOffsetPointer.offsetLeft;
	yPos += objOffsetPointer.offsetTop;
	objOffsetPointer = objOffsetPointer.offsetParent;
  }
  yPos += objBarSelActive.offsetHeight; // Position the drop menu below the calling element.
  switch (strBrowser) { // Position calculations don't allow for the 2 pixel border.  Add here.
	case "IE": // Adjustment for Internet Explorer if required.
      yPos += 2;
      xPos -= 0;
	  break;
	case "NS": // Adjustment for Netscape if required.
      yPos += 2;
      xPos -= 0;
	  break;
	case "FF": // Adjustment for Firefox if required.
      yPos += 4;
      xPos -= 0;
	  break;
	case "OP": // Adjustment for Opera if required.
      yPos += 2;
      xPos -= 0;
	  break;
	case "MO": // Adjustment for Mozilla if required.
      yPos += 4;
      xPos -= 0;
	  break;
	case "MC": // Adjustment for Mac if required.
      yPos -= 0;
      xPos -= 0;
	  break;
	default:
	  break;
  }
  // Correction for MacIE body margin factor.
/*  if (strBrowser = "MC" && document.body.leftMargin != "undefined") {
	xPos += document.body.leftMargin;
	yPos += document.body.topMargin;
  } */
  yPos += 0; // Adjustment for any other reason such as nav bar border.
  xPos -= 0;
  /* Drop menus project to the right of the nav bar selection. The menuLOffset moves them left to prevent
    running off the visible window. */
  xPos = xPos - menuLOffset;
  objDropMenu.style.left = xPos + 'px';
  objDropMenu.style.top = yPos + 'px';
  objDropMenu.style.visibility = 'visible';
  objPrevDropMenu = objDropMenu;
  objPrevBarSelActive = objBarSelActive;
  // Set the left indent of the drop menu selections.
  strDropSelNameSuff = menu.substr(8);
  numDropSelNum = 1;
  strDropSelName = 'dropChoice' + strDropSelNameSuff + '-' + numDropSelNum;
    while (document.getElementById(strDropSelName)) {
	  if (document.getElementById(strDropSelName).innerHTML.charAt(0) != '+' && document.getElementById(strDropSelName).innerHTML.charCodeAt(0) != '8211') {
		numDropSelIndent = numAllPad + numSelChar;
	  }
	  else numDropSelIndent = numAllPad;
	  document.getElementById(strDropSelName).style.paddingLeft = numDropSelIndent + 'px';
      numDropSelNum += 1;
      strDropSelName = 'dropChoice' + strDropSelNameSuff + '-' + numDropSelNum;
    }
}

// Called by several functions that need to hide a visible drop menu.
function hideDropMenu() {
  document.onclick = null;
  if (objPrevDropMenu) {
    objPrevDropMenu.style.visibility = 'hidden';
    objPrevDropMenu = null;
	// Below assumes that the nav ber selection is no longer hovered, that the page is clicked.
	if (objPrevBarSelActive != objBarSelCurrent) objPrevBarSelActive.className = clBarSelOff;
  }
  objBarSelActive = null;
}

/* Called when mouse is over a drop menu selection that when clicked, will take the user to a different page.
  'objSelection' is the nav bar item object (this). */
function  dropSelHoverGoto(objSelection) {
  document.onclick = null;
  objDropSelHover = objSelection;
  if (objDropSelHover != objDropSelActive) {
	objDropSelHover.className = clDropSelOn;  
  }
}

/* Called when mouse is over a drop menu selection that when clicked, activates an expansion menu.
  'objSelection' is the nav bar item object (this). */
function  dropSelHoverExpand(objSelection) {
  document.onclick = null;
  objDropSelHover = objSelection;
  if (objDropSelHover != objDropSelActive) {
	objDropSelHover.className = clDropSelOn;  
  }
}

// Called when mouse leaves a drop menu selection.  'objSelection' is the nav bar item object (this).
function dropSelOut(objSelection) {
  document.onclick = hideDropMenu;
  objDropSelOut = objSelection;
  if (objDropSelOut != objDropSelActive) {
	objDropSelOut.className = clDropSelOff;  
  }
}

// Called by clicking on a drop menu selection, which directs the user to a new page.
// Takes the place of using a link, allowing consistent CSS control of appearance.
function dropSelActGoto(whereto) {
  window.open(whereto, "_self", "junk");
}

/* This portion contains routines to manage the drop sub menus, expansion menus.
  Submenu selections are divisions defined in sequence, right after the menu selection that expands them.
  The title of the drop menu selection must start with a +, a visual indication that there is a submenu.
  As part of the drop menu, the expansion selections are already 'visible'.  However, with a display value of 'none'
  they are not displayed.
  Changing the display value to 'block' causes them to be displayed, expanding the menu.
  Reverse the process to shrink the menu. */

// Expand a submenu. Called by clicking on a drop menu selection that expands a submenu.
function dropSelActExpand(objSelection) {
  var dropTitle = objSelection.innerHTML;
  var dropSelName = objSelection.id;
  var strDropExpNameSuff = dropSelName.substr(10);
  var numDropExpNum = 1; // Set up the first expansion element
  var strDropExpName = 'dropChoice' + strDropExpNameSuff + '-' + numDropExpNum;
  /* Test the class of the first element to be expanded.  If the class is None (not displayed), go ahead and expand.
    If the class is Off (displayed and Mouse Off), then the menu is already expanded, contract it. */
  if (document.getElementById(strDropExpName).className == clDropExpSelNone) {
    while (document.getElementById(strDropExpName)) {
	  document.getElementById(strDropExpName).className = clDropExpSelOff;
	  numDropExpPos = -1;
	  for (i=0; i<strDropExpNameSuff.length; i++) {
		if (strDropExpNameSuff.charAt(i) == '-') numDropExpPos += 1;
	  }
	  if (document.getElementById(strDropExpName).innerHTML.charAt(0) != '+') {
	    numDropExpIndent = numAllPad + numExpChar + (numDropExpPos * numExpIndent);
	  }
	  else numDropExpIndent = numAllPad + (numDropExpPos * numExpIndent);
	  document.getElementById(strDropExpName).style.paddingLeft = numDropExpIndent + 'px';
	  numDropExpNum += 1;
      strDropExpName = 'dropChoice' + strDropExpNameSuff + '-' + numDropExpNum;
    }
	/* Change the appearance of the drop menu selection that was clicked.  
       In the selection text, change the '+' to a '-'. */
    dropTitle = '&#8211;' + dropTitle.substr(1);
	objSelection.innerHTML = dropTitle;
  }
  else {
    while (document.getElementById(strDropExpName)) {
	  document.getElementById(strDropExpName).className = clDropExpSelNone;
	  numDropExp2Num = 1;
	  strDropExp2Name = strDropExpName + '-' + numDropExp2Num;
	  while (document.getElementById(strDropExp2Name)) {
	    document.getElementById(strDropExp2Name).className = clDropExpSelNone;
	    numDropExp2Num += 1;
        strDropExp2Name = strDropExpName + '-' + numDropExp2Num;
	  }
	  if (document.getElementById(strDropExpName).innerHTML.charCodeAt(0) == '8211') {
		dropExpTitle = document.getElementById(strDropExpName).innerHTML;
		dropExpTitle = "+" + dropExpTitle.substr(1);
		document.getElementById(strDropExpName).innerHTML = dropExpTitle;
	  }
	  numDropExpNum += 1;
      strDropExpName = 'dropChoice' + strDropExpNameSuff + '-' + numDropExpNum;
    }
    dropTitle = "+" + dropTitle.substr(1);
	objSelection.innerHTML = dropTitle;
  }
}

/* Called when mouse is over a drop menu expansion selection that is a link to a page.
  'objSelection' is the nav bar item object (this). */
function  dropExpSelHover(objSelection) {
  document.onclick = null;
  objDropSelHover = objSelection;
  if (objDropSelHover != objDropSelActive) {
	objDropSelHover.className = clDropExpSelOn;  
  }
}

/* Called when mouse is over a drop menu expansion selection that controls a further expansion.
  'objSelection' is the nav bar item object (this). */
function  dropExpSelHoverExpand(objSelection) {
  document.onclick = null;
  objDropSelHover = objSelection;
  if (objDropSelHover != objDropSelActive) {
	objDropSelHover.className = clDropExpSelOn;  
  }
}

// Called when mouse leaves a drop menu expansion selection.  'objSelection' is the nav bar item object (this).
function dropExpSelOut(objSelection) {
  document.onclick = hideDropMenu;
  objDropSelOut = objSelection;
  if (objDropSelOut != objDropSelActive) {
	objDropSelOut.className = clDropExpSelOff;  
  }
}

/* This portion positions the footer just off of the screen. */
function moveFooter() {
  var tsAvailHeight = findPageHeight();  // Determine height of the working window.
  var tsFooterVis = 48;  // The maximum pixels of footer to be visible.  Allow for the 10 pixel footer top margin.
  var tsTargetPos = tsAvailHeight - tsFooterVis;
  /* This portion works backward through the positioning reference to calculate the botom of the maincontent.
    This tests the start position and height of the 'mainContent' division of the page. */
  yPos = 0;
  xPos = 0
  objOffsetPointer = document.getElementById("mainContent");
  while (objOffsetPointer) {
	yPos += objOffsetPointer.offsetTop;
	objOffsetPointer = objOffsetPointer.offsetParent;
  }
  // Correction for MacIE body margin factor.
/*  if (navigator.userAgent.indexOf("Mac") != -1 && document.body.leftMargin != "undefined") {
	yPos += document.body.topMargin;
  } */
  switch (strBrowser) { // Position calculations sometimes need adjusting.
	case "IE": // Adjustment for Internet Explorer if required.
      yPos += 0;
      xPos += 10;
	  break;
	case "NS": // Adjustment for Netscape if required.
      yPos += 0;
      xPos -= 0;
	  break;
	case "FF": // Adjustment for Firefox if required.
      yPos += 0;
      xPos -= 0;
	  break;
	case "OP": // Adjustment for Opera if required.
      yPos += 0;
      xPos -= 0;
	  break;
	case "MO": // Adjustment for Mozilla if required.
      yPos += 0;
      xPos -= 0;
	  break;
	case "MC": // Adjustment for Mac if required.
      yPos -= 0;
      xPos -= 0;
	  break;
	default:
	  break;
  }
  yHeight = document.getElementById("mainContent").offsetHeight;
  yPos = yPos + yHeight;  // Should give the bottom of the mainContent element.
  if (yPos < tsTargetPos) document.getElementById("footer").style.top = tsTargetPos + "px";
  else document.getElementById("footer").style.top = yPos + "px";
  document.getElementById("footer").style.left = xPos + "px";
  document.getElementById("footer").style.visibility = "visible";
}

function findPageHeight() {
  if (window.innerHeight) return window.innerHeight;
  if (document.body.clientHeight) return document.body.clientHeight;
  return(null);
}

/* This portion contains scripts to manage popup 'help' and information boxes.
  The boxes are divisions, normally hidden, that are revealed by hovering over designated objects on the page. */
  
  var objPopUp = null;
  var objPopTrig = null;

//  Function to reveal context help.  Called on mouseover.
function showHelp(objCalling, nameHelp) {
  document.onclick = null;  // This line needed for compatibility with menu system.
  objHelpTrig = document.getElementById(objCalling);
  objHelpBox = document.getElementById(nameHelp);
  // This portion works backward through the positioning reference to get the xy position of the calling element.
  xPos = 0;
  yPos = 0;
  objOffsetPointer = document.getElementById(objCalling);
  while (objOffsetPointer) {
	xPos += objOffsetPointer.offsetLeft;
	yPos += objOffsetPointer.offsetTop;
	objOffsetPointer = objOffsetPointer.offsetParent;
  }
  yPos += objHelpTrig.offsetHeight; // Position the popup below the calling element.
  // Correction for MacIE body margin factor.
/*  if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
	xPos += document.body.leftMargin;
	yPos += document.body.topMargin;
  }
  if (isIE) {  // Adjustment for Internet Explorer if required.
    yPos -= 0;
    xPos -= 0;
  }
  yPos += 0; // Adjustment for any other reason such as nav bar border.
  xPos -= 0; */
  // Test to see if help will run off the screen.
  if (xPos + objHelpBox.offsetWidth >  document.body.clientWidth) xPos = xPos - objHelpBox.offsetWidth;
  if (yPos + objHelpBox.offsetHeight >  document.body.clientHeight) yPos = yPos - objHelpBox.offsetHeight - objHelpTrig.offsetHeight;
  // Position the context help and make visible.
  objHelpBox.style.left = xPos + 'px';
  objHelpBox.style.top = yPos + 'px';
  objHelpBox.style.visibility = 'visible';
}

//  Function to hide the context help.  Called on mouseoff.
function hideHelp() {
  document.onclick = hideDropMenu;  // This line needed for compatibility with menu system.
  objHelpBox.style.visibility = 'hidden';
  objHelpBox = null;
}
