﻿function SlideShow() {
    var _ssName = "ul.slideShow";
    var _liName = _ssName + "> li";
    var _duration = 1000;
    var _imagePos = 0;
    var _self = this;

    this.isReady = function () {
        return jQuery(_ssName).length == 1;
    };

    this.imageCount = function () {
        if (this.isReady()) {
            return jQuery(_liName).length;
        }
        return false;
    };

    this.setDuration = function (duration) {
        if (duration >= 0) {
            _duration = duration;
        }
    };

    this.begin = function () {
        if (this.isReady()) {
            var images = jQuery(_liName);
                
            jQuery(images[0]).css("display", "list-item");

            for (var count = 1; count < (images.length); count++) {
                jQuery(images[count]).css("display", "none");
            }

            setInterval(this.switchImage, 5000);
        }
    };

    this.switchImage = function () {
        if (_self.isReady()) {
            var images = jQuery(_liName);
            jQuery(images[_imagePos]).fadeOut(_duration);
            _imagePos = ((_imagePos + 1) == _self.imageCount()) ? 0 : _imagePos + 1;
            jQuery(images[_imagePos]).fadeIn(_duration);
        }
    };

};


jQuery(function () {
    var slideShow = new SlideShow();
    slideShow.begin();
});


