/*==================================================
  $Id: tab.js,v 1.9 2006/04/27 20:51:51 pat Exp $

  Copyright (c) 2006 Patrick Fitzgerald

  ==================================================*/

function tabObj(argsObj)
{
  var arg; 

 
  this.div = null;

  this.classMain = "tab";
 
  this.classMainLive = "tablive";

  this.classTab = "tabtab";

  this.classTabDefault = "tabtabdefault";

  this.classNav = "tabnav";

  this.classTabHide = "tabtabhide";

  this.classNavActive = "tabactive";
  
  this.titleElements = ['h2','h3','h4','h5','h6'];
  
  this.titleElementsStripHTML = true;

  this.removeTitle = true;

  this.addLinkId = false;

  this.linkIdFormat = '<tabid>nav<tabnumberone>';

  for (arg in argsObj) { this[arg] = argsObj[arg]; }

  this.REclassMain = new RegExp('\\b' + this.classMain + '\\b', 'gi');
  this.REclassMainLive = new RegExp('\\b' + this.classMainLive + '\\b', 'gi');
  this.REclassTab = new RegExp('\\b' + this.classTab + '\\b', 'gi');
  this.REclassTabDefault = new RegExp('\\b' + this.classTabDefault + '\\b', 'gi');
  this.REclassTabHide = new RegExp('\\b' + this.classTabHide + '\\b', 'gi');

  this.tabs = new Array();

  if (this.div) {

    this.init(this.div);

    this.div = null;
  }
}

tabObj.prototype.init = function(e)
{

  var
  childNodes, /* child nodes of the tab div */
  i, i2, /* loop indices */
  t, /* object to store info about a single tab */
  defaultTab=0, /* which tab to select by default */
  DOM_ul, /* tabnav list */
  DOM_li, /* tabnav list item */
  DOM_a, /* tabnav link */
  aId, /* A unique id for DOM_a */
  headingElement; /* searching for text to use in the tab */

  if (!document.getElementsByTagName) { return false; }

  if (e.id) {
    this.id = e.id;
  }

  this.tabs.length = 0;

  childNodes = e.childNodes;
  for(i=0; i < childNodes.length; i++) {

    if(childNodes[i].className &&
       childNodes[i].className.match(this.REclassTab)) {
      
      t = new Object();
      
      t.div = childNodes[i];
      
      this.tabs[this.tabs.length] = t;

      if (childNodes[i].className.match(this.REclassTabDefault)) {
	defaultTab = this.tabs.length-1;
      }
    }
  }

  DOM_ul = document.createElement("ul");
  DOM_ul.className = this.classNav;
  
  for (i=0; i < this.tabs.length; i++) {

    t = this.tabs[i];


    t.headingText = t.div.title;

    if (this.removeTitle) { t.div.title = ''; }

    if (!t.headingText) {

   
      for (i2=0; i2<this.titleElements.length; i2++) {
	headingElement = t.div.getElementsByTagName(this.titleElements[i2])[0];
	if (headingElement) {
	  t.headingText = headingElement.innerHTML;
	  if (this.titleElementsStripHTML) {
	    t.headingText.replace(/<br>/gi," ");
	    t.headingText = t.headingText.replace(/<[^>]+>/g,"");
	  }
	  break;
	}
      }
    }

    if (!t.headingText) {
     
      t.headingText = i + 1;
    }


    DOM_li = document.createElement("li");

    t.li = DOM_li;

    DOM_a = document.createElement("a");
    DOM_a.appendChild(document.createTextNode(t.headingText));
    DOM_a.href = "javascript:void(null);";
    DOM_a.title = t.headingText;
    DOM_a.onclick = this.navClick;


    DOM_a.tab = this;
    DOM_a.tabIndex = i;

    /* Do we need to add an id to DOM_a? */
    if (this.addLinkId && this.linkIdFormat) {

      /* Determine the id name */
      aId = this.linkIdFormat;
      aId = aId.replace(/<tabid>/gi, this.id);
      aId = aId.replace(/<tabnumberzero>/gi, i);
      aId = aId.replace(/<tabnumberone>/gi, i+1);
      aId = aId.replace(/<tabtitle>/gi, t.headingText.replace(/[^a-zA-Z0-9\-]/gi, ''));

      DOM_a.id = aId;
    }


    DOM_li.appendChild(DOM_a);


    DOM_ul.appendChild(DOM_li);
  }

  e.insertBefore(DOM_ul, e.firstChild);

  e.className = e.className.replace(this.REclassMain, this.classMainLive);

  this.tabShow(defaultTab);

  if (typeof this.onLoad == 'function') {
    this.onLoad({tab:this});
  }

  return this;
};


