var TabMenu = {
	
	scrolling: false,
	
	config: {},
	
	tabTmpl: '<li class="scroller %s"><a href="%s" id="%s"><span class="navButtonLeft"></span><span class="navButtonTitle">%s</span><span class="navButtonRight"></span></a></li>',

	init: function(config) {
		this.config = config;
		this.menu = $(config.list);
		this.tabs = $(config.items);
		
		this.firstTab = 0;
		this.lastTab = null;
		
		this.run();
	},
	run: function() {
		// hack pro IE7, neumi spocitat sirku vsech zalozek (protoze se hned zalomi)
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 8) {
			var parentContainerWidth = $('#wideContainer').width();
// hack, ktery asi uz neni potreba
//			$('#wideContainer').css('width', 'auto');
		}
		if (this.getTotalTabsWidth() > $(this.menu).width()) {
			if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 8) {
				$('#wideContainer').css('width', parentContainerWidth + 'px');
			}
			// nastaveni pozadovaneho stylu rodicovskemu boxu
			$(this.menu).parent().css('overflow', 'hidden');
			
			// pokud se taby nevejdou, pridame soupatka
			$(this.menu).prepend(sprintf(this.tabTmpl, 'left', this.config.list, this.config.ID + '-tabMenuPrev', '&lt;'));
			this.scrollerLeft = $('#' + this.config.ID + '-tabMenuPrev').parent();
			$(this.menu).append(sprintf(this.tabTmpl, 'right', this.config.list, this.config.ID + '-tabMenuNext', '&gt;'));
			this.scrollerRight = $('#' + this.config.ID + '-tabMenuNext').parent();
			
			// pozice praveho soupatka
			$(this.scrollerRight).css('left', $(this.menu).width() - $(this.scrollerRight).width());
			
			// pridani stinu + pozice
			$(this.menu).prepend('<li class="shadow left"></li>');
			this.shadowLeft = $('li.shadow.left');
			$(this.menu).append('<li class="shadow right"></li>');
			this.shadowRight = $('li.shadow.right');
			$(this.shadowLeft).css('left', $(this.scrollerLeft).width());
			$(this.shadowRight).css('left', $(this.menu).width() - $(this.scrollerRight).width() - $(this.shadowRight).width());
			
			var ref = this;
			
			$(this.scrollerLeft).find('a').click(function(e) {
				e.preventDefault();
				e.stopPropagation();
				$(this).blur();
			});
			$(this.scrollerRight).find('a').click(function(e) {
				e.preventDefault();
				e.stopPropagation();
				$(this).blur();
			});
			$(this.scrollerLeft).find('a').mouseenter(function(e) {
				ref.onScrollDirection('left');
			});
			$(this.scrollerRight).find('a').mouseenter(function(e) {
				ref.onScrollDirection('right');
			});
			
			$(this.scrollerRight).find('a').mouseleave(function(e) {
				ref.stopScroll();
			});
			$(this.scrollerLeft).find('a').mouseleave(function(e) {
				ref.stopScroll();
			});

			this.tabs = $(this.tabs).filter(':not(.scroller, .shadow)');
			
			// ulozeni puvodni sirky pro pozdejsi ucely a nastaveni nove
			this.config.menuWidth = $(this.menu).width();
			$(this.menu).width(this.getTotalTabsWidth() + this.getScrollersWidth());

		}
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 8) {
			$('#wideContainer').css('width', parentContainerWidth + 'px');
		}
	},
	
	stopScroll: function() {
		$(this.tabs).stop();
		$(this.shadowRight).fadeOut();
		$(this.shadowLeft).fadeOut();
	},
	
	getTabWidthWithMargin: function(el) {
		return $(el).width() + parseInt($(el).css('margin-right'), 10);
	},
	
	getTotalTabsWidth: function() {
		var totalWidth = 0;
		var ref = this;
		$.each($(this.tabs), function(index, item) {
			totalWidth += ref.getTabWidthWithMargin(item);
		});
		return totalWidth;
	},
	
	getScrollersWidth: function() {
		if (typeof this.scrollerLeft != 'undefined' && typeof this.scrollerRight != 'undefined') {
			var margin = parseInt($(this.scrollerLeft).css('margin-right'), 10);
			return $(this.scrollerLeft).width() + margin + $(this.scrollerRight).width();
		} else {
			return 0;
		}
	},
	
	onScrollDirection: function(direction) {
		var pixelsToEnd = this.getTotalTabsWidth() - this.getVisibleArea();
		this.scroll(direction == 'left' ? 0 : -pixelsToEnd, direction, -pixelsToEnd);
	},
	
	getVisibleArea: function() {
		return this.config.menuWidth - this.getScrollersWidth();
	},
	
	getActualOffset: function() {
		return parseInt($(this.tabs).first().css('left'), 10);
	},
	
	getVisibleTabsWidth: function() {
		var totalWidth = 0;
		var ref = this;
		$.each($(this.tabs), function(index, item) {
			if (!$(item).hasClass('hidden')) {
				totalWidth += ref.getTabWidthWithMargin(item);
			}
		});
		return totalWidth;
	},

	scroll: function(total, direction, width) {
		/*
		if ($(this.tabs).css('left') != '0px') {
			$(this.shadowLeft).fadeIn();
		}
		if ($(this.tabs).css('left') != total + 'px') {
			$(this.shadowRight).fadeIn();
		}
		*/
		var ref = this;

		var multiplier = parseInt(Math.abs(width) * 3);
		
		var propTabsLeft = $(this.tabs).position().left - 33;
		switch (direction) {
			case 'right':
				var duration = parseInt((parseInt(width, 10) - parseInt(propTabsLeft, 10)) / (parseInt(width, 10)) * multiplier, 10);
				break;
			case 'left':
				var duration = parseInt((parseInt(total, 10) + parseInt(propTabsLeft, 10)) / parseInt(width, 10) * multiplier, 10);
				break;
		}

		if (duration > 0) {
			$(this.tabs).animate({left: total}, duration, 'linear');
		}
	}
	
}
