//-----------------------------------------------------------------------------
function TabSet(container_id) {
  this.container = document.getElementById(container_id);
}


//-----------------------------------------------------------------------------
TabSet.prototype.initialize = function() {
  this.selectedIndex = -1;
}


//-----------------------------------------------------------------------------
TabSet.prototype.add = function(title, callback, pageelementid) {

  var pel = pageelementid ? document.getElementById(pageelementid) : null;
  if (!pel || pel.innerHTML == '') {
    return;
  }

  var tab = document.createElement('div');
  tab.className = 'tab';
	
  var tableft = document.createElement('div');
  tableft.className = 'tab_left';
  tableft.origClass = tableft.className;
  tab.appendChild(tableft);

  var tabcenter = document.createElement('div');
  tabcenter.className = 'tab_center';
  tabcenter.origClass = tabcenter.className;
  tabcenter.innerHTML = title;
  tab.appendChild(tabcenter);

  var tabright = document.createElement('div');
  tabright.className = 'tab_right';
  tabright.origClass = tabright.className;
  tab.appendChild(tabright);
	
  tab.tabindex = this.container.childNodes.length;
  tab.owner = this;
  tab.onmouseover = this.tab_mouseover;
  tab.onmouseout = this.tab_mouseout;
  tab.onclick = this.tab_click;
  tab.deselect = this.tab_deselect;
  tab.callback = callback;
  tab.pageelement = pel;
  this.container.appendChild(tab);
}


//-----------------------------------------------------------------------------
TabSet.prototype.tab_mouseover = function() {

  if (this.tabindex == this.owner.selectedIndex) {
    return;
  }

  for (var i = 0; i < this.childNodes.length; i++) {
    this.childNodes[i].className += ' ' + this.childNodes[i].className + '_hover';
  }
}


//-----------------------------------------------------------------------------
TabSet.prototype.tab_mouseout = function() {

  if (this.tabindex==this.owner.selectedIndex) {
    return;
  }

  for (var i = 0; i < this.childNodes.length; i++) {
    this.childNodes[i].className = this.childNodes[i].origClass;
  }
}


//-----------------------------------------------------------------------------
TabSet.prototype.tab_click = function() {

  if (this.tabindex==this.owner.selectedIndex) {
    return;
  }	

  this.owner._set_selected(this.tabindex);
  for (var i=0; i < this.childNodes.length; i++) {
    this.childNodes[i].className = this.childNodes[i].origClass + ' ' + 
      this.childNodes[i].origClass + '_active';
  }	
}


//-----------------------------------------------------------------------------
TabSet.prototype.tab_deselect = function() {
  for (var i = 0; i < this.childNodes.length; i++) {
    this.childNodes[i].className = this.childNodes[i].origClass;
  }	
}


//-----------------------------------------------------------------------------
TabSet.prototype._set_selected = function(tabindex) {
  this.selectedIndex = tabindex;
	
  var tab = document.getElementsByName('tab');
  if (tab && tab.length == 1) {
    tab[0].value = tabindex;
  }

  for (var i = 0; i < this.container.childNodes.length; i++) {
    if (i == tabindex) {
      continue;
    }

    this.container.childNodes[i].deselect();
    if (this.container.childNodes[i].pageelement) {
      this.container.childNodes[i].pageelement.style.display = 'none';
    }
  }
	
  if (this.container.childNodes[tabindex].pageelement) {
    this.container.childNodes[tabindex].pageelement.style.display = 'block';
  }

  if (this.container.childNodes[tabindex].callback) {
    this.container.childNodes[tabindex].callback(this, tabindex);
  }
}


//-----------------------------------------------------------------------------
TabSet.prototype.select = function(tabindex) {

  if (!this.container.childNodes[tabindex]) {
    return;
  }

  this.container.childNodes[tabindex].onclick();
}
