﻿
var __pageModalBackgroundOverlay = document.createElement("div");
var __pageModalLoadingTemplate = document.createElement("div");  
var __pageModalOverlayTemplateCreated = false;

function createLoadingOverlay()
{
    document.body.appendChild(__pageModalLoadingTemplate);
    document.body.appendChild(__pageModalBackgroundOverlay);
    __pageModalOverlayTemplateCreated = true;
}

function __OnLoadStarting(sender, loadingTemplate, args) 
{        
    if(!__pageModalOverlayTemplateCreated)
        createLoadingOverlay();
    __UIEnableStateChange(false, loadingTemplate);    
}

function __OnLoadEnding(sender, loadingTemplate, args) 
{
    __UIEnableStateChange(true, loadingTemplate);
}

function __UIEnableStateChange(state, loadingTemplate) 
{
    if (!state)
    {
        __pageModalBackgroundOverlay.style.display = '';
        __pageModalBackgroundOverlay.style.position = 'absolute';
        __pageModalBackgroundOverlay.style.left = '0px';
        __pageModalBackgroundOverlay.style.top = '0px';
        
        //var clientBounds = this._getClientBounds();
        //var clientWidth = clientBounds.width;
        //var clientHeight = clientBounds.height;
        
        var clientWidth = ks_clientwidth();
        var clientHeight = ks_clientheight();
        
        if(typeof(clientWidth) == 'undefined' || typeof(clientHeight) == 'undefined')
            return;
        
        var overlayWidth = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth);
        var overlayHeight = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight);
        __pageModalBackgroundOverlay.style.width = overlayWidth+'px';
        __pageModalBackgroundOverlay.style.height = overlayHeight+'px';
        __pageModalBackgroundOverlay.style.zIndex = 10000;
        __pageModalBackgroundOverlay.className = "modalBackground";
        
        __pageModalLoadingTemplate.innerHTML = '';        
        __pageModalLoadingTemplate.innerHTML = loadingTemplate;    
        __pageModalLoadingTemplate.style.display = '';
        __pageModalLoadingTemplate.style.position = 'absolute';
        __pageModalLoadingTemplate.style.top = (overlayHeight / 2) + 'px';
        __pageModalLoadingTemplate.style.left = (overlayWidth / 2 - 60) + 'px';
        __pageModalLoadingTemplate.style.zIndex = 10002;
        __pageModalLoadingTemplate.className = "pageLoadTemplate";
    }
    else
    {
        __pageModalBackgroundOverlay.style.display = 'none';
        __pageModalLoadingTemplate.innerHTML = '';        
        __pageModalLoadingTemplate.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);
//}    

function ks_clientheight()
{
	if(window.innerHeight!=window.undefined){
		return window.innerHeight;
	}
	if(document.compatMode=="CSS1Compat"){
		return document.documentElement.clientHeight;
	}
	if(document.body){
		return document.body.clientHeight;
	}
	return window.undefined;
}

function ks_clientwidth()
{
	if(window.innerWidth!=window.undefined){
		return window.innerWidth;
	}
	if(document.compatMode=="CSS1Compat"){
		return document.documentElement.clientWidth;
	}
	if(document.body){
		return document.body.clientWidth;
	}
	return window.undefined;
}

//var __docAspNetForm = document.forms['aspnetForm'];
//if (!__docAspNetForm) {
//    __docAspNetForm = document.aspnetForm;
//}
//if(__docAspNetForm) __docAspNetForm.onsubmit = __OnBeginRequest;