// NOTE THIS PROGRAM MUST BE CALLED AFTER MENUCONTENT AND MENULAYOUT


// User defined variables - change defaults as desired
var HomePageText = 'Home Page';
var HomePageRef  = 'index.html';

var ChildrenTableTags = 'align="center" cellspacing="2" border="0"'

// Style definitions
var ParentLinkSymbol = ' > ';
var ParentLinkStyle = 'sdukParentLink'
var ChildLinkStyle = 'sdukChildLink'
var LinkHover = 1

// Default style options
var ParentLinkFontHighColor = FontLowColor;
var ParentLinkFontLowColor = 'black';
var ParentLinkHighBgColor = HighBgColor;
var ParentLinkLowBgColor = 'white';
var ParentLinkFontFamily = FontFamily;
var ParentLinkFontSize = 10;
var ParentLinkFontBold = 0;
var ParentLinkFontItalic = 0;
var ParentLinkPadding = '1px';
var ParentLinkLineHeight='14px'
var ParentLinkTextDecoration = 'none'

var ChildLinkFontHighColor = FontHighColor;
var ChildLinkFontLowColor = FontLowColor;
var ChildLinkHighBgColor = HighBgColor;
var ChildLinkLowBgColor = LowBgColor;
var ChildLinkFontFamily = FontFamily;
var ChildLinkFontSize = FontSize;
var ChildLinkFontBold = FontBold;
var ChildLinkFontItalic = FontItalic;
var ChildLinkPadding = '3px';
var ChildLinkTextDecoration = 'none'

var sdukParentLink = '.sdukParentLink { '
  sdukParentLink += ' color: ' + ParentLinkFontLowColor + '; '
  sdukParentLink += ' background-color :' + ParentLinkLowBgColor + '; '
  sdukParentLink += ' font-family: "' + ParentLinkFontFamily + '"; '
  sdukParentLink += ' font-size: ' + ParentLinkFontSize + '; '

  if (ParentLinkFontBold > 0) { sdukParentLink += ' font-style: italic; ' }
  if (ParentLinkFontItalic > 0) { sdukParentLink += ' font-weight: 900; ' }
  if (ParentLinkPadding!='') { sdukParentLink += ' padding: ' + ParentLinkPadding + '; ' }
  if (ParentLinkLineHeight!='') { sdukParentLink += ' line-height: ' + ParentLinkLineHeight + '; ' }

  sdukParentLink += ' text-decoration: ' + ParentLinkTextDecoration + ' ;'
  sdukParentLink += ' } '

var sdukChildLink = '.sdukChildLink { '
  sdukChildLink += ' color: ' + ChildLinkFontLowColor + '; '
  sdukChildLink += ' background-color :' + ChildLinkLowBgColor + '; '
  sdukChildLink += ' font-family: ' + ChildLinkFontFamily + '; '
  sdukChildLink += ' font-size: ' + ChildLinkFontSize + '; '

  if (ChildLinkFontBold > 0) { sdukChildLink += ' font-style: italic; ' }
  if (ChildLinkFontItalic > 0) { sdukChildLink += ' font-weight: 900; ' }
  if (ChildLinkPadding!='') { sdukChildLink += ' padding: ' + ChildLinkPadding + '; ' }

  sdukChildLink += ' text-decoration: ' + ChildLinkTextDecoration + ' ;'
  sdukChildLink += ' } '

var sdukLinkHover = ' A:hover { '
  sdukLinkHover += ' color: ' +  ChildLinkFontHighColor + '; '
  sdukLinkHover += ' background-color: ' +  ChildLinkHighBgColor + '; '
  sdukLinkHover += ' } '

// Write Style if using default ChildLinkStyle
WriteStyle()

// Internal program variables
var NavigatorPageID=''
var NavigatorMenuArray=''




function RegisterPageID(PageID) {
  // This function finds the MenuArray for a given PageID,
  // and adds it to a global variable for future use

  GetNavigatorArray(PageID)

  }


function WritePageID() {
  // This function outputs the registered PageID (ie its indexed name)

  var MenuArray = NavigatorMenuArray

  if (MenuArray != '') {
    PageID = eval(MenuArray + '[0]')
    if (PageID != '') { document.write(PageID) }
  }

}


