var TTabSheets = function (name,sheets,rounded,base_class)
{
	this.self = name;
	this.active_sheet = 0;
	this.sheets = sheets;
	this.rounded = rounded || false;
	this.OnClick = new Array();
	this.OnFirstClick = new Array();
	this.base_class = base_class || 'TTabSheets_';
}

TTabSheets.prototype.draw = function (intoTag)
{
	var bc = this.base_class;
	var s = this.self;

	var parent = document.getElementById(intoTag);
	if (parent==null)
	{
		alert('Parent div of '+s+' is null!');
		return false;
	}
	
	var tabs_set = document.createElement("div");
	tabs_set.id = s+'_tabsset';
	tabs_set.className = bc+'tabsset';
	
	if (this.rounded==true)
	{
		var top_line = document.createElement("div");
		top_line.className = bc+'top';
		tabs_set.appendChild(top_line);
	}
	
	var workplace = document.createElement("div");
	workplace.id = s+'_workplace';
	workplace.className = bc+'workplace';
	
	for (var i=0;i<sheets.length;i++)
	{
		var tab = document.createElement("div");
		tab.id = s+'_tab_'+i;
		if (this.active_sheet == i) tab.className = bc+'tab_act'; else tab.className = bc+'tab';
		tab.innerHTML = '<a href="javascript:'+s+'.set_active_sheet('+i+');" class="">'+sheets[i]+'</a>';
		tabs_set.appendChild(tab);
		
		var work = document.createElement("div");
		work.id = s+'_work_'+i;
		if (this.active_sheet == i) work.className = bc+'work_act'; else work.className = bc+'work';
		workplace.appendChild(work);
	}
	
	parent.appendChild(tabs_set);
	parent.appendChild(workplace);
	
	if (this.rounded==true)
	{
		var ltop = document.createElement("div");
		ltop.className = bc+'ltop';
		
		var rtop = document.createElement("div");
		rtop.className = bc+'rtop';
		
		var btop = document.createElement("div");
		btop.className = bc+'btop';
		
		top_line.appendChild(ltop);
		top_line.appendChild(btop);
		top_line.appendChild(rtop);
		
		var bottom_line = document.createElement("div");
		bottom_line.className = bc+'bottom';
		bottom_line.innerHTML = '<div class="'+bc+'lbot"></div>';
		bottom_line.innerHTML += '<div class="'+bc+'bbot"></div>';
		bottom_line.innerHTML += '<div class="'+bc+'rbot"></div>';

		parent.appendChild(bottom_line);
	}
}

TTabSheets.prototype.set_active_sheet = function(i)
{
	var tabsheet = document.getElementById(this.self+'_tab_'+this.active_sheet);
	var worksheet = document.getElementById(this.self+'_work_'+this.active_sheet);
	
	if (tabsheet!=null) tabsheet.className = this.base_class+'tab';
	if (worksheet!=null) worksheet.className = this.base_class+'work';
	
	this.active_sheet = i;
	
	var tabsheet = document.getElementById(this.self+'_tab_'+this.active_sheet);
	var worksheet = document.getElementById(this.self+'_work_'+this.active_sheet);
	
	if (tabsheet!=null) tabsheet.className = this.base_class+'tab_act';
	if (worksheet!=null) worksheet.className = this.base_class+'work_act';
	
	if (this.OnFirstClick[this.active_sheet]!=null&&this.OnFirstClick[this.active_sheet]!="undefined")
	{
		eval(this.OnFirstClick[this.active_sheet]);
		delete(this.OnFirstClick[this.active_sheet]);
	}
	if (this.OnClick[this.active_sheet]!=null&&this.OnClick[this.active_sheet]!="undefined") eval(this.OnClick[this.active_sheet]);

}

TTabSheets.prototype.add_sheet = function (caption,buttons,innerdata)
{
	this.sheets.push(caption);
	for (var last in this.sheets) {}
	
	var top_div = document.getElementById(this.self+'_tabsset');
	
	var tempdiv = document.createElement("div");
	tempdiv.id = this.self+'_tab_'+last;
	tempdiv.className = this.base_class+'tab';
	tempdiv.innerHTML = '<a href="javascript:'+this.self+'.set_active_sheet('+last+');">'+this.sheets[last]+'</a>';
	
	if (buttons != null)
	{
		for (var i=0;i<buttons.length;i++) tempdiv.innerHTML += '<a href="'+buttons[i].action+'" title="'+buttons[i].title+'">'+buttons[i].caption+'</a>';
	}

	top_div.appendChild(tempdiv);
	
	var bottom_div = document.getElementById(this.self+'_workplace');
	if (innerdata!=null && innerdata!='') 
	{
		var tempdiv = document.getElementById(innerdata);
		bottom_div.appendChild(tempdiv);
		tempdiv.id = this.self+'_work+'+last;
		tempdiv.className = this.base_class+'work';
	}
	else
	{
		var tempdiv = document.createElement("div");
		tempdiv.id = this.self+'_work+'+last;
		tempdiv.className = this.base_class+'work';
		tempdiv.innerHTML = '';
		bottom_div.appendChild(tempdiv);
	}
	
	this.set_active_sheet(last);
	return last;
}

TTabSheets.prototype.append_to_sheet = function (into,app_elem)
{
	var tempdiv = document.getElementById(this.self+'_work_'+into);
	var app_elem_el = document.getElementById(app_elem);
	app_elem_el.style.display = 'block';
	tempdiv.appendChild(app_elem_el);
}

TTabSheets.prototype.delete_sheet = function (i)
{
	if (i == null) i = this.active_sheet;
	var tab = document.getElementById(this.self+'_tab_'+i);
	var work = document.getElementById(this.self+'_work_'+i);
	tab.innerHTML = '';
	tab.style.display = 'none';
	work.innerHTML = '';
	work.style.display = 'none';
	delete(this.sheets[i]);
	if (this.active_sheet == i)
	{
		for (last in this.sheets) {}
		if (last!=null) this.set_active_sheet(last);
	}
}

TTabSheets.prototype.SetOnFirstSheetClick = function (sheet_id,action)
{
	if (this.OnFirstClick[sheet_id]!=null&&this.OnFirstClick[sheet_id]!="undefined") this.OnFirstClick[sheet_id] += '     '+action;
	else this.OnFirstClick[sheet_id] = action;
}

TTabSheets.prototype.SetOnSheetClick = function (sheet_id,action)
{
	if (this.OnClick[sheet_id]!=null&&this.OnClick[sheet_id]!="undefined") this.OnClick[sheet_id] += '     '+action;
	else this.OnClick[sheet_id] = action;
}