// coordArray is array of coordinates of the Current page. I.e., the first value is the position of this page 
// in the first-level menu, the second value is the position on the second level menu, etc. 
// Index.html has coordiantes [0-0]. None-menu pages have coordinate [0]. Coordinates are hand-coded on
// each page's meta tag "page-coordinates" as a string n-m-k... with n being the first coordinate, etc.
//
// homeElementsArray is the array of actual elements in the menu that lie on the tree path to the current page.

var coordArray = [];                        
var homeElementsArray = [];

getBreadCrumbChunk = function(myEl,getMarkup) {
  var anchorTags = myEl.getElementsByTagName("A");
  if ( (anchorTags == null ) || (anchorTags[0].nodeName !== "A" )) {
    return "";
  }
  else {
    var aTag = anchorTags[0];
    var hrefPart = "<a href=" + "\"" + aTag.href + "\">" ;
    var txtPart = aTag.innerHTML + "</a>";
    if (getMarkup) {
       return hrefPart + txtPart;
    }
    else {
      return txtPart;
    }
  }
}


getPageCoordinates = function() {
  var tags = document.getElementsByTagName("meta");
  for (var i=0; i < tags.length; i++) {
      if (tags[i].name == "page-coordinates") {
        var coord = tags[i].content;
        var cArray = coord.split(/-/g);
        for (var j=0; j < cArray.length; j++) {
            c = cArray[j];
            if ( /\d{1,2}/.test(c) && ! ( /\D{1,2}/.test(c) )) { 
                coordArray.push(Number(c));
            }                
        }
        break;
      }
  }
}
 
 
setCurrentPage = function() {
  var homeRow1Coord = coordArray[0]; 
  if ( ! (homeRow1Coord  >= 0 )) {
    coordArray = [0];
  }
  var homeRow1 = document.getElementById("menu-row-1-tab-" + homeRow1Coord );
  if (homeRow1 !== null) {
      homeRow1.className = "row1hovering";
      var homeRow1UL = homeRow1.getElementsByTagName("UL")[0];
      if (homeRow1UL !== null) {
          homeRow1UL.id = "home-row1";
      }
      homeElementsArray[0] = homeRow1;
  }
  
  var homeRow2Coord = coordArray[1] ;
  if ( homeRow2Coord > 0 ) {
    var row2LIs = homeRow1.getElementsByTagName("LI");
    if ( row2LIs.length >= homeRow2Coord-1) {
        var homeRow2 = row2LIs[homeRow2Coord-1] ;
        homeRow2.id = "home-row2" ;
        homeRow2.className = "row2hovering" ;
    }
    homeElementsArray[1] = homeRow2;
  }
}
    
setSideBar = function() {
  if (coordArray[0] >= 0) {
      var leftSideBarCurrentTab = document.getElementById("leftSideBar-tab-" + coordArray[0]);
      if (leftSideBarCurrentTab !== null) {
          var leftSideBarItems = leftSideBarCurrentTab.getElementsByTagName("LI");
          var rowNum = leftSideBarItems.length ;
          if ( rowNum > 0 ) {
              if (  coordArray[1] == 0 || coordArray[1] > rowNum ) {
                    var leftSideBarCurrent = leftSideBarItems[0];
                    leftSideBarCurrent.className = "currentLeftSideBarHeading";
              }
              else  {
                    var leftSideBarCurrent = leftSideBarItems[coordArray[1]];
                    leftSideBarCurrent.className = "currentLeftSideBar";
              }
          }
      }
  }
}

setBreadCrumbs = function() {
  var breadcrumbs = document.getElementById("breadcrumbs");
  if ( breadcrumbs == null ) {
      return ;
  }
  if ( coordArray[0] > 0 ) {
      var homePage =  document.getElementById("menu-row-1-tab-0");
      var crumbs = "<p>" + getBreadCrumbChunk(homePage,1);
      if (coordArray[1] > 0) {
          crumbs += " &raquo; " + getBreadCrumbChunk(homeElementsArray[0],1);
          crumbs += " &raquo; " + getBreadCrumbChunk(homeElementsArray[1],0);
        }
        else {
          crumbs += " &raquo;  " + getBreadCrumbChunk(homeElementsArray[0],0);
        }
        crumbs += "</p>";
        breadcrumbs.innerHTML = crumbs;
    }
}
  


setHoveringMethods = function() {
  
  var LIs = document.getElementById("MenuStrip").getElementsByTagName("LI");
  for (var i=0; i < LIs.length; i++) {
    if ( LIs[i].className.match(/\brow1\b/)) {
        
        LIs[i].onmouseover=function() {
          this.className = "row1hovering";
          if ( this.getElementsByTagName("UL").length > 0 ) {
            var row1UL = this.getElementsByTagName("UL")[0];
            if ( row1UL.id !== "home-row1") {
                var homeRow1UL = document.getElementById("home-row1");
                var homeRow1 = homeRow1UL.parentNode;
                homeRow1.className = "row1" ;
            }
          }
        }
    
        LIs[i].onmouseout=function() {
          this.className = "row1" ;
          if ( this.getElementsByTagName("UL").length > 0 ) {
              var row1UL = this.getElementsByTagName("UL")[0];
              if ( row1UL.id !== "home-row1") {
                  var homeRow1UL = document.getElementById("home-row1");
                  var homeRow1 = homeRow1UL.parentNode;
                  homeRow1.className = "row1hovering";
              }
          }
        }
    }
    
    else if ( LIs[i].className.match(/\brow2\b/)) {
        
        LIs[i].onmouseover=function() {
          this.className = "row2hovering";
          if ( this.id !== "home-row2") {
            homeRow2 = document.getElementById("home-row2");
            homeRow2.className = "row2" ;
          }
        }
    
        LIs[i].onmouseout=function() {
          this.className= "row2";
           if ( this.id !== "home-row2") {
            homeRow2 = document.getElementById("home-row2");
            homeRow2.className = "row2hovering";
          }
        }
    }
  }
}



menuStartup = function() {
  getPageCoordinates();
  setCurrentPage();
  setSideBar();
  setBreadCrumbs();
  setHoveringMethods();
}