function WriteParents() {
  // This function outputs the item's Parents

  var MenuArray = NavigatorMenuArray;

  if (MenuArray!='') {
    Parents = ExtractParents(MenuArray);
    if (Parents!='') {
      if (HomePageText != '') {BaseRef = '<a href="' + HomePageRef + '" class="sdukParentLink">' + HomePageText + '</a>' + ParentLinkSymbol }
      document.write( BaseRef + Parents)
    }
  }
}


function WriteChildren() {
  // This function outputs the item's Children

  var MenuArray = NavigatorMenuArray;
  if (MenuArray!='') {
    ChildrenCells = ExtractChildren(MenuArray)
    if (ChildrenCells!='') {
      ChildrenText = '<table ' + ChildrenTableTags + ' ><tr>'
      ChildrenText += ChildrenCells
      ChildrenText += '</tr></table>'

      document.write( ChildrenText )
    }
  }
}


function WriteStyle() {
// This function writes the default style to the document
// To use your own style, change the content of the variables "ChildLinkStyle" or
// "ParentLinkStyle" to the name in your own style definitions

  document.write('<style type="text/css"> ')
  if (ChildLinkStyle=='sdukChildLink') { document.write(sdukChildLink) }
  if (ParentLinkStyle=='sdukParentLink') { document.write(sdukParentLink) }

  if (LinkHover>0) {document.write(sdukLinkHover)}

  document.write(' </style>')

}


function GetNavigatorArray(PageID)
  {
// Function used to check if MenuArray pulled out for current PageID
// If so, use it. If not, get it.

    var MenuArray = NavigatorMenuArray;
    if (MenuArray=='') {
      NavigatorMenuArray = ReturnLink('Menu', 8, PageID);
      MenuArray = NavigatorMenuArray;
    }
    return MenuArray
  }

function ExtractParents(MenuArray) {
// This function finds the parents of the page represented by the
// selected MenuArray and returns an html list

    Parents = ''
    var TempMenu = MenuArray

    var LastIndex = TempMenu.lastIndexOf('_')
    while (LastIndex > 0) {
      TempMenu = TempMenu.substring(0,LastIndex)

      ParentRef = ParentLinkSymbol + '<a href="' + eval(TempMenu + '[1]') + '" class="sdukParentLink">' + eval(TempMenu + '[0]') + '</a>'

      Parents = ParentRef + Parents

      LastIndex = TempMenu.lastIndexOf('_')
    }


    ParentTail = ParentLinkSymbol + '<a href="' + eval(MenuArray + '[1]') + '" class="sdukParentLink">This page</a>'

    if (Parents != '') {
      Parents = Parents.substring(ParentLinkSymbol.length,Parents.length)
    }

    Parents += ParentTail
    return Parents
}


function ExtractChildren(MenuArray){
// This function finds the parents of the page represented by the
// selected MenuArray and returns an html list

    Children = ''

    var TempMenu = MenuArray;

    var NumberSubItems = eval(TempMenu + '[3]');
    if (NumberSubItems=='') {NumberSubItems=0};

    for (var i=1;i<NumberSubItems+1;i++) {
      WMnu = TempMenu + '_' + i;
      ChildRef = '<td class="' + ChildLinkStyle + '"><a href="' + eval(WMnu + '[1]') + '" class="' + ChildLinkStyle + '">' + eval(WMnu + '[0]') + '</a></td>'
      Children += ChildRef;
    }

    return Children
}


function ReturnLink(MenuItem, NumberSubItems, PageID) {
// This is a recursive function (ie. one that calls itself) which
// runs through all the MenuArrays to find the MenuArray with a
// PageID that matches the selected PageID. The first matched MenuArray
// is returned.

  LinkText = '';

  for(var i=1;i<NumberSubItems+1;i++) {

    if (LinkText == '') {
      WMnu= MenuItem + eval(i);
      TestName=eval(WMnu+'[0]');

      if ( TestName==PageID ) {
        LinkText = MenuItem + eval(i);
      }

      if (LinkText=='') {
        NumberNewSubItems = eval(WMnu+'[3]');
        if (NumberNewSubItems=='') {NumberNewSubItems=0}
        if (NumberNewSubItems > 0) {
          NewWMnu = WMnu + '_'
          LinkText = ReturnLink(NewWMnu, NumberNewSubItems, PageID);
        }
      }
    }
  }

  return LinkText;

}