/*
    This File creates Balloon style popup links from all Anchor tags with class='popuphelp'
    It depends on YUI yahoo, dom, event, animation
*/
BSD.namespace('BSD.balloon');

/*
if(typeof trace != 'function'){
    function trace(str){
        if(window.console && window.console.log){
            console.log(str);
        } else {
            document.body.appendChild(document.createTextNode(str));
        }
    }
}
*/
BSD.balloon.Balloon = function(){
    if(typeof(window.__customObjects) == "undefined"){
        window.__customObjects = [];
    }
    window.__customObjects[this.__gcObjID = window.__customObjects.length] = this;
    this.Init();
}
BSD.balloon.MakeDiv = function(cName, child){
        var d = document.createElement("DIV");
        d.className = cName;
        d.appendChild(child);
        return d;
}
BSD.balloon.Balloon.prototype.Init = function(){
    var MkDiv = BSD.balloon.MakeDiv;
    var p = document.createElement("DIV");
    p.id = 'balloon_text_container';
    p.innerText = "";
    var cont = MkDiv('b_container SEArr',
        MkDiv('b_bot',
            MkDiv('b_top',
                MkDiv('b_right',
                    MkDiv('b_left',
                        MkDiv('b_topR',
                            MkDiv('b_topL',
                                MkDiv('b_botR', MkDiv('b_botL', p))
                            )
                        )
                    )
                )
            )
        )
    );
    document.body.appendChild(cont);
    this.SetText = function(val) {
        p.innerText = val;
    }
    this.SetHTML = function(html){
        p.innerHTML = html;
    }
    this.GetHeight = function(){
        return parseInt(0 + cont.offsetHeight, 10);
    }
    this.GetWidth = function(){
        return cont.offsetWidth;
    }
    this.SetOrientation = function(newOr){
        var cName = BSD.balloon.Orientations[newOr];
        if(typeof cName == "undefined") {
            alert("Don't Understand Orientation'" + newOr + "'");
            return;
        }
        cont.className = BSD.balloon.Orientations[newOr];
    }
    this.visible = false;
    this.Hide = function(){
        cont.style.display='none';
        this.visible=false;
        this.SetMask(false);
    }
    this.Show = function(){
        cont.style.display='';
        this.visible=true;
        this.SetMask(true);
    }
    if(typeof YAHOO.util.Anim != 'undefined' && false == true ){
        YAHOO.util.Dom.setStyle(cont, 'opacity', 0);
        this.hide_anim = new YAHOO.util.Anim(cont, {opacity: {to:0.0}}, .51, YAHOO.util.Easing.None);
        this.hide_anim.onComplete.subscribe(function(){
            if(!BSD.balloon.obj.visible){
                cont.style.display = 'none';
            }
        });
        this.Hide = function(){
            this.visible=false;
            this.hide_anim.animate();
            this.SetMask(false);
        }
        this.show_anim = new YAHOO.util.Anim(cont, {opacity: {to:1}}, .51, YAHOO.util.Easing.None);
        this.FadeIn = function(){
            this.visible=true;
            cont.style.display='';
            this.show_anim.animate();
            this.SetMask(true);
        }
    } else {
        this.FadeIn = this.Show;
        this.FadeOut = this.Hide;
    }
    if(document.all){
        var mask = document.createElement("IFRAME");
        mask.style.display = 'none';
        mask.frameBorder = 0;
        mask.id = "IFRAME_BALLOON_MASK";
        mask.style.position='absolute';
        mask.style.top = '0px';
        mask.style.left ='0px';
        mask.style.backgroundColor = '#eee';
        mask.src="/javascript/balloon/blank.html" ;     /* essential for IE6 #24382 and #24527 */
        document.body.insertBefore(mask, cont);
        this.SetMask = function(show){
/*             mask.zIndex = cont.zIndex-4;  */
            mask.style.width=(cont.offsetWidth) + 'px';
            mask.style.height=Math.max(0, cont.offsetHeight-10) + 'px';
            mask.style.display = show ? '' : 'none';
        }
    } else {
        this.SetMask = function(){}
    }

    this.ShowAtEl = function(el, offsetTop, offsetLeft){
        var viewport = [
            YAHOO.util.Dom.getViewportWidth()/2,
            YAHOO.util.Dom.getViewportHeight()/2
        ];

        var left = YAHOO.util.Dom.getX(el);
        var pxtop = YAHOO.util.Dom.getY(el);
        var scrollTop = YAHOO.util.Dom.getDocumentScrollTop();
        //if the element is above 1/2 the document, then point the arrow down
        this.Show();
        var N_S = 'N';
        if(pxtop > viewport[1] + scrollTop){
            N_S = 'S';
            var offsetTop = (this.GetHeight()) * -1;
        } else {
            /* we're showing underneath the element give an extra 4px of padding so we don't show up under the mouse by accident  */
            offsetTop = el.offsetHeight + 4;
        }

        var E_W = "W";
        if(left> viewport[0]){
            E_W = 'E';
            var offsetLeft = (this.GetWidth() - 20) * -1;
        }

        cont.style.zIndex = el.style.zIndex + 1;
        this.SetOrientation(N_S + E_W);
        this.SetPosition(offsetTop ? pxtop + offsetTop : pxtop, offsetLeft ? left + offsetLeft : left);
        this.FadeIn();

    }
    this.SetPositionRelative = function(el, offsetTop, offsetLeft){
        var left = YAHOO.util.Dom.getX(el);
        var pxtop = YAHOO.util.Dom.getY(el);
        this.SetPosition(offsetTop ? pxtop + offsetTop : pxtop, offsetLeft ? left + offsetLeft : left);
    }
    this.SetPosition = function(pxtop, pxleft) {
        if(pxtop && pxleft){
            cont.style.top = parseInt(pxtop, 10) + 'px';
            cont.style.left = parseInt(pxleft, 10) + 'px';
        }
        if(mask){
            mask.style.left= (parseInt(pxleft, 10) + 0) + 'px';
            mask.style.top = (parseInt(pxtop, 10) + 0) + 'px';
            mask.style.zIndex - cont.style.zIndex - 1;
        }
    }
    this.Dispose = function() {
        for(var prop in this){
            this[prop] = null;
        }
    }
    this.domObjID = cont.id = 'balloon_' + this.__gcObjID;
    this.Hide();
    return cont;
}
BSD.balloon.Orientations = {
    "NE" : "b_container NEArr",
    "NW" : "b_container NWArr",
    "SE" : "b_container SEArr",
    "SW" : "b_container SWArr"
};
BSD.balloon.hide_balloon = function(){
    if(!BSD.balloon.interrupt){
        BSD.balloon.obj.Hide();
    }
    BSD.balloon.interrupt = false;
}
BSD.balloon.show_balloon = function(el){
    var help = el.parentNode.help_msg || el.parentNode.getAttribute('help_msg');
    if(!BSD.balloon.obj){
        BSD.balloon.obj = new BSD.balloon.Balloon();
    }
    BSD.balloon.obj.SetHTML(help);
    BSD.balloon.obj.ShowAtEl(el);
}
BSD.balloon.link_mouseover = function(evt){
    /* TODO: make a timeout for showing, so that you can brush over a link without turning it on.  */
    if(BSD.balloon.hide_timeout){
        BSD.balloon.interrupt = true;
        window.clearTimeout(BSD.balloon.hide_timeout);
        BSD.balloon.hide_timeout = null;
    }
    BSD.balloon.show_balloon(this);
}
BSD.balloon.link_mouseout = function(e){
    if(BSD.balloon.obj && BSD.balloon.obj.visible && !BSD.balloon.hide_timeout){
        BSD.balloon.hide_timeout = window.setTimeout('BSD.balloon.obj.Hide();', 40);
    }
}
YAHOO.util.Event.addListener(window, 'load', function(){
    BSD.balloon.obj = new BSD.balloon.Balloon();
    YAHOO.util.Dom.getElementsByClassName('popuphelp', 'a' , null, function(el){
        var img = el.getElementsByTagName('img')[0];
        img.alt = '';
        YAHOO.util.Event.addListener(img, 'mouseover', BSD.balloon.link_mouseover);
        YAHOO.util.Event.addListener(img, 'mouseout', BSD.balloon.link_mouseout);
    });
});
