﻿Type.registerNamespace("MyLib.Script.UI");

/* ### Window Element */
MyLib.Script.UI.Window = function(element)
{
	MyLib.Script.UI.Window.initializeBase(this,[element]);		

	// Properties
	this._MakeFormChildren = false;
	this._Width = null;
	this._Height = null;	
	this._ContainerControlID = null;
	this._DragHandleControlID = null;	
	this._HideHandleControlID = null;
	this._ShowHandleControlID = null;
	this._VisibilityControlID = null;
	
	// Variables
	this._dragBehavior = null;
	this._showHandler = null;
	this._hideHandler = null;
	this._beginRequestHandler = null;
	this._pageLoadingHandler = null;
	this._pageLoadedHandler = null;	
	this._mouseDownHandler = null;
}

MyLib.Script.UI.Window.prototype = 
{	
	// regista-se no WindowManager, para gest�o de zIndex
	initialize: function()
	{
		// adicionar-se ao gestor de janelas
		MyLib.Script.UI.WindowManager.addWindow(this);	
		
		var targetElement = null;
		
		// re-alocar o elemento nota: se estiver contido num update panel pode falhar
		if( this._MakeFormChildren == true )	
		{
			targetElement = document.getElementsByTagName('form')[0];
			targetElement.appendChild(this.get_element());
		}
		
		// evocar base	
		MyLib.Script.UI.Window.callBaseMethod(this,'initialize');
		
		// registar os eventos pageLoading e pageLoaded
		this._beginRequestHandler = Function.createDelegate(this,this._beforeRequest);
		this._pageLoadingHandler = Function.createDelegate(this,this._beforeUpdate);
		this._pageLoadedHandler = Function.createDelegate(this,this._afterUpdate);
		this._mouseDownHandler = Function.createDelegate(this, this._mouseDown)
		
		Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(this._beginRequestHandler);
		Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(this._pageLoadingHandler);
		Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(this._pageLoadedHandler);
	},	
	dispose: function() 
	{	
		// remove behaviors			
		this._dettachDragBehavior();
			
		// remove from WindowManager
		MyLib.Script.UI.WindowManager.removeWindow(this);	
		
		// remove from PageRequestManager
		Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(this._beginRequestHandler);
		Sys.WebForms.PageRequestManager.getInstance().remove_pageLoading(this._pageLoadingHandler);
		Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(this._pageLoadedHandler);
		
		// le dispose!
		MyLib.Script.UI.Window.callBaseMethod(this,'dispose');
	},

	// show behavior
	_attachShowBehavior: function()
	{
		/// <summary>
		/// Attach the show behavior for the window
		/// </summary>
		
		if( this._ShowHandleControlID == null )
			return;
		
		var showHandleElement = $get(this._ShowHandleControlID);
		
		if ( showHandleElement )
		{
			this._showHandler = Function.createDelegate(this,this.show);
			$addHandler(showHandleElement, 'click', this._showHandler);
		}
	},
	_dettachShowBehavior: function()
	{
		if( this._ShowHandleControlID == null )
			return;
		
		var showHandleElement = $get(this._ShowHandleControlID);
		
		if ( showHandleElement )
			$removeHandler(showHandleElement,'click', this._showHandler);
	},

	// hide behavior
	_attachHideBehavior: function()
	{
		/// <summary>
		/// Attach the hide behavior for the window
		/// </summary>
		
		if( this._HideHandleControlID == null )
			return;
		
		var hideHandleElement = $get(this._HideHandleControlID);
		
		if ( hideHandleElement )
		{
			this._hideHandler = Function.createDelegate(this,this.hide);
			$addHandler(hideHandleElement, 'click', this._hideHandler);
		}
	},
	_dettachHideBehavior: function()
	{
		if( this._HideHandleControlID == null )
			return;
		
		var hideHandleElement = $get(this._HideHandleControlID);
		
		if ( hideHandleElement )
			$removeHandler(hideHandleElement,'click', this._hideHandler);
	},

	// drag behavior
	_attachDragBehavior: function()
	{
		/// <summary>
		/// Attach the drag behavior for the window
		/// </summary>			
		if( this._DragHandleControlID == null )
			return;
		
		var dragHandleElement = $get(this._DragHandleControlID);
		
		if ( dragHandleElement && !this._dragBehavior)
		{
			
			$addHandler( dragHandleElement, "mousedown", this._mouseDownHandler);
			this._dragBehavior = $create(AjaxControlToolkit.FloatingBehavior, {"handle" : dragHandleElement}, null, null, this.get_element());
		}
	},
	_dettachDragBehavior: function()
	{
		/// <summary>
		/// Dettach the drag behavior for the window
		/// </summary>
		if (this._dragBehavior) 
		{
			var dragHandleElement = $get(this._DragHandleControlID);
			if ( dragHandleElement )
				$removeHandler( dragHandleElement, "mousedown", this._mouseDownHandler);
			this._dragBehavior.dispose();
			this._dragBehavior = null;
		}
	},

	// partial page update
	_beforeRequest: function(sender,args)
	{
		// to persist or not the visibility during partial page update
		if( this._persistVisibility() )
			this._set_Visibility( this.isHidden() == 1 ? 0:1 ); 			
	},
	_beforeUpdate: function(sender,args)
	{
		// de-attach behaviors before update
		this._dettachShowBehavior();
		this._dettachHideBehavior();
		this._dettachDragBehavior();
	},
	_afterUpdate: function(sender,args)
	{
		// is partial page update?
		if( Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack() )
		{
			// attcha after updatre
			this._attachShowBehavior();
			this._attachHideBehavior();
			
			// load persisted visibility?
			if( this._persistVisibility() )
			{
				if( this._get_Visibility() )
				{						
					if( this.isHidden() )
						this.show();
					else
						this._attachDragBehavior();
				}
				else
				{
					if( !this.isHidden() )
						this.hide();
				}
			}
			else
				this._attachDragBehavior();
		}	
	},

	// public
	hide: function()
	{
		this.get_element().style.display = 'none';
		this._dettachDragBehavior();
		
		// to persist or not the visibility
		if( this._persistVisibility() )
			this._set_Visibility(0);
	},
	
	show: function()
	{
		if( this.isHidden() )
		{
			this._attachDragBehavior();
			this._setSize(); // twice because of firefox and safari
			this._setSize();
			MyLib.Script.UI.WindowManager.showInCenter(this.get_element());
		}					
		
		// to persist or not the visibility during partial page update
		if( this._persistVisibility() )
			this._set_Visibility(1);
			
		MyLib.Script.UI.WindowManager.moveToTop(this);
	},
	
	_mouseDown: function(ev)
	{
		MyLib.Script.UI.WindowManager.moveToTop(this);
	},
	
	isHidden: function()
	{
		return this.get_element().style.display == 'none';
	},	

	_setSize: function()
	{
		var windowElement = this.get_element();
		var containerElement = this._ContainerControlID == null ? null : $get(this._ContainerControlID);
	
		if( this._Width != null )
		{
			windowElement.style.width = this._Width+'px';			
				
			if( containerElement != null )			
				containerElement.style.width = this._Width+'px';
		}
		
		if( this._Height != null )
		{
			windowElement.style.height = this._Height+'px';			
		
			if( containerElement != null )
				containerElement.style.height = this._Height+'px';
		}		
	},
			
	// *** Properties ****
	
	// Visibility
	_get_Visibility: function()
	{
		return this._VisibilityControlID == null ? true: $get(this._VisibilityControlID).value == 1;
	},
	_set_Visibility: function(value)
	{
		if( this._VisibilityControlID != null )
			$get(this._VisibilityControlID).value = (value == 1 ? 1:0);
	},	
	_persistVisibility: function()
	{
		return this._VisibilityControlID != null;
	},

	// MakeFormChildren
	get_MakeFormChildren: function()
	{
		return this._MakeFormChildren;
	},
	set_MakeFormChildren: function(value)
	{
		this._MakeFormChildren = value;
	},	
		
	// Width
	get_Width: function()
	{
		return this._Width;
	},
	set_Width: function(value)
	{
		this._Width = value;
	},
	
	// Height
	get_Height: function()
	{
		return this._Height;
	},
	set_Height: function(value)
	{
		this._Height = value;
	},	

	// ContainerControlID
	get_ContainerControlID: function()
	{
		return this._ContainerControlID;
	},
	set_ContainerControlID: function(value)
	{
		this._ContainerControlID = value;
	},

	// DragHandleControlID
	get_DragHandleControlID: function()
	{
		return this._DragHandleControlID;
	},
	set_DragHandleControlID: function(value)
	{
		this._DragHandleControlID = value;
	},
	
	// HideHandleControlID
	get_HideHandleControlID: function()
	{
		return this._HideHandleControlID;
	},
	set_HideHandleControlID: function(value)
	{
		this._HideHandleControlID = value;
		this._attachHideBehavior();
	},
	
	// ShowHandleControlID
	get_ShowHandleControlID: function()
	{
		return this._ShowHandleControlID;
	},
	set_ShowHandleControlID: function(value)
	{
		this._ShowHandleControlID = value;
		this._attachShowBehavior();
	},
	
	// VisibilityControlID
	get_VisibilityControlID: function()
	{
		return this._VisibilityControlID;
	},
	set_VisibilityControlID: function(value)
	{
		this._VisibilityControlID = value;
	}
}

