/*
Module: BubbleToolTips.js
    This module manages the pop-up windows of the footnotes and the cross-references within MyBST panels.
*/



// remove this
function initTooltips() {}


/*
    Function: enableTooltips
    Enables the bubble window to the object (if id not null), else it enables the bubble window
    to every image tag, which name contains 'iconpopupCrossref' and 'iconpopupFootnote'.

    Parameters:
        id - element id.

    Returns:
        nothing.

    See Also:
        <Prepare(el)>
        <showTooltip(event)>
        <showLeftTooltip(event)>
        <showRightTooltip(event)>
        <hideTooltip(event)>
        <LocateTooltips(event, object)>
*/
function enableTooltips(id)
{

    var loc="left";
    var links,i,h;
    if(!document.getElementById || !document.getElementsByTagName) return;

    if(id==null) links=document.getElementsByTagName("img");
    else links=document.getElementById(id).getElementsByTagName("img");

    for(i=0;i<links.length;i++)
    {
        var name = links[i].id;

        // cross-references
        var idx = name.indexOf( 'iconpopupCrossref' );
        if (idx != -1) {
            var left = links[i].offsetLeft;

            if (singleView) {
                if (left <= 600)
                    loc="left";
                else
                    loc="right";
            }
            else  {
                if (left <= 280)
                    loc="left";
                else
                    loc="right";
            }
            Prepare(links[i],loc, 'crossref');
        }

        // footnotes
        var idx = name.indexOf( 'iconpopupFootnote' );
        if (idx != -1) {
            var left = links[i].offsetLeft;

            if (singleView) {
                if (left <= 600)
                    loc="left";
                else
                    loc="right";
            }
            else  {
                if (left <= 280)
                    loc="left";
                else
                    loc="right";
            }
            Prepare(links[i],loc, 'footnote');
        }
    }
    return;
}


/*
    Function: Prepare
    Creates a bubble window with data.

    Parameters:
        el - element id.
        loc - left or right
        divtype - div type

    Returns:
        nothing.

    See Also:
        <showTooltip(event)>
        <showLeftTooltip(event)>
        <showRightTooltip(event)>
        <hideTooltip(event)>
        <LocateTooltips(event, object)>
*/
function Prepare(el, loc, divtype)
{
    var tooltip, topSpan, bottomSpan, middleSpan, closebubbleimage;
    t=el.getAttribute("longdesc");

    if(t==null || t.length==0) t="link:";
    tooltip=CreateEl("span","tooltip");

    topSpan=CreateEl("span","top"+loc+divtype);
    closebubbleimage=CreateEl("div","leftimage"+divtype);
    topSpan.appendChild(closebubbleimage);
    tooltip.appendChild(topSpan);

    middleSpan=CreateEl("span","middle"+divtype);
    middleSpan.appendChild(document.createTextNode(t));
    tooltip.appendChild(middleSpan);

    bottomSpan=CreateEl("span","bottom"+divtype);
    tooltip.appendChild(bottomSpan);

    setOpacity(tooltip);
    el.tooltip=tooltip;
    if ( loc == "left" ) {
        el.onclick=showLeftTooltip;
    }
    else if ( loc == "right" ) {
        el.onclick=showRightTooltip;
    }
    closebubbleimage.onclick=hideTooltip;
}


/*
    Function: showTooltip
    Shows the bubble window.

    Parameters:
        e - event.

    Returns:
        nothing.

    See Also:
        <hideTooltip(e)>
*/
function showTooltip(e) {
    try {
        hideTooltip(e);
        document.getElementById("btc").appendChild(this.tooltip);
        LocateTooltips(e);
        isBubbleWinVisible = true;
    }
    catch(err) {
    }
    return;
}


/*
    Function: showLeftTooltip
    Shows the bubble window.

    Parameters:
        e - event.

    Returns:
        nothing.

    See Also:
        <hideTooltip(e)>
*/
function showLeftTooltip(e) {
    try {
        hideTooltip(e);
        document.getElementById("btc").appendChild(this.tooltip);
        LocateTooltips(e, "left");
        isBubbleWinVisible = true;
    }
    catch(err) {
    }
    return;
}


/*
    Function: showRightTooltip
    Shows the bubble window.

    Parameters:
        e - event.

    Returns:
        nothing.

    See Also:
        <hideTooltip(e)>
*/
function showRightTooltip(e) {
    try {
        hideTooltip(e);
        document.getElementById("btc").appendChild(this.tooltip);
        LocateTooltips(e,"right");
        isBubbleWinVisible = true;
    }
    catch(err) {
    }
    return;
}


/*
    Function: hideTooltip
    Hides the bubble window.

    Parameters:
        e - event.

    Returns:
        nothing.

    See Also:
        <showTooltip(event)>
        <showLeftTooltip(event)>
        <showRightTooltip(event)>
*/
function hideTooltip(e)
{
    var d=document.getElementById("btc");
    try {
        if ((d) && (d.childNodes.length>0))
					d.removeChild(d.firstChild);
        isBubbleWinVisible = false;
    }
    catch(err) {
    }
    return;

}


/*
    Function: LocateTooltips
    Locates the bubble window.

    Parameters:
        e   - event.
        obj - object name

    Returns:
        nothing.
*/
function LocateTooltips(e,loc)
{
    var posx=0,posy=0;
    if(e==null) e=window.event;
    if(e.pageX || e.pageY)
    {
        posx=e.pageX; posy=e.pageY;
    }
    else if(e.clientX || e.clientY)
    {
        if(document.documentElement.scrollTop)
        {
            posx=e.clientX+document.documentElement.scrollLeft;
            posy=e.clientY+document.documentElement.scrollTop;
        }
        else{
            posx=e.clientX+document.body.scrollLeft;
            posy=e.clientY+document.body.scrollTop;
        }
    }

        if ( loc == "left" ) {
            document.getElementById("btc").style.top=(posy+5)+"px";
            document.getElementById("btc").style.left=(posx-5)+"px";
        }
        else if ( loc == "right" ) {
            document.getElementById("btc").style.top=(posy+5)+"px";
            document.getElementById("btc").style.left=(posx-5-185)+"px";
        }
    return;
}
