// A message scroller. Scrolls through a list of messages decorated with classes:
//  'evthead' : Heading for event list
//  'evtdate' : Date for following events (through next 'evtdate')
//  'evt'     : Event messages
//
// Constructor is passed 'container' which is the id of the element containing
// message list and 'interval' which is the floating-point interval in seconds
// that a message will appear. R. Abeles, 1/25/2008.

var Scroller = Class.create({
    // constructor
    initialize: function(container, interval) {
        this.container = $(container);
        this.interval = interval;
        this.msglines = $A(this.container.getElementsByTagName('li'));
        this.fillqueue();
    },

    // fills global effect queue according to list in container
    fillqueue: function() {
        var interval = this.interval;
        var prevdate = null;
        var prevevent = null;
        this.msglines.each(function (e) {
            e = $(e); // our friend i.e.
            if (e.hasClassName('evthead')) {
                new Effect.Appear(e, {queue: 'end'});
                new Effect.Fade(e, {delay: interval, queue: 'end'});
            } else if (e.hasClassName('evtdate')) {
                if (prevevent) {
                    new Effect.Fade(prevevent, {delay: interval, queue: 'end'});
                    prevevent = null;
                }
                if (prevdate)
                    new Effect.Fade(prevdate, {queue: 'end'}); 
                new Effect.Appear(e, {queue: 'end'});
                prevdate = e;
            } else {
                if (prevevent)
                    new Effect.Fade(prevevent, {delay: interval, queue: 'end'});
                new Effect.Appear(e, {queue: 'end'});
                prevevent = e;
            }
        });
        if (prevevent)
            new Effect.Fade(prevevent, {delay: interval, queue: 'end'});

        // Our final queue entry will call fillqueue() again when it has finished.
        // So, we bind this instance of Scroller to fillqueue and pass it as
        // the 'afterFinish' callback. (Javascript separates this and context, so
		// the callback is invoked with fillqueue's context, which is good, but
		// also with the current call chain's this, which is very, very bad.)
        var fq = this.fillqueue.bind(this);
        if (prevdate)
            new Effect.Fade(prevdate, {queue: 'end',
                                       afterFinish: function() {fq();}
                                      });
        else
            new Effect.Event({afterFinish: function() {fq();}});
    }
});

