(function($){
	// Needs the mousewheel plugin and the imageReady plugin
	$.fn.prettyOverflow = function(options) {
		var options = $.extend({}, options);
		this.filter(':not(.jq_prettyOverflow)').addClass('jq_prettyOverflow').each(function() {
			var helper = $(this), content, helperWidth, contentWidth, currentLeft;
			
			// Wait for images to be loaded; otherwise we don't know the width of the element
			$('img', helper).imageReady(goOnThen);
			
			function goOnThen() {
				if (options.innerWrap) helper.css({position: 'relative'}).wrapInner('<div/>');
				helperWidth = helper.parent().width();
				content = helper.children(':first').css({position: 'absolute'});
				contentWidth = content.width();
				var checkButtons = function() {
					bUp.css('display', (currentLeft < 0 ? 'block' : 'none'));
					bDown.css('display', (-(currentLeft - helperWidth) < contentWidth ? 'block' : 'none'));
				};
				
				currentLeft = content.position().left;
				if (contentWidth > helperWidth) {
					helper.css({overflow: 'hidden', zoom: 1});
					var helperParent = helper.parent();
					var bDown = $('<img class="nextButton"/>')
						.appendTo(helperParent)
						.attr('src', rootURL + 'assets/buttons/next.png')
						.click(function(e) {
							currentLeft -= helperWidth;
							if (-(currentLeft - helperWidth) > contentWidth) currentLeft = -contentWidth + helperWidth;
							content.stop().animate({left: currentLeft}, 'normal', 'easeOutCubic', checkButtons);
						});
					var bUp = $('<img class="prevButton"/>')
						.prependTo(helperParent)
						.attr('src', rootURL + 'assets/buttons/prev.png')
						.click(function() {
							currentLeft += helperWidth;
							if (currentLeft > 0) currentLeft = 0;
							content.stop().animate({left: currentLeft}, 'normal', 'easeOutCubic', checkButtons);
						});
					
					checkButtons();
					
					var scrollTimeout = false;
					helper.mousewheel(function(e, delta) {
						e.preventDefault();
						currentLeft += delta * 150;
						if (currentLeft > 0) currentLeft = 0;
						if (-(currentLeft - helperWidth) > contentWidth) currentLeft = -contentWidth + helperWidth;
						content.get(0).style.left = currentLeft + 'px';
						checkButtons();
					});
				}
			}
			
		});
		return this;
	}
})(jQuery);