// Code by Jeremy Gillick http://blog.mozmonkey.com/2007/semantic-tab-box-v20/
function Tabbox(id,config) {
 this.init(id,config);
}
Tabbox.prototype = {
 tabbox : null,
 tabs : [],
 selected : null,
 config : {header: "h3",  vertical: false,  select : -1,  padding: 0,  autosize: true},
 init : function(id, config){
  var obj = this;
  if(config){
   var name;
   for(name in config){
    this.config[name] = config[name];
   }
  }
  this.pageLoaded = false;
  if(window.addEventListener){
   window.addEventListener("load", function(){ obj.pageLoaded = true; }, false);
  }
  else if(window.attachEvent){
   window.attachEvent("onload", function(){ obj.pageLoaded = true; });
  }
  function poll(){
   var tabbox = document.getElementById(id);
   var node = tabbox;
   while(node){
    if(node.nextSibling || obj.pageLoaded){
     obj.tabbox = tabbox;
     obj.formatTabbox();
     return;
    }
    node = tabbox.parentNode;
   }
   setTimeout(poll, 25);
  }
  poll();
 },
 formatTabbox : function(){
  var obj = this;
  this.tabbox.className += " tabbox";
  this.tabbox.className += " horzTabs";
  var divs;
  var tabs = this.tabbox.getElementsByTagName(this.config.header);
  for(var i=0;i<tabs.length;i++){
   this.tabs[i] = { id: tabs[i].id, header: tabs[i], container: tabs[i].parentNode };
   divs = this.tabs[i].container.getElementsByTagName("div");
   for(var n = 0; n < divs.length; n++){
    if(divs[n].className.match(/(^|\s)tabContent(\s|$)/)){
     this.tabs[i].id = divs[n].id;
     this.tabs[i].content = divs[n];
     break;
    }
   }
  }
  var anchor;
  for(var i=0;i<this.tabs.length;i++){
   anchor = this.tabs[i].container.getElementsByTagName("a");
   if(anchor.length > 0){
    anchor = anchor[0];
    anchor.tab = this.tabs[i];
    anchor.onfocus = function(evt){
      evt = window.event || evt;
      obj.selectTab(this.tab, evt);
     }
    anchor.onclick = function(evt){
      evt = window.event || evt;
      obj.selectTab(this.tab, evt);
     }
   }
  }
  this.getSelected();
  if(this.config.select){
   this.selectTab(this.config.select);
  }
  this.adjustFootprint();
 },
 selectTab : function(tab, evt){
  if(evt){
   if(evt.preventDefault) {
    evt.preventDefault();
   } 
   else {
    evt.returnValue=false;
   }
  }
  if(!isNaN(tab)){
   tab=this.tabs[parseInt(tab)];
  }
  else if(typeof tab == "string"){
   tab=this.getTabById(tab);
  }
  if(!tab){
   return;
  }
  if(this.selected){
   this.selected.container.className=this.selected.container.className.replace(/selected/g,"");
  }
  tab.container.className+=" selected";
  this.selected=tab;
  this.adjustFootprint();
 },
 getTabById : function(id){
  for(var i=0;i<this.tabs.length;i++){
   if(this.tabs[i].id==id){
    return this.tabs[i];
   }
  }
  return null;
 },
 getSelected : function(){
  for(var i=0;i<this.tabs.length;i++){
   if(this.tabs[i].container.className.match(/(^|\s)selected(\s|$)/)){
    this.selected=this.tabs[i];
    return i;
    break;
   }
  }
 },
 adjustFootprint : function(){
  if(this.config.autosize==false){return;}
  if(!this.selected){return;}
  var height=this.selected.content.clientHeight+5;
  height+=this.selected.header.clientHeight;
  this.tabbox.style.height = height+"px";
 }
}