tabObj.prototype.navClick = function(event)
{

  var
  rVal, /* Return value from the user onclick function */
  a, /* element that triggered the onclick event */
  self, /* the tab object */
  tabIndex, /* index of the tab that triggered the event */
  onClickArgs; /* args to send the onclick function */

  a = this;
  if (!a.tab) { return false; }

  self = a.tab;
  tabIndex = a.tabIndex;

  a.blur();

  if (typeof self.onClick == 'function') {

    onClickArgs = {'tab':self, 'index':tabIndex, 'event':event};

    if (!event) { onClickArgs.event = window.event; }

    rVal = self.onClick(onClickArgs);
    if (rVal === false) { return false; }
  }

  self.tabShow(tabIndex);

  return false;
};


tabObj.prototype.tabHideAll = function()
{
  var i; /* counter */

  for (i = 0; i < this.tabs.length; i++) {
    this.tabHide(i);
  }
};


tabObj.prototype.tabHide = function(tabIndex)
{
  var div;

  if (!this.tabs[tabIndex]) { return false; }

  div = this.tabs[tabIndex].div;

  if (!div.className.match(this.REclassTabHide)) {
    div.className += ' ' + this.classTabHide;
  }
  this.navClearActive(tabIndex);

  return this;
};


tabObj.prototype.tabShow = function(tabIndex)
{

  var div;

  if (!this.tabs[tabIndex]) { return false; }

  this.tabHideAll();

  div = this.tabs[tabIndex].div;

  div.className = div.className.replace(this.REclassTabHide, '');

  this.navSetActive(tabIndex);

  if (typeof this.onTabDisplay == 'function') {
    this.onTabDisplay({'tab':this, 'index':tabIndex});
  }

  return this;
};

tabObj.prototype.navSetActive = function(tabIndex)
{
 

  this.tabs[tabIndex].li.className = this.classNavActive;

  return this;
};


tabObj.prototype.navClearActive = function(tabIndex)
{
  this.tabs[tabIndex].li.className = '';

  return this;
};



function tabAutomatic(tabArgs)
{

  var
    tempObj, /* Temporary tab object */
    divs, /* Array of all divs on the page */
    i; /* Loop index */

  if (!tabArgs) { tabArgs = {}; }

  tempObj = new tabObj(tabArgs);

  divs = document.getElementsByTagName("div");
  for (i=0; i < divs.length; i++) {
    
    if (divs[i].className &&
	divs[i].className.match(tempObj.REclassMain)) {
      
      tabArgs.div = divs[i];
      divs[i].tab = new tabObj(tabArgs);
    }
  }
  
  return this;
}



function tabAutomaticOnLoad(tabArgs)
{

  var oldOnLoad;

  if (!tabArgs) { tabArgs = {}; }


  oldOnLoad = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = function() {
      tabAutomatic(tabArgs);
    };
  } else {
    window.onload = function() {
      oldOnLoad();
      tabAutomatic(tabArgs);
    };
  }
}


if (typeof tabOptions == 'undefined') {

    tabAutomaticOnLoad();

} else {

  if (!tabOptions['manualStartup']) {
    tabAutomaticOnLoad(tabOptions);
  }

}
