/* @use Prototype 1.6 */
var tabs = function() {
	
	var undefinedGroup = '__undefinedGroup__';
	var groups = {};
	
	var buttonHandler = function(e) {
		Event.stop(e);
		if ($(this).hasClassName('g-current')) {
			$(this).removeClassName('g-current');
			$(groups[this.tabGroupName][this.tabName].content).removeClassName('g-current');
			return;
		}
		var i;
		for (i in groups[this.tabGroupName]) {
			$(groups[this.tabGroupName][i].button).removeClassName('g-current');
			$(groups[this.tabGroupName][i].content).removeClassName('g-current');
		}
		$(this).addClassName('g-current');
		$(groups[this.tabGroupName][this.tabName].content).addClassName('g-current');
	};
	
	$A($$('.tab')).each(function(item) {
	
		
		var tabType = /tab-(content|button)/.exec(item.className);
		if (tabType[1] === undefined) {
			return;
		}
		tabType = tabType[1];
		
		var tabName = /tab-name\-([^\s]+)/.exec(item.className);
		if (tabName[1] === undefined) {
			return;
		}
		tabName = tabName[1];
		
		var tabGroupName = /tab-group\-([^\s]+)/.exec(item.className);
		tabGroupName = tabGroupName[1] === undefined ? undefinedGroup : tabGroupName[1];
		
		if (groups[tabGroupName] === undefined) {
			groups[tabGroupName] = {};
		}
		if (groups[tabGroupName][tabName] === undefined) {
			groups[tabGroupName][tabName] = {};
		}
		groups[tabGroupName][tabName][tabType] = item;
		if (tabType == 'button') {
			item.tabGroupName = tabGroupName;
			item.tabName = tabName;
			item.observe('click', buttonHandler);
		}
	});
};
Event.observe(window, 'load', tabs);
