//
// Class:      tab
//
// Usage:      Creates tabs generally used for site navigation
//    Define each tab in your code with: tabs.push(new tab(.....))

//************************************************************************
//                   Global Variables
//************************************************************************
var tabs = new Array();                   // Array of all the tabs for the page
var currentTab = new Array(0, 1, 3, 6);      // Array of tabs array indexes of the currently selected tabs on each level
var tabsBuilt = false;

//****************************************************
//                         Object definitions
//****************************************************

//
//    Object that holds all the selected sleds in an array
//
function tab(theId, displayText, level, nextLevel, displayDiv)
{
   this.level = level;
   this.theId = theId;
   this.displayText = displayText;
   this.displayDiv = displayDiv;
   this.ind = tabs.length;
   this.nextIndex = -1;            // This is set when the nextlevelhtml is built

   // Can't actually build next level tabs yet.  Save levels in the html field for later
   this.nextLevelHTML = nextLevel;

   this.buildLevelHTML = buildLevelHTML;
}

function buildLevelHTML()
{
   if(this.nextLevelHTML)
   {
      var lis = "";
      var theIds = this.nextLevelHTML.split(";");
      var l = theIds.length;

      for(var i=0; i<l; i++)
      {
         // Turn on the first tab on this level
         var theClass = (i == 0) ? 'class="tabOn"' : "";
         var ind = getTabIndex(theIds[i]);

         // Check to make sure we have valid index before proceeding
         if(ind < 0)
            return false;

         // Set the nextIndex of this tab to the index of the first tab on the next level
         if(i == 0)
            this.nextIndex = ind;
         lis += '<li onclick="changeTab(' + ind + ')" ';
         //alert('i is '+i+' theids[i] is '+theIds[i]);
         lis += 'id="' + theIds[i] + 'Tab" ' + theClass + '>';
         lis += tabs[ind].displayText + '</li>';
      }

      this.nextLevelHTML = lis;
      return true;
   }
}

//********************************************************************************
function getTabIndex(id)
{
   var l = tabs.length;

   for(var i=0; i<l; i++)
   {
      if(tabs[i].theId == id)
         return i;
   }

   // The id was not found among the tabs, so return a bad index
   return -1;
}

function buildTabHTML()
{
   var l = tabs.length;

   for(var i=0; i<l; i++)
      tabs[i].buildLevelHTML();
}

function changeTabByID(id)
{
   changeTab(getTabIndex(id));
}

function changeTab(ind)
{
   // Check for index out of range and quit this function gracefully
   if( (ind < 0) || (ind >= tabs.length) )
      return;

   var x;      // use for element objects
   var newTab = tabs[ind];
   var level = newTab.level * 1;
   var oldTab = tabs[currentTab[level]];

//   // If the old tab and the new tab are the same, get out of here
   if((newTab.ind == oldTab.ind) && tabsBuilt)
      return;

   // Save new tab as current tab
   currentTab[level] = ind;

   // Turn previous off first (just incase they are the same) (does not exist if level 0)
   x = document.getElementById(oldTab.theId+'Tab');
   if(x) x.className = "";

//   // Enable the onclick of the previous tab so we can click it again
//   if(x) x.onclick = "changeTab(" + oldTab.ind + ")";

   // Turn tab on (does not exist if level 0
   x = document.getElementById(newTab.theId+'Tab');
   if(x) x.className = "tabOn";

//   // Disable the onclick of the current tab, because we just turned it on
//   if(x) x.onclick = "";

   // Draw next level
   x = document.getElementById('tabs'+(level+1));
   if(x) x.innerHTML = newTab.nextLevelHTML;

   // Display Div or not
   if(newTab.displayDiv)
   {
      // Turn off current div
      x = document.getElementById(oldTab.theId+'DIV');
      if(x) x.style.display = "none";

      // Turn on new div
      x = document.getElementById(newTab.theId+'DIV');
      if(x) x.style.display = "block";
      x = document.getElementById(newTab.theId+'H1');
      if(x) x.innerHTML = locationText();
   }

   if(newTab.nextIndex >= 0)
      changeTab(newTab.nextIndex);

   // Call any local change tab functionality
   localChangeTab(ind);
}

function locationText()
{
   var l = currentTab.length;
   var result = "";

   for(var i=1; i<l; i++)
   {
      if(i > 1)
         result += " -> ";

      result += tabs[currentTab[i]].displayText;
   }

   return result;
}