/**
 * @author franca02
 */
 
tab_switcher=function(){
	// intialising the dom methods from newsi
	this.dom = new newsi.HTML.DOM();
	this.ev = new newsi.Event();
	}
	
tab_switcher.prototype.init=function(o){
	
	if( document.getElementById && document.createElement ) {
	
	this.tabArray = new Array();
	this.contentArray = new Array();
	this.tabHeaderArray = new Array();
	
	this.contVisible;
	this.tabVisible;
	this.selected;
	
	// this.tabArray is an array of the outer h3 elements of the tabs
	this.tabArray = this.dom.gebtn({parent:this.dom.e_gebid("tabbedFactBox"), el:"h3"});
	// this.contentArray is an array of the definition lists that make up the content
	this.contentArray = this.dom.gebtn({parent:this.dom.e_gebid("tabbedFactBox"), el:"dl"});
	// sets the first tab as being selected
	
	//init listeners and push the tab headers into an array for later use	
	// 	
		for (z=0;z<this.tabArray.length;z++){	
			this.tabHeaderArray.push(this.tabArray[z].innerHTML);
			this["tab_"+z] = new newsi.Event () ;
			this["tab_"+z].addListener ( "onclick" , this.tabArray[z].id , this , "_switchIt" , "false", z) ;
		}
	
	// set selected tab
	this.dom.setClass({el:this.tabArray[0], className:"tab_selected"});

	// Shift the tab headings to the top if we have js enabled
		for(i=1;i<this.contentArray.length;i++){
			var fContElm = this.dom.e_gebid("tabWrapper");
			fContElm.appendChild(this.dom.e_gebid("tab_"+(i+1)));
			this.dom.setClass({el:this.contentArray[i], className:"hide"});
			this._deselectTab(i);
		}

	// set the default selected tab (hard coded to be the first)
	this.contVisible = this.contentArray[0];
	this.tabVisible = this.tabArray[0];
	this.selected = 0;
	
	}
	

}

tab_switcher.prototype._switchIt=function(elementId){
	
	// get the element id from the event broadcaster
	var switchId = elementId[0]
	
	// only execute if we click on a tab that isnt currently selected
	if(switchId != this.selected){
		this.dom.setClass({el:this.contVisible, className:"hide"});
		this.dom.setClass({el:this.tabVisible, className:"tab"})
		// remove the anchor tag and replace the inner html
		this._deselectTab(this.selected)
	
		this.dom.setClass({el:this.contentArray[switchId], className:"supertues"});
		this.dom.setClass({el:this.tabArray[switchId], className:"tab_selected"});
		// put the anchor tag in and the inner html
		this._selectTab(switchId)
	
		this.contVisible = this.contentArray[switchId];
		this.tabVisible = this.tabArray[switchId];
		this.selected = switchId;
	
	} 
	
	
}


tab_switcher.prototype._selectTab=function(selectedId){
	this.tabArray[selectedId].removeChild(this.dom.e_gebid("link_"+selectedId));
	this.tabArray[selectedId].innerHTML = this.tabHeaderArray[selectedId];
	
}

tab_switcher.prototype._deselectTab=function(deselectedId){
	
		var tempAnchor = document.createElement("span");
		tempAnchor.setAttribute("class","mockLink");
		tempAnchor.setAttribute("id","link_"+deselectedId);
		tempAnchor.innerHTML = this.tabArray[deselectedId].innerHTML;
		this.tabArray[deselectedId].innerHTML = "";
		this.tabArray[deselectedId].appendChild(tempAnchor);
	
}
