(function ($) {
    $.fn.slider = function (speed,number_of_visible_items) {
		var pause = speed*2;
		var index=0;
		var delta=0;
		var items = null;
		var container = null;
		var locked = false;
		var stop = false;
		
		function animate() {
            if (locked || index == items)
                return;

           	locked = true;
            container.children().eq(0).animate({"top": "-=" + delta}, speed, function () {
                locked = false;
                index = index + 1;
            });
		}
		
		function reset_animation() {
            locked = true;
            container.children().eq(0).animate({"top": "0"}, speed, function () {
                locked = false;
                index = 0;
            });
		}
		
		return this.each(function () {
			container = $(this);
			delta = container.children().eq(0).children().eq(0).height();
			container.css("height", (delta * number_of_visible_items));
			
			items = container.children().eq(0).children().size() - number_of_visible_items;
			
			setInterval(function () {
				if (!stop) {
	                if (index == items)
	                    reset_animation();
	                else
	                    animate();
				}
            }, pause);

			container.mouseenter(
				function(){
					stop = true;
				}
			);

			container.mouseleave(
				function(){
					stop = false;
				}
			);

        });
	}
})(jQuery);


// start the animation
$(document).ready(function(){
	$("#slider").slider(500,3);
});
