function eventTarget(e)
{
	var t;
	if (!e) var e = window.event;
	if (e.target) t = e.target;
	else if (e.srcElement) t = e.srcElement;
	if (t.nodeType == 3) t = t.parentNode;
	return t;
}

TabBox = {

	activeTab : null,

	init : function()
	{
		this.element = $('tabbox');
		if(this.element)
		{
			this.tablinks = this.element.getElements('li a');
			$each(this.tablinks, function(link)
			{				
				if(link.className == 'active') { this.activeTab = link; }
				var scope = this;
				link.onclick = function(e) { scope.tabClickEvent(e) };
			}, this);
			
			this.contentDivs = this.element.getElements('div.content > div');
			$each(this.contentDivs, function(content)
			{
				content.style.display = 'none';
			}, this);
				
			var hash = window.location.hash;
			if(hash)
			{
				this.selectTabByName(hash.substring(1));
			}
			else 
			{
				var targetTab = $('tabbox').getElements('ul.tabs li:nth-child(last) a')[0];
				this.selectTabByName(targetTab.href.match(/#(.+)/i)[1]);
			}
		}
	},
	
	tabClickEvent : function(e)
	{
		this.selectTabByName(eventTarget(e).href.match(/#(.+)/i)[1]);
	},
	
	deselectCurrentTab : function()
	{
		this.activeContent.style.display = 'none';
		this.activeTab.className = '';
		
		this.activeContent = null;
		this.activeTab = null;
	},
	
	selectTabByName : function(name)
	{
		if(this.activeTab != null && this.activeContent != null) { this.deselectCurrentTab(); }
	
		var id = 'tab' + name;
		var newContent = $(id);
		var newTab = $('tabbox').getElements('ul.tabs li a[href*=' + name + ']')[0];
		
		newContent.style.display = 'block';
		newTab.className = 'active';
		
		this.activeContent = newContent;
		this.activeTab = newTab;
	}
};

