﻿var Quotes = Class.create({
    run: function(quoteType, quoteIndex)
    {
        if (AjaxPro && AjaxPro.Request)
        {
            AjaxPro.Request.prototype.onTimeout = function() { };
            AjaxPro.Request.prototype.onLoading = function() { };
        }

        this.fadeOutDuration = 2000;
        this.fadeInDuration = 2000;
        this.nextQuoteDelay = 10000;

        this.quoteContainer = $("quote_container");
        this.quoteText = $("quote_text");
        this.quotePerson = $("quote_person");
        this.quoteCompany = $("quote_company");

        this.quoteType = quoteType;
        this.quoteIndex = quoteIndex;

        if (this.quoteContainer == null || this.quoteText == null || this.quotePerson == null || this.quoteCompany == null)
            return;

        window.setTimeout(this.showQuote.bind(this), this.nextQuoteDelay)
    },
    showQuote: function()
    {
        // Grab the next quote from the server.
        var response = Controls_Sidebar_Quotes.GetNextQuote(this.quoteType, this.quoteIndex);

        if (response == null || response.error != null)
            return;

        // If there weren't any errors, stash the quote in an instance variable.
        this.newQuote = response.value;

        // Start the quote fade out.
        this.animator = new Animator(this.fadeOut_tick.bind(this), this.fadeOutDuration, 60);
        this.animator.start();

        // This is to get around binding issues with the annonymous functions.
        var that = this;

        window.setTimeout(function()
        {
            // This runs after the fade out.
            this.quoteText.update(this.newQuote.getValue("quote"));
            this.quotePerson.update("-" + this.newQuote.getValue("person"));
            this.quoteCompany.update(this.newQuote.getValue("company"));
            this.quoteIndex = this.newQuote.getValue("quoteIndex");

            // Start the fade in.
            this.animator = new Animator(this.fadeIn_tick.bind(this), this.fadeInDuration, 60);
            this.animator.start();

            window.setTimeout(function()
            {
                // This runs after the fade in and begins the next cycle.
                this.showQuote();

            } .bind(that),

            // Wait this long in between quotes.
            // (wait for the fade in to finish + next quote delay)
            this.fadeInDuration + this.nextQuoteDelay);

        } .bind(that),

        // Wait this long before fading in. 
        // (wait for the fade out to finish + 1/4 second)
        this.fadeOutDuration + 250);
    },

    // This controls each frame of the fade out animation.
    fadeOut_tick: function(percent)
    {
        percent = 1 - Math.sin(percent * Math.PI / 2);
        this.setOpacity(percent);
    },

    // This controls each frame of the fade in animation.
    fadeIn_tick: function(percent)
    {
        percent = Math.sin(percent * Math.PI / 2);
        this.setOpacity(percent);
    },
    setOpacity: function(percent)
    {
        this.quoteContainer.setOpacity(percent);
        this.quotePerson.setOpacity(percent);
        this.quoteCompany.setOpacity(percent);
    }
});

Quotes.run = function(quoteType, quoteIndex)
{
    window.quotes = new Quotes();
    window.quotes.run(quoteType, quoteIndex);
}