//--- Snurra -------------------------------------------->
var intervalDur;
var scrollDur;
var playing;
var currentAnimation;
var currentObject;
var first;
var navItems;
var scroller;
var scrollerApi;
var scrollerHolder;
var autoScroll;
var startTimeout;
var doAutoScroll;
var stopTimeout;
var stopAutoScroll;
var startAutoScroll;

$(document).ready(function () {
  
  var browser = BrowserDetect.browser;
  
    // Change the variables below to adjust scrolling behavior
    intervalDur = 4000; // how long a slide stays on the screen, before it scrolls to the next one (ms)
    scrollDur = 2000; // how long the transition between slides takes (ms)

    playing = true;
    currentAnimation;
    currentObject;
    first = false;
    navItems = $(".Scroll ul li");
    scroller = $(".Scroll").scrollable({
        speed: scrollDur,
        circular: true
    }).navigator({
        navi: ".ScrollerNav .NavItems ul"
    });
    scrollerApi = scroller.data("scrollable");
    scrollerHolder = $(".Scroll .items li");
  
    autoScroll;

    startTimeout = function () {
        if (first == true) {
            scrollerApi.next();
            first = false;
        }
        autoScroll = setTimeout("doAutoScroll()", intervalDur);
    };

    doAutoScroll = function () {
        stopTimeout();
        scrollerApi.next();
    }
      
    scrollerApi.seekTo(0);
  
    startTimeout();

    stopTimeout = function () {
        clearTimeout(autoScroll);
    }


    navItems.click(function () { stopAutoScroll(); });

    scrollerApi.onBeforeSeek(function (obj, index) {
        if (playing == true) {
            if (currentAnimation != null && currentObject != null) {
                currentAnimation.stop();
                currentObject.css('background-position', '-23px 0px');
            }
        }
    });

    scrollerApi.onSeek(function (obj, index) {
        if (playing == true) {
            stopTimeout();
            startTimeout();

            navItems.eq(index).css('background-position', '-23px 0px');
            currentObject = navItems.eq(index);
        } else {

        }
    });

    stopAutoScroll = function () {
        if (currentAnimation != null) {
            currentAnimation.stop();
        }
        navItems.each(function (index) { $(this).css('background-position', '-23px 0px'); });
        stopTimeout();
        first = true;
        playing = false;
    };

    startAutoScroll = function () {
        startTimeout();
        playing = true;
    };

    $("#playPause").click(function () {
        if (playing == true) {
            stopAutoScroll();
        } else {
            startAutoScroll();
        }
    });

    var focusedElement;

    var getItemIndex = function (element) {
        var index = -1;
        for (i = 0; i <= 100; i = i++) {
            if (element.parent().attr("class") == "items") {
                index = scrollerApi.getItems().index(element);
                break;
            } else {
                element = element.parent();
            }
        }
        return index;
    };

    var triggerScroll = function (element) {
        stopAutoScroll();
        if (element != null) {
            var index = getItemIndex(element);
            if (index != scrollerApi.getIndex() && index >= 0) {
                scrollerApi.seekTo(index, scrollDur);
            }
        }
    };

    $(".LargeBanner .Scroll").scroll(function (event) { if ($(this).scrollLeft() != 0) { $(this).scrollLeft(0); } });
});
