/*
 *  Konstruktor pro Záložky
 */

var studenta = window.studenta = window.studenta || {};

studenta.Tabs = function(element, options){
	this.$element = element.jquery ? element : $(element);
	this.options = $.extend({
		current: 0,
		item: 'a',
		eventName: 'click touchstart',
		tabHideClass: 'fb-tabs-hide',
		menuActiveClass: 'fb-tabs-selected',
		onActiveTab: function(){}
	}, options)
	this.currentIndex = null;
	this.currentId = null;
	// tabs menu
	this.$menu = this.$element.find(this.options.item);
	// tabs fragment
	var $tabs = $([]);
	this.$menu.each(function(){
		$tabs = $tabs.add($(this).prop('hash'))
	})
	this.$tabs = $tabs;

	return this;
};

studenta.Tabs.prototype.init = function(){
	if(!this.$element.length){
		return this;
	}
	
	var o = this.options;
	this.$menu.bind(o.eventName, $.proxy(this.handle, this));
	
	this.set(o.current)
	
	return this;
};
studenta.Tabs.prototype.destroy = function(){
	var o = this.options;
	this.$menu.unbind(o.eventName, this.handle);
}; 

studenta.Tabs.prototype.handle = function(e){
	var $target = $(e.currentTarget);
	this.set($target.prop('hash'))
	e.preventDefault();
};

studenta.Tabs.prototype.set = function(current){
	var id = typeof current == 'number' ? this.$menu.eq(current).prop('hash') : current;	
	this.currentIndex = this.$menu.filter('[href*="'+id+'"]').index();
	this.currentId = id;
	this.setTab(id);	
	this.setMenu(id);
};

studenta.Tabs.prototype.setTab = function(id){
	this.$tabs
		.filter(id)
		.removeClass(this.options.tabHideClass)
		.end()
		.not(id)
		.addClass(this.options.tabHideClass);
		
	this.options.onActiveTab.call(this)
};

studenta.Tabs.prototype.setMenu = function(id){
	this.$menu
		.removeClass(this.options.menuActiveClass)
		.filter('[href*="'+id+'"]')
		.addClass(this.options.menuActiveClass);	
};

