$(document).ready(function () {
    Rotator.start()
});
var Rotator = {
    settings: {
        interval: 3500,
        fadeTime: 500,
        forceFadeTime: 50
    },
    _timer: null,
    _index: -1,
    _items: [],
    _root: null,
    _hoverTimer: null,
    _onHover: function () {
        if (this._hoverTimer) {
            window.clearTimeout(this._hoverTimer)
        }
        this._root.find('.console').fadeTo(100, 0.95)
    },
    _onUnhover: function () {
        var a = this;
        this._hoverTimer = window.setTimeout(function () {
            a._root.find('.console').fadeTo(500, 0)
        }, 1000)
    },
    _populateConsole: function () {
        var a = this;
        var b = $("<div class='console'></div>");
        b.fadeTo(2000, 0);
        this._root.append(b);
        for (var i = 0; i < this._items.length; ++i) {
            var c = $('<a href="#">' + (i + 1) + '</a>');
            c[0].index = i;
            c.click(function () {
                a.flipTo(this.index);
                return false
            });
            b.append(c)
        }
        this._root.hover(function () {
            _this._onHover()
        }, function () {
            _this._onUnhover()
        })
    },
    _stopTimer: function () {
        if (this._timer) {
            window.clearTimeout(this._timer)
        }
    },
    _startTimer: function () {
        this._stopTimer();
        this._timer = window.setTimeout(function () {
            _this.flip()
        }, this.settings.interval)
    },
    start: function () {
        var a = this;
        this._root = $('#rotator');
        this._items = this._root.find('.rotatee');
        this._items.css({
            display: 'none'
        });
        this._populateConsole();
        this._startTimer();
        this.flip()
    },
    flipTo: function (a) {
        this._stopTimer();
        this._items.stop();
        this._items.css({
            zIndex: 0
        });
        $(this._items[a]).css({
            zIndex: 1
        }).fadeTo(1, 1.0);
        for (var i = 0; i < this._items.length; ++i) {
            if (i != a) {
                $(this._items[i]).fadeTo(1, 0.0)
            }
        }
        this._index = a;
        this._updateConsole(this._index);
        this._startTimer()
    },
    _updateConsole: function () {
        var a = this._root.find('.console a');
        a.removeClass('active');
        $(a[this._index]).addClass('active')
    },
    flip: function () {
        _this = this;
        old_el = null;
        if (_this._index != -1) {
            old_el = $(_this._items[_this._index])
        }
        _this._index++;
        if (_this._index >= _this._items.length) {
            _this._index = 0
        }
        new_el = $(this._items[this._index]);
        fadeTime = this.settings.fadeTime;
        if (!old_el) {
            var a = _this._items;
            a.css({
                zIndex: 0
            }).show().fadeTo(1, 0.0);
            new_el.css({
                zIndex: 1
            });
            new_el.show().fadeTo(1, 1.0);
            this._index = 0;
            this._updateConsole()
        } else {
            old_el.css({
                zIndex: 0
            });
            new_el.css({
                zIndex: 1
            });
            this._items.stop();
            var b = this;
            new_el.fadeTo(fadeTime, 1.0, function () {
                old_el.fadeTo(1, 0.0);
                b._updateConsole()
            })
        }
        this._startTimer()
    }
};
