$(document).ready(function () {
    //  home slideshow
    var container = $('.home-features .slides');
    var slides = container.find('.slide');
    var slide_width = 780;
    var rotate_pause = 5000;
    var rotate_time = 1000;
    var rotate_timer;
    var rotating = true;
    var animating = false;
    var cur = 0;
    var num = slides.size();
    
    // clone first slide so we can rotate to it without rewinding
    container.css('width', ((num+2)*slide_width)+'px');
    $(slides.get(0)).clone().appendTo(container);
    // clone last slide for the same reason
    $(slides.get(num-1)).clone().prependTo(container);
    // reposition so first slide is still showing
    container.css({'margin-left':(-1*slide_width)+'px'});
    
    function show_slide(which, by_rotate, by_prev)
    {
        if (cur == which || animating)
        {
            return;
        }
        if (which < 0)
        {
            which = num-1;
        }
        if (which >= num)
        {
            which = 0;
        }
        if (rotate_timer)
        {
            window.clearTimeout(rotate_timer);
            rotate_timer = false;
        }
        var target_l = -1*which*slide_width - slide_width;
        if (by_rotate && which == 0)
        {
            target_l = -1*num*slide_width - slide_width;
        }
        if (by_prev && which == num-1)
        {
            target_l = 0;
        }
        animating = true;
        container.animate({marginLeft:target_l+"px"}, rotate_time, function () {
            cur = which;
            if (by_rotate && which == 0)
            {
                container.css({"margin-left":(-1*slide_width)+'px'});
            }
            if (by_prev && which == num-1)
            {
                container.css({"margin-left":(-1*slide_width*num)+'px'});
            }
            if (rotating)
            {
                rotate_timer = window.setTimeout(rotate, rotate_pause);
            }
            animating = false;
        });
    }

    
    function rotate()
    {
        if (!rotating || animating)
        {
            return; // and don't reschedule
        }
        var next = cur+1;
        if (next >= num)
        {
            next = 0;
        }
        show_slide(next, true);
    }
    
    // controls
    $('.home-features .controls a.prev').click(function (e) {
        e.preventDefault();
        show_slide(cur-1, false, true);
    });
    $('.home-features .controls a.next').click(function (e) {
        e.preventDefault();
        show_slide(cur+1, true);
    });
    
    // pause on hover
    $('.home-features').hover(function () {
        rotating = false;
        if (rotate_timer)
        {
            window.clearTimeout(rotate_timer);
            rotate_timer = false;
        }
    },
    function () {
        rotating = true;
        if (!rotate_timer)
        {
            rotate_timer = window.setTimeout(rotate, rotate_pause/2);
        }
    });
    
    // start rotating
    rotate_timer = window.setTimeout(rotate, rotate_pause);
});
