(function($){

	function Carousel(el, options) {

		//Defaults:
		this.defaults = {
			interval: 2000,
			pauseOnHover: true
		};

		//Extending options:
		this.opts = $.extend({}, this.defaults, options);

		//Privates:
		this.$el = $(el);
	}

	// Separate functionality from object creation
	Carousel.prototype = {

		init: function() {
			var _this = this;
			var _el = this.$el;
			
        	var children = _el.children();
			children.hide();

			_el.bind("mousewheel click", function(event, delta) {
				if (delta > 0)
				{
					_this.next();
				}
				else
				{
					_this.previous();
				}
			});

			_this.next();
		},

		next: function() {
			var _this = this;
			var _el = this.$el;
			
			var children = _el.children();
			var visible = children.filter(":visible");
			
			if (visible)
			{
				var current = visible.first().index();
				var next = (current + 1) % children.size();
				var nextEl = children.eq(next);
				nextEl.fadeIn();
				visible.hide();
			}
			else
			{
				children.first().fadeIn();
			}
		},

		previous: function() {
			var _this = this;
			var _el = this.$el;

			var children = _el.children();
			var visible = children.filter(":visible");
			
			if (visible)
			{
				var current = visible.first().index();
				var previous = ((current + children.size()) - 1) % children.size();
				var previousEl = children.eq(previous);
				previousEl.fadeIn();
				visible.hide();
			}
			else
			{
				children.first().fadeIn();
			}
		}
	};

	// The actual plugin
	$.fn.carousel = function(options) {
		if(this.length) {
			this.each(function() {
				var rev = new Carousel(this, options);
				rev.init();
				$(this).data('carousel', rev);
			});
		}
	};
})(jQuery);
