/*
 * 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) { // added by Alex Kellner to remove error message in IE
			var options = Object.extend({}, arguments[1] || {});
			this.menu = $A(this.element.getElementsByTagName('a'));
			this.show(this.getInitialTab());
			this.menu.each(this.setupTab.bind(this));
      this.element.up('form').addClassName('fabtabs-active');
		}
	},
	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.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
    this.tabBrowser();
	},
	hide : function(elm) {
    $(elm).up().removeClassName('active-tab-li');
		$(elm).removeClassName('active-tab');
		$(this.tabID(elm)).removeClassName('active-tab-body');
	},
	show : function(elm) {
		$(elm).addClassName('active-tab');
    $(elm).up().addClassName('active-tab-li');
		$(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();
		}
	},
  activateNextTab : function() {
    this.menu = $A($('tabs').getElementsByTagName('a'));
    
    if($$('.active-tab').first().up().next()) {
      var elm = $$('.active-tab').first().up().next().down('a');
    }

    if(elm) {
      this.show(elm);
      this.menu.without(elm).each(this.hide.bind(this));
      this.tabBrowser();
    }
  },
  activatePreviousTab : function() {
    this.menu = $A($('tabs').getElementsByTagName('a'));

    if($$('.active-tab').first().up().previous()) {
      var elm = $$('.active-tab').first().up().previous().down('a');
    }

    if(elm) {
      this.show(elm);
      this.menu.without(elm).each(this.hide.bind(this));
      this.tabBrowser();
    }
  },
  tabBrowser : function() {
    if($('tabBrowser')) {
      if($$('.active-tab').first().up().previous() === null) {
        $('tabBrowser').down('li.prevLink').hide();
      }
      else {
        $('tabBrowser').down('li.prevLink').show();
      }

      if($$('.active-tab').first().up().next() === null) {
        $('tabBrowser').down('li.nextLink').hide();
      }
      else {
        $('tabBrowser').down('li.nextLink').show();
      }
    }
  }
}

Event.observe(window,'load',function(){
  $('tabs').show();
  $('tabs').up().insert('<ul id="tabBrowser" style=""><li class="prevLink"><a href="#" onclick="Fabtabs.prototype.activatePreviousTab(); return false">vorheriger Schritt</a></li><li class="nextLink"><a href="#" onclick="Fabtabs.prototype.activateNextTab(); return false">n&auml;chster Schritt</a></li></ul>');

  new Fabtabs('tabs'); 
  Fabtabs.prototype.tabBrowser();
},false);

