$(document).ready(function () {
    var events = $('.app\\/sidebar\\/events .item');
    var num = events.size();
    if (num == 0)
    {
        return;
    }
    var max_height = 0;
    events.each(function () {
        var height = $(this).outerHeight();
        if (height > max_height)
        {
            max_height = height;
        }
    });
    $('.app\\/sidebar\\/events .interior').css({height:(max_height+12)+'px'});
    var container = $('.app\\/sidebar\\/events .items');
    var prev = $('.app\\/sidebar\\/events .nav .prev');
    var next = $('.app\\/sidebar\\/events .nav .next');
    var cur = 0;
    var showing = $(events.get(cur));
    var rotating = true;
    var animating = false;
    
    // nav initially hidden
    $('.app\\/sidebar\\/events .nav').show();
    
    function show(which)
    {
        if (animating || which == cur || which < 0 || which > num-1)
        {
            return;
        }
        animating = true;
        var to_show = $(events.get(which));
        container.height(to_show.height());
        var start_css = {display:"block",position:"absolute"};
        var end_css = {};
        if (which > cur)
        {
            // coming in from the right
            start_css.left = container.outerWidth()+'px';
            end_css.left = (-1*container.outerWidth())+'px';
        }
        else
        {
            // coming in from the left
            start_css.left = (-1*container.outerWidth())+'px';
            end_css.left = container.outerWidth()+'px';
        }
        to_show.css(start_css);
        showing.css({position:'absolute',left:'0px'});
        showing.animate(end_css);
        to_show.animate({left:'0px'}, function () {
            cur = which;
            showing = to_show;
            animating = false;
            if (cur == 0)
            {
                prev.addClass('prev-disabled');
            }
            else
            {
                prev.removeClass('prev-disabled');
            }
            if (cur == num-1)
            {
                next.addClass('next-disabled');
            }
            else
            {
                next.removeClass('next-disabled');
            }
        });
    }
    
    prev.click(function (e) {
        e.preventDefault();
        show(cur-1);
    });
    
    next.click(function (e) {
        e.preventDefault();
        show(cur+1);
    });

    // rotation
    var timer = false;
    var rotate_pause = 5000;
    
    function rotate()
    {
        if (!rotating)
        {
            return;
        }
        if (cur == num-1)
        {
            // go back to the first one
            show(0);
        }
        else
        {
            show(cur+1);
        }
        timer = window.setTimeout(rotate, rotate_pause);
    }
    
    $('.app\\/sidebar\\/events').hover(function () {
        rotating = false;
        window.clearTimeout(timer);
    }, function () {
        rotating = true;
        timer = window.setTimeout(rotate, Math.round(rotate_pause/2));
    });
    timer = window.setTimeout(rotate, rotate_pause);
});
