/*
 * Makes a bunch of tickers out of an array of container ids.
 * Runs them sequentially.
 */
var ChainedTickers = new Class({
    initialize: function(container_ids) {
        this.tickers = new Array();
        var _this = this;
        container_ids.each(function(x) {
            var t = new Ticker(x);
            if (t.workable())
                _this.tickers.include(t);
        });

        this.ticker_pos = 0;
        this.scroll_ticker.delay(5000, this);
    },

    // Alternate between each ticker
    scroll_ticker: function() {
        var next_pos = this.ticker_pos + 1;
        if (next_pos >= this.tickers.length)
            next_pos = 0;

        this.tickers[this.ticker_pos].scroll_next();
        this.scroll_ticker.delay(7000, this);
        this.ticker_pos = next_pos;
    }
});


/*
 * Containers are div with children, and a Ticker will only show one child at a time.
 * Children should span the entire width of the container.
 * The ticker will scroll to each child in turn.
 */
var Ticker = new Class({
    initialize: function(table_id) {
        this._is_workable = false;

        this.table_id = table_id;
        this.table = $(table_id);
        var table = this.table;
        this.children = table.getChildren();
        if (this.children.length > 0) {
            var children = this.children;
            
            var max_size = 0;
            var has_images = false;
            
            // We want our container to be the height of the highest child, and we
            // also want each child to be of that same height so everything looks
            // consistent.

            // Loading images can affect the height of the container, we'll take
            // care of this by recalculating the sizes when an image is loaded.
            children.getElements('img').each(function(image) {
                if (image.get('src') != '') {
                    has_images = true;
                    image.addEvent('load', function() {
                        children.each(function(child) {
                            max_size = Math.max(child.getSize().y, max_size);
                        });
                        
                        children.each(function(child) {
                            child.setStyle('height', max_size);
                        });
                        
                        // Only one child should be displayed at a time.
                        table.setStyle('height', max_size);
                    });
                }                
            });
            
            if (!has_images) {
                children.each(function(child) {
                    max_size = Math.max(child.getSize().y, max_size);
                });
            
                children.each(function(child) {
                    child.setStyle('height', max_size);
                });
                
                // Only one child should be displayed at a time.
                table.setStyle('height', max_size);
            }
            
            table.setStyle('overflow', 'hidden');
            this.current_pos = 0;
            this._is_workable = true;
        }

    },

    workable: function() {
        return this._is_workable;
    },

    scroll_next: function() {
        var next_pos = this.current_pos + 1;
        if (next_pos >= this.children.length)
            next_pos = 0;
        
        // Scroll to the next child.
        var _this = this;
        new Fx.Scroll(this.table, {
            duration: 2000,
            onComplete: function() {
                _this.current_pos = next_pos;
            }
        }).toElement(this.children[next_pos]);
    }
});
