var Ev;
if (!Ev) Ev = {};
Ev.Carousel = function (featurePagesDomElement, navigationDomElement, autoMiliseconds) {
    this._featureDomElement = $(featurePagesDomElement)
    this._pagesDomElement = $('li', featurePagesDomElement);
    this._navigationDomElement = $(navigationDomElement);
    this._currentItem = $('li.active', this._featureDomElement);
    this._currentNavLink = $('li.active', this._navigationDomElement);
    this._animating = false;
    this._currentItemIndex = 0;
    this._autoMiliseconds = autoMiliseconds;
    this._initItems()
    this._initListeners();
}
Ev.Carousel.prototype = {
    _pagesDomElement: null,
    _navigationDomElment: null,
    _currentItem: null,
    _currentNavLink: null,
    _currentItemIndex: null,
    _autoInterval: null,
    _autoMiliseconds: null,
    _animating: null,
    _initItems: function () {
        $(this._pagesDomElement).each(function () {
            if (!$(this).hasClass('active')) $(this).hide();
        })
    },
    _initListeners: function () {
        var xThis = this;
        $('li', this._navigationDomElement).click(function () {
            if (xThis._animating == true) { return false; }
            clearInterval(xThis._autoInterval);
            if ((!$(this).hasClass('active')) && (xThis._animating == false)) {
                xThis._animating = true;
                xThis._doItemSelected($(this).index());
            }
        });
        this._autoInterval = setInterval(function () {
            if (xThis._currentItemIndex == $(xThis._pagesDomElement).size() - 1) {
                xThis._doItemSelected(0);
            } else {
                xThis._doItemSelected(xThis._currentItemIndex + 1);
            }
        }, this._autoMiliseconds);
    },
    _doItemSelected: function (index) {
        var xThis = this;
        $('li:nth-child(' + parseInt(index + 1) + ')', this._navigationDomElement).addClass('active');
        $('li:nth-child(' + parseInt(this._currentItemIndex + 1) + ')', this._navigationDomElement).removeClass('active');
        xThis._currentNavLink = $('li:nth-child(' + parseInt(this._currentItemIndex + 1) + ')', this._navigationDomElement);
        this._currentItemIndex = index;
        var fadeSpeed = ($.support.opacity) ? 'slow' : 0;
        $(this._currentItem).fadeOut(fadeSpeed, function () {
            xThis._animating = false;
            $(xThis._currentItem).removeClass('active');
            xThis._currentItem = $(xThis._pagesDomElement[index]);
            $(xThis._pagesDomElement[index]).fadeIn(fadeSpeed).addClass('active');
        });
    }
}
