$(document).ready(function () {

    // The number of seconds that the slider will auto-advance in
    var changeEvery = 5; // Seconds
    var fadeSpeed = 400; // Milliseconds

    //Click function for the Lawson rotator
    $('.slider-menu ul li a').click(function (e, keepScroll) {
        $('li.menuItem').removeClass('act').addClass('inact');
        $(this).parent().addClass('act');

        e.preventDefault();

        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();

        // Stopping the auto-advance if manually clicked
        if (!keepScroll) {
            clearInterval(itvl);
            $('.slides').cycle('pause');

            // FadeOut the old slide, and FadeIn the selected slide
            var slide;
            var pos = $(this).parent().prevAll('.menuItem').length;
            var slides = $('.slides').children();
            for (var i = 0; i < slides.length; i++) {
                slide = $('.slides').children('$:eq(' + i + ')');
                slide.fadeOut(fadeSpeed);
                slide.attr('style', 'left:0px;top:0px;width:735px;height:414px;display:none;position:absolute;z-index:1;opacity:0;')
            }
            slide = $('.slides').children('$:eq(' + pos + ')');
            slide.attr('style', 'left:0px;top:0px;width:735px;height:414px;display:visible;position:absolute;z-index:5;')
            slide.fadeIn(fadeSpeed);
        }
        return false;
    });

    // Mark the first thumbnail as active
    $('.slider-menu  ul li.menuItem:first').addClass('act').siblings().addClass('inact');

    changeEvery = changeEvery * 1000; //milliseconds
    var current = 1;

    // Start the animations
    var itvl = setInterval(function () { autoAdvance() }, changeEvery);
    $('.slides').cycle({
        fx: 'fade',
        speed: fadeSpeed,
        timeout: changeEvery
    });

    // Enable auto-advance. //
    function autoAdvance() {
        if (current == -1) return false;
        $('.slider-menu  a').eq(current % $('.slider-menu a').length).trigger('click', [true]); // [true] will be passed as the keepScroll parameter of the click function on line 28
        current++;
    }
});
