﻿(function($) {

    $.fn.scrollThumbs = function(selector) {
        selector = selector || "img"
        var $self = this,
            $thumbs = $self.find(selector);
        $thumbs.css({ float: "left", display: "none" });

        var maxHeight = 0;
        $.each($thumbs, function() { maxHeight = Math.max($(this).height(), maxHeight); });
        $self.height(maxHeight);

        var currentFirst = 0,
            currentThumbsWidth = 0;
        $thumbs.each(function() {
            if (currentThumbsWidth + $(this).width() > $self.width()) return false;

            $(this).show();
            currentThumbsWidth += $(this).width();
        });

        var moveNext = function() {
            var next = $self.find(selector + ":hidden:first");
            next = $(next[Math.floor(next.length * Math.random())]);
            $self.find(selector + ":visible:first").hide("slow", function() {
                next.remove().appendTo($self).fadeIn("slow");
            });
            setTimeout(function() { $self.queue(moveNext); $self.dequeue(); }, 5000);
        };
        moveNext();


    }

})(jQuery)