/**
  A stupid simple tab viewer.

  @author: Sadrul Habib Chowdhury
 */

/**
  tabheaders = {'tabname' : 'Tab Title', ... };
 **/
TabGroup = function(tabheaders)
{
	var self = this;

	self.tabheaders = tabheaders;
	self.contents = {};
	self.selected = null;
	self.tabtitles = {};
	self.hidden = {};      /* Whether a particular tab should be hidden for the moment */
	self.num = 0;
	for (var p in self.tabheaders)
		self.num++;

	self.container = document.createElement('table');
	self.container.style.width = '100%';
	self.container.cellPadding = 0;
	self.container.cellSpacing = 0;
	self.rowtabs = 0;
}

TabGroup.prototype.set_row_tabs = function(num)
{
	this.rowtabs = num;
}

TabGroup.prototype.hide_tab = function(name)
{
	var self = this;
	self.hidden[name] = true;

	if (self.tabtitles && self.tabtitles[name]) {
		self.tabtitles[name].style.display = 'none';
		self.select(null);
	}
}

TabGroup.prototype.unhide_tab = function(name)
{
	var self = this;
	if (!self.hidden[name])
		return;

	self.hidden[name] = false;
	if (self.tabtitles[name])
		self.tabtitles[name].style.display = '';

	if (self.selected == null)
		self.select(null);
}

TabGroup.prototype.set_container = function(name, content)
{
	var self = this;
	self.contents[name] = content;
}

TabGroup.prototype.get_container = function(name)
{
	var self = this;
	return self.contents[name];
}

TabGroup.prototype.get_contents = function()
{
	var self = this;
	var table = self.container;
	
	while (table.firstChild)
		table.removeChild(table.firstChild);

	var smtable = document.createElement('table');
	smtable.cellSpacing = 0;
	smtable.cellPadding = 0;
	var tr = smtable.insertRow(-1);

	var count = 0;

	for (var name in self.tabheaders) {
		var tab = tr.insertCell(-1); //document.createElement('font');
		var img;
		tab.mystuff = name;
		tab.noWrap = true;
		tab.className = 'tabTD';

		var left = document.createElement('font');
		left.className = 'tabLeft';

		var text = document.createElement('font');
		text.className = 'tabText';
		text.innerHTML = "&nbsp;" + self.tabheaders[name] + "&nbsp;";
		text.onmouseover = function() { this.style.textDecoration = 'underline'; }
		text.onmouseout  = function() { this.style.textDecoration = ''; }
		left.appendChild(text);

		var right = document.createElement('font');
		right.className = 'tabRight';
		right.innerHTML = '&nbsp;';
		left.appendChild(right);

		tab.appendChild(left);

		if (self.hidden[name])
			tab.style.display = 'none';

		tab.onclick = function() {
			self.select(this.mystuff);
		}

		self.tabtitles[name] = tab;
		if (self.selected == null)
			self.selected = name;
		count++;
		if (self.rowtabs && count < self.num - 1 && count % self.rowtabs == 0) {
			tr.insertCell(-1).style.width = '100%';
			table.insertRow(-1).insertCell(-1).appendChild(smtable);
			var smtable = document.createElement('table');
			smtable.cellSpacing = 0;
			smtable.cellPadding = 0;
			tr = smtable.insertRow(-1);
		}
	}
	var und = tr.insertCell(-1);
	und.style.background = 'url(/images/black.gif) bottom left repeat-x';
	und.style.width = '100%';

	table.insertRow(-1).insertCell(-1).appendChild(smtable);

	tr = table.insertRow(-1);
	self.tabcontents = tr.insertCell(-1);
	self.tabcontents.colSpan = 50;
	/*self.tabcontents.style.backgroundColor = 'white';*/
	self.tabcontents.align = 'center';
	self.tabcontents.style.borderStyle = 'solid';
	self.tabcontents.style.borderWidth = '0px 1px 1px 1px';
	self.tabcontents.style.borderColor = 'black';

	self.select(self.selected);

	return self.container;
}

TabGroup.prototype.select = function(name)
{
	var self = this;
	if (!self.tabcontents)
		return;

	/*for (var i = 0; i < self.tabcontents.childNodes.length; i++)*/
		/*self.tabcontents.childNodes[i].style.display = 'none';*/
	for (var c in self.contents)
		self.contents[c].style.display = 'none';

	if (self.selected && self.selected != name) {
		var td = self.tabtitles[self.selected];
		td.className = 'tabTD';
	}

	if (name == null) {
		self.autoselect();
		name = self.selected;
	}

	if (self.contents[name])
		self.contents[name].style.display = '';
	if (self.contents[name].parentNode != self.tabcontents)
		self.tabcontents.appendChild(self.contents[name]);

	self.selected = name;
	td = self.tabtitles[self.selected];
	td.className = 'tabTDSel';
	if (self.onselect)
		self.onselect(name);
}

TabGroup.prototype.autoselect = function()
{
	var self = this;
	for (var name in self.tabheaders) {
		if (!self.hidden[name]) {
			self.selected = name;
			return;
		}
	}
}


