﻿Type.registerNamespace("MyLib.Script.UI");

/* ### ProgressMousePointer Element */
MyLib.Script.UI.ProgressMousePointer = function(element)
{
	MyLib.Script.UI.ProgressMousePointer.initializeBase(this,[element]);		

	// Properties
	this._OffsetX = 0;
	this._OffsetY = 0;	

	// Variables
	this._beginRequestHandler = null;
	this._endRequestHandler = null;
	this._mouseMoveHandler = null;
	
	this._mouseX = null;
	this._mouseY = null;	
}

MyLib.Script.UI.ProgressMousePointer.prototype = 
{	
	// regista-se no WindowManager, para gest�o de zIndex
	initialize: function()
	{		
		// evocar base	
		MyLib.Script.UI.ProgressMousePointer.callBaseMethod(this,'initialize');
		
		// registar os eventos pageLoading e pageLoaded
		this._beginRequestHandler = Function.createDelegate(this,this._beginRequest);
		this._endRequestHandler = Function.createDelegate(this,this._endRequest);
		
		Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(this._beginRequestHandler);
		Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this._endRequestHandler);
		
		Sys.Net.WebRequestManager.add_completedRequest(this._endRequestHandler);
		Sys.Net.WebRequestManager.add_invokingRequest(this._beginRequestHandler);
		
		this._attachMoveBehavior();	
		
		this.get_element().style.zIndex = 50000;
	},	
	dispose: function() 
	{	
		// hide just in case
		this._refresh(false);
		
		// remove behaviors			
		this._dettachMoveBehavior();
					
		// remove from PageRequestManager
		Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(this._beginRequestHandler);
		Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(this._endRequestHandler);
		
		// le dispose!
		MyLib.Script.UI.ProgressMousePointer.callBaseMethod(this,'dispose');
	},

	// move behavior
	_attachMoveBehavior: function()
	{
		if( this._mouseMoveHandler == null )
			this._mouseMoveHandler = Function.createDelegate(this,this._mouseMove);
			
		$addHandler(document, 'mousemove', this._mouseMoveHandler);
	},
	_dettachMoveBehavior: function()
	{
		if( this._mouseMoveHandler == null )
			return;
			
		$removeHandler(document, 'mousemove', this._mouseMoveHandler);
		this._mouseMoveHandler = null;
	},

	// partial page update
	_beginRequest: function(sender,args)
	{	
		this._refresh(true);
	},
	_endRequest: function(sender,args)
	{
		this._refresh(false);
	},
	
	// events
	_mouseMove: function(ev)
	{
		this._mouseX = ev.clientX;
		this._mouseY = ev.clientY;
		this._refresh(null);
	},
	
	// private methods
	_refresh: function(show)
	{
		var x;
		var y;
		
		var domElement = this.get_element();
		
		// no forced refresh and element is hidden
		if( show == null && domElement.style.display == 'none' )
			return;
		
		// hide element
		if( show == false )
		{
			if( domElement.style.display == 'block' )
				domElement.style.display = 'none';
			return;
		}
		
		// show element
		if (document.documentElement && document.documentElement.scrollTop) 
		{
			 x = document.documentElement.scrollLeft;
			 y = document.documentElement.scrollTop;
		} 
		else 
		{
			 x = document.body.scrollLeft;
			 y = document.body.scrollTop;
		} 
	
		Sys.UI.DomElement.setLocation(domElement,(x+this._mouseX)+this._OffsetX,(y+this._mouseY)+this._OffsetY);				
	
		if( domElement.style.display == 'none' )
			domElement.style.display = 'block';	
	},
	
	// *** Properties ****
	
	// OffsetX
	get_OffsetX: function()
	{
		return this._OffsetX;
	},
	set_OffsetX: function(value)
	{
		this._OffsetX = value;
	},	
		
	// OffsetY
	get_OffsetY: function()
	{
		return this._OffsetY;
	},
	set_OffsetY: function(value)
	{
		this._OffsetY = value;
	}
}

MyLib.Script.UI.ProgressMousePointer.registerClass('MyLib.Script.UI.ProgressMousePointer',Sys.UI.Control);

if (typeof(Sys) !== "undefined") 
	Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();