(function ($) {
	$(function () {
		// setup
		var $rotatorContainer = $('div#rotator');
		var $rotatorItem = $('div.rotator-item', $rotatorContainer);
		var $rotatorNav = $('div#rotatorNav');
		var $rotatorNavLinks = $('a', $rotatorNav);
		var animationDelay = 500;
		var rotationDelay = 5000;
		$rotatorItem.not(':first-child').hide();
		$rotatorNavLinks.filter(':first-child').addClass('current');
		
		// rotation
		var rotateImages = function () {
			$currentRotator = $rotatorItem.filter(':visible').fadeOut(animationDelay);
			$currentLink = $rotatorNavLinks.filter('.current').removeClass('current');
			$nextRotator = $currentRotator.next('.rotator-item').length ? $currentRotator.next('.rotator-item') : $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);
