function readCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1)
				c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return '';
}

var tabs = Class.create();

tabs.prototype = {
	initialize : function(element, align) {
		if (this != null)
		{
			this.align = align;
			if ($(element) != null)
			{
			this.element = $(element);
			var options = Object.extend({}, arguments[1] || {});
			this.menu = $A(this.element.getElementsByTagName('li'));
			$(this.menu.first()).addClassName('first');
			$(this.menu.last()).addClassName('last');
			this.links = $A(this.element.getElementsByTagName('a'));
			this.links.each(this.setupTab.bind(this));
			var initTab = this.getInitialTab();
			this.show(initTab);
			this.refreshTabs(initTab);
			}
		}	
	},
	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.up()).each(this.hide.bind(this));
		this.elm = elm;
		this.refreshTabs(elm);

		var Selm = new String(elm);
		var pos = Selm.indexOf("tab1")
		if ( pos >= 0 )
			document.cookie = "2d=1";
		else
			document.cookie = "2d=0";

	},
	hide : function(elm) {
		$(elm).removeClassName('current');
		$(this.tabID(elm)).removeClassName('active-tab-body');
	},
	show : function(elm) {
		$(elm).up().addClassName('current');
		$(this.tabID(elm.up())).addClassName('active-tab-body');
	},
	tabID : function(elm) {
		return $A(elm.getElementsByTagName('a')).first().href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if ( readCookie('3d') && (readCookie('3d') == '1') ) {
			//var loc = RegExp.$1;
			//var elm = this.links.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			//return elm || this.menu.last();
			this.menu.first().addClassName('current');
			return this.menu.last();
		} else {
			return this.links.last();
		}
	},
	refreshTabs : function(elm) {
		if(this.align == 'right') {
	                for(i=this.menu.length-1; i > this.menu.indexOf(elm.up()); i--) {
				$(this.menu[i]).removeClassName('after');
	                        $(this.menu[i]).addClassName('before');
			}
			for(i=this.menu.indexOf(elm.up())-1; i >= 0; i--) {
				$(this.menu[i]).removeClassName('before');
				$(this.menu[i]).addClassName('after');
			}
		}
		else if(this.align == 'left') {
			for(i=0; i < this.menu.indexOf(elm.up()); i++) {
				this.menu[i].removeClassName('after');
				this.menu[i].addClassName('before');
			}
			for(i=this.menu.indexOf(elm.up())+1; i <= this.menu.length-1; i++) {
				this.menu[i].removeClassName('before');
				this.menu[i].addClassName('after');
			}
		}
	}
}

