$(document).ready(function ()
{
    var splash = $('div.app\\/home\\/splash .splash');
    var splash_img;
    var splash_src = splash.children('img').attr('src');
    var rotating = true;
    
    var scroll_duration = 2000;
    scroll_duration = 11583;
    var pause_duration = 2500;
    pause_duration = 0;
    var timer;

    var cur_step = 1;
    var gotoStep = function (step)
    {
        if (step > 5)
        {
            step = 1;
        }
        if (step == cur_step)
        {
            return;
        }
        
        var target_left;
        var cur_left = splash_img.css('left');
        if (!cur_left)
        {
            cur_left = 0;
        }
        else
        {
            cur_left = parseInt(cur_left.substr(0, cur_left.length-2));
        }
        
        if (step == 1 && cur_step == 5)
        {
            target_left = -1*695*5;
        }
        else
        {
            target_left = -1*695*(step-1);
        }

        var remaining = Math.abs(target_left - cur_left);
        splash_img.animate({ left : target_left+'px' }, scroll_duration*(remaining/695), 'linear',
                function ()
                {
                    if (step == 1)
                    {
                        splash_img.css({ left : '0px' });
                    }
                    cur_step = step;
                    if (pause_duration == 0)
                    {
                        rotate();
                    }
                    else
                    {
                        timer = window.setTimeout(rotate, pause_duration);
                    }
                }
        );
    };
    
    var rotate = function ()
    {
        if (!rotating)
        {
            return; // and don't reschedule
        }
        // show the next slide and reschedule
        var new_step = cur_step + 1;
        gotoStep(new_step);
    };
    
    var setupControls = function ()
    {        
        // time till next frame
        
        // start rotating
        splash.hover(
                function (e)
                {
                    rotating = false;
                    splash_img.stop(true);
                    if (pause_duration > 0)
                    {
                        window.clearTimeout(timer);
                    }
                },
                function (e)
                {
                    rotating = true;
                    gotoStep(cur_step+1);
                }
            );
        if (pause_duration == 0)
        {
            timer = window.setTimeout(function () { gotoStep(2); }, 2000);
        }
        else
        {
            timer = window.setTimeout(function () { gotoStep(2) }, pause_duration*2);
        }
    };
    
    $('<img style="display:none;" />').bind('load', function ()
        {
            splash.removeClass('loading');
            splash_img = $('<div style="position:absolute;left:0;top:0;width:4170px;height:330px;background-image:url('+splash_src+')"></div>').appendTo(splash);
            splash_img.fadeIn('slow', function () { setupControls(); });
        }).attr('src', splash_src);
});