﻿// Gets all child elements and creates an array of all loaded sections and articles withing them
function GetChildObjects(objParent, childTagName) {
  var i, cnt = 0;
  var childObjects = new Array();
  if (objParent) {
    for(i=0; i<objParent.childNodes.length; i++) {
      if(objParent.childNodes[i].nodeName==childTagName)
        childObjects[cnt++] = objParent.childNodes[i];
    }
  }
  return childObjects;
}

var navigationIdList = new Array();
var sectionNavigationList = new Array();
var qsTopicId = getQueryString("topicId");
var qsarticleId = getQueryString("articleId");
var sectionId;
var iteratorId = 1;
var articleNumbers = 0;
var currentArticle;
//creates and array of child element id's on page load
do {
  var objUL = document.getElementById("articleSection"+iteratorId);
  var childLiObjects = GetChildObjects(objUL, "LI");
  var nodeId;
  for(i=0; i<childLiObjects.length; i++)  {
    nodeId = childLiObjects[i].id.replace("article", "-");
    navigationIdList[articleNumbers] = nodeId;
    articleNumbers ++;
  }
  iteratorId++;
}
while (document.getElementById("articleSection"+iteratorId) != null)   

function setTabState(currentSection) {
  //used to create single id array
  var currentIdPosition = 0;
  // on pageLoad if no value is passed for currentSection the section ID is set to 1
  if(!currentSection){
    if(qsTopicId){
      sectionId = qsTopicId;
    } else {
      sectionId = 1;
    }
  } else {
      sectionId = currentSection;
      currentArticle = 1;
  }
  // used for do while statement
  /*
  var i = 1;
  do {
      document.getElementById("articleSection"+i).style.display = "none";
      document.getElementById("Topic"+i).style.fontWeight = "normal";
      i++;
  }
  while (document.getElementById("articleSection"+i) != null)
  */
  for (var i = 1; document.getElementById("articleSection"+i) != null; i++) {
    document.getElementById("articleSection"+i).style.display = "none";
    document.getElementById("Topic"+i).style.fontWeight = "normal";
  }
  // builds sectionNavigationList array which stores ids for current section
  for(j=0; j<navigationIdList.length; j++) {
    sectionNavigationArray = navigationIdList[j].split("-");
    if(sectionNavigationArray[0] == sectionId){
      sectionNavigationList[currentIdPosition] = sectionNavigationArray[1];
      currentIdPosition++;
    }
  }
  // set Tab styling to reflect selected state
  document.getElementById("Topic"+sectionId).style.fontWeight = "bold";
  var currentTopic = $("#Topic"+sectionId+" a").html();
  $("h2#currentTopic").html(currentTopic);
  // show current Article section
  if (document.getElementById("articleSection"+sectionId)) document.getElementById("articleSection"+sectionId).style.display = "block";
  // select the first article on tab change
  directSelect(sectionId, currentArticle);
}
if(qsarticleId){
    currentArticle = qsarticleId;
} else {
    currentArticle = 1;
}
setTabState();
function directSelect(sectionId, articleId) {
  window.scrollTo(0, 0);
  var currentIdPosition = 0;
  // Reset array
  sectionNavigationList = new Array();
  /*
  var i = 1;
  do {
      elementToStyle = document.getElementById(sectionId + "article" + i);
      elementToStyle.className = elementToStyle.className.replace(new RegExp("selected\\b"), "");
      i++;
  }
  while (document.getElementById(sectionId + "article" + i) != null)
  */
  for (var i = 1; document.getElementById(sectionId + "article" + i) != null; i++) {
    elementToStyle = document.getElementById(sectionId + "article" + i);
    elementToStyle.className = elementToStyle.className.replace(new RegExp("selected\\b"), "");
  }
  //highlight selected node
  if (document.getElementById(sectionId + "article" + articleId)) document.getElementById(sectionId + "article" + articleId).className = "selected";
  // builds sectionNavigationList array which stores ids for current section
  for(j=0; j<navigationIdList.length; j++) {
    sectionNavigationArray = navigationIdList[j].split("-");
    if(sectionNavigationArray[0] == sectionId){
      sectionNavigationList[currentIdPosition] = sectionNavigationArray[1];
      currentIdPosition++;
    }
  }
  for (j=0; j < sectionNavigationList.length; j++){
    if(articleId == sectionNavigationList[j]){
      currentArticle = j+1;
    }
  }
  if (document.getElementById("UCTopicList_LbttnNextArticle")) document.getElementById("UCTopicList_LbttnNextArticle").className = "nextLink";
  if (document.getElementById("UCTopicList_LbttnPreviousArticle")) document.getElementById("UCTopicList_LbttnPreviousArticle").className = "previousLink";
  if (currentArticle == 1){
    if (document.getElementById("UCTopicList_LbttnPreviousArticle")) document.getElementById("UCTopicList_LbttnPreviousArticle").className = "previousDisabledLink";
  }
  if (currentArticle == sectionNavigationList.length){
    if (document.getElementById("UCTopicList_LbttnPreviousArticle")) document.getElementById("UCTopicList_LbttnPreviousArticle").className = "previousLink";
    if (document.getElementById("UCTopicList_LbttnNextArticle")) document.getElementById("UCTopicList_LbttnNextArticle").className = "nextDisabledLink";
  }
}

function moveToNextArticle(){
    window.scrollTo(0, 0);
  if (currentArticle < sectionNavigationList.length){
    currentArticle++;
  }
  var i = 1;
  do {
    elementToStyle = document.getElementById(sectionId + "article" + i);
    elementToStyle.className = elementToStyle.className.replace(new RegExp("selected\\b"), "");
    i++;
  }
  while (document.getElementById(sectionId + "article" + i) != null)
  document.getElementById(sectionId + "article" + currentArticle).className += " selected";
  document.getElementById("UCTopicList_LbttnPreviousArticle").className = "previousLink";
  if (currentArticle == sectionNavigationList.length){
    document.getElementById("UCTopicList_LbttnNextArticle").className = "nextDisabledLink";
  }
}

function moveToPreviousArticle(){
    window.scrollTo(0, 0);
  if (currentArticle > 1){
    currentArticle--;
  }
  var i = 1;
  do {
    elementToStyle = document.getElementById(sectionId + "article" + i);
    elementToStyle.className = elementToStyle.className.replace(new RegExp("selected\\b"), "");
    i++;
  }
  while (document.getElementById(sectionId + "article" + i) != null)
  document.getElementById(sectionId + "article" + currentArticle).className += " selected";
  document.getElementById("UCTopicList_LbttnNextArticle").className = "nextLink";
  if (currentArticle == 1){
    document.getElementById("UCTopicList_LbttnPreviousArticle").className = "previousDisabledLink";
  }
}