//****************************************
// Load Required YUI Scripts
//****************************************
var onLoadFunction = function() {
	YAHOO.util.Get.script("scripts/Cookie.js", {});
	if (YAHOO) {
		YAHOO.util.Get.script("yui/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js", {
			onSuccess: function() {
				YAHOO.util.Get.script("yui/2.8.0r4/build/connection/connection-min.js", {
					onSuccess: function() {
						YAHOO.util.Get.script("yui/2.8.0r4/build/animation/animation-min.js", {});
						YAHOO.util.Get.script("yui/2.8.0r4/build/json/json-min.js", {

						    onSuccess: function () { if (Quoter) { Quoter.Init(); } }
						});
					}
				});
			}
		});
	}
};

if(typeof(window.attachEvent) !== "undefined") {
	window.attachEvent('onload', onLoadFunction);
}
else {
	window.addEventListener("load", onLoadFunction, false);
}


//****************************************
// Quoter class
//****************************************
var Quoter = function () {

    var _control = null;
    var _running = false;
    var _quotes = null;
    var _currentQuote = -1;

    var _quoterId = "quotator";
    var _timeToViewQuote = 10000; // ms
    var _timeBeteenQuotes = 1600; // ms
    var _timeToFade = 1; // secs

    //
    // Loads the next quote into the quoter elements
    //
    function loadQuote() {
        if (_control && _quotes && _quotes && _quotes.length > 0) {
            var ps = _control.getElementsByTagName("p");
            if (ps.length >= 2) {
                _currentQuote = (_currentQuote + 1 >= _quotes.length) ? 0 : _currentQuote + 1;

                Cookie.CreateCookie("Quoter.CurrentQuote", _currentQuote, 31);

                ps[0].innerHTML = _quotes[_currentQuote].quote;
                var name = _quotes[_currentQuote].name;
                ps[1].innerHTML = name.toString().length > 0 ? ("- " + _quotes[_currentQuote].name) : "";
            }
        }
    }

    //
    // Displays the next quote
    //
    function showNext() {

        if (_control) {
            var fadeOutAnim = new YAHOO.util.Anim(_control, { opacity: { to: 0} }, _timeToFade);
            fadeOutAnim.animate();

            if (_running) {
                window.setTimeout(function () {
                    loadQuote();
                    var fadeInAnim = new YAHOO.util.Anim(_control, { opacity: { to: 1} }, _timeToFade);
                    fadeInAnim.animate();
                    window.setTimeout(showNext, _timeToViewQuote);
                }, _timeBeteenQuotes);
            }
        }
    }

    //
    // Starts the quotes cycling
    //
    function start() {
        if (_control && !_running) {
            loadQuote();
            var fadeInAnim = new YAHOO.util.Anim(_control, { opacity: { to: 1} }, _timeToFade);
            fadeInAnim.animate();
            _running = true;
            window.setTimeout(showNext, _timeToViewQuote);
        }

    }

    //
    // Stops the quotes from cycling
    //
    function stop() {
        _running = false;
    }

    // Initializes the Quoter class
    function init(args) {
        var oArgs = (args) ? args : {};

        if (oArgs.quoterId) {
            _quoterId = oArgs.quoterId;
        }

        if (oArgs.timeToViewQuote) {
            var time = parseFloat(oArgs.timeToViewQuote);
            if (!isNaN(time)) {
                _timeToViewQuote = time * 1000;
            }
        }

        if (oArgs.timeBeteenQuotes) {
            var time = parseFloat(oArgs.timeBeteenQuotes);
            if (!isNaN(time)) {
                _timeBeteenQuotes = time * 1000;
            }
        }

        if (oArgs.timeToFade) {
            var time = parseFloat(oArgs.timeToFade);
            if (!isNaN(time)) {
                _timeToFade = time;
            }
        }

        if (Cookie) {
            var value = Cookie.ReadCookie("Quoter.CurrentQuote");
            if (value) {
                var curQuote = parseInt(value, 10);
                if (!isNaN(curQuote)) {
                    _currentQuote = curQuote;
                }
            }
        }

        _control = document.getElementById(_quoterId);

        var callbacks = {

            success: function (o) {

                _quotes = null;
                try {

                    _quotes = YAHOO.lang.JSON.parse(o.responseText);
                    if (_quotes.quotes) {
                        _quotes = _quotes.quotes;
                    }
                    else {
                        _quotes = null;
                    }

                    if (_control) {
                        start();
                    }
                }
                catch (x) {
                    return;
                }
            },

            failure: function (o) {
                alert(o);
                return;
            },

            timeout: 3000
        }
        YAHOO.util.Connect.asyncRequest('GET', "quotes.txt", callbacks);
    }

    //
    // PUBLIC METHODS
    //
    return {
        Init: init,
        Start: start,
        Stop: stop
    };

} ();
