//script for periodically changing quotes on the home page

quotes = new function() {
    this.blockquotes=null;  //the blockquote elements containing the quotes
    
    this.quote_container=null;    //outer div
    this.inner_container=null;    //inner div
    
    this.used_indexes=null;   //holds used indexes of blockquotes array as they are cycled through
    this.current_index=null;   //the current index of blockquotes array
    
    this.timer=null;
    this.quote_duration=5;  //how long quotes stay, in seconds
    this.delay=0.25;  //delay between quotes, in seconds
    
    this.initialize = function() {
        var self=quotes;
        
        //find outer and inner divs holding blockquotes
        var quote_container=document.getElementById('quotes');
        self.inner_container=quote_container.getElementsByTagName('div')[0];
        
        //get all blockquotes in div and initialize variables
        self.blockquotes=self.inner_container.getElementsByTagName('blockquote');
        self.used_indexes=new Array();
        var max_height=0;
        
        var container_height=null;
        var quotes_length=self.blockquotes.length;
        for (x=0; x<quotes_length; x++) {
            //loop through all blockquotes and hide
            self.blockquotes[x].style.display='none';
        }
        for (x=0; x<quotes_length; x++) {
            //loop through all blockquotes again and get the height of the inner div with each
            self.blockquotes[x].style.display='block';
            container_height=self.inner_container.offsetHeight;
            
            if (container_height>max_height) {
                //record height if it's the largest
                max_height=container_height;
            }
            
            //hide blockquote again
            self.blockquotes[x].style.display='none';
        }

        //set the div to the largest height
        self.inner_container.style.height=max_height+'px';
        
        //get a random quote and display
        self.switchQuote();
        
        //schedule cleanup function for page exit
        addEvent(window, 'unload', self.doCleanup, false);
    };
    
    //generate a random index for blockquotes array
    this.getQuoteIndex = function() {
        var self=quotes;
        
        var available_indexes=new Array();
        
        var blockquotes_length=self.blockquotes.length;
        for (z=0; z<blockquotes_length; z++) {
            //loop through all blockquotes - if it hasn't yet been used and isn't the current one, add its index to 'available indexes'
            if (!self.used_indexes[z] && z!=self.current_index) {
                available_indexes[available_indexes.length]=z;
            }
        }
        
        //get a random integer in the range of available indexes
        var range=available_indexes.length;
        var rand_num=Math.floor(Math.random()*range);
        
        //get the corresponding index
        var index=available_indexes[rand_num];
        
        //add the index to 'used indexes'
        self.used_indexes[index]=true;
        if (available_indexes.length==1) {
            //reset the used indexes array if this was the last choice
            self.used_indexes=new Array();
        }
        
        return index;
    };
    
    //do the quote switch
    this.switchQuote = function() {
        var self=quotes;
        
        //get a random index and unhide that blockquote
        self.current_index=self.getQuoteIndex();
        self.blockquotes[self.current_index].style.display='block';
        
        //schedule next delay
        self.timer=setTimeout(self.doDelay, self.quote_duration*1000);
    };
    
    //execute the short delay between quotes
    this.doDelay = function() {
        var self=quotes;
        
        //hide the current blockquote
        self.blockquotes[self.current_index].style.display='none';
        
        //schedule the next switch
        self.timer=setTimeout(self.switchQuote, self.delay*1000);
    };
    
    //release variables holding DOM elements, to avoid IE memory leaks
    this.doCleanup = function() {
        var self=quotes;
        
        self.blockquotes=null;
        self.quote_container=null;
        self.inner_container=null;
    };
}