MyLib.Script.UI.Window.registerClass('MyLib.Script.UI.Window',Sys.UI.Control);

/* ### Elementos "Window" t�m o zIndex -par-, os valores -impar- s�o para os div's para simular janelas modais ### */
MyLib.Script.UI._WindowManager = function()
{
	this._zIndex = 5000;
	this._windows = new Array();
}

MyLib.Script.UI._WindowManager.prototype =
{
	// adiciona janelas para gest�o do zIndex
	addWindow: function(window)
	{
		this._windows.push(window);
		window.get_element().style.zIndex = this._zIndex + (this._windows.length*2);
	},
	removeWindow: function(window)
	{
		Array.remove(this._windows,window);
		this._reOrder();
	},
	getWindow: function(id)
	{
		var pos;
		var windowCount = this._windows.length;
		
		for(pos=0;pos<windowCount;pos++)
			if( this._windows[pos].get_element().id == id )
				return this._windows[pos];
				
		return null;
	},	
	// coloca a dada janela no topo e decrementa as restantes
	moveToTop: function(window)
	{	
		var topo;
		var tmp;
		var index=null;
		var windowCount = this._windows.length;
		
		// obter indice da janela na pilha
		for(pos=0;pos<windowCount&&index==null;pos++)
			if( this._windows[pos].get_element().id == window.get_element().id )
				index = pos;
		
		// existe na pilha?
		if( index == null )
			alert(window.get_element().id + " is not registered in WindowManager");
		
		// est� em primeiro? ent�o n�o � nada a fazer
		if( index == windowCount-1 )
			return;
		
		// re-ordenar a pilha
		tmp = this._windows[index];
		for(pos=index;pos<windowCount-1;pos++)
			this._windows[pos] = this._windows[pos+1];
		this._windows[windowCount-1] = tmp;
		
		this._reOrder();
	},
	// reordena os indices
	_reOrder: function()
	{
		var window;
		var windowCount = this._windows.length;
		
		for(pos=0;pos<windowCount;pos++)
		{
			window = this._windows[pos]
			window.get_element().style.zIndex = this._zIndex + (2*pos);
		}
	},
	// centra um elemento e mostra-o se for caso disso
	showInCenter: function(domElement)
	{						
		if (document.documentElement && document.documentElement.scrollTop) 
		{
			 x = document.documentElement.scrollLeft;
			 y = document.documentElement.scrollTop;
		} 
		else 
		{
			 x = document.body.scrollLeft;
			 y = document.body.scrollTop;
		}   
					
		var bodySize = CommonToolkitScripts.getClientBounds();
		var contSize = Sys.UI.DomElement.getBounds(domElement);				

		if( domElement.style.display == 'none' )
		{
			domElement.style.visibility = 'hidden';
			domElement.style.display = '';		
			contSize = Sys.UI.DomElement.getBounds(domElement);
			domElement.style.visibility = 'visible';
		}
		
		Sys.UI.DomElement.setLocation(domElement,Math.floor((bodySize.width-contSize.width)/2)+x,Math.floor((bodySize.height-contSize.height)/2)+y);		
	},
	// n�o faz nada, apenas usada para hiperliga��es sem href
	doEvents: function()
	{
	}
}

MyLib.Script.UI._WindowManager.registerClass('MyLib.Script.UI._WindowManager');

MyLib.Script.UI.WindowManager = new MyLib.Script.UI._WindowManager();

var $doEvents = MyLib.Script.UI.WindowManager.doEvents;
var $getWindow = Function.createDelegate(MyLib.Script.UI.WindowManager,MyLib.Script.UI.WindowManager.getWindow);

if (typeof(Sys) !== "undefined") 
	Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();