(function ($) {
	$(function () {
		// setup
		var $rotatorContainer = $('div#banner');
		var $rotatorItem = $('a.banner', $rotatorContainer);
		var $rotatorNavLinks = $('ul > li > a', $rotatorContainer);
		var animationDelay = 500;
		var rotationDelay = 7000;
		$rotatorItem.not(':first-child').hide();

		// rotation
		var rotateImages = function () {
			$currentRotator = $rotatorItem.filter(':visible').fadeOut(animationDelay);
			$currentLink = $rotatorNavLinks.removeClass('current');
			$nextRotator = $currentRotator.next('.banner').length ? $currentRotator.next('.banner') : $rotatorItem.filter(':first-child');
			$nextLink = $rotatorNavLinks.filter('.link_' + $nextRotator.attr('id'));
			$nextRotator.fadeIn(animationDelay);
			$nextLink.addClass('current');
		};
		rotationInterval = setInterval(rotateImages, rotationDelay);

		// pause on hover
		$rotatorItem.bind('mouseenter mouseleave', function (e) {
			if (e.type == 'mouseenter') {clearInterval(rotationInterval);}
			else {rotationInterval = setInterval(rotateImages, rotationDelay);}
		});

		// jump links
		$rotatorNavLinks.bind('click', function (e) {
			e.preventDefault();
			$this = $(e.currentTarget);
			if (!$this.hasClass('current')) {
				// next two lines clear/set (reset) rotation interval
				clearInterval(rotationInterval);
				//rotationInterval = setInterval(rotateImages, rotationDelay);
				target = $this.attr('href');
				$rotatorNavLinks.filter('.current').removeClass('current');
				$rotatorItem.filter(':visible').fadeOut(animationDelay);
				$this.addClass('current');
				$rotatorItem.filter(target).fadeIn(animationDelay);
			}
		});
	});
})(jQuery);
