$j.fn.frontpageCarousel = function (options) {
	var $ = $j;
	
	var timeout;

	settings = $j.extend({
		arrowClass: "bigarrow",
		backgroundArrow: "bg-arrow",
		startPage: 1,
		offset: 31,
		tabs: true,
		tabSelector: '#showcase-tab .campaign-bullet:not(.empty, .cloned)',
		transitionSpeed: 7000
	}, options);

	return this.each(function () {

		var wrapper = $(this).css('overflow', 'hidden'),
		showcase = wrapper.find('> div'),
		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);

		if (settings.tabs) {
			var tabs = $(settings.tabSelector);
			tabs.eq(currentPage-1).addClass('selected');
		}

		// 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;
			
			slider.filter(':not(:animated)').animate({
				left : '+=' + left
			}, 1100, 'easeOutQuint', function () {
				if (page == 0) {
					$(slider).css('left', -singleWidth * pages -singleWidth);
					page = pages;
				} else if (page > pages) {
					$(slider).css('left', -singleWidth * visible);
					// reset back to start position
					page = settings.startPage;
				} 
				currentPage = parseInt(page,10);

				// update tabs with selected image
				if (settings.tabs) {
					tabs.removeClass('selected');
					tabs.eq(page-1).addClass('selected');
				}
			});
			
			timedMove();

			return false;
		}
	
		// Move to next page in transitionSpeed time
		function timedMove() {
			clearTimeout(timeout);
			timeout = setTimeout(function() { 
				gotoPage(currentPage + 1); 
			}, settings.transitionSpeed);
		}

		slider.before('<div class="'+settings.backgroundArrow+' back"></div><div class="'+settings.backgroundArrow+' forward"></div>'); 
		slider.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', this).click(function () {
			return gotoPage(currentPage - 1);
		});
		
		$('div.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);
		});
	});
}
