var home = {
    option: {
        idTimer: 'sc-links',
        classWrapper: 'article',
        classMainLink: 'fancy-link',
        classLink: 'link',
        classLinkActive: 'showing',
        timerSpeed: 80,
        backgroundMove: 35,
        backgroundMax: 2680
    },
    backgroundPosition: 0,
    timerStarted: false,
    init: function () {
        // Start timer
        home.timerStarted = true;
        home.timer();
        // Clear background image from body
        // PC - Changed the background property from none to black to stop the
        // white strip towards the bottom of the page appearing on high res displays
        $('body').css('background', '#000');
        // Setup click links
        $('.' + home.option.classLink).click(function () {
            // Setup active state
            home._setLinkActive(this);
            // Reset the timer
            home.backgroundPosition = 0;
            home._setTimerBackground();
            // Change showcase info
            home._changeShowcase(this);
            return false;
        });
    },
    timer: function () {
        // Work out the background position and change showcase if needed
        if (home.backgroundPosition >= home.option.backgroundMax) {
            // Reset background position
            home.backgroundPosition = 0;
            // Work out next showcase, or first if no more
            element = home._findNextShowcase();
            // Setup active state
            home._setLinkActive(element);
            // Change showcase info
            home._changeShowcase(element);
        } else {
            // Increment the background position
            home.backgroundPosition += home.option.backgroundMove;
        }
        // Change the background position
        home._setTimerBackground();
        // After some millseconds start it all again
        setTimeout('home.timer()', home.option.timerSpeed);
    },
    _changeShowcase: function (element) {
        var li = $('.' + $(element).attr('id'));
        var overlay = $('<div></div>').css({
            background: $('.whole-page').css('backgroundImage') + ' no-repeat center top',
            width: $('body').width(),
            height: $('body').height(),
            position:'absolute',
            top:0
        });
        $('.whole-page').prepend(overlay);
        $('.whole-page').css({backgroundImage: 'url(' + li.find('img').attr('src') + ')'});

        overlay.fadeOut(function () {
            $('.' + home.option.classWrapper + ' ul.showcase li').addClass('no-show');
            li.removeClass('no-show');
            overlay.remove();
        });
    },
    _setTimerBackground: function () {
        $('#' + home.option.idTimer).css({backgroundPosition: '0px -' + home.backgroundPosition + 'px'});
    },
    _setLinkActive: function (element) {
        $('.' + home.option.classLink).removeClass(home.option.classLinkActive);
        $(element).addClass(home.option.classLinkActive);
    },
    _findNextShowcase: function () {
        // Find the next in the list
        var element = $('#' + home.option.idTimer + ' .' + home.option.classLink + '.' + home.option.classLinkActive).next();
        if (element.length == 0) {
            // If next does not exist (end of the list), return the first
            element = $('#' + home.option.idTimer + ' .' + home.option.classLink + ':first');
        }
        return element;
    }
}
$(document).ready(function () {
    home.init();
});
