/**
 * domTab.js 
 * version 2.2
 * written by Chris Heilmann
 * more info: http://www.onlinetools.org/tools/domtab.php
 *
 * Rewritten by Pontus Östlund (http://www.poppa.se) 2006-09-06
 * Added the "tabon" to the active LI as well
 * Made it an object!
*/

DOMTabs = function(localID)
{
  // Global variables
  this.currentTab  = null;
  this.currentLink = null;
  this.currentList = [];

  // Change if you want to use another class for highlighting
  this.tabHighlightClass = 'tabon';
  this.initTabs(localID);
};

DOMTabs.prototype =
{
  initTabs: function (localID)
  {
    // change if you have another main navigation ids for tabbed or normal element id
    var navElement = localID || 'mainnav';
    var navElementTabbedId = localID ? localID + 'tabbed' : 'mainnavtabbed';

    // pattern to check against to identify "back to menu" links
    var backToMenu=/#top/;

    var n, as, id, i, cid, linklength, lastlink, re;

    if (document.getElementById && document.createTextNode) {
      cid = window.location.toString().match(/#(\w.+)/);

      if (cid && cid[1]) { cid = cid[1]; }

      var n = document.getElementById(navElement);

      if (!n) return;

      n.id = navElementTabbedId;
      n = document.getElementById(navElementTabbedId);
      var as = n.getElementsByTagName('a');

      for (i = 0; i < as.length; i++) {
	as[i].__master__ = this;
	as[i].gotOnClick = false;
	
	try {
	  as[i].gotOnClick = 
	    as[i].onclick.toString().match(/(\w.+[; ]?)+/g)[1];
	} catch (e) { }
	
	as[i].onclick = function()
	{
	  this.__master__.showTab(this);
	  if (this.gotOnClick) eval(this.gotOnClick);
	  return false;
	}

	id = as[i].href.match(/#(\w.+)/)[1];

	if (!cid && i == 0) {
	  this.currentTab  = id;
	  this.currentLink = as[i];
	  this.currentList = as[i].parentNode;
	}
	else if (cid && id != cid && i == 0) {
	  this.currentTab = id;
	  this.currentLink = as[i];
	  this.currentList = as[i].parentNode;
	}
	else if (id == cid) {
	  this.currentTab  = id;
	  this.currentLink = as[i];
	  this.currentList = as[i].parentNode;
	}

	if (document.getElementById(id)) {
	  linklength = document.getElementById(id)
			       .getElementsByTagName('a').length;
	  if (linklength > 0) {
	    lastlink = document.getElementById(id)
			       .getElementsByTagName('a')[linklength-1];
	    if (backToMenu.test(lastlink.href))
	      lastlink.parentNode.removeChild(lastlink);
	  }
	  document.getElementById(id).style.display = 'none';
	}
	//if (cid) {
	//	window.location.hash = 'top'; 
	//}
      }

      if (this.currentTab && document.getElementById(this.currentTab))
	document.getElementById(this.currentTab).style.display = 'block';

      re = new RegExp('\\b'+ this.tabHighlightClass + '\\b');

      try {
	if (!re.test(this.currentLink.className)) {
	  this.currentLink.className = 
		  this.currentLink.className + ' ' + 
		  this.tabHighlightClass;
	}
	if (!re.test(this.currentList.className)) {
	  this.currentList.className = 
		  this.currentList.className + ' ' + 
		  this.tabHighlightClass;
	}
      }
      catch (e) { }
    }
  },

  showTab: function(o)
  {
    var id;
    if (this.currentTab) {
      if (document.getElementById(this.currentTab)) {
	document.getElementById(this.currentTab).style.display = 'none';
      }
      this.currentLink.className = 
	this.currentLink.className.replace(this.tabHighlightClass,'');
      this.currentList.className = 
	this.currentList.className.replace(this.tabHighlightClass,'');
    }

    var id = o.href.match(/#(\w.+)/)[1];
    this.currentTab  = id;
    this.currentLink = o;
    this.currentList = o.parentNode;

    if (document.getElementById(id)) {
      document.getElementById(id).style.display = 'block';
    }

    var re = new RegExp('\\b' + this.tabHighlightClass + '\\b');

    if (!re.test(o.className)) {
      o.className = o.className + ' ' + this.tabHighlightClass;
    }

    if (!re.test(this.currentList.className)) {
      this.currentList.className = this.currentList.className + ' ' + 
                                   this.tabHighlightClass;
    }
  }
};