(function($) {
	$.fn.outerWidth = function() {
		return parseInt(this.width()) + parseInt(this.css('paddingLeft')) + parseInt(this.css('paddingRight')) + parseInt(this.css('marginLeft')) + parseInt(this.css('marginRight')) + parseInt(this.css('borderLeftWidth')) + parseInt(this.css('borderRightWidth'));
	}

	$.fn.sliddingtabs = function(options) {
		var defaults = {
			fixFirstTab: false,
			fixedClass: 'forms',
			currentTabClass: 'current',
			previousTabImg: null,
			previousTabLabel: 'Previous',
			previousTabClass: 'previousTab',
			nextTabImg: null,
			nextTabLabel: 'Next',
			nextTabClass: 'nextTab'
		};

		var options = $.extend(defaults, options);

		return this.each(function() {
			var tabBar = $(this).find('ul');

			if (tabBar) {
				tabBar.getFirstTab = function() {
					if (options.fixFirstTab) {
						return tabBar.find('li:eq(1)');
					} else {
						var firstTab = tabBar.find('li:first');

						if (firstTab.is('.' + options.fixedClass)) {
							return firstTab.next();
						} else {
							return firstTab;
						}
					}
				};

				var tabsWidth = 0;

				tabBar.find('li').each(function() {
					tabsWidth += $(this).outerWidth();
				});

				if (tabsWidth > tabBar.outerWidth()) {
					var previousTab = null;
					var nextTab = null;

					if (options.previousTabImg) {
						previousTab = $('<img class="' + options.previousTabClass + '" title="' + options.previousTabLabel + '" src="' + options.previousTabImg + '"/>');
					} else {
						previousTab = $('<div class="' + options.previousTabClass + '">' + options.previousTabLabel + '</div>');
					}

					previousTab.click(function () {
						tabBar.getFirstTab().before(tabBar.find('li:last'));
					});

					if (options.nextTabImg) {
						nextTab = $('<img class="' + options.nextTabClass + '" title="' + options.nextTabLabel + '" src="' + options.nextTabImg + '"/>');
					} else {
						nextTab = $('<div class="' + options.nextTabClass + '">' + options.nextTabLabel + '</div>');
					}

					nextTab.click(function () {
						tabBar.find('li:last').after(tabBar.getFirstTab());
					});

					tabBar.before(previousTab).after(nextTab);

					var currentTab = tabBar.find('li:has(a[@href*=' + location.href.replace(new RegExp('^http://.+/'), '') + '])');

					if (currentTab) {
						currentTab.addClass(options.currentTabClass);

						if (!currentTab.is(':first-child')) {
							var tabsWidth = currentTab.outerWidth();
							var previousTabs = currentTab.prevAll();

							previousTabs.each(function() {
								tabsWidth += $(this).outerWidth();
							});

							if (tabsWidth > tabBar.outerWidth()) {
								previousTabs.each(function() {
									if (options.fixFirstTab == false || (options.fixFirstTab == true && !$(this).is(':first-child'))) {
										tabBar.find('li:last').after(tabBar.getFirstTab());
									}
								});
							}
						}
					}
				}
			}
		});
	};
})(jQuery);
$(document).ready(function() {$('.level_1').sliddingtabs({fixFirstTab: true, previousTabImg: 'medias/images/link_previous.png', nextTabImg: 'medias/images/link_next.png'})});

