/**
 * Malinko upraveno pro nasi potrebu. 
 * 
 * Pridana metoda activateRemote( tabId ) pro aktivaci obsahu pri kliku na odkaz mimo taby
 */

/*
 * 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, remoteClassName) {
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		
		// shovame vsechny krome inicialniho tabu
		this.menu.without(this.getInitialTab()).each(function(elm) {
			this.initialhide(elm);
		}.bind(this));
	
		this.menu.each(this.setupTab.bind(this));
		$$( '.' + remoteClassName ).each( this.setupLink.bind(this) );
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false);
	},
	setupLink : function(link) {
		Event.observe(link,'click',this.activateRemote.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));
	},
	activateRemote : function( ev ) {
		var link = Event.findElement(ev, "a");
		Event.stop(ev);
		var tabId = this.tabID( link );
		// look for tab link with href=#tabId 
		this.menu.find( function(item) {
			if( tabId == this.tabID(item) ) {
				this.show( item );
				this.menu.without( item ).each(this.hide.bind(this));
				return true;
			}
		}.bind(this));
	},
	initialhide : function(elm) {
		$(this.tabID(elm)).addClassName('inactive-tab-body');
	},
	hide : function(elm) {
		$(elm).ancestors()[0].removeClassName('selected');
		$(this.tabID(elm)).addClassName('inactive-tab-body');
	},
	show : function(elm) {
		$(elm).ancestors()[0].addClassName('selected');
		$(this.tabID(elm)).removeClassName('inactive-tab-body');
		// move the window to the top
		document.body.scrollTo();

	},
	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();
		}
	}
}

