﻿Type.registerNamespace('BIT.WebControls');

BIT.WebControls.UpdateProgressEx = function () 
{
    BIT.WebControls.UpdateProgressEx.initializeBase(this);

    this._element = null;
    this._displayAfter = 0;
    this._dynamicLayout = true;
    this._associatedUpdatePanelId = null;
    this._beginRequestHandlerDelegate = null;
    this._startDelegate = null;
    this._endRequestHandlerDelegate = null;
    this._pageRequestManager = null;
    this._timerCookie = null;
    this._controlIdsToHide = null;
    this._controlIdsToShow = null;
}

BIT.WebControls.UpdateProgressEx.prototype = 
{
    /* --- Properties --- */
    get_element : function () 
    {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._element;
    },

    get_displayAfter : function () 
    {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._displayAfter;
    },
    
    set_displayAfter : function (value) 
    {
        var e = Function._validateParams(arguments, [{name: "value", type: Number}]);
        if (e) throw e;

        this._displayAfter = value;
    },
    
    get_dynamicLayout : function () 
    {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._dynamicLayout;
    },
    
    set_dynamicLayout : function (value) 
    {
        var e = Function._validateParams(arguments, [{name: "value", type: Boolean}]);
        if (e) throw e;

        this._dynamicLayout = value;
    },
    
    get_associatedUpdatePanelId : function () 
    {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._associatedUpdatePanelId;
    },
    
    set_associatedUpdatePanelId : function (value) 
    {
        var e = Function._validateParams(arguments, [{name: "value", type: String, mayBeNull: true}]);
        if (e) throw e;

        this._associatedUpdatePanelId = value;
    },


    get_controlIdsToHide : function () 
    {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._controlIdsToHide;
    },
    
    set_controlIdsToHide : function (value) 
    {
        var e = Function._validateParams(arguments, [{name: "value", type: Array, elementType: String, mayBeNull: true, elementMayBeNull: false}]);
        if (e) throw e;

        this._controlIdsToHide = value;
    },
    
    
    get_controlIdsToShow : function () 
    {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._controlIdsToShow;
    },
    
    set_controlIdsToShow : function (value) 
    {
        var e = Function._validateParams(arguments, [{name: "value", type: Array, elementType: String, mayBeNull: true, elementMayBeNull: false}]);
        if (e) throw e;

        this._controlIdsToShow = value;
    },
    
    /* --- Events --- */
    add_beginRequest : function(handler) 
    {
        var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
        if (e) throw e;

        this.get_events().addHandler("beginRequest", handler);
    },
    
    remove_beginRequest : function(handler) 
    {
        var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
        if (e) throw e;

        this.get_events().removeHandler("beginRequest", handler);
    },

    raiseBeginRequest : function() 
    {
        var handler = this.get_events().getHandler("beginRequest");
        
        if (handler) 
            handler(this, Sys.EventArgs.Empty);
        
    },

    add_endRequest : function(handler) 
    {
        var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
        if (e) throw e;

        this.get_events().addHandler("endRequest", handler);
    },
    
    remove_endRequest : function(handler) 
    {
        var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
        if (e) throw e;

        this.get_events().removeHandler("endRequest", handler);
    },
    
    raiseEndRequest : function() 
    {
        var handler = this.get_events().getHandler("endRequest");
        
        if (handler) 
            handler(this, Sys.EventArgs.Empty);
        
    },

    
    /* --- Methods --- */
    _handleBeginRequest : function (sender, arg) 
    {
        var curElem = arg.get_postBackElement();
        
        var showProgress = !this._associatedUpdatePanelId; 
        
        if (curElem)
        {
			showProgress = !Array.contains(BIT.WebControls.UpdateProgressEx._ignoreIds, curElem.id);
			
			if (!showProgress)
				curElem = null;
        }
        
        while (!showProgress && curElem) 
        {
            if (curElem.id && this._associatedUpdatePanelId === curElem.id) 
                showProgress = true; 
            
            curElem = curElem.parentNode; 
        } 
        
        if (showProgress)
        {
			if (this._displayAfter <= 0)
				this._startRequest();
			else
				this._timerCookie = window.setTimeout(this._startDelegate, this._displayAfter);
		}
    },
    
    _setElementVisibility : function(element, visibility)
    {
        var e = Function._validateParams(arguments, [
            {name: "element", domElement: true, mayBeNull: true},
            {name: "visibility", type: Boolean, optional: true}
        ]);
        
        if (element && element.style)
        {
            if (this._dynamicLayout) 
                element.style.display = (visibility) ? '' : 'none';
            else 
                element.style.visibility = (visibility) ? 'visible' : 'hidden';
        }
    },
    
    _startRequest : function () 
    {
        if (this._pageRequestManager.get_isInAsyncPostBack()) 
        {
            this.raiseBeginRequest();
            
            this._setElementVisibility(this.get_element(), true); 
            
            if (this._controlIdsToHide)
            {
                for(var i = 0 ; i < this._controlIdsToHide.length; i++)
                    this._setElementVisibility($get(this._controlIdsToHide[i]), false); 
            }
            
            if (this._controlIdsToShow)
            {       
                for(var i = 0 ; i < this._controlIdsToShow.length; i++)
                    this._setElementVisibility($get(this._controlIdsToShow[i]), true); 
            }
        }
        
        this._timerCookie = null;
    },
    
    _handleEndRequest : function (sender, arg) 
    {
		if (arg)
		{
			window.setTimeout(Function.createDelegate(this, this._handleEndRequest), 50);
			return;
		}
		
        if (this._timerCookie) 
        {
            window.clearTimeout(this._timerCookie);
            this._timerCookie = null;
        }
        
        if (this._controlIdsToShow)
        {       
            for(var i = 0 ; i < this._controlIdsToShow.length; i++)
                this._setElementVisibility($get(this._controlIdsToShow[i]), false); 
        }
             
        if (this._controlIdsToHide)
        {
            for(var i = 0 ; i < this._controlIdsToHide.length; i++)
                this._setElementVisibility($get(this._controlIdsToHide[i]), true); 
        }
        
        this.raiseEndRequest();
        
        this._setElementVisibility(this.get_element(), false); 
    },
    
    
    
    dispose : function () 
    {
       if (this._pageRequestManager !== null) 
       {
           this._pageRequestManager.remove_beginRequest(this._beginRequestHandlerDelegate);
           this._pageRequestManager.remove_endRequest(this._endRequestHandlerDelegate);
       }
       
       BIT.WebControls.UpdateProgressEx.callBaseMethod(this, 'dispose');
    },
    
    initialize : function () 
    {
        BIT.WebControls.UpdateProgressEx.callBaseMethod(this, 'initialize');

        
        if(this.get_id())
            this._element = $get(this.get_id());
                
    	this._beginRequestHandlerDelegate = Function.createDelegate(this, this._handleBeginRequest);
    	this._endRequestHandlerDelegate = Function.createDelegate(this, this._handleEndRequest);
    	this._startDelegate = Function.createDelegate(this, this._startRequest);
    	
    	if (Sys.WebForms && Sys.WebForms.PageRequestManager) 
           this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();

        this._setElementVisibility(this.get_element(), false); 
            	
    	if (this._pageRequestManager !== null ) 
    	{
            this._pageRequestManager.add_beginRequest(this._beginRequestHandlerDelegate);
    	    this._pageRequestManager.add_endRequest(this._endRequestHandlerDelegate);
    	}
    }
}

BIT.WebControls.UpdateProgressEx.registerClass('BIT.WebControls.UpdateProgressEx', Sys.Component);


BIT.WebControls.UpdateProgressEx._ignoreIds = [];

BIT.WebControls.UpdateProgressEx.AddToIgnore = function(object)
{
	if (!object)
		return;
	
	var id = null;
	
	if (typeof(object) === 'string')
		id = object
	else if (object.get_id)
		id = object.get_id();
	else if (object.id)
		id = object.id;
	
	var ids = BIT.WebControls.UpdateProgressEx._ignoreIds;
	
	if (id)
	{
		Array.remove(ids, id);
		Array.add(ids, id);
	}
	
	ids = null;
}


BIT.WebControls.UpdateProgressEx.RemoveFromIgnore = function(object)
{
	if (!object)
		return;
	
	var id = null;
	
	if (typeof(object) === 'string')
		id = object
	else if (object.get_id)
		id = object.get_id();
	else if (object.id)
		id = object.id;
	
	var ids = BIT.WebControls.UpdateProgressEx._ignoreIds;
	
	if (id)
		Array.remove(ids, id);
	
	ids = null;
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();