function TabbedPane(idTable, cssClasses) {
	this.id = idTable;
	this.ths = null;
	this.tds = null;
	this.table = document.getElementById(idTable);
	if(this.table.offsetHeight>50) {
		var cssClasses = (cssClasses) ? cssClasses : new Object();
		this.foregroundClass = cssClasses.foreground || 'tabbed_pane_on';
		this.backgroundClass = cssClasses.background || 'tabbed_pane_off';
		this.contentClass = cssClasses.content || 'tabbed_pane_content';
		this.setTabsAndDivs();
		this.setBehaviour();
		this.setLayout();
		this.setTab(0);
	}
}

TabbedPane.prototype.constructor = TabbedPane;

TabbedPane.prototype = {
	setTabsAndDivs: function() {
		this.table.className = 'tabbed_pane';
		
		this.thead = Element.getChildElements(this.table,'thead')[0];
		this.thead.trs = Element.getChildElements(this.thead,'tr');
		if(this.thead.trs.length != 1) {
			Log.error('Table ' + this.id + ' must have one "tr" in "thead".');
		}
		this.ths = Element.getChildElements(this.thead.trs[0],'th');
		
		this.tbody = Element.getChildElements(this.table,'tbody')[0];
		this.tbody.trs = Element.getChildElements(this.tbody,'tr');
		if(this.tbody.trs.length != 1) {
			Log.error('Table ' + this.id + ' must have one "tr" in "tbody".');
		}
		this.tds = Element.getChildElements(this.tbody.trs[0],'td');
		
		if(this.ths.length != this.tds.length) {
			Log.error('Table ' + this.id + ' must have as many "th" in "thead" as "td" in the "tbody".');
		}
	},
	
	setBehaviour: function() {
		var TabbedPane = this;
		for(i=0;i<this.ths.length;i++) {
			this.ths[i].setAttribute('tab_index',i);
			this.ths[i].onclick = function() {
				TabbedPane.setTab(this.getAttribute('tab_index'));
			}
		}
	},
	
	setLayout: function() {
		this.table.setAttribute('cellspacing',0);
		this.table.cellspacing = '0';
		
		var height = 0, width = 0, display;
		
		for(var i=0;i<this.ths.length;i++) {
			this.ths[i].style.width = Math.round(100/this.ths.length)+'%';
			this.ths[i].style.cursor = 'pointer';
			
			this.setTab(i);
			
			this.tbody.trs[0].firstChild.className = this.contentClass;
			this.tbody.trs[0].firstChild.className = this.contentClass;
			this.tbody.trs[0].firstChild.setAttribute('colspan', this.ths.length);
			this.tbody.trs[0].firstChild.colSpan = this.tds.length;
			
			if(this.tbody.trs[0].firstChild.offsetHeight > height) {
				height = this.tbody.trs[0].firstChild.offsetHeight;
			}
			if(this.tbody.trs[0].firstChild.offsetWidth > width) {
				width = this.tbody.trs[0].firstChild.offsetWidth;
			}
		}
		
		
		for(var i=0;i<this.ths.length;i++) {
			this.setTab(i);
			
			this.tbody.trs[0].firstChild.style.height = height;
			//this.tbody.trs[0].firstChild.style.width = width;
		}
		
		this.table.style.width = width;
	},
	
	setTab: function(index)	{
		while(this.tbody.trs[0].childNodes.length > 0) {
			this.tbody.trs[0].removeChild(this.tbody.trs[0].firstChild);
		}
		this.tbody.trs[0].appendChild(this.tds[index]);
		for(var i=0;i<this.ths.length;i++) {
			this.ths[i].className = this.backgroundClass;
		}
		this.ths[index].className = this.foregroundClass;
	}
}
