/**
 * @author dmccuen
 */
var BubbleController = Class.create({
    suppressBubbles: false,
    
    initialize : function() {
        this.bubbles = {};
        this.currentBubble = null;
        // add any bubble passed in the constructor
        for(var i = 0; i < arguments.length; i++){
            this.addBubble(arguments[i]);
        }
    },
    // PUBLIC METHODS
    addBubble : function(bubble){
        if (bubble.pbType && bubble.pbType == "Bubble") {
            this.bubbles[bubble.id] = bubble;
        }
    },
    close: function(id) {
        var bubble = this.bubbles[id];
        if(typeof(bubble) != "undefined"){
            this.closeBubble(bubble);
        }    
    },
    hide : function(id) {
        var bubble = this.bubbles[id];
        if(typeof(bubble) != "undefined"){
            this.hideBubble(bubble);
        }
    },
    show : function(id){
        var bubble = this.bubbles[id];
        if(typeof(bubble) != "undefined"){
            this.showBubble(bubble);
        }
    },
    getBubbleByTargetId: function(id) {
        var bubble = null;
        for (var n in this.bubbles) {
            var b = this.bubbles[n];
            if (b.targetId == id) {
                bubble = b;
                break;
            }
        }
        return bubble;
    },
    getBubbleByElementId: function(id) {
        var bubble = null;
        for (var n in this.bubbles) {
            var b = this.bubbles[n];
            if (b.elemId == id) {
                bubble = b;
                break;
            }
        }
        return bubble;
    },
    // PRIVATE METHODS
    showBubble : function(bubble) {
        if (this.currentBubble && this.currentBubble !== bubble) this.currentBubble.hide();
        if (!this.suppressBubbles) {
            if (bubble) bubble.show();
            this.currentBubble = bubble;
        }
    },
    hideBubble : function(bubble) {
        if (bubble) bubble.hide();
        if (bubble === this.currentBubble) this.currentBubble = null;
    },
    closeBubble : function(bubble) {
        if (bubble) bubble.close();
        if (bubble === this.currentBubble) this.currentBubble = null;
    },
    hideAllBubbles : function() {
        for (var n in this.bubbles) {
            this.hide(n);
        }
    },
    setSuppressBubbles : function(val) {
        this.suppressBubbles = val;
    }
});

