/*
 * 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) {
		this.element = $(element);

		if (!this.element){
			return;
		}

		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		//this.show(this.getInitialTab());

		// Rather than hide all and show the selected by default,
		// we'll do the opposite for non-javascript
		this.show(this.getInitialTab());
		this.menu.without(this.getInitialTab()).each(this.hide.bind(this));

		this.menu.each(this.setupTab.bind(this));
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false);
		// Stop the annoying focus border
		Event.observe(elm,'focus',
		function(ev){
			var elm = Event.findElement(ev, "a");
			Event.stop(ev);
			elm.blur();
		}.bindAsEventListener(this),false);

		getElementsByClassName('no-js').each(Element.hide);
	},
	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');
		//$(this.tabID(elm)).removeClassName('active-tab-body');
		$(this.tabID(elm)).setStyle({
			position: 'absolute',
			visibility: 'hidden'
		});
	},
	show : function(elm) {
		$(elm).addClassName('active-tab');
		//$(this.tabID(elm)).addClassName('active-tab-body');

		$(this.tabID(elm)).setStyle({
			position: 'static',
			visibility: 'visible'
		});
	},
	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(){

	getElementsByClassName('tabs').each(function(value){
		if (typeof(value.id) != 'undefined'){
			new Fabtabs(value.id);
		}
	});

},false);
*/
