/* apps in tabs */

// all tabbed sections
function TabbedItems(){
	// go through all tabbed-sections on the page
	this.sections=new Array;
	var sections=getElementsByClassName("tabbed-section",document);
	for(var i=0;i<sections.length;i++){
		this.sections[this.sections.length]=new TabbedSection(sections[i],"ts"+i);
	}
	addClass(document.getElementsByTagName("body")[0],"tabbeditems-ok");
}

// destroy proprietary attributes
TabbedItems.prototype.destroy=function(){
	for(var i=0;i<this.sections.length;i++){
		for(var j=0;j<this.sections[i].tabs.childNodes;j++){
			this.sections[i].tabs.childNodes[j].context=null;
			this.sections[i].tabs.childNodes[j].index=null;
			this.sections[i].tabs.childNodes[j].onclick=null;
		}
	}
}

// tabbed section object
function TabbedSection(obj,id){
	// gather children and construct the menu
	this.items=getElementsByClassName("tabbed-item",obj);
	this.headers=getElementsByClassName("tabbed-header",obj);
	// create tabbed menu
	this.tabs=document.createElement("ul");
	this.tabs.className="tabs";
	for(var i=0;i<this.headers.length;i++){
		var tab=document.createElement("li");
		tab.context=this; tab.index=i;
		if(this.headers[i].title) var label=this.headers[i].title
		else var label=this.headers[i].innerHTML;
		tab.innerHTML="<a href=\"#\">"+label+"</a>";
		tab.onclick=function(){ 
			this.context.resetAll(this.index);
			return false;
		}
		this.tabs.appendChild(tab);
	}
	this.tabs.childNodes[0].className="first";
	obj.insertBefore(this.tabs,this.items[0]);
	// set defaults
	this.resetAll(0);
}

// set states
TabbedSection.prototype.resetAll=function(exception){
	for(var i=0;i<this.items.length;i++){
		if(i!=exception){
			removeClass(this.items[i],"current");
			removeClass(this.tabs.childNodes[i],"current");
		} else {
			addClass(this.items[i],"current");
			addClass(this.tabs.childNodes[i],"current");
		}
	}
}