/*
 * Author: Michael Carmack <karmak@karmak.org>
 * License: GPL <http://www.gnu.org/licenses/gpl.aspl>
 *
 * TextFader cycles through an array of strings, displaying one at a time,
 * with optional fade-in/fade-out.
 */
function TextFader (objname) {

    this.messageArray = new Array();
    this.colorArray = new Array();
    this.pause = 2000;
    this.elementId="";
    this.objectName = objname;
    this.startDelay = 0;
    
    this.icolor = 0;
    this.messagenumber = 0;
    this.totality = false;
    this.onUpswing = true;
    this.cycleDelay = 50;
    this.repeatCount = -1;

    this.finalHTML = null;

    /*
     * Set the message array.
     */
    this.setMessageArray = function(a) {
        this.messageArray = a;
    }

    /*
     * Set how many times the messages should repeat.
     */
    this.setRepeatCount = function(i) {
        this.repeatCount = i;
    }
    
    /*
     * Set the color arrays.
     */
    this.setColorArrays = function (a1, a2) {
        this.colorArray = a1;
    }

    /*
     * Set the color arrays.
     */
    this.setColorArray = function (a) {
        this.colorArray = a;
    }

    /*
     * Set the length of time each string should be displayed.
     */    
    this.setPause = function (p) {
        this.pause = p;
    }

    /*
     * A start delay, if any.
     */
    this.setStartDelay = function(i) {
        this.startDelay = i;
    }

    /*
     * Optional HTML to put in the element at the end of the cycle.
     */
    this.setFinalHTML = function(s) {
        this.finalHTML = s;
    }


    /*
     * The cycle delay.
     */
    this.setCycleDelay = function(i) {
        this.cycleDelay = i;
    }

    /*
     * The name of the element that should contain the text strings.
     */
    this.setElementId = function(str) {
        this.elementId = str;
    }

    /*
     * Start the ball rolling.
     */
    this.start = function() {
        if (this.startDelay > 0) {
            tmpdelay = this.startDelay;
            this.startDelay = 0;
            setTimeout(this.objectName + ".start()", tmpdelay);
        }
        else {
            this.totality = false;
            this.step();
        }
    }


    /*
     * Write the message to the HTML document.
     */
    this.writeMessage = function() {
        var colorstring = "#" + this.contentcolor;
        var message = "<span class='Message'>"
                + this.messageArray[this.messagenumber]
                + "</span>";
        var elt = document.getElementById(this.elementId);
        elt.style.color = colorstring;
        elt.innerHTML = message;
    }

    /*
     * Steps through the cycle.
     */    
    this.step = function() {
        if (this.totality == true) {
            this.totality = false;
            setTimeout(this.objectName + ".step()", this.pause);
        }
        else {
            if (this.onUpswing == true) {
                if (this.icolor >= this.colorArray.length - 1) {
                    this.icolor--;
                    this.onUpswing = false;
                    this.totality = true;
                }
                else {
                    this.icolor++;
                }
            }
            else if (this.onUpswing == false) {
                if (this.icolor <= 0) {
                    if (this.messagenumber == (this.messageArray.length - 1)) {
                        if (this.repeatCount != 0) {
                            this.messagenumber = 0;
                            this.repeatCount--;
                        }
                        else {
                            this.end();
                            return;
                        }
                    }
                    else {
                        this.messagenumber++;
                    }
                    this.icolor++;
                    this.onUpswing = true;
                }
                else {
                    this.icolor--;
                }
            }
            this.contentcolor = this.colorArray[this.icolor];
            this.writeMessage();
            setTimeout(this.objectName + ".step()", this.cycleDelay);
        }
    }

    /*
     * The endgame.
     */
    this.end = function() {
        if (this.finalHTML != null) {
            message = this.finalHTML;
            elt = document.getElementById(this.elementId);
            elt.innerHTML = message;
        }
    }

}


