var version = GetCookie('version');

var Fabtabs = Class.create();
var heightActivTabs;
var heightContent = null;

Fabtabs.prototype = {
	initialize : function(element) {
        if (!heightActivTabs) {
            heightContent = document.getElementById("content").offsetHeight;
        }
        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));
    },
	hide : function(elm) {
		$(elm).removeClassName('pushed');
        $(this.tabID(elm)).removeClassName('activeTabBody');
	},
	show : function(elm) {
		$(elm).addClassName('pushed');
        $(this.tabID(elm)).addClassName('activeTabBody');
        version = this.tabID(elm);
        if (version == null) {
	        version = 'full';
	    } else {
            pathname = location.pathname;
            myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';

            var largeExpDate = new Date ();
            largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
            SetCookie('version',version,largeExpDate,myDomain);
        }
        /*Start for height columns*/
        heightActivTabs = document.getElementById(version).offsetHeight;
        /*if (heightActivTabs != 0) {
            matchColumns(heightActivTabs + heightContent,'');
        }*/
        /*End for height columns*/
    },
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/) || version != null) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) {return value.href.match(/#(\w.+)/)[1] == version; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	}
}

function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg)
                    return getCookieVal (j);
            i = document.cookie.indexOf(" ", i) + 1;
                    if (i == 0)
                            break;
            }
   return null;
}
function SetCookie (name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
            //((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
            ((path == null) ? "" : ("; path=" + path)) +
            ((domain == null) ? "" : ("; domain=" + domain)) +
            ((secure == true) ? "; secure" : "");
}

if (version == null) {
    version = 'full';
}
