/*
 * 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();

function Lesen(n){
         r = document.cookie;
         res = '';
         while(r != '')
{ while(r.substr(0,1) == ' '){r = r.substr(1,r.length);}
  cookiename = r.substring(0,r.indexOf('='));
  if(r.indexOf(';') != -1){
    cookiewert = r.substring(r.indexOf('=')+1,r.indexOf(';'));
  } else {
    cookiewert = r.substr(r.indexOf('=')+1,r.length);
  }
  if(n == cookiename){
    res = cookiewert;
  }
  i = r.indexOf(';')+1;
  if(i == 0){i = r.length}
  r = r.substring(i,r.length);
 }
return(res)
}

function Schreiben(n,w,e){
         var r = new Date();
         r = new Date(r.getTime() +e);
         document.cookie = n+'='+w+'; expires='+r.toGMTString()+';'+'path=/;';
 }

function setCookie(elm){
         var r = new Date();
         r = new Date(r.getTime() +60*60*1000);
         document.cookie = 'fjatab'+'='+elm+'; expires='+r.toGMTString()+';'+'path=/;';
}

Fabtabs.prototype = {

        initialize : function(element) {
      		if (!document.cookie) {
                  document.cookie = 'fjatab=meinwert;'+'path=/;';
//      		  if (!document.cookie) {alert("Bitte aktivieren Sie temporäre Cookies in Ihrem Browser, um die volle Funktionalität des Menüs zu erhalten!");}
      		  document.cookie = 'fjatab=meinwert;'+' expires=Thu, 01-Jan-70 00:00:01 GMT;'+'path=/;';
                }
		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.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
		document.cookie = 'fjatab=meinwert;'+' expires=Thu, 01-Jan-70 00:00:01 GMT;'+'path=/;';
		setCookie(this.tabID(elm));
	},
	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 {
                      if(document.cookie ==''){
                        return this.menu.first();
                      } else {
                           var loc = Lesen('fjatab');
                           var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
                           return elm || this.menu.first();


                      }
    		     }
	}
}
