window.addEvent('domready', function()
{
    $$('#xb-event-rotator').each(function(o)
    {
        var fx = null, timer = null;
        var delay = 5000;
        var height = 0;
        var stretchers = o.getElements('li.stretcher');
        var togglers = o.getElements('li.toggler a');
        if (stretchers.length) {

            var parent = stretchers[0].getParent('ul');

            stretchers.each(function(o)
            {
                var h = o.getStyle('height').toInt(); 
                if (h > height) {
                    height = h;
                }
            });

            stretchers.each(function(o)
            {
                o.setStyle('height', height);            
            });

            ['padding-top',
             'padding-bottom', 
             'margin-top',
             'margin-bottom'].each(function(s)
            {
                height += o.getStyle(s).toInt();
            });
            parent.setStyle('height', height);

            fx = new Fx.Accordion(togglers, stretchers, {
                height: false,
                onActive: function(toggler)
                {
                    toggler.getParent('li').addClass('active');
                },
                onBackground: function(toggler)
                {
                    toggler.getParent('li').removeClass('active');
                },
                onComplete: function()
                {
                    fx.rotate();
                }
            });
            fx.rotate = function() {
                $clear(timer);
                fx.cancel();
                timer = setInterval(function()
                {
                    fx.display((fx.previous + 1) % fx.togglers.length);
                }, delay);
            };
            togglers.each(function(o)
            {
                o.addEvent('click', function(e) {
                    e.stopPropagation();
                    this.blur();
                    $clear(timer);
                    if (null !== fx) {
                        setTimeout(function()
                        {
                            fx.rotate();
                        }, delay);
                    }
                    return false;
                });
            });
        }
    });
});