﻿var _updateProgressDiv;
var _backgroundDiv;
var _gridView;

function pageLoad(sender, args) {
    //  register for our events
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);

    //  get the updateprogressdiv
    _updateProgressDiv = $get('updateProgressDiv');


    //  fetch the gridview


    _gridView = $get('contentArea');



    //  create the div that we will position over the gridview
    //  during postbacks
    _backgroundDiv = document.createElement('div');
    _backgroundDiv.style.display = 'none';
    _backgroundDiv.style.zIndex = 10000;
    _updateProgressDiv.style.zIndex = 10001;
    _backgroundDiv.className = 'background';

    //  add the element to the DOM
    _gridView.parentNode.appendChild(_backgroundDiv);
}



function beginRequest(sender, args) {
    // make it visible
    _updateProgressDiv.style.display = '';
    _backgroundDiv.style.display = '';

    // get the bounds of both the gridview and the progress div
    var gridViewBounds = Sys.UI.DomElement.getBounds(_gridView);
    var updateProgressDivBounds = Sys.UI.DomElement.getBounds(_updateProgressDiv);

    //  center of gridview
    var x = gridViewBounds.x + Math.round(gridViewBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2);
    var y = gridViewBounds.y + Math.round(gridViewBounds.height / 2) - Math.round(updateProgressDivBounds.height / 2);

    //  set the dimensions of the background div to the same as the gridview
    _backgroundDiv.style.width = gridViewBounds.width + 'px';
    _backgroundDiv.style.height = gridViewBounds.height + 'px';

    //    set the progress element to this position
    Sys.UI.DomElement.setLocation(_updateProgressDiv, x, y);
    //  place the div over the gridview
    Sys.UI.DomElement.setLocation(_backgroundDiv, gridViewBounds.x, gridViewBounds.y);
}


function endRequest(sender, args) {
    // make it invisible
    _updateProgressDiv.style.display = 'none';
    _backgroundDiv.style.display = 'none';
}