﻿var pbe, prm;
if (isIE)
    window.attachEvent('onload', ajaxLoading_Init);
else
    window.addEventListener('load', ajaxLoading_Init, false);

function ajaxLoading_Init() {
    prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);
}

function InitializeRequest(sender, args) {
    if (prm.get_isInAsyncPostBack())
        args.set_cancel(true);
    var p = null;
    if (sender._postBackSettings.panelsToUpdate != null)
        p = sender._postBackSettings.panelsToUpdate[0].replace(/\$/gi, '_');
    else if (sender._updatePanelClientIDs[0] != null)
        p = sender._updatePanelClientIDs[0];
    else
        return;
    var panel = $get(p);
    if (!panel) return;
    panel.className = "opacity";
    var ldDiv = document.getElementById(panel.id + '_loading');
    if (ldDiv) panel.removeChild(ldDiv);
    ldDiv = document.createElement('div');
    ldDiv.id = panel.id + '_loading';
    ldDiv.className = 'ajax_loading';
    panel.parentNode.appendChild(ldDiv);
    var ld = document.createElement('img');
    ld.src = '/images/ajax_loading.gif';
    var panelMove = function() {
        var koor = elementXY(panel);
        ldDiv.style.top = koor.Top + (panel.offsetHeight / 2 - ldDiv.offsetHeight / 2) + 'px';
        ldDiv.style.left = koor.Left + (panel.offsetWidth / 2 - ldDiv.offsetWidth / 2) + 'px';
    };
    ld.onload = panelMove;
    ldDiv.appendChild(ld);
    panelMove();
}
function EndRequest(sender, args) {
    var p = null;
    if (sender._postBackSettings.panelsToUpdate != null)
        p = sender._postBackSettings.panelsToUpdate[0].replace(/\$/gi, '_');
    else if (sender._updatePanelClientIDs[0] != null)
        p = sender._updatePanelClientIDs[0];
    else
        return;
    var panel = $get(p);
    if (!panel) return;
    panel.className = "";
    var ldDiv = document.getElementById(panel.id + '_loading');
    if (ldDiv) panel.parentNode.removeChild(ldDiv);
    try {
        header_onresize();
    } catch (err) { } 
}
function AbortPostBack() {
    if (prm.get_isInAsyncPostBack())
        prm.abortPostBack();
}
