/*
  PFrameworkCS API
  
  Copyright (C) 2004 by Jernej Kos <kostko@jweb-network.net>
  Copyright (C) 2004 by Unimatrix-One (www.unimatrix-one.org)
  
  This javascript is part of the PFramework CMS system. Any unauthorised
  use outside of this system is strictly prohibited.
*/

function getObj(name)
{
  if (document.getElementById) {
    this.obj = document.getElementById(name);
    
    if (!this.obj)
      return;
      
    this.style = document.getElementById(name).style;
  } else if (document.all) {
    this.obj = document.all[name];
    
    if (!this.obj)
      return;
    
    this.style = document.all[name].style;
  } else if (document.layers) {
    this.obj = document.layers[name];
    this.style = document.layers[name];
  }
}

function getQueryVariable(variable)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  
  return "";
}

var s_instances = new Array();

var PTabs = function(name)
{
  this.name = name;
  this.activeTab = 0;
  this.onSwitchCallback = 0;
  
  s_instances[name] = this;
}

PTabs.prototype.Register = function()
{
  var i = 1;
  
  do {
    var tab = new getObj(this.name + "_" + i);
    tab = tab.obj;
    
    if (tab) {
      this.Hide(i);
      
      // Set properties
      tab.style.cursor = 'hand';
      tab.onclick = this.OnTabClick;
      
      i++;
    }
  } while (tab);
  
  // Show the first tab
  var selectedTab = getQueryVariable("tbsl");
  if (selectedTab == "") {
    var result = window.location.href.match(/tbsl\/(\d)/);
    if (result != null)
      selectedTab = result[1];
  }

  if (selectedTab == "")
    this.Show(1);
  else
    this.Show(parseInt(selectedTab));
}

PTabs.prototype.SetOnSwitchCallback = function(callback)
{
  this.onSwitchCallback = callback;
}

PTabs.prototype.OnTabClick = function()
{
  var object = this.id.substring(0, this.id.indexOf('_'));
  s_instances[object].SwitchTo(this.id);
}

PTabs.prototype.SwitchTo = function(id)
{
  id = parseInt(id.substring(this.name.length + 1));
  
  var oldId = this.activeTab;
  this.Hide(this.activeTab);
  this.Show(id);
  
  // Call the onSwitch callback if one is registred
  if (this.onSwitchCallback)
    this.onSwitchCallback(oldId);
}

PTabs.prototype.Hide = function(id)
{
  var content = new getObj(this.name + id + "_content");
  var tab = new getObj(this.name + "_" + id);
  
  if (!content || !tab)
    return;
  
  tab.obj.className = 'ptabNormal';
  content.style.display = 'none';
}

PTabs.prototype.Show = function(id)
{
  var content = new getObj(this.name + id + "_content");
  var tab = new getObj(this.name + "_" + id);
  
  if (!content || !tab)
    return;
    
  tab.obj.className = 'ptabActive';
  content.style.display = 'block';
  
  this.activeTab = id;
}
