/*
 * 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/
 *
 *  edits added by MRP 2007-08
 *
 */
var Fabtabs = Class.create();

Fabtabs.prototype = {
	initialize : function(element,doInit) {
//alert(doInit);
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.menu.each(this.hide.bind(this));
/*		for(var i=0;i<this.menu.length;i++){
alert((this.menu[i].id));
this.hide(this.menu[i]);
//			$(this.menu[i]).removeClassName('active-tab');
//			$(this.tabID(this.menu[i])).removeClassName('active-tab-body');
		}*/
		this.show(this.getInitialTab(doInit));
		this.menu.each(this.setupTab.bind(this));
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
		try{
			var elm = Event.findElement(ev, "a");
			Event.stop(ev);
			this.show(elm);
			this.menu.without(elm).each(this.hide.bind(this));
		}catch(e){}
	},
	hide : function(elm) {
//alert('hiding '+elm.id);
		try{
			$(elm).removeClassName('active-tab');
			$(this.tabID(elm)).removeClassName('active-tab-body');
			var liChildren = $(elm).parentNode.getElementsByTagName('input');
			liChildren[0].disabled = true;
//			alert('set disabled to true');
			liChildren[0].addClassName('disabledInput');
		}catch(e){}
	},
	show : function(elm) {
//alert('showing '+elm.id);
		try{
			$(elm).addClassName('active-tab');
			$(this.tabID(elm)).addClassName('active-tab-body');
			var liChildren = $(elm).parentNode.getElementsByTagName('input');
			liChildren[0].disabled = false;
//			alert('set disabled to false');
			liChildren[0].removeClassName('disabledInput');
		}catch(e){}
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function(doFirst) {
		if(doFirst) whichTab = this.menu.first();
		else whichTab = this.menu.last();
		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 || whichTab;
		} else {
			return whichTab;
		}
	}
}

Event.observe(window,'load',function(){ delete myTabs; myTabs = new Fabtabs('tabs',1); },false);
