﻿

/* Constructor */
BLJS.Controls.ToolTip = function(CheckWrap, w, HeaderText, BodyText, ShowIcon){
    
    this.CheckWrap      = CheckWrap;
    this.HeaderText     = HeaderText;
    this.BodyText       = BodyText;
    this.ShowIcon       = typeof ShowIcon == "undefined" ?  true : ShowIcon;
    this.Width          = w;
    
    this.CreateMarkup();
    this.SetText(this.MiddleInnerInnerDiv);
    
    this.PopUp          = this.OuterDiv;
    this.PopUpButton    = this.CloseButtonDiv;
    var  CheckWrapE     = document.getElementById(this.CheckWrap);
    var  CheckInput     = CheckWrapE.getElementsByTagName('INPUT')[0];
    this.CheckInput     = CheckInput;

    this.AdjustPositionOffsets();
    this.AttachEvent();
    this.MakeRefVar();
    this.AddOnResizeHandler();
    
    
}
/* Methods */

BLJS.Controls.ToolTip.prototype = {
    CreateMarkup : function(){
        /* outermost div */
        var OuterDiv = document.createElement('DIV');
        /* checks if id has already been given */
        var newp; var p = 0;
        while(typeof document.getElementById('NotifPopUp' + p + '') == null)
        {
            p += 1;
        }
        OuterDiv.id = 'NotifPopUp' + p + '';
        OuterDiv.style.display = "none";
        OuterDiv.style.position = "absolute";
        OuterDiv.style.left = "0px";
        OuterDiv.style.top = "0px";
        OuterDiv.style.width = "0px";
        OuterDiv.style.overflow = "hidden";
        OuterDiv.style.backgroundColor = "Transparent";
        OuterDiv.style.zIndex = 1000000;
        
        /* adds outer div to the page */
        document.body.appendChild(OuterDiv);
        
        /* inner divs */
        var CloseButtonDiv = document.createElement('DIV');
        var TopDiv = document.createElement('DIV');
        var TopDivInner = document.createElement('DIV');
        var MiddleDiv = document.createElement('DIV');
        var BottomDiv = document.createElement('DIV');
        var BottomDivInner = document.createElement('DIV');
        OuterDiv.appendChild(CloseButtonDiv);
        OuterDiv.appendChild(TopDiv);
        TopDiv.appendChild(TopDivInner);
        OuterDiv.appendChild(MiddleDiv);
        OuterDiv.appendChild(BottomDiv);
        BottomDiv.appendChild(BottomDivInner);
        CloseButtonDiv.className = 'NotifPopUp-CloseButton'
        TopDiv.className = 'NotifPopUp-Top';
        TopDivInner.className = 'NotifPopUp-Top-Inner';
        MiddleDiv.className = 'NotifPopUp-Middle';
        BottomDiv.className = 'NotifPopUp-Bottom';
        BottomDivInner.className = 'NotifPopUp-Bottom-Inner';
        
        /* inner inner divs */
        var MiddleInnerDiv = document.createElement('DIV');
        MiddleDiv.appendChild(MiddleInnerDiv);
        MiddleInnerDiv.className = "NotifPopUp-Middle-Inner";   
        
        if(this.ShowIcon){
        var IconDiv = document.createElement('DIV');
        MiddleInnerDiv.appendChild(IconDiv);
        IconDiv.className = "NotifPopUp-Middle-Inner-Icon";
        }
        
        var MiddleInnerInnerDiv = document.createElement('DIV');
        MiddleInnerDiv.appendChild(MiddleInnerInnerDiv);
        MiddleInnerInnerDiv.className = "NotifPopUp-Middle-Inner-Inner";   
        
        this.OuterDiv = OuterDiv   
        this.CloseButtonDiv = CloseButtonDiv 
        this.TopDiv = TopDiv        
        this.MiddleDiv = MiddleDiv  
        this.BottomDiv = BottomDiv 
        this.MiddleInnerDiv = MiddleInnerDiv 
        this.MiddleInnerInnerDiv = MiddleInnerInnerDiv 
        this.IconDiv = IconDiv 
    },
    AdjustPositionOffsets : function(){
        /* every browsers returns the locaiton in a slightly different place */
        if(this.CheckInput.type == "text"){
            this.XOffset = 0;
            this.YOffset = 17;
            if(BLJS.Env.isFirefox){
                this.XOffset = 0;
                this.YOffset = 17;
            } else if (BLJS.Env.isSafari) {
                this.XOffset = 0;
                this.YOffset = 17;
            }
        } else {
            this.XOffset = -15;
            this.YOffset = 18;
            if (BLJS.Env.isFirefox) {
                this.XOffset = -19;
                this.YOffset = 15;
            } else if (BLJS.Env.isSafari) {
                this.XOffset = -20;
                this.YOffset = 11;
            }
        }
    },
    AttachEvent : function(){
        /* attach events */
        addEvent(this.PopUpButton, "click", ClosePopUp)
        function ClosePopUp(){
            e = arguments[0];
            e = e || window.event;
            ReturnTarg(e).parentNode.style.display = "none";
        }
    },
    MakeRefVar : function(){
        /* create reference variable for this object */
        if (typeof window.NotifPopUpReferences == "undefined"){
            window.NotifPopUpReferences = [];
            NotifPopUpReferences.push(this)
        }
    },
    AddOnResizeHandler : function(){
        /* handle on resize event */
        /* create a global onresize event handler that will update everypopup on the page whenever the onresize event is called */
        if (typeof window.NotifPopUpOnResizeHandler == "undefined"){
            window.NotifPopUpOnResizeHandler = function(){
                for(var pops = 0, max = NotifPopUpReferences.length; pops < max; pops++ )
                {
                    NotifPopUpReferences[pops].Update();
                }
            }
            BLJS.registerOnResizeFunction(window.NotifPopUpOnResizeHandler);
        }
    },
    SetText : function(el){
        el.innerHTML = "<b>" + E(this.HeaderText).innerHTML + "</b><br />" + E(this.BodyText).innerHTML
    },
    Appear : function()
    {
        this.PopUp.style.width = this.Width + "px";
        this.PopUp.style.display = "";
        this.PopUp.style.position = "absolute";
        this.Update();
    },
    Disappear : function()
    {
        this.PopUp.style.display = "none";
    },
    GetE : function()
    {
        return this.CheckInput
    },
    Update : function()
    {
        var LoginPageOffset = 0;
        if(document.getElementById("BuildingNameAndWelcomeText") && BLJS.Env.isInternetExplorer){
            var LoginPageOffset = document.getElementById("OuterWrapperDiv").offsetTop / 2;
        }
        this.PopUp.style.left = BLJS.getOffsetX(this.CheckInput) + this.XOffset + "px";
        this.PopUp.style.top = (BLJS.getOffsetY(this.CheckInput) - LoginPageOffset) + this.YOffset + "px";
    },
    GetOuterDiv : function(){
        return this.OuterDiv;
    }
}