if (typeof(MooTools) == "undefined")
	alert('You have to include MooTools before tabs.js!');

if (!TabControls)
var TabControls = [];

if (!Tabs)
var Tabs = {
	initialized : false,
	
	init : function () {
		$$('.tabButton').each( function(el) {
			var group = el.getAttribute('group');
			var tab = el.getAttribute('tab');
			
			if (!group) group = 0;
			
			if (!TabControls[group])
				TabControls[group] = new TabControl(group);
			
			tab = new TabPanel(tab, TabControls[group], el, el.getAttribute('tab'));
			TabControls[group].addTabPanel(tab);
			tab.setActive(el.getAttribute('active'));
			el.addEvent('click', function() {
				this._tab.activate();					
			}, false);
			el.addEvent('mouseover', function() {
				this._tab.mouseover();					
			}, false);
			el.addEvent('mouseout', function() {
				this._tab.mouseout();					
			}, false);
		});
		Tabs.initialized = true;
	},
	
	onChange: function() {}
}

var TabControl = new Class({
	initialize : function(name) {
		this.name = name;
		this.activeTab = null;
		this.tabs = {};
	},
	
	addTabPanel : function(panel) {
		//if (!this.tabs[panel.name])
		this.tabs[panel.name] = panel;
		panel.parent = this;
		if (panel.active)
			panel.activate();
	}
});

var TabPanel = new Class({
	initialize : function(name, parent, button, panel) {
		this.name = name;
		this.enabled = true;
		this.parent = $(parent);
		this.button = $(button);
		if (this.button) {
			this.button._tab = this;
			this.button.setActive = this.setActive.bind(this);
			this.button.activate = this.activate.bind(this);
			this.button.deactivate = this.deactivate.bind(this);
			this.button.enable = this.enable.bind(this);
			this.button.disable = this.disable.bind(this);
			if (this.button.getAttribute('onactivate')) {
				var a = new Function(this.button.getAttribute('onactivate'));
				this.onActivate = a.bind(this);
			}
			if (this.button.getAttribute('ondeactivate')) {
				a = new Function(this.button.getAttribute('ondeactivate'));
				this.onDeactivate = a.bind(this);
			}
		}
		this.panel = $(panel);
		if (this.panel) 
		{
			this.panel._tab = this;
			this.panel.setActive = this.setActive.bind(this);
			this.panel.activate = this.activate.bind(this);
			this.panel.deactivate = this.deactivate.bind(this);
			this.panel.activate = this.activate.bind(this);
			this.panel.deactivate = this.deactivate.bind(this);
			this.panel.enable = this.enable.bind(this);
			this.panel.disable = this.disable.bind(this);
		}
		this.active = false;
	},
	
	setActive : function(active) {
		active = (active && active=='true')?true:false;
		if (active)
			this.activate();
		else
			this.deactivate();
	},
	
	mouseover: function() {
		if (!this.enabled) return;	
		if (this.button)
			this.button.addClass('overTabButton');
	},

	mouseout: function() {
		if (!this.enabled) return;	
		if (this.button)
			this.button.removeClass('overTabButton');
	},
	
	enable: function() {
		this.enabled = true;
		if (this.button) {
			this.button.removeClass('disabledTabButton');
		}
		if (this.active)
			this.activate();		
		else
			this.deactivate();		
	},

	disable: function() {
		this.enabled = false;
		if (this.button) {
			this.button.removeClass('activeTabButton');
			this.button.removeClass('inactiveTabButton');
			this.button.removeClass('overTabButton');
			this.button.addClass('disabledTabButton');
		}
	},
	
	activate : function() {
		if (!this.enabled) return;	
		var ot = null;
		if (this.parent && this.parent.activeTab && this.parent.activeTab != this)
			ot = this.parent.activeTab;
		this.parent.activeTab = this;
		this.active = true;
		if (this.button) {
			this.button.removeClass('inactiveTabButton');
			this.button.addClass('activeTabButton');
			this.button.setAttribute('active', 'true');
		}
		if (ot)
			ot.deactivate();
		if (this.panel)
			Element.show(this.panel);
		if (this.onActivate) this.onActivate();
		if (Tabs.onChange)
			Tabs.onChange();
	},
	
	deactivate : function() {
		if (this.parent && this.parent.activeTab == this)
			this.parent.activeTab = null;
		this.active = false;
		if (this.button) {
			this.button.removeClass('activeTabButton');
			if (this.enabled)
				this.button.addClass('inactiveTabButton');
			this.button.setAttribute('active', 'false');
		}
		if (this.panel)
			Element.hide(this.panel);
		if (this.onDeactivate) this.onDeactivate();
	}
});	