var Fabtabs = Class.create();
Fabtabs.prototype = {
	initialize : function(element) {
		this.element = $(element);
		if (!this.element) { return; }
		this.initDone=false;
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.initialTab = this.getInitialTab();
		this.show(this.initialTab);
		this.tabCount=0;
		this.selectedIndex=0;
		this.menu.each(this.setupTab.bind(this));
		this.initDone=true;
	},
	setupTab : function(elm) {
		elm.tabIndex=this.tabCount;
		this.tabCount++;
		if (elm != this.initialTab)
			this.hide(elm);
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
	},
	hide : function(elm) {
		$(elm).removeClassName('active-tab');
		var panel = $(this.tabID(elm));
		panel.removeClassName('active-tab-body');
		panel.style.display='none';
	},
	show : function(elm) {
		elm = $(elm);
		this.currentTab = elm;
		this.selectedIndex = elm.tabIndex;
		if (this.initDone)
			Event.dispatchCustom("tabSelected",elm);
		elm.addClassName('active-tab');
		var panel = $(this.tabID(elm));
		panel.addClassName('active-tab-body');
		panel.style.display='block';
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	}
}
//Event.observe(window,'load',function(){ new Fabtabs('tabs'); },false);
