/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw
 * version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/
 */
var Fabtabs = Class.create();

Fabtabs.prototype = {
	initialize : function(element,firstTab) {
		this.element = $(element);
		this.firstTab = firstTab;
		if (!(this.firstTab)) { this.firstTab = '' }
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));
	},
	setupTab : function(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');
		$(this.tabID(elm)).removeClassName('active_body');
	},
	show : function(elm) {
		$(elm).addClassName('active');
		$(this.tabID(elm)).addClassName('active_body');

	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if (this.firstTab != '') {
			var tabName = this.firstTab;
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == tabName; });
			return elm || this.menu.first();
		} else 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 {
			var elm = this.menu.find(function(value) { return value.className.match(new RegExp('(\\s|^)' + 'active' + '(\\s|$)')); });
			return elm || this.menu.first();
		}
	}
}

//return this.menu.first();

//var loc = RegExp.$1;
//var elm = this.menu.find(function(value) { return value.className.match(/(\w.+)/)[1] == 'active'; });
//var elm = this.menu.find(function(value) { return value.className.match(new RegExp('(\\s|^)' + 'active' + '(\\s|$)')); });
//return elm || this.menu.first();
