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.without(this.getInitialTab()).each(this.hide.bind(this));
		this.menu.each(this.setupTab.bind(this));
		// next 2 lines added by jamos@campbell-ewald.com to ensure css is not controlling the hidden div - turn js off and the divs are still visible
		this.panels = document.getElementsByClassName('panel');
		this.panels.each(function(option) { option.style.position = 'absolute'; if (option.className.indexOf('active-tab-body') == -1) { option.style.opacity = 0; option.style.top = 0; option.style.left = 0;  } else { option.style.opacity = 1; option.style.top = -999; } });
	},
	setupTab : function(elm) {
	Event.observe(elm,'click',this.activate.bindAsEventListener(this),false);
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		this.show(elm);
		Event.stop(ev);
		this.menu.without(elm).each(this.hide.bind(this));

	},
	hide : function(elm) {
		$(elm).removeClassName('active-tab');
		$(this.tabID(elm)).removeClassName('active-tab-body');
		//added by jamos@campbell-ewald.com
		new Effect.Fade($(this.tabID(elm)), { fps:250, duration:0.5, mode: 'absolute' });
	},
	show : function(elm) {
		$(elm).addClassName('active-tab');
		$(this.tabID(elm)).addClassName('active-tab-body');
		
		// next line added by jamos@campbell-ewald.com
		new Effect.Appear($(this.tabID(elm)), { fps:250, duration:0.8, delay:0.3, mode: 'absolute' });
	},
	// next 2 methods added by jamos@campbell-ewald 
	shift : function(ev) {
		var elm = Event.findElement(ev, "a");
		new Effect.Morph(elm, { style: 'margin-left:10px; background-color:#ccc;', fps:250, duration:0.1 });
	},	
	shiftback : function(ev) {
		var elm = Event.findElement(ev, "a");
		new Effect.Morph(elm, { style: 'margin-left:0px; background-color:#eee;', fps:250, duration:0.1 });
	},	
	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() {
	if($('gm-tabs')){
   	  new Fabtabs('gm-tabs');
     }
} );
