jQuery.fn.rotate = function(options)
{
	$(this).each(function()
	{

		settings = jQuery.extend(
		{
			delay: 2000,
			speed: 500
		}, options);
	
		banners = [];
		pins = [];
		delays = [];

		current = 0;
		next = 0;
	
		timer = null;
		in_transition = false;
		


	
		// helpers
		advance = function() // re-points to the next banner and continues the looping
		{
			current = next;
			if (current >= banners.length) current = 0;

			next++;
			if (next >= banners.length) next = 0;
			
			if (timer == null)
				timer = setTimeout(function()
				{
					show_next();
					timer = null;
					
				}, delays[current]);
		}


		show_next = function()
		{
			if (next == current) return; // in case there is only one banner

			if (in_transition) return false;
			in_transition = true;
			
			// right before transition light up the next pin - current pin will be dimmed right after the transition
			pins[next].addClass('active');

			banners[next].fadeIn(settings.speed, function()
			{
				banners[next].addClass('active');
				banners[current].removeClass('active');
				banners[current].css({ 'display': 'none' });
				
				pins[current].removeClass('active');

				advance();
				
				in_transition = false;
			});
		}

		show_prev = function()
		{
			next = current - 1;
			if (next < 0) next = banners.length - 1;

			if (next == current) return; // in case there is only one banner

			if (in_transition) return false;
			in_transition = true;
			
			
			// right before transition light up the next pin - current pin will be dimmed right after the transition
			pins[next].addClass('active');

			banners[next].fadeIn(settings.speed, function()
			{
				banners[next].addClass('active');
				banners[current].removeClass('active');
				banners[current].css({ 'display': 'none' });

				pins[current].removeClass('active');

				advance();
				
				in_transition = false;
			});
		}

		show_at = function(index)
		{
			next = index;

			if (next == current) return; // in case there is only one banner

			if (in_transition) return false;
			in_transition = true;
			

			// right before transition light up the next pin - current pin will be dimmed right after the transition
			pins[next].addClass('active');

			banners[next].fadeIn(settings.speed, function()
			{
				banners[next].addClass('active');
				banners[current].removeClass('active');
				banners[current].css({ 'display': 'none' });

				pins[current].removeClass('active');

				advance();
				
				in_transition = false;
			});
		}

		
		play = function()
		{
			if (timer == null)
				timer = setTimeout(function()
				{
					show_next();
					timer = null;
				}, delays[current]);
		}
		stop = function()
		{
			if (timer != null)
			{
				clearTimeout(timer);
				timer = null;
			}
		}




		// callbacks
		$('.rotator').mouseenter(function()
		{
			stop();
		});
	
		$('.rotator').mouseleave(function()
		{
			play();
		});
	
		
		$('.buttons .prev').click(function()
		{
			show_prev();
		});
		$('.buttons .next').click(function()
		{
			show_next();
		});

		$('.buttons .pin').click(function()
		{
			index = $(this).attr('rel');
			show_at(index);
		});
		
		$('.buttons .pin, .buttons .prev, .buttons .next').dblclick(function()
		{
			return false;
		});



		// collect banner object references
		$(this).find('.banners .banner').each(function(index)
		{
			banners.push($(this));

			rel = parseInt($(this).attr('rel'));
			delay = settings.delay;
			if (rel > 0) delay = rel;
			delays.push(delay);

			
			if ($(this).hasClass('active')) current = index;

			next = current + 1;
			if (next >= banners.length) next = 0;
		});
		
		// collect pin object references
		$(this).find('.buttons .pin').each(function(index)
		{
			pins.push($(this));
			if ($(this).hasClass('active')) current_pin = index;

			$(this).attr('rel', index);
		});

		
		play();

	});
	
	return this;
}

$(document).ready(function()
{
	$('.rotator').rotate({ delay: 7000 });
});

