(function($){
	$.fn.madcarousel = function(params) {
		
		var params = $.extend({
			height: 500,
			width: 200,
			loop: true,
			speed: 2500,
			pause: 1500
		}, params);
		
		var slideTimer = null;
		var loop = true;
		var scroll = true;						

		function slideUp(jElt) {
			
			var moveHeight = jElt.children(':first').innerHeight();
			
			jElt.animate({
				top: '-' + moveHeight
			}, params.speed, function() {
				jElt.children(':first').appendTo(jElt);
				jElt.css('top', 0);
				if(loop) {
					slideTimer = setTimeout(function() {
						slideUp(jElt);
					}, params.pause);
				} else {
					loop = true;
				}
			});
			
		};
		
		function slideDown(jElt) {

			var moveHeight = jElt.children(':last').prependTo(jElt).innerHeight();
			
			jElt
				.css({
					top: '-' + moveHeight + 'px'
				})
				.animate({
					top: 0
				}, params.speed, function(){
					if(loop) {
						slideTimer = setTimeout(function() {
							slideDown(jElt);
						}, params.pause);
					} else {
						loop = true;
					}
				});
				
		}
		
		return this.each(function() {
			
			var jElt = $(this);
			
			jElt.wrap($(document.createElement('div')).addClass('madcarousel')).css({
				position: 'absolute',
				top: 0,
				left: 0
			});
			
			jElt.parent('.madcarousel')
				.wrapInner(
					$(document.createElement('div'))
						.addClass('clip')
						.css({
							position: 'relative',
							height: params.height,
							width: params.width,
							overflow: 'hidden'
						})
				)
				.prepend(
					$(document.createElement('div'))
						.addClass('arrow arrowUp')
						.click(function() {
							clearTimeout(slideTimer);
							loop = false;
							slideUp(jElt);
						})
				)
				.append(
					$(document.createElement('div'))
						.addClass('arrow arrowDown')
						.click(function() {
							clearTimeout(slideTimer);
							loop = false;
							slideDown(jElt);
						})
				);
			jElt.children().css('display', 'block');
			
			if(jElt.height() > params.height) {
				slideUp(jElt);
			} else {
				scroll = false;
				jElt.parents('.madcarousel').find('.arrow').css('display', 'none');
			}
		});
	};
})(jQuery);