var g_lightBoxTimer = null;
var g_locked = false;

function resizeLightbox( usePageOffsets )
{
    var lightbox = $(".lightbox");
    if ( lightbox.length == 0 )
        return;

    var nHeight = windowHeight();
    if ( document.body )
        if ( document.body.clientHeight > nHeight )
           nHeight = document.body.clientHeight;

    $('.lightbox-background').width( windowWidth() ).height( nHeight );

    var nOffset_top  = 0;
    var nOffset_left = 0;
    if ( usePageOffsets ) 
    {
        var nOrientation = window.orientation;
        var isPortrait = (nOrientation === undefined || nOrientation == 0);
        if ( g_isIE )
        {
            nOffset_top  = isPortrait ? document.documentElement.scrollTop : document.documentElement.scrollLeft;
            nOffset_left = isPortrait ? document.documentElement.scrollLeft : document.documentElement.scrollTop;
        }
        else
        {
            nOffset_top  = isPortrait ? window.pageYOffset : window.pageXOffset;
            nOffset_left = isPortrait ? window.pageXOffset : window.pageYOffset;
        }
    }
    //console_log( "width = " + windowWidth() + ", height = " + windowHeight() + " (" + nHeight + "), offset = (" + nOffset_top + ", " + nOffset_left + ")" );

    var nTop = ~~( nOffset_top + (windowHeight() - lightbox.height() ) / 2 );
    var nLeft = ~~( nOffset_left + (windowWidth() - lightbox.width() ) / 2 );

    //console_log( "width = " + windowWidth() + ", height = " + nHeight );
    //console_log( "off left = " + nOffset_left + ", off top = " + nOffset_top );
    //console_log( "LB width = " + lightbox.width() + ", LB height = " + lightbox.height() );
    //console_log( "setting to top = " + nTop + ", left = " + nLeft );
    lightbox.css( { top: nTop, left: nLeft } );
}

/**
 * options:
 *   text: message to be displayed
 *   canClose: true or false determines whether a Close link is shown
 *   onClose: optional javascript string to be executed upon clicking a Close link (no apostrophes allowed in callback code)
 *   timeout: optional timeout before self-closing
 *   page: id of page to attach lightbox to.  Omit or set to null to use the current page.
 *   size: one of 'small' (default for canClose=false), 'medium' (default for canClose=true), 'big'
 */
function openLightBox( options )
{
    // we can only have one lightbox open at a time
    if ( ! closeLightBox() )        // were we denied closure? (because of a extant modal)
        return;                     // ... then don't create another one

    var pageID = options.page;
    if ( pageID == null )
        pageID = getCurrentPage();     // use the current page

    $("#" + pageID).append( "<div class='lightbox'></div>" );

    // lightboxes require a dark background div, which goes after the lightbox content (but has a lower z-index)
    // destroy any extant background div from any current lightboxes, and create a new one for us
    $(".lightbox-background").remove();

    var lightbox = $(".lightbox");
    lightbox.after( "<div class='lightbox-background'></div>" );
    var strSize = "";

    var strContent = "";
    if ( options.canClose )
    {
        var strCallback = options.onClose;
        if ( strCallback === undefined )
            strCallback = "";

        strContent += "<div class='lightbox-close'><a href='javascript:void(0)' onclick='" + strCallback + ";g_locked=false;closeLightBox();'>Close</a></div>";
        strContent += "<div class='lightbox-message closeable'>" + options.text + "</div>";

        if ( options.size !== undefined )
            strSize = options.size;
        else
            strSize = "medium";

        g_locked = true;
    }
    else
    {
        //strContent += "<div class='lightbox-size'></div>";
        strContent += "<div class='lightbox-message'>" + options.text + "</div>";

        if ( options.size !== undefined )
            strSize = options.size;

        g_locked = false;

        if ( options.timeout !== undefined )
            closeLightBox( options.timeout );
    }
    lightbox.addClass( strSize );

    //console_log( "pageID = '" + pageID + "', text='" + options.text + "', canClose = '" + options.canClose + "'" );
    lightbox.html( strContent );

    if ( options.align === undefined )
       $('div.lightbox-message').css( 'text-align', 'center' );
    else
       $('div.lightbox-message').css( 'text-align', options.align );


    if ( g_isIE )
    {
        var nOldHeight = $('div.lightbox-message').css( 'height' );
        if ( nOldHeight )
        {
            nOldHeight = nOldHeight.replace( /px/, "" );
            $('div.lightbox-message').css( "height", "" + (nOldHeight-9) + "px" );
        }
    }
    resizeLightbox( true );
    lightbox.show();
};

function rehomeLightBox( selector )
{
    var lightbox = $(".lightbox").detach();
    var background = $(".lightbox-background").detach();

    if ( lightbox.length > 0 && background.length > 0 )
    {
        $(selector).append( lightbox );
        lightbox.after( background )
    }
};
/**
 * Close the currently open lightbox, with an optional delay in milliseconds
 */
function closeLightBox( delay )
{
    // is there already a pending close?  If so, cancel it
    if ( g_lightBoxTimer )
    {
        window.clearTimeout( g_lightBoxTimer );
        g_lightBoxTimer = null;
    }

    if ( g_locked )
        return false;

    if ( delay === undefined )
        delay = 0;

    if ( delay == 0 )
    {
        $(".lightbox-background").remove();
        $(".lightbox").remove();
    }
    else
    {
        g_lightBoxTimer = window.setTimeout( function() {
            $(".lightbox-background").remove();
            $(".lightbox").remove();
            g_lightBoxTimer = null;
        }, delay );
    }

    return true;
};


