var Fabtabs = Class.create();
Fabtabs.prototype = {
	initialize : function(element) {
		var menu = new Array();
		$A(this.initialize.arguments).each(function (val) {
			if (typeof(val) != 'string' || !$(val)) return;
			menu.push($A($(val).getElementsByTagName('a')).findAll(function (e) {
				return /^#big(?:[^T]+)?Tab[0-9]+Content/.test(e.hash)
			}));
		});

		this.menu = menu.flatten();

		if (!this.menu.length) {
			//window.status = 'No tabs found';
			return false;
		}

		this.Content = $('content');
		var cont_text = '';
		if (this.Content) {
			cont_text = this.Content.textContent || this.Content.innerText;
			cont_text = cont_text.replace(/\s+/g, '');
		}

		//var liga = '';
		//liga = new RegExp('(?:^|&)liga=([0-9]+)', '').test(window.location.href.split('?')[1]);

		if (cont_text) {
			// do nothing
		} else {
			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('active');

		window.status = '';

		var e;
		e = $(this.tabID(elm));
		if (e) {
			e.removeClassName('active');
		} else {
			//window.status += '"'+this.tabID(elm)+'" not found; ';
		}
		e = $(this.tabLink(elm));
		if (e) {
			e.removeClassName('active');
		} else {
			//window.status += '"'+this.tabLink(elm)+'" not found; ';
		}

		e = $(this.tabLine(elm));
		if (e) {
			e.removeClassName('active');
		} else {
			//window.status += '"'+this.tabLink(elm)+'" not found; ';
		}
	},

	show : function(elm) {
		var e = $(elm);
		if (e) e.addClassName('active');

		if (e = $('stakeLigas')) {
			if (new RegExp('#bigLeftTab', '').test(elm.href)) {
				e.addClassName('active');
			} else {
				e.removeClassName('active');
			}
		}

		if (this.Content) {
			this.Content.hide();
		}

		e = $(this.tabID(elm));
		if (e) {
			e.addClassName('active');
		} else {
			//window.status += '"'+this.tabID(elm)+'" not found; ';
		}

		e = $(this.tabLink(elm));
		if (e) {
			e.addClassName('active');
		} else {
			//window.status += '"'+this.tabLink(elm)+'" not found; ';
		}

		e = $(this.tabLine(elm));
		if (e) {
			e.addClassName('active');
		} else {
			//window.status += '"'+this.tabLine(elm)+'" not found; ';
		}
	},

	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},

	tabLink : function(elm) {
		return elm.href.match(/#(\w.+)/)[1]+"Link";
	},

	tabLine : function(elm) {
		return elm.href.match(/#(\w.+)/)[1]+"Line";
	},

	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 {
			var arr = $$('.mayBeActive');
			if (arr.length) return arr.first().getElementsByTagName('A')[0];
			return this.menu.first();
		}
	}
}
Event.observe(window,'load',function(){ new Fabtabs('bigTabs', 'sportNav'); },false);


