﻿var prm = Sys.WebForms.PageRequestManager.getInstance();
var requestQueue = new Array();
var progressList = new Array();

ProgressIndicator = function(postbackId, indicator, panel, offsetY, isExactMatch) {
    this._postbackId = postbackId;
    this._indicator = indicator;
    this._panel = panel;
    this._offsetY = offsetY;
    this._isExactMatch = isExactMatch;
    
    this.Activate=Activate;
    this.Deactivate=Deactivate;
    this.IsActive=IsActive;
}
function Activate() {
    var indicator = $get(this._indicator);
    var panel = $get(this._panel);
    var indBounds = Sys.UI.DomElement.getBounds(indicator);
    var pnlBounds = Sys.UI.DomElement.getBounds(panel);
    
    if(indBounds.width==0) indBounds.width=100;
    
    indBounds.x = pnlBounds.x + Math.round((pnlBounds.width-indBounds.width)/2);
    if (this._offsetY>=0)
        indBounds.y = pnlBounds.y + this._offsetY;
    else
        indBounds.y = pnlBounds.y + Math.round((pnlBounds.height-indBounds.height)/2);
    
    Sys.UI.DomElement.setLocation(indicator, indBounds.x, indBounds.y);
    indicator.style.display='block';
    // hack to make the animated gif play in IE6
    indicator.innerHTML=indicator.innerHTML;   
}
function Deactivate() {
    $get(this._indicator).style.display='none';
}
function IsActive() {
    return $get(this._indicator).style.display=="block";
}

prm.add_initializeRequest(ProgressInitHandler);
prm.add_endRequest(ProgressEndHandler);

function ProgressInitHandler(sender, args){
    if (prm.get_isInAsyncPostBack()){
        args.set_cancel(true);
        Array.add(requestQueue, args.get_postBackElement());  
    } 
    else{
        for(i=0; i<progressList.length; i++){
            if(progressList[i]._isExactMatch){
                if(args.get_postBackElement().id == progressList[i]._postbackId){
                    progressList[i].Activate();
                    break;
                }
            }
            else {
                if(args.get_postBackElement().id.indexOf(progressList[i]._postbackId)>=0){
                    progressList[i].Activate();
                    break;
                }
            }
        }
    }
}

function ProgressEndHandler(sender, args){
    for(i=0; i<progressList.length; i++)
        if(progressList[i].IsActive())
            progressList[i].Deactivate();
            
    if(requestQueue.length > 0)
    {// fire correspond event again of the item cached
        var el = $get(requestQueue[0].id);
        if (el.click) el.click();
        Array.removeAt(requestQueue,0);
    }    
}

