﻿/************************************************************
Author: David Muto
Date: January 08, 2007

Quote Rotator class:
    Will rotate through all quotes in the collection
Parameters:
    elmId - The id of the HTML element associated with this instance
    isRandom - Whether or not the rotator should display quotes in
                random order or sequencially (true = random)
    interval - The time (in milliseconds) to wait between each
                quote
Usage:
    //this script will display a random quote in the element 'quoteHolder'
    //every 30 seconds
    var qr = new QuoteRotator('quoteHolder', true, 30000);
    qr.addQuote('This is quote 1');
    qr.addQuote('This is quote 2');
    qr.addQuote('This is quote 3');
    qr.addQuote('This is quote 4');
    qr.init();  
    
************************************************************/
function QuoteRotator(elmId, isRandom, interval) {
    this.ElementId = elmId;
    this.IsRandom = isRandom;
    this.Interval = interval;
    this.Quotes = new Array();
    this.CurrentQuoteIndex = -1;
}

QuoteRotator.prototype = {
    /****************************************************
    Add a quote to the collection
    ****************************************************/
    addQuote : function(strQuote) {
        this.Quotes.push(strQuote);
    },
    /****************************************************
    Retrieve a quote and display it in the HTML container
        - Will call itself every [interval]
    ****************************************************/
    generateQuote : function() {            
        var index;
        
        if(this.IsRandom) {
            index = Math.floor(Math.random() * this.Quotes.length);
            //prevent the same quote from appearing twice in a row
            while(index == this.CurrentQuoteIndex)
                index = Math.floor(Math.random() * this.Quotes.length);
        } else {
            index = this.CurrentQuoteIndex + 1;
            
            //allow wrap-around
            if(index >= this.Quotes.length)
                index = 0;
        }
        
        //store current index
        this.CurrentQuoteIndex = index;
        
        //show the new quote
        var elm = document.getElementById(this.ElementId);
        if(elm) {
            elm.innerHTML = "<span class='cent'><div style='height:53px;'></div>"+ this.Quotes[index] +"</span>";
        
            //for this to work, the instance must be the same as the element id
            setTimeout(this.ElementId +".generateQuote();", this.Interval);
        }
    },
    init : function() {
        this.generateQuote();
    }
};