
/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw (+ mods by Casper)
 * version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/
 */
var Tabify = Class.create();

Tabify.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));
   },

   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.menu.without(elm).each(this.hide.bind(this));
      this.show(elm);
   },

   hide : function(elm) {
      $(elm).removeClassName('active-tab');

      if (this.tabID(elm) != "tab-all") {
         $(this.tabID(elm)).removeClassName('active-tab-body');
      }
   },

   show : function(elm) {
      $(elm).addClassName('active-tab');

      if (this.tabID(elm) == "tab-all") {
         $$(".tab-panel").each(this.showBody);
      } else {
         this.showBody($(this.tabID(elm)));
      }
   },

   showBody : function(elm) {
      elm.addClassName('active-tab-body');
      new Effect.Highlight(elm, {
            endcolor: "#ffffff",
            restorecolor: "#ffffff"
      });
   },

   tabID : function(elm) {
      return elm.href.match(/#(\w.+)/)[1];
   },

   getInitialTab : function() {
      if(document.location.href.match(/#(tab\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() { new Tabify('tabify-tabs'); }, false);

