$j.fn.PanagoraRotator = function (options) {
	var $ = $j;
	/*	By Gabriel Bonander, Panagora Room AB, 2009
		Inspired by House Ind. */

	var timeout;

	var settings = $.extend({
		direction: 'down',
		transitionSpeed: 7000
	}, options);

	function repeat(str, num) {
		return new Array( num + 1 ).join( str );
	}

	return this.each(function () {

		var wrapper = $(this).css('overflow', 'hidden'),
			slider = wrapper.find('> ul'),
			items = slider.find('> li'),
			single = items.filter(':first'),
			singleHeight = single.outerHeight(), 
			visible = Math.ceil(wrapper.innerHeight() / singleHeight), // note: doesn't include padding or border
			currentPage = 0,
			pages = Math.ceil(items.length / visible);
		
		if (!items.length)
			return;

		// 1. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
		items.filter(':first').before(items.slice(- visible).clone().addClass('cloned'));
		items.filter(':last').after(items.slice(0, visible).clone().addClass('cloned'));
		items = slider.find('> li'); // reselect
		
		// 2. Set the top position to the first 'real' item
		$(slider).css('top', -singleHeight * visible);

		function slideToPage(page) {
			var dir = page < currentPage ? -1 : 1,
				n = Math.abs(currentPage - page),
				top = singleHeight * dir * visible * n;

			slider.filter(':not(:animated)').animate({
				top : '-=' + top
			}, 1100, 'easeOutQuint', function () {
				if (page <= 0) {
					$(slider).css('top', -singleHeight * pages);
					page = pages;
				} else if (page >= pages) {
					$(slider).css('top', -singleHeight * visible);
					page = 0;
				} 
				currentPage = parseInt(page,10);
			});
			
			timedMove();
			return false;
		}

		// Move to next page in transitionSpeed time
		function timedMove() {
			clearTimeout(timeout);
			timeout = setTimeout(function() { 
				if (settings.direction == 'down')
					slideToPage(currentPage + 1);
				else
					slideToPage(currentPage - 1);
			}, settings.transitionSpeed);
		}

		$('.campaign img', this).mouseenter(function () {
			clearTimeout(timeout);
		});

		$('.campaign img', this).mouseleave(function () {
			timedMove();
		});

		timedMove();
	});
}
