(function () {

    jQuery.slideviewer = {
        instanceList: []
    };


    jQuery.fn.slideviewer = function (options) {
        this.each(function (i) {
            var slideViewer = new SlideViewer(this, options.imageUrlList);
            slideViewer.onChange = options.onChange;
            if (options.transitionSpeed) {
                slideViewer.transitionSpeed = options.transitionSpeed;
            }
            jQuery.slideviewer.instanceList.push(slideViewer);
            slideViewer.index = jQuery.slideviewer.instanceList.length - 1;
        });
    }


    var SlideViewer = function (containerElement, imageUrlList) {
        var containerElement = jQuery(containerElement);
        this.imageUrlList = imageUrlList;
        this.preload(imageUrlList);
        this.currentIndex = 0;
        this.transitionSpeed = 1000;
        this.onChange = null;
        this.isTransitioning = false;
        this.backImage = containerElement.find('img');
        if (containerElement.css('position') == 'static') {
            containerElement.css({
                position: 'relative'
            });
        }
        containerElement.height(this.backImage.height());
        containerElement.width(this.backImage.width());
        this.backImage.css({
            position: 'absolute',
            top: '0px',
            left: '0px'
        });
        this.backImage.css('position', 'absolute');
        this.frontImage = this.backImage.clone();
        containerElement.append(this.frontImage);
    }


    SlideViewer.prototype.preload = function (imageList) {
        this.imageCache = [];
        for (var i=0; i < imageList.length; i++) {
            var image = document.createElement('img');
            image.src = imageList[i];
            this.imageCache.push(image);
        }
    }


    SlideViewer.prototype.changeTo = function (index) {
        if (this.isTransitioning) {
            return;
        }
        this.isTransitioning = true;
        var thisSlideviewer = this;
        thisSlideviewer.frontImage.show();
        this.backImage.attr('src', this.imageUrlList[index]);
        this.frontImage.fadeOut({
            complete: function () {
                thisSlideviewer.frontImage.attr('src', thisSlideviewer.backImage.attr('src'));
                thisSlideviewer.currentIndex = index;
                thisSlideviewer.isTransitioning = false;
                if (thisSlideviewer.onChange) {
                    thisSlideviewer.onChange.call(thisSlideviewer);
                }
            },
            duration: this.transitionSpeed
        });
    }


    SlideViewer.prototype.previous = function () {
        var previousIndex = this.currentIndex - 1;
        if (previousIndex < 0) {
            previousIndex = this.imageUrlList.length - 1;
        }
        this.changeTo(previousIndex);
        return previousIndex;
    }


    SlideViewer.prototype.next = function () {
        var nextIndex = this.currentIndex + 1;
        if (nextIndex == this.imageUrlList.length) {
            nextIndex = 0;
        }
        this.changeTo(nextIndex);
        return nextIndex;
    }





})();
