/*
# $Id$
*/

GCms.TabInterface = function(id) {

	// Vars
	var div = $(id);
	var tabGroups = [];
	this.tabs = [];
	var tabBlocks = [];
	this.currentTab = null;
	this.listeners = {
		onselecttab: []
	};

	// Apply styles
	div.className += ' GCmsTabInterface';

	// Gather UL nodes
	// The UL node(s) may or may not be contained within a DIV, depending if the ULs are floated.
	var ulNodeContainer = div.getElementsByTagName('ul')[0].parentNode;
	for(var i=0; i<ulNodeContainer.childNodes.length; i++) {
		if(ulNodeContainer.childNodes[i].nodeName.toString().toLowerCase()!='ul') {
			continue;
		}
		ulNodeContainer.childNodes[i].className += ' GCmsTabInterface-tab-group';
		tabGroups.push(ulNodeContainer.childNodes[i]);
	}
	if(ulNodeContainer.childNodes[0].parentNode!==div) {
		ulNodeContainer.childNodes[0].parentNode.className += ' GCmsTabInterface-tab-container';
	}

	// Gather DIV nodes
	for(var i=0; i<div.childNodes.length; i++) {

		// Skip #text nodes
		var node = div.childNodes[i];
		if(node.nodeType!=1) {
			continue;
		}

		// Apply styles according to node type
		switch(node.nodeName.toString().toLowerCase()) {

			/* UL */
			case 'ul':
				break;

			/* DIV */
			case 'div':
				if(!node.className.toString().match(/GCmsTabInterface\-tab\-container/)) {
					node.className += ' GCmsTabInterface-tab-block';
					tabBlocks.push(node);
				}
				break;

			/* default */
			default:
				break;
		}
	}

	// Assign event handlers to all tabs
	var count = 0;
	var _this = this;
	for(var i=0; i<tabGroups.length; i++) {

		// Assign onclick handlers to all tabs in the group
		var liNodes = tabGroups[i].getElementsByTagName('li');
		for(var j=0; j<liNodes.length; j++) {
			var a = liNodes[j].getElementsByTagName('a')[0];
			a.tabNumber = count;
			if(!a.onclick) {
				a.onclick = function() {
					_this.selectTabByNode(this);
					return false;
				}
			}
			this.tabs.push({liNode:liNodes[j], aNode:a, block:tabBlocks[count], isDisabled:false, number:this.tabs.length});
			count++;
		}
	}

	// Open the first tab
	this.selectTab(0);
}

GCms.TabInterface.create = function(id) {

	return new GCms.TabInterface(id);
}

GCms.TabInterface.prototype.selectTabByNode = function(node) {

	// Select the tab
	var tabNumber = node.tabNumber;
	return this.selectTab(tabNumber);
}

GCms.TabInterface.prototype.selectTab = function(num) {

	// Ensure the chosen tab is not disabled first
	if(this.tabs[num].isDisabled) {
		return false;
	}

	// Disable currently selected tab
	if(this.currentTab!=null) {
		this.currentTab.liNode.className = this.currentTab.liNode.className.toString().replace(/\s*GCmsTabInterface\-active/, '');
		if(this.currentTab.block) {
			this.currentTab.block.style.display = 'none';
		}
	}

	// Enable the chosen tab
	var tab = this.tabs[num];
	tab.liNode.className += ' GCmsTabInterface-active';
	if(tab.block) {
		tab.block.style.display = 'block';
	}
	this.currentTab = tab;

	// Trigger listeners
	for(var i=0; i<this.listeners['onselecttab'].length; i++) {
		this.listeners['onselecttab'][i](this.currentTab);
	}
}

GCms.TabInterface.prototype.disableTab = function(num) {
	this.tabs[num].isDisabled = true;
	this.tabs[num].liNode.className += ' GCmsTabInterface-disabled';
}

GCms.TabInterface.prototype.addListener = function(eventType, func) {
	this.listeners[eventType].push(func);
}
