﻿var panoSlideShow = {
    itemsToProcess: '',
    showCaption: true,
    hoverToPause: false,
    currentTimeOutFunction: null,
    findNextItem: function (currentItem) {
        if (currentItem.next().length) {
            return currentItem.next();
        }

        return currentItem.parent().children().first();
    },
    findPreviousItem: function (currentItem) {
        if (currentItem.prev().length)
            return currentItem.prev();

        return currentItem.parent().children().last();
    },
    showItem: function (currentItem) {
        var _this = this;
        var prevItem = _this.findPreviousItem(currentItem);
        var nextItem = _this.findNextItem(currentItem);

        if (prevItem.is(':visible'))
            prevItem.fadeOut('slow');

        currentItem.fadeIn('slow');

        if (_this.showCaption) {
            $('.caption span').text(currentItem.children().attr('alt'));
        }

        _this.currentTimeOutFunction = setTimeout(function () { _this.showItem(nextItem) }, 4000);
    },
    startSlideShow: function () {
        var _this = this;

        if (_this.itemsToProcess != '') {
            $(_this.itemsToProcess).hide();

            if (_this.hoverToPause) {
                $(_this.itemsToProcess).each(function () {
                    $(this).hover(function () {
                        clearTimeout(_this.currentTimeOutFunction);
                    }, function () {
                        _this.showItem(_this.findNextItem($(this)));
                    });
                });
            }

            _this.showItem($(_this.itemsToProcess).first());
        }
    }
};
