﻿/*
WaitCursor support.

Copyright(C) 2007 Active Web Services, LLC.
All Rights Reserved.
*/
Type.registerNamespace('AWS.UI');

AWS.UI.WaitCursor = function AWS$UI$WaitCursor() {
    /// <summary>
    /// The WaitCursor is used to blocking page during async request
    /// </summary>
    AWS.UI.WaitCursor.initializeBase(this);  
    this._pageRequestManager = null;
    this._keypressHandlerDelegate = null;
    this._endRequestHandlerDelegate = null;  
    this._beginRequestHandlerDelegate = null;      
    this._initializeRequestHandlerDelegate = null;
}
AWS.UI.WaitCursor.prototype = {
    /// <summary>
    /// Initialization
    /// </summary>
    initialize : function() {
        AWS.UI.WaitCursor.callBaseMethod(this, 'initialize');    		   	    	
    	if (Sys.WebForms && Sys.WebForms.PageRequestManager) {
            this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
    	    if (this._pageRequestManager !== null ) {
    	        this._endRequestHandlerDelegate = Function.createDelegate(this, this._handleEndRequest);      	
    	        this._beginRequestHandlerDelegate = Function.createDelegate(this, this._handleBeginRequest);
    	        this._initializeRequestHandlerDelegate = Function.createDelegate(this, this._handleInitializeRequest);
    	        this._pageRequestManager.add_endRequest(this._endRequestHandlerDelegate);
                this._pageRequestManager.add_beginRequest(this._beginRequestHandlerDelegate);
    	        this._pageRequestManager.add_initializeRequest(this._initializeRequestHandlerDelegate);
    	    }
    	}       
    }, 
    /// <summary>
    /// Dispose
    /// </summary>      
    dispose : function() {	
    	if (this._keypressHandlerDelegate) {
    	    $removeHandler(document, 'keypress', this._keypressHandlerDelegate); 
    	}  	
        if (this._pageRequestManager !== null) {
            this._pageRequestManager.remove_endRequest(this._endRequestHandlerDelegate);
            this._pageRequestManager.remove_beginRequest(this._beginRequestHandlerDelegate);
            this._pageRequestManager.remove_initializeRequest(this._initializeRequestHandlerDelegate);
        }
        this._keypressHandlerDelegate = null;
        this._endRequestHandlerDelegate = null;
        this._beginRequestHandlerDelegate = null;
        this._initializeRequestHandlerDelegate = null;    
        AWS.UI.WaitCursor.callBaseMethod(this, "dispose");
    },
    
    _isInAsyncPostBack : function() {  
        return this._pageRequestManager.get_isInAsyncPostBack();
    },

    _handleInitializeRequest : function(sender, arg) {
        if (this._isInAsyncPostBack()) { 
            arg.set_cancel(true);
        }
    },
    
    _handleBeginRequest : function(sender, arg) { 
        this._beginWaiting();
    },
    
    _handleEndRequest : function(sender, arg) {
        this._endWaiting();
    },
      
    _beginWaiting : function() {
        this._blockKeyboard();
		var blocker = $get("screenblocker");
		if (!blocker) {
			blocker = document.createElement("span");
		    blocker.style.top = 0;
			blocker.style.left = 0;
			blocker.style.zIndex = 10000;
		    blocker.id = "screenblocker";
			blocker.style.position = "absolute";
			blocker.style.width = document.body.scrollWidth;
			blocker.style.height = document.body.scrollHeight;
			document.body.appendChild(blocker);
		}
		// IE specific
		if ( blocker.setCapture )
		    blocker.setCapture();
		blocker.style.cursor = "wait";
		blocker.style.display = "inline";
	},
	
	_endWaiting : function() {
		var blocker = $get("screenblocker");
		if (blocker) {
			blocker.style.cursor = "";
			blocker.style.display = "none";
			// IE Specific
			if ( blocker.releaseCapture )
			    blocker.releaseCapture();
		}
		this._unblockKeyboard();
	},
	
    _blockKeyboard : function() {
        if (!this._keypressHandlerDelegate) {
            this._keypressHandlerDelegate = Function.createDelegate(this, this._onkeypress);
            $addHandler(document, 'keypress', this._keypressHandlerDelegate);
        }
    },
    
    _unblockKeyboard : function() {
        if (this._keypressHandlerDelegate) {
            $removeHandler(document, 'keypress', this._keypressHandlerDelegate);
            this._keypressHandlerDelegate = null;
        }
    },
    
    _onkeypress : function(evt) {
        evt = evt || window.event;
        evt.cancelBubble = true;
	    evt.cancelBubble = true;
	    evt.returnValue = false;
	    //e.stopPropagation works in Firefox.
	    if (evt.stopPropagation) {
		    evt.stopPropagation();
		    evt.preventDefault();
	    }
	    return false;
    }
}
AWS.UI.WaitCursor.registerClass('AWS.UI.WaitCursor', Sys.Component);


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();