/**********
*
*   (c) AlwinBlok 2004-2005
*
*   TabbedPanel.js
*
*   The TabbedPanel object takes `bookmark`-anchors (ie. href="#id_name") in an element.
*   It rewrites them in such a way that the referred items get shown when one of
*   those links is activated, while the previously active item gets hidden.
*   
**********/

/**********
*
*   TabbedPanel
*
**********/

// purpose: creates a new menu for a dom element containing <a> tags

function TabbedPanel(dom_obj, config_obj){
  this.config = config_obj || TabbedPanel.defaultConfig;
  this.tabs = [];
	this.refs = [];
  this.currentTab = null;
  this.init(dom_obj)
}


// purpose: provide a default config
TabbedPanel.defaultConfig = {'activeTabClass' : 'tab selected', 'inactiveTabClass' : 'tab'}


// purpose: rewrites all href's with javascript calls to show/hide the referred elements

TabbedPanel.prototype.init = function(dom_obj){
  var tabs = dom_obj.getElementsByTagName('A');
  for(var i=0; i< tabs.length; i++){
    var ref = new String(tabs[i].href);
		var p = ref.search('#')
    if(p > 0){
			tabs[i].onclick = TabbedPanel.createEventHandler(this, i)
      this.tabs.push(tabs[i]);
      this.refs.push(document.getElementById(ref.substr(p+1)));
      this.deselectTab(i);
    }
  }
	this.selectTab(0)
}


TabbedPanel.createEventHandler = function(scope, i){
  return function(){scope.selectTab(i)};
}

// purpose: set the style of the tab to current, and show the referred pane

TabbedPanel.prototype.selectTab = function(nr){
  if(this.currentTab != null)
    this.deselectTab(this.currentTab);
  this.tabs[nr].className = this.config.activeTabClass;
  this.refs[nr].style.display = 'block';
  this.currentTab = nr;
}


// purpose: set the style of the tab to non_current, and hide the referred pane

TabbedPanel.prototype.deselectTab = function(nr){
  this.tabs[nr].className = this.config.inactiveTabClass;
  this.refs[nr].style.display = 'none';
}
