﻿Type.registerNamespace("OmniWeb");

OmniWeb.PostBackProgress = function(element)
{
    OmniWeb.PostBackProgress.initializeBase(this, [element]);

    /* Client Properties */
    this._timerCookie = null;

    /* Server Properties */
    this._AssociatedUpdatePanelId = null;
    this._AutoCenter = true;
    this._DisplayAfter = 500;
    this._DynamicLayout = true;

    /* Event Handlers */
    this._beginRequestHandler = null;
    this._endRequestHandler = null;
    this._startHandler = null;
}

OmniWeb.PostBackProgress.prototype =
{
    initialize: function()
    {
        OmniWeb.PostBackProgress.callBaseMethod(this, 'initialize');

        var Prm = Sys.WebForms.PageRequestManager.getInstance();
        if (Prm) {
            this._beginRequestHandler = Function.createDelegate(this, this._onBeginRequest);
            Prm.add_beginRequest(this._beginRequestHandler);

            this._endRequestHandler = Function.createDelegate(this, this._onEndRequest);
            Prm.add_endRequest(this._endRequestHandler);

            this._startHandler = Function.createDelegate(this, this._onStartRequest);
        }

    },

    dispose: function()
    {
        this._clearTimeout();

        var Prm = Sys.WebForms.PageRequestManager.getInstance();
        if (Prm) {
            Prm.remove_beginRequest(this._beginRequestHandler);
            delete this._beginRequestHandler;
            Prm.remove_endRequest(this._endRequestHandler);
            delete this._endRequestHandler;
            delete this._startRequestHandler;
        }

        OmniWeb.PostBackProgress.callBaseMethod(this, 'dispose');
    },

    get_AssociatedUpdatePanelId: function()
    {
        return this._AssociatedUpdatePanelId;
    },
    set_AssociatedUpdatePanelId: function(value)
    {
        this._AssociatedUpdatePanelId = value;
    },

    get_AutoCenter: function()
    {
        return this._AutoCenter;
    },
    set_AutoCenter: function(value)
    {
        this._AutoCenter = value;
    },

    get_DisplayAfter: function()
    {
        return this._DisplayAfter;
    },
    set_DisplayAfter: function(value)
    {
        this._DisplayAfter = value;
    },

    get_DynamicLayout: function()
    {
        return this._DynamicLayout;
    },
    set_DynamicLayout: function(value)
    {
        this._DynamicLayout = value;
    },

    _clearTimeout: function()
    {
        if (this._timerCookie) {
            window.clearTimeout(this._timerCookie);
            this._timerCookie = null;
        }
    },

    _onBeginRequest: function(sender, args)
    {
        var Cntrl = args.get_postBackElement();
        var Show = !this._AssociatedUpdatePanelId;
        while (!Show && Cntrl) {
            if (Cntrl.id && this._AssociatedUpdatePanelId === Cntrl.id) Show = true;
            Cntrl = Cntrl.parentNode;
        }
        if (Show) {
            this._timerCookie = window.setTimeout(this._startHandler, this._DisplayAfter);
        }
    },

    _onEndRequest: function(sender, args)
    {
        if (this._DynamicLayout) {
            this.get_element().style.display = 'none';
        } else {
            this.get_element().style.visibility = "hidden";
        }
        this._clearTimeout();
    },

    _onStartRequest: function()
    {
        var Cntrl = this.get_element();
        var Prm = Sys.WebForms.PageRequestManager.getInstance();
        if (Prm && Prm.get_isInAsyncPostBack()) {
            if (this._DynamicLayout) {
                $OmniWeb.SetDisplay(Cntrl, true);
            } else {
                Cntrl.style.visibility = "visible";
            }
            if (this._AutoCenter) {
                $OmniWeb.CenterElement(Cntrl);
            }
        }
        this._timerCookie = null;
    }

}

OmniWeb.PostBackProgress.registerClass('OmniWeb.PostBackProgress', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();