(function($){
	$.fn.smartpunkRotator = function(settings)
	{
		settings = jQuery.extend({
			autoRotate: false, 	// optional, accepts true or false
			rotateSpeed: 3000	// optional, time in ms
		}, settings);
		
		return this.each(function()
		{
			// Create the controls
			$('.header', this).append('<div class="controls"><a class="control previous" href="javascript:void(0);">&lt;</a><span class="info"></span><a class="control next" href="javascript:void(0);">&gt;</a></div>');
			
			var $features = $('.listing > div', this),
				$info = $('.info', this),
				$previous = $('.previous', this),
				$next = $('.next', this),
				count = $features.length,
				current,
				rotatorInterval;

			function showFeature(number)
			{
				if( number > count )
				{
					number = 1;
				}
				else if( number < 1 )
				{
					number = count;
				}
				
				current = number;
				// Using nth-child because it's one-based
				// instead of eq, which is zero-based;
				// this makes updating the paging easier.
				$features.hide().filter(':nth-child(' + number + ')').show();
				$info.text( number + ' of ' + count );
			}
			
			function createInterval(){
				rotatorInterval = setInterval(function(){
					$next.trigger('click');
				}, settings.rotateSpeed);
			}
			
			function killInterval(){
				clearInterval(rotatorInterval);
			}

			$previous.click(function(){ showFeature( (current - 1) ); });
			$next.click(function(){ showFeature( (current + 1) ); });
			
			$(this).mouseenter(function(){
				killInterval();
			}).mouseleave(function(){
				createInterval();
			});
			
			if(settings.autoRotate === true && count > 1){
				createInterval();
			}

			// init
			showFeature(1);
		});
	};
})(jQuery);