(function($) {
	$.fn.typo3BannerFader = function() {
		var fader = this,
			current,
			showNext = function() {
				if (current) {
					current.fadeOut();
					current = current.next();
					if (current.length == 0) {
						current = fader.children().first();
					}
				} else {
					current = fader.children().eq(Math.floor(Math.random() * fader.children().length));
				}
				current.fadeIn();
			};
		fader.addClass('js-active');
		fader.children().hide();
		showNext();
		setInterval(showNext, 7000);
		return this;
	};
	$.fn.typo3Stage = function() {
		var switcher = this.find('ul.stage-switcher'),
			current,
			currentTab,
			autoplay,
			showNext = function() {
				var nextTab;
				if (!currentTab) {
					nextTab = switcher.find('li').first();
				} else {
					nextTab = currentTab.next();
					if (nextTab.length == 0) {
						nextTab = switcher.find('li').first();
					}
				}
				nextTab.click();
				//autoplay = setTimeout(showNext, 6000);
			};
		switcher.children('li').each(function() {
			var li = $(this),
				item = $(this).find('.stage-item').detach();
			item.hide();
			item.appendTo(switcher.parent());
			li.click(function(event) {
				clearTimeout(autoplay);
				if (currentTab) currentTab.removeClass('active');
				currentTab = li;
				li.addClass('active');
				if (current) current.fadeOut(500);
				item.fadeIn(500);
				current = item;
			});
			li.mouseover(function() {
				li.addClass('hover');
			});
			li.mouseout(function() {
				li.removeClass('hover');
			});
		});
		showNext();
		//autoplay = setTimeout(showNext, 6000);
		switcher.animate({right: -150}, {queue: false});
		switcher.mouseenter(function() {
			switcher.animate({right: 0}, {queue: false});
		});
		switcher.mouseleave(function() {
			switcher.animate({right: -150}, {queue: false});
		});
	};
})(jQuery);

$(function() {
	$('#banner-fader').typo3BannerFader();
	$('#reference-fader').typo3BannerFader();
	$('.stage-area').typo3Stage();
	$('.target-groups').children().each(function() {
		var group = $(this);
		group.find('.target-button').hide();
		group.find('.target-icon').css({opacity: 0.2});
		group.mouseenter(function() {
			group.find('.target-button').fadeIn('fast');
			group.find('.target-icon').animate({opacity: 1.0}, {queue: false});
		});
		group.mouseleave(function() {
			group.find('.target-button').fadeOut('fast');
			group.find('.target-icon').animate({opacity: 0.2}, {queue: false});
		});
	});
});

