if (typeof dropit=="undefined"){var dropit=new Object();}
if (typeof dropit.tabStrip=="undefined"){dropit.tabStrip=new Object();}

dropit.tabStrip.Init = function()
{
	if (dropit_tabStrip_controls)
	{
		for( var i=0; i<dropit_tabStrip_controls.length; i++ ) 
		{
			var o	= dropit_tabStrip_controls[i];
			var ts	= document.getElementById(o.tabstrip);
			if (ts != null)
			    ts.tabStrip = new dropit.tabStrip.TabStrip(ts, o.activetab, o.tabs, o._activeTabChanged);
		}
	}
}

dropit.tabStrip.TabStrip = function(ts, at, t, _activeTabChanged)
{
	this.element	= ts;
	this.activeTab = getFormElementByName(at);
	this.activeTabChanged = _activeTabChanged;
	
	// Setup tabs
	this.tabs = new Array(t.length);
	for ( var i=0; i<t.length; i++)
		this.tabs[i] = new dropit.tabStrip.Tab(this, i, t[i], i == this.activeTab.value);

	this.activateTab(this.activeTab.value);
}

dropit.tabStrip.TabStrip.prototype = 
{
	activateTab: function(index)
	{
		this.activeTab.value = index;
		this.tabs[index].activate();
		if (this.activeTabChanged != null)
		    this.activeTabChanged(this.tabs[index]);
	},

	switchTab: function(index)
	{
		var current = this.activeTab.value;
		if ( current != index )
		{
			this.tabs[current].deactivate();
			this.activateTab(index);
		}
	}

}

dropit.tabStrip.Tab = function(ts, index, s, active, nav)
{
	var el = xGetElementById(s.tab);
	el.tab			= this;
	this.element	= el;
	this.tabStrip	= ts;
	this.index		= index;
	this.hoverCss	= s.hoverCss;
	this.activeCss	= s.activeCss;
	this.panel		= xGetElementById(s.panel);
	addHoverClassName(this.element,this.hoverCss);
	if ( !s.link ) addEvent(this.element,"click",dropit.tabStrip.Tab.eventHandlers.click);

	if (!active) this.deactivate();
}

dropit.tabStrip.Tab.eventHandlers = 
{
	click: function(e)
	{
		var tab = dropit.tabStrip.Tab.eventHandlers.getTab(e);
		tab.tabStrip.switchTab(tab.index);
	},

	getEventTarget:	function(e)
	{
		if (e) { return e.target || window.event.srcElement; }
		return null;
	},
	
	getTab: function(e)
	{
		var el = dropit.tabStrip.Tab.eventHandlers.getEventTarget(e);
		while (el != null && el.tab == null)
			el = el.parentNode;
		return el ? el.tab : null;
	}
}

dropit.tabStrip.Tab.prototype = 
{
	deactivate: function()
	{
		removeClassName(this.element, this.activeCss);
		// Hide panel
		if ( this.panel )
			this.panel.style.display='none';
	},
	
	activate: function()
	{
		appendClassName(this.element, this.activeCss);
		// Show panel
		if ( this.panel )
			this.panel.style.display='block';
	}

}

