// This will "show" a light box
function ShowLightBox(iFrameUrl, iWidth, iHeight)
{
    var LightBoxContainer = window.top.document.getElementById("LightBoxContainer");
    var LightBoxDiv = window.top.document.getElementById("LightBoxDiv");
    if (LightBoxDiv != null && LightBoxContainer != null)
    {
        // If lightbox is bigger than the browser window, then do not exceed the window
        var WindowWidth = GetWindowWidth();
        var WindowHeight = GetWindowHeight();
        if (WindowWidth-20 < iWidth)
        {iWidth = WindowWidth-20}
        if (WindowHeight-20 < iHeight)
        {iHeight = WindowHeight-20}
        
        // Set light box size to match iframe with room for the "close" link
        LightBoxDiv.style.width = iWidth+"px";
        LightBoxDiv.style.height = iHeight+18+"px";

        // Center light box on page (need .51 width so right margin is not chopped when oversized)
        LightBoxDiv.style.margin = "-"+(iHeight+18)*.5+"px 0px 0px -"+iWidth*.51+"px";
        
        // Handle the iframe (this is done by tagname for Mozilla compatibilty
        var iFrames = window.top.document.getElementsByTagName("iframe");
        if (iFrames != null)
        {
            for (var i = 0; i < iFrames.length; i++)
            {
                // Double check src to make sure that you have the right iframe.
                if (iFrames[i].name == "LightBoxIframe")
                {
                    iFrames[i].width = iWidth;
                    iFrames[i].height = iHeight;
                    iFrames[i].src = iFrameUrl;
                }
            }
        }
        // Show light box
        LightBoxContainer.style.display = "block";
    }
}


// This will "hide" a light box
function HideLightBox()
{
    var LightBoxContainer = window.top.document.getElementById("LightBoxContainer");
    if (LightBoxContainer != null)
    {
        LightBoxContainer.style.display = "none";
    }

    // Handle the iframe (this is done by tagname for Mozilla compatibilty
    var iFrames = window.top.document.getElementsByTagName("iframe");
    if (iFrames != null)
    {
        for (var i = 0; i < iFrames.length; i++)
        {
            // Double check src to make sure that you have the right iframe.
            if (iFrames[i].name == "LightBoxIframe")
            {
                // We cler the src so the next light box does not flash this src before loading the correct one
                iFrames[i].src = "";
            }
        }
    }
}


// This returns a width for the browser window size
function GetWindowWidth()
{
    var myWidth = 0
    if( typeof( window.innerWidth ) == 'number' )
    {
        //Non-IE
        myWidth = window.innerWidth;
    }
    else if( document.documentElement && document.documentElement.clientWidth )
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    }
    else if( document.body && document.body.clientWidth )
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return(myWidth);
}


// This returns a width,height array for the browser window size
function GetWindowHeight()
{
    var myHeight = 0;
    if( typeof( window.innerHeight ) == 'number' )
    {
        //Non-IE
        myHeight = window.innerHeight;
    }
    else if( document.documentElement && document.documentElement.clientHeight )
    {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    }
    else if( document.body && document.body.clientHeight )
    {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return(myHeight);
}


// This will close the light box on an escape key press
document.onkeydown = function(e){ 
    if (e == null)
    { // ie
        keycode = event.keyCode;
    }
    else
    { // mozilla
        keycode = e.which;
    }
    if(keycode == Sys.UI.Key.esc)
    {
        HideLightBox();

    }
}

// This will close the SERVERLightBox
function CloseServerLightBox(ServerLightBoxCloser)
{
    $(ServerLightBoxCloser).parent('.CloseServerLightBoxDiv').parent('.ServerLightBoxDiv').parent('.ServerLightBoxContainer').hide();
}

// This will check the ServerLightBox for an AutoClose attribute and "close" it if neccessary.
$(document).ready(function(ServerLightBoxDelay) {
    // Must locate a client side control, then find the server side control by navigating from there.
    var closer = document.getElementById("CloseServerLightBox");
    var container = $(closer).parent('.CloseServerLightBoxDiv').parent('.ServerLightBoxDiv').parent('.ServerLightBoxContainer');
    var delay = $(container).attr('AutoCloseDelay');

    // If the delay is not 0, then create a delayed call to close the lightbox
    if(delay > 0)
    {
        setTimeout("FindCloseServerLightBox()",delay);
    }
});

// This will close a ServerLightBox without being handed a control to locate the light box
function FindCloseServerLightBox()
{
    $(document.getElementById("CloseServerLightBox")).parent('.CloseServerLightBoxDiv').parent('.ServerLightBoxDiv').parent('.ServerLightBoxContainer').hide();
}