(function () {

    jQuery.fn.quotes = function () {
        this.each(function () {
            new Quotes(this);
        });
    };


    var Quotes = function (containerElement) {
        this.containerElement = jQuery(containerElement);
        this.quoteList = this.containerElement.find('.quotation');
        this.currentQuoteIndex = jQuery('.first_quotation').index();
        this.delay = 4000;
        this.changeAfterDelay();
    };


    Quotes.prototype.change = function () {
        var thisQuotes = this;
        var nextQuoteIndex = this.currentQuoteIndex + 1;
        if (nextQuoteIndex >= this.quoteList.length) {
            nextQuoteIndex = 0;
        }
        thisQuotes.quoteList.eq(this.currentQuoteIndex).fadeOut(1000, function () {
            thisQuotes.quoteList.eq(nextQuoteIndex).fadeIn(1000, function () {
                thisQuotes.changeAfterDelay();
            });
        });
        this.currentQuoteIndex = nextQuoteIndex;
    };


    Quotes.prototype.changeAfterDelay = function () {
        var thisQuotes = this;
        setTimeout(
            function () { thisQuotes.change(); },
            this.delay
        );
    };
    

})();

