$j.fn.frontpageCarousel = function (options) {
	var $ = $j;
	
	var timeout;

	var settings = $j.extend({
		arrowClass: "bigarrow",
		backgroundArrow: "bg-arrow",
		startPage: 1,
		pageSteps: 1,
		transitionSpeed: 7000
	}, options);

	return this.each(function () {

		var wrapper = $(this),
		showcase = wrapper.find('> div:not(#showcase-tab)'),
		slider = showcase.find('> ul'),
		items = slider.find('> li'),
		single = items.filter(':first'),
		singleWidth = single.outerWidth(), 
		visible = Math.ceil(wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
		currentPage = settings.startPage,
		pages = Math.ceil(items.length);
		
		// 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
		
		// Set the width of the slider
		slider.css('width', singleWidth * $('> li', slider).length);

		// Set the left position to the first 'real' item
		$(slider).css('left', -singleWidth * visible);
		
		// paging function
		function gotoPage(page) {
			var dir = page < currentPage ? 1 : -1,
			n = Math.abs(currentPage - page),
			left = singleWidth * dir * n,
			postAnimateFunction = function () {
				$j('#campaign-count').html(currentPage);
				return false;
			};
			currentPage = parseInt(page,10);
			
			if (page == 0) {
				currentPage = parseInt(pages,10);
				postAnimateFunction = function () {
					$j('#campaign-count').html(currentPage);
					$(slider).css('left', -singleWidth * pages);
					page = pages;
				}
			} else if (page > pages) {
				currentPage = parseInt(settings.startPage,10);
				postAnimateFunction = function () {
					$j('#campaign-count').html(currentPage);
					$(slider).css('left', -singleWidth * visible);
					// reset back to start position
					page = settings.startPage;
				}
			} 

			slider.filter(':not(:animated)').animate({
				left : '+=' + left
			}, 1100, 'easeOutQuint', postAnimateFunction);
			timedMove();
			return false;
		}
	
		// Move to next page in transitionSpeed time
		function timedMove() {
			clearTimeout(timeout);
			timeout = setTimeout(function() { 
				gotoPage(currentPage + settings.pageSteps); 
			}, settings.transitionSpeed);
		}

		showcase.after('<div class="'+settings.arrowClass+' back"></div><div class="'+settings.arrowClass+' forward"></div>'); 

		$('.campaign img', this).mouseenter(function () {
			clearTimeout(timeout);
		});

		$('.campaign img', this).mouseleave(function () {
			timedMove();
		});
		
		// Bind to the forward and back buttons
		$('div.back, a.back', this).click(function () {
			return gotoPage(currentPage - 1);
		});
		
		$('div.forward, a.forward', this).click(function () {
			return gotoPage(currentPage + 1);
		});
		
		timedMove();

		// create a public interface to move to a specific page
		$(this).bind('goto', function (event, page) {
			gotoPage(page);
		});
	});
}
