
///////////////////////////////////////////////////////
// TabStrip v1.1 (c) 2006-2008 suvadia.com
///////////////////////////////////////////////////////
function BHTabStrip(bh)
{
	bhUI.createObject('TabStrip',this);
	var div;
	if(bh.id.nodeName)div=bh.id;
	else div=bhUI.element(bh.id);
	this.strips=new Array();
	this.tab=null;
	this.div=div;
	this.skin='TabStrip TabStrip'+(bh.skin?bh.skin:'Default');
	var contents=bhUI.createElement();
	contents.style.position='relative';
	div.parentNode.insertBefore(contents,div);
	contents.style.top=div.offsetHeight+'px';
	this.tabsContents=contents;
	if(bh.tabs){	
		var t,i;
		for(i=0;i<bh.tabs.length;i++){
			t=bh.tabs[i];
			if(bh.flags){
				if(bh.flags&0x01)this.addLevel(t[0],bh.height,true);
			}
			this.addTab(t[0],t[1],t[2],t[3]);
		}
	}
	this.tabClick=bh.onclick?bh.onclick:__DF;
}

BHTabStrip.prototype.addLevel=function(level,h ,mode)
{
	var strip,div;
	if(typeof(this.strips[level])!=__UF)return;
	div=this.div.addElement();
	div.className=this.skin;
	div=div.addElement();
	div.className="levelright";
    div=div.addElement();
	div.className='levelwrap level'+level;
	strip=div.addElement('UL');
	strip.className="levelleft";
	if(mode){
	div=this.div.addElement();
	div.style.position='relative';
	div.style.clear="both";
	strip.tabsContents=div;
	}else{
	strip.tabsContents=this.tabsContents;}
	this.strips[level]=strip;
	strip.mode=mode;strip.oheight=h;
	return strip;
}

BHTabStrip.prototype.addTab=function(level, id, caption, tip)
{
	var strip=this.strips[level];
	if(typeof(strip)==__UF){
	strip=this.addLevel(level,0,0);}
	var s=id+'-content';
	var content=bhUI.element(s);
	if(content==null){
	content=strip.tabsContents.addElement();
	content.id=s;}else{
	strip.tabsContents.appendChild(content);}
	content.style.position='absolute';
	content.style.visibility='hidden';
	content.style.left=content.style.top='0px';
	content.style.display='block';
	var li=strip.addElement('LI');li.id='';
	var	a=li.addElement('A');a.id=id;a.href='';
	if(strip.mode)li.className='panel';
	s=typeof(caption)=='string'?caption:id;
	a.innerHTML='<span class="wrap"><span class="innerWrap">'+s+'</span></span>';
	a.onclick=bhUI.eventHandle(this.Tx+'.onClick');
	a.strip=strip;
	a.tabno=(strip.childNodes.length-1);
}

BHTabStrip.prototype.setCaption=function(id,caption,tip)
{
	var span=this.findTab(id).firstChild.firstChild;
	span.innerHTML=caption;
	if(tip)span.title=tip;
}

BHTabStrip.prototype.getTabContent=function(ref)
{
	return this.findTab(ref).strip.tabsContents.childNodes[ref];
	
}

BHTabStrip.prototype.disableTab=function()
{
	var e=this.findTab(arguments[0]);
	e.className=(arguments.length==1||arguments[1])?'disabled':'';
	if(this.tab==e)
	{
		var nodes=this.tabsContents.childNodes;
		var i,id=e.id+'-content';
		for(i=0;i<nodes.length;i++)
	  	{
			if(nodes[i].id==id)
			{
				nodes[i].style.visibility='hidden';
				if(bhUI.Browser.IE)nodes[i].style.left='-1000px';
			   	break;
			}
	  	}
		this.tab=null;	
	}	
}

BHTabStrip.prototype.findTab=function(ref)
{
	var l, i, n;
	switch(typeof(ref))
	{
	case 'object':return ref;
	case 'string':
		{			
			for(l=1;l<this.strips.length;l++)
			{
				n=this.strips[l].childNodes;
				for(i=0;i<n.length;i++)
				{						
					if(n[i].firstChild.id==ref)
					return n[i].firstChild;
				}
			}
		}
		break;
	default:
		{
			n=this.strips;
			for(i=1;i < n.length; i++)
			{
				l = n[i].childNodes.length;	
				if(ref >= l)ref-=l;
				else return n[i].childNodes[ref].firstChild;
			}
		}
	}
	return null;
}

BHTabStrip.prototype.Tab=function(ref)
{
	var changed=false,e,i,nodes,id;
		
	if(!(e=this.findTab(ref)))return;
	
	if(e.className=='disabled')
		  return;
		  
	var strip;	
	if(this.tab!=e)
	{
		if(this.tab)
		{
			strip=this.tab.strip;
			nodes=strip.tabsContents.childNodes;
			this.tab.className='';id=this.tab.id+'-content';
	  		for(i=0;i<nodes.length;i++)
	  		{
	  		    if(nodes[i].id==id)
				{
					nodes[i].style.visibility='hidden';					
					if(bhUI.Browser.IE)nodes[i].style.left='-1000px';
					if(strip.mode){
					if(strip!=e.strip){
					strip.tabsContents.style.height='0px';
					strip.tabsContents.style.visibility='hidden';}}
			   	    break;
				}
	  		}
		}	
		id=e.id+'-content';
		strip=e.strip;
		nodes=strip.tabsContents.childNodes;
		for(i=0;i<nodes.length;i++)
		{
			if(nodes[i].id==id)
			{
				if(strip.mode){
				strip.tabsContents.style.height=strip.oheight+'px';
				strip.tabsContents.style.visibility='inherit';}
				nodes[i].style.visibility='inherit';
				if(bhUI.Browser.IE)nodes[i].style.left='0px';
				break;
			}
		}
		e.className=strip.mode?'panel-selected':'selected';
		this.tab=e;changed=true;
	}
	this.tabClick(e.id,e.tabno,changed);
}

BHTabStrip.prototype.onClick=function(evt)
{
	var e=evt.target;
	if(e.nodeName!='A')
	{
		while(e.className!='wrap')e=e.parentNode;
		e=e.parentNode;
	}
	this.Tab(e);
	evt.preventDefault();
	return false;
}
