
ModalDialog = function(dialogBackgroundElementID, modalDialogElementID, dialogWidth, dialogHeight)
{
    this._dialogBackgroundElementID = dialogBackgroundElementID;
    this._modalDialogElementID = modalDialogElementID;
    this._dialogWidth = dialogWidth;
    this._dialogHeight = dialogHeight;
    this._onScrollEventHandler = null;
    this._onResizeEventHandler = null;
}

ModalDialog.prototype = 
{
	initialize : function()
	{
		this._initDialog();
	},

	dispose : function()
	{
	},
	
	toggleVisibility : function()
	{
		if (this.isVisible())
		{
			this.hide();
		}
		else
		{
			this.show();
		}
	},

	show : function()
	{
		this._layout();
	
		$(this._modalDialogElementID).style.display = 'block';
		$(this._dialogBackgroundElementID).style.display = 'block';
		
		this._attachEventHandlers();
	},

	hide : function()
	{
		this._detachEventHandlers();
	
		$(this._modalDialogElementID).hide();
		$(this._dialogBackgroundElementID).hide();
	},

	isVisible : function()
	{
		return ($(this._modalDialogElementID).visible());
	},

	_onScroll : function()
	{
		this._layout();
	},

	_onResize : function()
	{
		this._layout();
	},
	
	_attachEventHandlers : function()
	{
		if (this._onScrollEventHandler == null)
		{
			this._onScrollEventHandler = this._onScroll.bind(this);
			Event.observe(window, 'scroll', this._onScrollEventHandler);
		}

		if (this._onResizeEventHandler == null)
		{
			this._onResizeEventHandler = this._onResize.bind(this);
			Event.observe(window, 'resize', this._onResizeEventHandler);
		}
	},
	
	_detachEventHandlers : function()
	{
		if (this._onScrollEventHandler != null)
		{
			Event.stopObserving(window, 'scroll', this._onScrollEventHandler);
			this._onScrollEventHandler = null;
		}

		if (this._onResizeEventHandler != null)
		{
			Event.stopObserving(window, 'resize', this._onResizeEventHandler);
			this._onResizeEventHandler = null;
		}
	},

	_initDialog : function()
	{
		var modalSubmitDialog = $(this._modalDialogElementID);
		var dialogBackground = $(this._dialogBackgroundElementID);

		dialogBackground.hide();
		dialogBackground.style.position = 'absolute';
		dialogBackground.style.zIndex = 100000;

		modalSubmitDialog.hide();
		modalSubmitDialog.style.position = 'absolute';
		modalSubmitDialog.style.zIndex = dialogBackground.style.zIndex + 1;
	},

	_layout : function()
	{
		var modalSubmitDialog = $(this._modalDialogElementID);
		var dialogBackground = $(this._dialogBackgroundElementID);

		var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
		var clientWidth;
		var clientHeight;
	    
		if (window.innerWidth)
		{
			clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
		}
		else
		{
			clientWidth = document.documentElement.clientWidth;
		}

		if (window.innerHeight)
		{
			clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
		}
		else
		{
			clientHeight = document.documentElement.clientHeight;
		}

		dialogBackground.style.left = scrollLeft+'px';
		dialogBackground.style.top = scrollTop+'px';
		dialogBackground.style.width = clientWidth+'px';
		dialogBackground.style.height = clientHeight+'px';
	    
		modalSubmitDialog.style.left = scrollLeft + ((clientWidth / 2)-(this._dialogWidth / 2)) + 'px';
		modalSubmitDialog.style.top = scrollTop + ((clientHeight / 2) - (this._dialogHeight / 2)) + 'px';
	}
	
}
