$(document).ready(function () {
    var container = $('.app\\/home\\/video .thumbs');
    container.addClass('thumbs-js');
    var thumbs = container.children('.thumb');
    var _thumbs = [];
    thumbs.each(function () {
        _thumbs.push($(this));
    });
    _thumbs.sort(function () { return 0.5 - Math.random(); });
    _thumbs[0].show();
    
    var pause_time = 3000;
    var anim_time = 1000;
    var cur_thumb = 0;
    var h = thumbs.eq(0).outerHeight();
    var animating = false;
    function rotate_thumbs()
    {
        if (animating)
        {
            return;
        }
        animating = true;
        var next_thumb = cur_thumb+1;
        if (next_thumb >= thumbs.size())
        {
            next_thumb = 0;
        }
        var $cur = _thumbs[cur_thumb];
        var $next = _thumbs[next_thumb];
        $cur.animate({top:(h)+'px'}, anim_time, function () { $(this).hide(); });
        $next.css({top:(-1*h)+'px'}).show().animate({top:'0px'}, anim_time, function () {
            cur_thumb = next_thumb;
            animating = false;
        });
    }
    var timer = window.setInterval(rotate_thumbs, pause_time);
    container.hover(function () {
        window.clearInterval(timer);
    }, function () {
        timer = window.setInterval(rotate_thumbs, pause_time);
    });
});
