
/* Script by: www.jtricks.com
 * Version: 20060314
 * Latest version:
 * www.jtricks.com/javascript/window/box.html
 */
// Moves the box object to be directly beneath an object.
function move_box(an, box, width, height, maxWidth, maxHeight)
{
    var cleft = 0;
    var ctop = 0;
    var obj = an;

    while (obj.offsetParent)
    {
        cleft += obj.offsetLeft;
        ctop += obj.offsetTop;
        obj = obj.offsetParent;
    }

    
    // Check if we will exceed the bottom margin
    if (ctop + height >= maxHeight)
    {
//        alert(height);
        ctop -= ctop + height - maxHeight;
        // in this case the right margin of the tooltip
        // must be to the left of the image
        cleft -= width + 20;
    }
    else
    {
        // Check if we will exceed the right margin
        if (cleft + width >= maxWidth)
        {
            cleft -= cleft + width - maxWidth;
        }
    }

    box.style.left = cleft + 'px';

    ctop += an.offsetHeight + 8;

    // Handle Internet Explorer body margins,
    // which affect normal document, but not
    // absolute-positioned stuff.
    if (document.body.currentStyle &&
        document.body.currentStyle['marginTop'])
    {
        ctop += parseInt(
            document.body.currentStyle['marginTop']);
    }

    box.style.top = ctop + 'px';
}

// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is curre
function show_hide_box(an, width, height, borderStyle, bubbleNumber, maxWidth, maxHeight, subfolder)
{
    var href = an.href;
    var boxdiv = document.getElementById(href);

    if (!subfolder)
    {
        subfolder = '';
    }

    if (boxdiv != null)
    {
        if (boxdiv.style.display=='none')
        {
            bubbleNumberSpan = document.getElementById('bubbleNumberSpan');            
            bubbleNumberSpan.innerHTML = bubbleNumber;
            
            contents = document.getElementById('bubbleIFrame');            
            setHyperlink(bubbleNumber, contents, subfolder)
            
            // Show existing box, move it
            // if document changed layout
            move_box(an, boxdiv, width, height, maxWidth, maxHeight);
            
            boxdiv.style.display='block';
        }
        else
            // Hide currently shown box.
            boxdiv.style.display='none';
        return false;
    }

    // Create box object through DOM
    boxdiv = document.createElement('div');

    // Assign id equalling to the document it will show
    boxdiv.setAttribute('id', href);

    boxdiv.style.display = 'block';
    boxdiv.style.position = 'absolute';
    boxdiv.style.width = width + 'px';
    boxdiv.style.height = height + 'px';
    boxdiv.style.border = borderStyle;
    //boxdiv.style.textAlign = 'right';
    boxdiv.style.padding = '4px';
    boxdiv.style.background = '#FFFFFF';
    document.body.appendChild(boxdiv);

    var offset = 0;

//    // Remove the following code if 'Close' hyperlink
//    // is not needed.
//    var close_href = document.createElement('a');
//    close_href.href = 'javascript:void(0);';
//    close_href.onclick = function()
//        { show_hide_box(an, width, height, borderStyle); }
//    close_href.appendChild(document.createTextNode('Close'));
//    boxdiv.appendChild(close_href);
//    offset = close_href.offsetHeight;
//    // End of 'Close' hyperlink code.


    var bubbleNumberHeight = 30;
    
    var bubbleNumberSpan = document.createElement('span');
    bubbleNumberSpan.id = 'bubbleNumberSpan';
    bubbleNumberSpan.innerHTML = bubbleNumber;
    bubbleNumberSpan.style.fontFamily = 'tahoma, verdana';
    bubbleNumberSpan.style.fontSize = '8px';
    bubbleNumberSpan.style.textAlign = 'justify';
    bubbleNumberSpan.style.height = bubbleNumberHeight;
    boxdiv.appendChild(bubbleNumberSpan);


    var contents = document.createElement('iframe');
    contents.id = 'bubbleIFrame';
    contents.scrolling = 'no';
    contents.frameBorder = '0';
    contents.style.width = (width) + 'px';
    contents.style.height = (height - offset - bubbleNumberHeight) + 'px';

    boxdiv.appendChild(contents);

    move_box(an, boxdiv, width, height, maxWidth, maxHeight);

    setHyperlink(bubbleNumber, contents, subfolder)
    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false;
}

function setHyperlink(bubbleNumber, contents, subfolder)
{
    var tooltipLink = subfolder + 'bubbles/bubble' + bubbleNumber + '.html';
    if (contents.contentWindow)
        contents.contentWindow.document.location.replace(
            tooltipLink);
    else
        contents.src = tooltipLink;
}

var helpPageTimer = null;
var helpPage = null;

function showHelpPage(helpPageNumber, subfolder)
{
    if (!subfolder)
    {
        subfolder = '';
    }
    
    var left   = (screen.width  - 425)/2;
    var top    = (screen.height - 275)/2;
 
    helpPage = window.open(subfolder + 'helpfiles/help.html', 'helpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=yes,copyhistory=yes,width=425,height=275,' + 'top=' + top + ',left=' + left + " ");

    helpPageTimer = setInterval("fillHelpPage('" + helpPageNumber + "', '" + subfolder + "');", 100);    
}

function fillHelpPage(helpPageNumber, subfolder)
{
    var helpPageNumberSpan = helpPage.document.getElementById('helpPageNumber');
    var helpContent = helpPage.document.getElementById('helpPageContent');
    if (helpPageNumberSpan != null && helpContent != null)
    {
        clearInterval(helpPageTimer);
        // Retrieve the help page number span and set its content
        helpPageNumberSpan.innerHTML = helpPageNumber;    
        
        // Retrieve the help page inner frame and set the source
        var helpPageLink = subfolder + 'helpfiles/help' + helpPageNumber + '.html';
        if (helpContent.contentWindow)
            helpContent.contentWindow.document.location.replace(helpPageLink);
        else
            helpContent.src = helpPageLink;
    }
}