//////////////////////////////////////////////////////////////////////////////////////////////////
//  Foxbright Overlay object.
//   Manages an overlay on a page.
//   Assumes jQuery and geometry.js are present.
//////////////////////////////////////////////////////////////////////////////////////////////////


function FoxOverlay()
{
};


//////////////////////////////////////////////////////////////////////////////////////////////////
// Variables.
//////////////////////////////////////////////////////////////////////////////////////////////////

// overlay div id.
FoxOverlay.overlay_id = "fb_overlay";
// content div id.
FoxOverlay.overlay_content_id = "fb_overlay_content";

// time (in milliseconds) that the overlay should fade.
FoxOverlay.fade_duration = 200;

// are we running in IE?
FoxOverlay.ie4 = document.all;
// are we running in Netscape?
FoxOverlay.ns6 = document.getElementById&&!document.all;

//////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////
// Places the overlay over the document and displays the dialog.
//  OPTIONAL PARAMETERS
//  fade_duration - the time, in milliseconds, that the overlay should be faded in/out.
//              this will override the default set when the FoxOverlay object was created.
FoxOverlay.showOverlay = function(fade_duration)
{
    var duration = FoxOverlay.fade_duration
    if (typeof(fade_duration) != "undefined")
    {
        duration = fade_duration;
    }

    // select elements are windowed controls and bleed through in IE, so hide temporarily
    //jQuery('admin_content').getElementsBySelector('select').invoke('toggle');

    // stretch overlay to fill page and fade in
    jQuery( '#' + FoxOverlay.overlay_id ).height(Geometry.getDocumentHeight() + "px");
    jQuery( '#' + FoxOverlay.overlay_id ).width(Geometry.getDocumentWidth() + "px");

    // position editor
    FoxOverlay.resizeContent();

    jQuery( '#' + FoxOverlay.overlay_id ).fadeIn( duration );
    jQuery( '#' + FoxOverlay.overlay_content_id ).fadeIn( duration );
}//end showOverlay


//////////////////////////////////////////////////////////////////////////////////////////////////
// Closes the content and removes the overlay from the screen.
//  OPTIONAL PARAMETERS
//  fade_duration - the time, in milliseconds, that the overlay should be faded in/out.
//              this will override the default set when the FoxOverlay object was created.
FoxOverlay.closeOverlay = function(fade_duration)
{
    var duration = FoxOverlay.fade_duration
    if (typeof(fade_duration) != "undefined")
    {
        duration = fade_duration;
    }

    jQuery( '#' + FoxOverlay.overlay_id ).fadeOut( duration );
    jQuery( '#' + FoxOverlay.overlay_content_id ).fadeOut( duration );

    jQuery( '#' + FoxOverlay.overlay_content_id ).empty();
    // select elements are windowed controls and bleed through in IE, so reshow
    //jQuery('admin_content').getElementsBySelector('select').invoke('toggle');
}//end closeOverlay


//////////////////////////////////////////////////////////////////////////////////////////////////
// Resizes the content div (utility in case the content inside the div has changed).
FoxOverlay.resizeContent = function()
{
    // position editor
    var imageEditorTop = Math.floor(Geometry.getVerticalScroll() + (Geometry.getViewportHeight() / 2) - (jQuery( '#' + FoxOverlay.overlay_content_id ).height() / 2));
    var imageEditorLeft = Math.floor((Geometry.getViewportWidth() / 2) - (jQuery( '#' + FoxOverlay.overlay_content_id ).width() / 2));

    jQuery( '#' + FoxOverlay.overlay_content_id ).css( 'top', imageEditorTop + "px");
    jQuery( '#' + FoxOverlay.overlay_content_id ).css( 'left', imageEditorLeft + "px");
}//end resizeContent


//////////////////////////////////////////////////////////////////////////////////////////////////
// Initializes the popup.
//  OPTIONAL PARAMETERS
//  fade_duration - the default time, in milliseconds, that the overlay should be faded in/out.
//              default - 200
//  overlay_id - id to be used for the overlay element, <overlay_id>_content is used for the content element.
//              default - fb_overlay
FoxOverlay.initOverlay = function(fade_duration, overlay_id)
{
    if (typeof(fade_duration) != "undefined")
    {
        FoxOverlay.fade_duration = fade_duration;
    }
    if (typeof(overlay_id) != "undefined")
    {
        FoxOverlay.overlay_id = overlay_id;
        FoxOverlay.overlay_content_id = overlay_id + '_content';
    }

    if (jQuery('#' + FoxOverlay.overlay_id).length > 0)
    {
        // don't bother creating the divs if they already exist.
        return;
    }

    if (FoxOverlay.ie4||FoxOverlay.ns6)
    {
        var overlayDiv = document.createElement('div');
        overlayDiv.setAttribute('id', FoxOverlay.overlay_id);
        overlayDiv.setAttribute('style', 'display: none');
        document.body.appendChild(overlayDiv);
        jQuery('#' + FoxOverlay.overlay_id).addClass('fb_overlay');

        var overlayContentDiv = document.createElement('div');
        overlayContentDiv.setAttribute('id', FoxOverlay.overlay_content_id);
        overlayContentDiv.setAttribute('style', 'display: none');
        document.body.appendChild(overlayContentDiv);
        jQuery('#' + FoxOverlay.overlay_content_id).addClass('fb_overlay_content');
    }
}//end initOverlay
