﻿(function($){

    $.fn.scroll = function(height, pause, speed) {
        
        var defaults = { height : 200, pause : 3, speed : 5 };
        var settings = $.extend({}, defaults, { height : height, pause : pause, speed : speed });
        
        var scroll = function ($content) {
            //console.log($content);
            $content
                .animate({ marginTop: -($content.height() - settings.height) }, $content.height() * settings.speed * 10, "linear", function() {
                    setTimeout(function() { resetScroller($content); }, settings.pause * 1000);
                });
        };
        
        var resetScroller = function($content) {
            $content
                .fadeOut(function() {
                    $content
                        .css("marginTop", 0)
                        .fadeIn(function() {
                            setTimeout(function() { scroll($content); }, settings.pause * 1000);
                        });
                });
        };
        
        $(this).each(function(){
            $(this)
                .css({ height: settings.height, overflow: "hidden" })
                .wrapInner("<div class=\"scroller-content\"></div>");
            
            var $content = $(".scroller-content", this); // Creates a closure to scope $content
            setTimeout(function() { scroll($content); }, settings.pause * 1000);
        });
                
        return this;
    };

})(jQuery);