﻿Type.registerNamespace("Framework.Controls");

Framework.Controls.AsyncPostbackProgress = function(element) {
    Framework.Controls.AsyncPostbackProgress.initializeBase(this, [element]);
    this._delay = 0;
    this._app = null;
    this._app_initHandle = Function.createDelegate(this, this._app_init);
    this._requestManager = null;
    this._app_beginRequestHandle = Function.createDelegate(this, this._app_beginRequest);
    this._app_endRequestHandle = Function.createDelegate(this, this._app_endRequest);
    this._showProgressHandle = Function.createDelegate(this, this._showProgress);
    this._isInProgress = false;
    this._loader = null;
};
Framework.Controls.AsyncPostbackProgress.prototype =
{
    get_delay: function() { return this._delay; },
    set_delay: function(value) { this._delay = value; },
    get_loader: function() { return this._loader; },
    set_loader: function(value) { this._loader = $get(value); },

    initialize: function() {
        Framework.Controls.AsyncPostbackProgress.callBaseMethod(this, "initialize");
        this._app = Sys.Application;
        if (this._app) this._app.add_init(this._app_initHandle);
    },
    dispose: function() {
        Sys.Application.remove_init(this._app_initHandle);
        Framework.Controls.AsyncPostbackProgress.callBaseMethod(this, "dispose");
    },
    _app_init: function(sender) {
        this._requestManager = Sys.WebForms.PageRequestManager.getInstance();
        this._requestManager.add_beginRequest(this._app_beginRequestHandle);
        this._requestManager.add_endRequest(this._app_endRequestHandle);
    },
    _app_beginRequest: function(sender, args) {
        if (!this._isInProgress) {
            this._isInProgress = true;
            setTimeout(this._showProgressHandle, this._delay);
        }
    },
    _app_endRequest: function(sender, args) {
        this._hideProgress();
        this._isInProgress = false;
    },
    _showProgress: function() {
        if (this._isInProgress) {
            if (this._loader) {
                this._loader.style.display = "";

                // Get window size
                var _viewPort = { width: $(window).width(), height: $(window).height() };
                var _viewPortScroll = { top: $(document).scrollTop(), left: $(document).scrollLeft() };
                var _elBounds = Sys.UI.DomElement.getBounds(this._loader);

                // Center loader
                this._loader.style.left = (((_viewPort.width / 2) - (_elBounds.width / 2)) + _viewPortScroll.left) + "px";
                this._loader.style.top = (((_viewPort.height / 2) - (_elBounds.height / 2)) + _viewPortScroll.top) + "px";
            }
            this._isInProgress = false;
        }
    },
    _hideProgress: function() {
        if (this._loader) this._loader.style.display = "none";
    }
};

Framework.Controls.AsyncPostbackProgress.registerClass("Framework.Controls.AsyncPostbackProgress", 
                                                       Sys.UI.Control);
if(typeof(Sys)!=="undefined")Sys.Application.notifyScriptLoaded(); 
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();