Type.registerNamespace('Wait');


Wait.WaitBehavior = function(element) {

    Wait.WaitBehavior.initializeBase(this, [element]);


    this._pageRequestManager = null;
    this._partialUpdateBeginRequestHandler = null;
    this._partialUpdateEndRequestHandler = null;
    this._loadingTextValue = null;
    this._loadingCssClassValue = null;
    this._oldCss = null;
    this._postbackElement = null;
    this._backgroundElement = null;
    this._foregroundElement = null;
    this._resizeHandler = null;
    this._doShow = null;

}



Wait.WaitBehavior.prototype = {

    initialize : function() {
        Wait.WaitBehavior.callBaseMethod(this, 'initialize');
        
        this._foregroundElement = this.get_element();        
        this._backgroundElement = document.createElement('div');
        this._backgroundElement.style.display = 'none';
        this._backgroundElement.style.position = 'absolute';
        this._backgroundElement.style.left = '0px';
        this._backgroundElement.style.top = '0px';
        this._backgroundElement.style.zIndex = 10000;
        if (this._loadingCssClassValue) {
            this._backgroundElement.className = this._loadingCssClassValue;
        }
        this._foregroundElement.parentNode.appendChild(this._backgroundElement);

        this._foregroundElement.style.display = 'none';
        this._foregroundElement.style.position = 'absolute';
        this._foregroundElement.style.zIndex = this.getCurrentStyle(this._backgroundElement, 'zIndex', this._backgroundElement.style.zIndex) + 1;
        this._resizeHandler = Function.createDelegate(this, this._onLayout);
        $addHandler(window, 'resize', this._resizeHandler);

        this.registerPartialUpdateEvents();
        
    },
    
        getCurrentStyle : function(element, attribute, defaultValue) {

        var currentValue = null;
        if (element) {
            if (element.currentStyle) {
                currentValue = element.currentStyle[attribute];
            } else if (document.defaultView && document.defaultView.getComputedStyle) {
                var style = document.defaultView.getComputedStyle(element, null);
                if (style) {
                    currentValue = style[attribute];
                }
            }
            
            if (!currentValue && element.style.getPropertyValue) {
                currentValue = element.style.getPropertyValue(attribute);
            }
            else if (!currentValue && element.style.getAttribute) {
                currentValue = element.style.getAttribute(attribute);
            }       
        }
        
        if ((!currentValue || currentValue == "" || typeof(currentValue) === 'undefined')) {
            if (typeof(defaultValue) != 'undefined') {
                currentValue = defaultValue;
            }
            else {
                currentValue = null;
            }
        }   
        return currentValue;  
    },

    
    registerPartialUpdateEvents : function() {

        if (Sys && Sys.WebForms && Sys.WebForms.PageRequestManager){
            this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
            if (this._pageRequestManager) {
                this._partialUpdateBeginRequestHandler = Function.createDelegate(this, this._partialUpdateBeginRequest);
                this._pageRequestManager.add_beginRequest(this._partialUpdateBeginRequestHandler);
                this._partialUpdateEndRequestHandler = Function.createDelegate(this, this._partialUpdateEndRequest);
                this._pageRequestManager.add_endRequest(this._partialUpdateEndRequestHandler);
            }
        }
    },
    
        getClientBounds : function() {


        var clientWidth;
        var clientHeight;
        switch(Sys.Browser.agent) {
            case Sys.Browser.InternetExplorer:
                clientWidth = document.documentElement.clientWidth;
                clientHeight = document.documentElement.clientHeight;
                break;
            case Sys.Browser.Safari:
                clientWidth = window.innerWidth;
                clientHeight = window.innerHeight;
                break;
            case Sys.Browser.Opera:
                clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
                clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
                break;
            default:  // Sys.Browser.Firefox, etc.
                clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
                clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
                break;
        }
        return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
    },
    
        _onLayout : function() {
        this._layout();
        
    },
      _layout:function() {
        var clientBounds = this.getClientBounds();
        var clientWidth = clientBounds.width;
        var clientHeight = clientBounds.height;
        this._backgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth)+'px';
        this._backgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight)+'px';
      
      },
    
      waiting:function() {
          if (this._doWaiting == true) {
             this._layout();
             this._backgroundElement.style.display = '';
             this._foregroundElement.style.display = 'block';
          }

      },
        
        _partialUpdateBeginRequest : function(sender, beginRequestEventArgs) {
          this._doWaiting = true;
          var self = this; 
          setTimeout(function(){ 
                self.waiting(); 
          }, 3000); 

    },
    _partialUpdateEndRequest : function(sender, endRequestEventArgs) {

        this._doWaiting = false;
        this._backgroundElement.style.display = 'none';
        this._foregroundElement.style.display = 'none';
        
    },


        

    dispose : function() {
        Wait.WaitBehavior.callBaseMethod(this, 'dispose');
        if (this._pageRequestManager) {
            if (this._partialUpdateBeginRequestHandler) {
                this._pageRequestManager.remove_beginRequest(this._partialUpdateBeginRequestHandler);
                this._partialUpdateBeginRequestHandler = null;
            }
            if (this._partialUpdateEndRequestHandler) {
                this._pageRequestManager.remove_endRequest(this._partialUpdateEndRequestHandler);
                this._partialUpdateEndRequestHandler = null;
            }
            this._pageRequestManager = null;
        }
        if (this._resizeHandler) {
            $removeHandler(window, 'resize', this._resizeHandler);
            this._resizeHandler = null;
        }
    },
    

    
    get_LoadingText : function() {
        return this._loadingTextValue;
    },
    set_LoadingText : function(value) {
        this._loadingTextValue = value;
    },
    get_LoadingCssClass : function() {
        return this._loadingCssClassValue;
    },

    set_LoadingCssClass : function(value) {
        this._loadingCssClassValue = value;
    }
}

Wait.WaitBehavior.registerClass('Wait.WaitBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();