﻿var _backgroundElement = document.createElement("div");

function pageLoad() {
    var manager = Sys.WebForms.PageRequestManager.getInstance();
    manager.add_beginRequest(OnBeginRequest);
    manager.add_endRequest(OnEndRequest);

    $get("pageContent").appendChild(_backgroundElement);
}

function OnBeginRequest(sender, args) {
    EnableUI(false);
}

function OnEndRequest(sender, args) {
    EnableUI(true);
}

function EnableUI(state) {
    if (!state) {
        _backgroundElement.style.display = '';
        _backgroundElement.style.position = 'fixed';
        _backgroundElement.style.left = '0px';
        _backgroundElement.style.top = '0px';

        var clientBounds = this._getClientBounds();
        var clientWidth = clientBounds.width;
        var clientHeight = clientBounds.height;
        _backgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth) + 'px';
        _backgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight) + 'px';
        _backgroundElement.style.zIndex = 10000;
        _backgroundElement.className = "modalBackground";
    }
    else {
        _backgroundElement.style.display = 'none';
    }
}

function _getClientBounds() {
    var clientWidth;
    var clientHeight;
    switch (Sys.Browser.agent) {
        case Sys.Browser.InternetExplorer:
            clientWidth = document.documentElement.clientWidth;
            clientHeight = document.documentElement.clientHeight;
            break;
        case Sys.Browser.Safari:
            clientWidth = window.innerWidth;
            clientHeight = window.innerHeight;
            break;
        case Sys.Browser.Opera:
            clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
            break;
        default:  // Sys.Browser.Firefox, etc.
            clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
            break;
    }
    return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
}    

