var FlashBarManager = Class.create({

    initialize: function() {
        this.notifications = $$(".flash_bar_wrapper");
        this.soundEffect = $("new_flash_sound");
        this.displayNotifications();
    },
    
    displayNotifications: function() {
        this.notifications.each(function(x){
            this.soundEffect.play();
            new Effect.BlindDown(x, {duration: 0.5});
            window.setTimeout(function(x) {
                new Effect.BlindUp(x, {duration: 0.5});
            }, 8000, x);
        }, this);
    }
        
});

var NotificationManager = Class.create({

    initialize: function() {
        this.togglers = $$(".notifications_toggler");
        this.updateCounts();
    },
    
    countForSection: function(toggler) {
        var notificationList = toggler.up().select(".expanded_notification_list > div > div > .notification");
        var i = 0;
        notificationList.each(function(x){
            if (x.visible())
                i++;
        });
        return i;
    },
    
    updateCount: function(toggler) {
        var n = this.countForSection(toggler);
        var countElement = toggler.select("span.unread").first();
        if (n > 0) {
            countElement.update(n);
            toggler.up().show();
        }
        else {
            countElement.hide();
            toggler.up().hide();
        }
    },
    
    updateCounts: function() {
        this.togglers.each(function(toggler){
            this.updateCount(toggler);
        }, this);
    }
        
});

document.observe("dom:loaded", function(){
    window.flash_bar_manager = new FlashBarManager;
    window.notification_manager = new NotificationManager;
});