(function($)
{
	$.fn.rotator = function(options)
	{	
		var defaults =
		{
			imageFadeInSpeed: 800,
			imageFadeOutSpeed: 400,
			headlineFadeInSpeed: 400,
			headlineFadeOutSpeed: 400,
			contentFadeInSpeed: 500,
			contentFadeOutSpeed: 400,
			rotateSpeed: 3500
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function()
		{
			var activeIndex = 0;
			var obj = $(this);
			var paused = false;
			var rotatorInterval;
						
			$('.controls a', obj).click(function()
			{
				var rotator = $(this).parents('.rotator');
				var clickedIndex = $('.controls ul li a', rotator).index($(this));
				
				activeIndex = rotateTo(activeIndex, clickedIndex, rotator);

				return false;
			});
			
			$('.block div.more a', obj).hover
			(
				function() { paused = true; },
				function() { paused = false; }
			);
			
			function start()
			{
				if (rotatorInterval == null)
				{
					rotatorInterval = setInterval(function()
					{
						if (!paused)
						{
							activeIndex = rotateTo(activeIndex, activeIndex + 1, obj);
						}
					}, options.rotateSpeed);
				}
			}
			
			function stop()
			{
				if (rotatorInterval != null)
				{				
					clearInterval(rotatorInterval);
					
					rotatorInterval = null;
				}
			}
			
			function rotateTo(activeIndex, clickedIndex, rotator)
			{			
				if (activeIndex != clickedIndex)
				{
					stop();
				
					if (clickedIndex > $('.block', rotator).length - 1)
					{
						clickedIndex = 0;
					}
					
					var activeBlock = $('.block', rotator).eq(activeIndex);
					var activeContent = $('.content', activeBlock);
					var activeHeadline = $('.headline', activeBlock);
					var controls = $('.controls', rotator);
					var nextBlock = $('.block', rotator).eq(clickedIndex);
					
					// Stop animation
					if ($(activeBlock).is(':animated'))
					{
						$(activeBlock).stop().hide().css('opacity', 1);
					}
					
					$('.headline:animated', activeBlock).stop().hide().css('opacity', 1);
					$('.content:animated', activeBlock).stop().hide().css('opacity', 1);
					
					// Fade out active
					$('.headline', activeBlock).fadeOut(options.headlineFadeOutSpeed);
					$('.content', activeBlock).fadeOut(options.contentFadeOutSpeed);
					$(activeBlock).fadeOut(options.imageFadeOutSpeed, function()
					{											
						// Fade in next
						$(nextBlock).fadeIn(options.imageFadeInSpeed, function()
						{
							$('.headline', nextBlock).fadeIn(options.headlineFadeInSpeed, function()
							{
								$('.content', nextBlock).fadeIn(options.contentFadeInSpeed, function()
								{
									$(activeBlock).removeClass('active');
									$(nextBlock).addClass('active');
									
									start();
								});
							});
						});
					});
					
					// Update controls
					$('a', controls).eq(activeIndex).removeClass('active');
					$('a', controls).eq(clickedIndex).addClass('active');					
				}
				
				return clickedIndex;
			}
			
			start();			
		});
	};
})(jQuery);

