$(function() {

    var totalImages = 3;
    var timeout = 10000;

    var startImage = Math.floor(Math.random() * 3) + 1;
    $("div.splash-image img:nth-child(" + startImage + ")").show();
    var startTesimonial = Math.floor(Math.random() * 3) + 1;
    $("#sidebar-testimonial-holder div.sidebar-testimonial:nth-child(" + startTesimonial + ")").show();

    $("#left-arrow").click(function () {
        clearTimeout(timeoutId);
        var current = '#' + $('.splash-image img:visible').attr('id');
        var id = current.split('_')[1];
        id--;
        if (id == 0)
            id = totalImages;
        var next = '#splash_' + id;
        switchSplash(current, next);
    });

    var timeoutId = -1;
    var switchSplash = function(current, next) {
        $(current).parent().css({backgroundImage: "url('" + $(current).attr('src') + "')"});
        $(current).hide();
        $(next).fadeIn(1200, function() {
            timeoutId = setTimeout(rightFunction, timeout);
        });
    }

    var rightFunction = function() {
        clearTimeout(timeoutId);
        var current = '#' + $('.splash-image img:visible').attr('id');
        var id = current.split('_')[1];
        id++;
        if (id == totalImages + 1)
            id = 1;
        var next = '#splash_' + id;
        switchSplash(current, next);
    };

    $("#right-arrow").click(rightFunction);

    timeoutId = setTimeout(rightFunction, timeout);
});


