﻿Type.registerNamespace("msfg.UpdateProgress");
msfg.UpdateProgress = function(element) {
    msfg.UpdateProgress.initializeBase(this, [element]);
    this._updatePanelId = null;
    this._showAfter = 500;
    this._timeoutCookie = null;
    this._pageRequestManager = null;
    this._progressHelper = null;
    
    this._resizeAgainst = (document.onresize ? document : window);
};

msfg.UpdateProgress.prototype = {
	requestStart: function(sender, args) {
		var postbackTrigger = args.get_postBackElement();
		var shouldShowProgress = false;
		while (!shouldShowProgress && postbackTrigger) {
			shouldShowProgress = (postbackTrigger.id && this._updatePanelId === postbackTrigger.id);
			postbackTrigger = postbackTrigger.parentNode;
		}
		
		if (shouldShowProgress) {
			this._timeoutCookie = window.setTimeout(this._showContainerDelegate, this._showAfter);
		}
	},
	
	requestEnd: function(sender, args) {
		this.detachResizing();
		$(this.get_element()).hide();
		if (this._timeoutCookie != null) {
			window.clearTimeout(this._timeoutCookie);
			this._timeoutCookie = null;
		}
	},
	
	showContainer: function() {
		this.attachResizing();
		$(this.get_element()).center(this._updatePanelId).show();
	},
	
	resizing: function() {
		$(this.get_element()).center(this._updatePanelId);
	},
	
	attachResizing: function() {
		Event.observe(this._resizeAgainst, "resize", this._resizingDelegate);
	},
	
	detachResizing: function() {
		Event.stopObserving(this._resizeAgainst, "resize", this._resizingDelegate);
	},
	
    initialize: function() {
        msfg.UpdateProgress.callBaseMethod(this, "initialize");
        
        this._requestStartDelegate = Function.createDelegate(this, this.requestStart);
        this._requestEndDelegate = Function.createDelegate(this, this.requestEnd);
        this._showContainerDelegate = Function.createDelegate(this, this.showContainer);
        this._resizingDelegate = Function.createDelegate(this, this.resizing);
        
		if (Sys.WebForms && Sys.WebForms.PageRequestManager) {
			this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
		}
		if (this._pageRequestManager !== null ) {
			this._pageRequestManager.add_beginRequest(this._requestStartDelegate);
			this._pageRequestManager.add_endRequest(this._requestEndDelegate);
		}
    },
    
    dispose: function() {
        msfg.UpdateProgress.callBaseMethod(this, "dispose");
        
        if (this._pageRequestManager !== null ) {
			this._pageRequestManager.remove_beginRequest(this._requestStartDelegate);
			this._pageRequestManager.remove_endRequest(this._requestEndDelegate);
		}
    },
    
    getProgressHelper: function() {
		if (this._progressHelper !== null)
			return this._progressHelper;
		else if (this._updatePanelId !== null && this.get_element().id !== null)
			return this._progressHelper = new ProgressLoader(this.get_element().id, this._updatePanelId);
		return null;
    },
    
    get_updatePanelId: function() { return this._updatePanelId; },
    set_updatePanelId: function(value) {
		if (this._updatePanelId != null && this._updatePanelId !== value)
			throw "updatePanelId can only be set once.";
		this._updatePanelId = value;
    },
    
    get_showAfter: function() { return this._showAfter; },
    set_showAfter: function(value) { this._showAfter = value; }
};

msfg.UpdateProgress.registerClass("msfg.UpdateProgress", Sys.UI.Control);
if(typeof(Sys)!=="undefined")Sys.Application.notifyScriptLoaded(); 
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();