/*
 * 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);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));
		
		Event.observe($("toptab1"),'click',this.showtab1.bindAsEventListener(this),false);
		Event.observe($("toptab2"),'click',this.showtab2.bindAsEventListener(this),false);
		Event.observe($("toptab3"),'click',this.showtab3.bindAsEventListener(this),false);
	},
	setupTab : function(elm) {
		
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	showtab1 : function(ev) {
		
		//Event.stop(ev);
		this.showtab($("t1"));
	},
	showtab2 : function(ev) {
		
		//Event.stop(ev);
		this.showtab($("t2"));
	},
	showtab3 : function(ev) {
		
		//Event.stop(ev);
		this.showtab($("t3"));
	},
	showtab : function(elm) {
		
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
	},
	
	activate :  function(ev) {
		
		var elm = Event.findElement(ev, "a");
		//alert(elm);
		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');
	},
	show : function(elm) {
		$(elm).addClassName('active-tab');
		$(this.tabID(elm)).addClassName('active-tab-body');
		
	},
	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();
		}
	}
}

function showTab(elm) {
	if (elm == 'tab1') {
		//var element = document.getElementById("t1");
		//element.addEventListener("click", myFavoriteFunction, false);
		//element.removeEventListener("click", myFavoriteFunction, false);
		document.getElementById('t1').onclick;
	}
	if (elm == 'tab2') {
		document.getElementById('t2').onclick;
	}
	if (elm == 'tab3') {
		document.getElementById('t3').onclick;
	}
}

var fabs;

Event.observe(window,'load',function(){ fabs = new Fabtabs('tabs'); },false);
