﻿
JsBase.Controls.Slideshow = function (_settings) {

    var _this = this;

    var _selectedIndex = 0;
    this.SelectedIndex = function (value) {
        if (typeof value == "undefined")
            return _selectedIndex;
        else
            _selectedIndex = value;
    }

    var _autoPlayTimeout;
    this.AutoPlayTimeout = function (value) {
        if (typeof value == "undefined")
            return _autoPlayTimeout;
        else
            _autoPlayTimeout = value;
    }

    this.Init = function () {
        _settings.Pager.find("a").each(function (i) { $(this).click(function () { _this.SelectSlide(i); }); });
        _this.AutoPlay();
    }

    this.AutoPlay = function () {
        var nextIndex = _this.SelectedIndex() + 1;

        if (nextIndex > _settings.Slides.length - 1) {
            nextIndex = 0;
        }

        _this.AutoPlayTimeout(setTimeout(function () {
            _this.SelectSlide(nextIndex);
            _this.AutoPlay();
        }, 5000));
    }

    this.SelectSlide = function (slideIndex) {

        clearTimeout(_this.AutoPlayTimeout());
        _settings.Pager.find("a").removeClass("Selected");
        _settings.Pager.find("a").eq(slideIndex).addClass("Selected");

        if (_this.SelectedIndex() != slideIndex) {
            _settings.Slides.eq(slideIndex).show();
            _settings.Slides.eq(_this.SelectedIndex()).fadeOut(1500, function () {
                _settings.Slides.eq(_this.SelectedIndex()).removeClass("Index-Masthead-Selected-Slide");
                _settings.Slides.eq(slideIndex).addClass("Index-Masthead-Selected-Slide");
                _this.SelectedIndex(slideIndex);
            });
        }
    }

    _this.Init();
}
