$(function() {

        var a  = 1.6, // acceleration
            t  = 50,  // milliseconds between updates
            s0 = 1,   // size of first step (in px)
            interval = 5000,
            stageContainer = $('#stage-wrapper'),
            stage = $('#stage'),
            isMoving = false,
            timer;


        $('body').addClass('hasjs');

        $('<p>')
            .addClass('controls')
            .append($('<a>')
                    .addClass('prev')
                    .attr('href', '#')
                    .click(function() {
                            restartAutoAnimation();
                            slideLeft();
                            return false;
                        })
                    )
            .append($('<a>')
                    .addClass('next')
                    .attr('href', '#')
                    .click(function() {
                            restartAutoAnimation();
                            slideRight();
                            return false;
                        })
                    )
            .appendTo(stageContainer);

        restartAutoAnimation();
        stage.mousemove(restartAutoAnimation);
        stage.click(restartAutoAnimation);

        $('li:first', stage).clone().appendTo(stage);
        stage.width(stage.children().size() * stage.width());

        function restartAutoAnimation() {
            clearInterval(timer);
            timer = setInterval(function() { slideRight(); }, interval);
        }

        function slideLeft()
        {
            if (parseInt(stage.css('left').replace('px', '')) === 0) {
                stage.css('left', -1 * (stage.width() - stageContainer.width()) + 'px');
            }
            slide(1);
        }

        function slideRight()
        {
            if (parseInt(stage.css('left').replace('px', '')) === -1 * (stage.width() - stageContainer.width())) {
                stage.css('left', '0');
            }
            slide(-1);
        }

        function slide(d) {
            if (isMoving) {
                return;
            }
            isMoving = true;

            var l = animArray.length,
                pos = parseInt(stage.css('left').replace('px', ''));

            function updatePos(p) {
                return function() {
                        stage.css('left', p + 'px');
                    };
            }

            for (var i=0; i<l; i++) {
                pos += d * animArray[i];
                setTimeout(updatePos(pos), (i+1)*t);
            }
            setTimeout(function() {isMoving = false;}, (i+1)*t);
        }


        var animArray = function() {
            var s = stageContainer.width(),
                r = [],
                lft = parseInt(s/2),
                nxt = s0,
                nxtpx = 1,
                i = 0, k;

            while (lft >= nxtpx) {
                r[i++] = nxtpx;
                lft -= nxtpx;
                nxt *= a;
                nxtpx = parseInt(nxt);
            }

            if (lft < r[i-1]) {
                r[--i] += lft;
            } else {
                r[i] = lft;
            }

            k = i;
            if (s%2 === 1) {
                r[i+1] = r[i];
                r[i]++;
                k = i+1;
                i--;
            }

            while (i>=0) {
                r[++k] = r[i--];
            }

            return r;
        }();
    });
