/* Класс для работы с вкладками внутри некоторой области */
function __Tabs(oDiv, type)
{
	this.Area = oDiv;
	this.count = 0;
	this.oUl = null;
	this.data = {};
	if(type) this.type = type;
	else this.type = 'workspace';
}

//Создаёт новую вкладку
__Tabs.prototype.create = function(tabId, title, toBegin)
{
	var self = this;
	if(!this.dataLength)	//если вкладок ещё не было
	{
		if(!this.oUl)
		{
			this.oUl = ce('UL', this.Area, {className: 'tabs'});
			this.oUl.className = 'tabs tabs_'+this.type;
		}
	}
	if(this.oUl.lastChild)
	{
		this.oUl.lastChild.className = 'sep';
		this.oUl.lastChild.innerHTML = '';
	}
	this.data[tabId] = {};
	this.data[tabId].btn = ce('LI', null, {id: tabId +'_li', innerHTML: title});
	if(!toBegin || this.oUl.childNodes.length==0)
		this.oUl.appendChild(this.data[tabId].btn);
	else
		this.oUl.insertBefore(this.data[tabId].btn, this.oUl.firstChild);

	var tmp = ce('LI', null, {className: 'sep'});
	if(this.data[tabId].btn.nextSibling)	//вставляем довесок-сепаратор
		this.oUl.insertBefore(tmp, this.data[tabId].btn.nextSibling);
	else
		this.oUl.appendChild(tmp);
	this.oUl.lastChild.className = 'sep end';
	this.oUl.lastChild.innerHTML = '<table style="width: 99%; margin: 0px;"><tbody><tr><td style="width: 100%; margin: 0px; padding: 0px;">&nbsp;</td></tr></tbody></div>';

	this.dataLength++;
	this.data[tabId].btn.onclick = function()
	{
		self.show(tabId);
	}
	
	this.data[tabId].content = ce('DIV', this.Area, {id: tabId, className: 'tabs '+'tabs_'+this.type}, {display: 'none'});
	this.count++;
	return this.data[tabId].content;
}

//Показывает вкладку с указанным id
__Tabs.prototype.show = function(tabId)
{
	for(var tid in this.data)
	{
		if(tid != tabId)
		{
			this.data[tid].btn.className = this.type;
			this.data[tid].content.style.display = 'none';
		}
		else
		{
			this.data[tid].btn.className = this.type+' on';
			this.data[tid].content.style.display = 'block';
		}
	}
}

//Удаляет вкладку со всем содержимым
__Tabs.prototype.remove = function(tabId)
{
	if(!this.data[tid])
		return false;
	de(this.data[tid].btn.nextSibling);	//довесок-перемычка (издержки вёрстки)
	de(this.data[tid].btn);
	de(this.data[tid].content);
	delete this.data[tid];
	if(this.oUl.lastChild)
		this.oUl.lastChild.className = 'sep on';

	for(var i in this.data)	//надо бы переключиться на другую вкладку, если она есть
	{
		this.show(i);
		break;
	}
	this.count--;
}

//Удаляет все вкладки
__Tabs.prototype.removeAll = function()
{
	if(this.count <= 0)
		return;
	for(var i in this.data)
	{
		de(this.data[i].content);
		delete this.data[i];
	}
	de(this.oUl);
	this.oUl = null;
	this.count = 0;
}
