// ********** !!!!! DEPRECATED !!!!! ************
// PLEASE USE /include/js/common/bubbles/*

// File: bubbles.js
// Requires: prototype.js

var BubblesController = Class.create();

BubblesController.prototype = {
    listCtrlsBubbles : null,
    eltCurrentBubble : null,
    targetIdAttribute: "targetId",
    idBubble : null,
    
    /** Constructor. Does the following:
     * - Argument list for each element/bubble combo
     * -
     */
    initialize : function(/*{idBubble: string (element), idTarg: string (bubble element), eventOn: string (display event), eventOff: string (hide event), callbackOn : function (), callbackOff : function ()}, ...*/)
    {
        var i, eltTarg, eltBbl, linkClose, list;
        
        this.listCtrlsBubbles = new Object();
        var argArray = arguments
        if(arguments.length == 1)
            argArray = $A(arguments[0]);
        
            
        $A(argArray).each(function(config){
            this.addBubble(config);
        }, this);
    },
    addBubble : function(obj){
        var linkClose = null;

            if(typeof(obj) == "object" &&
                typeof(obj.idTarg) == "string" && typeof(obj.idBubble) == "string" &&
                obj.idTarg && obj.idBubble &&
                (eltTarg = $(obj.idTarg)) != null && (eltBbl = $(obj.idBubble)) != null){
                this.idBubble = obj.idBubble;
                if(typeof(obj.eventOn) == "string"){
                    Event.observe(eltTarg, obj.eventOn, this.handleBubbleShow.bindAsEventListener(this), false);
                }
                if(typeof(obj.eventOff) == "string"){
                    Event.observe(eltTarg, obj.eventOff, this.handleBubbleHide.bindAsEventListener(this), false);
                }
                // Get Close Link in bubble and attach a close bubble handler
                var list;
                if((list = $$("#" + eltBbl.id + " .closebox")) != null){ // Note: Close Bubble links always have a class of "closebox"
                    Event.observe(list[0], "click", this.handleCloseLink.bindAsEventListener(this), false);
                    /*
                     * If you put a custom attr in your close link
                     * the id of the bubble (the target id) can be
                     * embedded there.  This is handy for callbackClose.
                     */
                    if (list[0].attributes[this.targetIdAttribute]) {
                        list[0].attributes[this.targetIdAttribute].value = obj.idTarg;
                    }
                }

                this.listCtrlsBubbles[obj.idTarg] = new Object();
                this.listCtrlsBubbles[obj.idTarg].bubble = eltBbl;

                if(typeof(obj.callbackOn) == "function"){
                    this.listCtrlsBubbles[obj.idTarg].callbackOn = obj.callbackOn;
                }
                if(typeof(obj.callbackOff) == "function"){
                    this.listCtrlsBubbles[obj.idTarg].callbackOff = obj.callbackOff;
                }
                if(typeof(obj.callbackClose) == "function"){
                    this.listCtrlsBubbles[obj.idTarg].callbackClose = obj.callbackClose;
                }
                
                // add an iframe to the bubble if it's IE6
                if(photobucket.browser.isIE6) this.appendIframe(eltBbl);

            }  
    },
    handleBubbleShow : function(evnt)
    {
        var eltTarg = Event.element(evnt);
        var eltLink = Event.findElement(evnt, "A");
        var bRet = true, eltBubble;

        if(evnt.type == "click" && eltLink && typeof(eltLink.tagName) != "undefined" && eltLink.tagName == "A"){
            bRet = false;
        }

        if (typeof(eltTarg.id) !== "undefined") {
            if(typeof(this.listCtrlsBubbles[eltTarg.id].callbackOn) != "function" || this.listCtrlsBubbles[eltTarg.id].callbackOn()){
    
                eltBubble = this.listCtrlsBubbles[eltTarg.id].bubble;
    
                this.showBubble(eltBubble);
            }
        }

        return bRet;
    },
    handleBubbleHide : function(evnt)
    {
        var eltTarg = Event.element(evnt);
        var eltLink = Event.findElement(evnt, "A");
        var bRet = true;

        if(evnt.type == "click" && eltLink && typeof(eltLink.tagName) != "undefined" && eltLink.tagName == "A"){
            bRet = false;
        }

        if(typeof(this.listCtrlsBubbles[eltTarg.id].callbackOff) != "function" || this.listCtrlsBubbles[eltTarg.id].callbackOff()){
            eltBubble = this.listCtrlsBubbles[eltTarg.id].bubble;

            this.hideBubble(eltBubble);
        }

        return bRet;
    },
    handleCloseLink : function(evnt)
    {
        var eltLink = Event.findElement(evnt, "A");
        var targId = eltLink.attributes[this.targetIdAttribute];
        // Note: Operate under the assumtion that the close link is always a direct child of the bubble div.
        if(eltLink && eltLink.parentNode && eltLink.parentNode.parentNode){
            if (!targId || typeof(this.listCtrlsBubbles[targId.value].callbackClose) != "function" || this.listCtrlsBubbles[targId.value].callbackClose()) {
                var eltBubble = eltLink.parentNode.parentNode;
                //this.listCtrlsBubbles[eltLink.parentNode.parentNode.id].bubble;
                this.hideBubble(eltBubble);
            }
        }

        return false;
    },
    // ***** Utils *****
    showBubble : function(eltBubble)
    {
        if(this.eltCurrentBubble && this.eltCurrentBubble !== eltBubble){
            Element.hide(this.eltCurrentBubble);
        }
        if(eltBubble){
            Element.show(eltBubble);
        }
        this.eltCurrentBubble = eltBubble;
    },
    hideBubble : function(eltBubble)
    {
        if(eltBubble){
            Element.hide(eltBubble);
        }

        if(eltBubble === this.eltCurrentBubble){
            this.eltCurrentBubble = null;
        }
    },
    getDimensionsOfChild: function(element, child) {
        var els = element.style;
        var originalVisibility = els.visibility;
        var originalPosition = els.position;
        var originalDisplay = els.display;
        els.visibility = 'hidden';
        els.position = 'absolute';
        els.display = 'block';
        var c = child.style;
        var originalWidth = child.offsetWidth;
        var originalHeight = child.offsetHeight;
        els.display = originalDisplay;
        els.position = originalPosition;
        els.visibility = originalVisibility;
        return {width: originalWidth, height: originalHeight};
    },
    appendIframe: function(element) {
        var content = element.select('.bubble_content')[0];
        var iframe = document.createElement("iframe");
        iframe.setAttribute("src","");
        iframe.className = "iframe";
        var d = this.getDimensionsOfChild(element,content);
        iframe.setAttribute("width",d.width-1);
        iframe.setAttribute("height",d.height-1);
        iframe.setAttribute("FRAMEBORDER",0);
        element.appendChild(iframe);
        content.addClassName("withIframe");
    },
    // ******************* External Show/Hide Controllers *******************
    hide : function(targId)
    {
        var eltBubble = null;

        if(typeof(eltBubble = this.listCtrlsBubbles[targId]) != "undefined"){
            eltBubble = eltBubble.bubble;
        }

        if(eltBubble){
            this.hideBubble(eltBubble);
        }
    },
    show : function(targId)
    {
        var eltBubble = null;

        if(typeof(eltBubble = this.listCtrlsBubbles[targId]) != "undefined"){
            eltBubble = eltBubble.bubble;
        }

        if(eltBubble){
            this.showBubble(eltBubble);
        }
    }
};

