var Dialog =
{
	_initialized: false,
	_dialogElement: null,

	show: function(dialogElement)
	{
		if (!this._initialized) this._init();
		if (typeof dialogElement == 'string') dialogElement = document.getElementById(dialogElement);
		this._dialogElement = dialogElement;
		
		Dialog.Overlay.show();
		this._dialogElement.style.display = 'block';
		this._dialogElement.style.position = 'absolute';
		this._dialogElement.style.zIndex = 1000;
		this._setElementPosition();
	},

	hide: function()
	{
		if (this._dialogElement == null) return;
		this._dialogElement.style.display = 'none';
		this._dialogElement = null;
		Dialog.Overlay.hide();
	},

	_init: function()
	{
		var thisRef = this;
		Dialog.Event.attachHandler(window, 'resize', function() { thisRef._onWindowResize(); });
		this._initialized = true;
	},

	_onWindowResize: function()
	{
		this._setElementPosition();
	},

	_setElementPosition: function()
	{
		if (this._dialogElement == null) return;
		this._dialogElement.style.top = ((Dialog.Viewport.getHeight() - this._dialogElement.offsetHeight) / 2) + 'px';
		this._dialogElement.style.left = ((Dialog.Viewport.getWidth() - this._dialogElement.offsetWidth) / 2) + 'px';
	}
}

Dialog.Overlay =
{
	_div: null,
	_initialized: false,
	_bodyOverflow: null,
	_documentElementOverflow: null,
	color: 'black',
	opacity: .7,
	zIndex: 100,

	show: function()
	{
		if (!this._initialized) this._init();
		this._disableScrolling();
		this._setProperties();
		this._div.style.display = 'block';
	},

	hide: function()
	{
		if (!this._initialized) return;
		this._enableScrolling();
		this._div.style.display = 'none';
	},

	_init: function()
	{
		var thisRef = this;
		this._createDiv();
		Dialog.Event.attachHandler(window, 'resize', function() { thisRef._onWindowResize(); });
		this._initialized = true;
	},

	_createDiv: function()
	{
		this._div = document.createElement('div');
		this._div.style.display = 'none';
		this._div.style.position = 'absolute';
		this._div.style.backgroundColor = this.color;
		this._setProperties();
		document.body.appendChild(this._div);
	},

	_setProperties: function()
	{
		this._setPosition();
		this._setDimensions();
		this._div.style.opacity = this.opacity;
		this._div.style.filter = 'alpha(opacity=' + (100 * this.opacity) + ')';
		this._div.style.zIndex = this.zIndex;
	},

	_disableScrolling: function()
	{
		this._bodyOverflow = document.body.style.overflow;
		document.body.style.overflow = 'hidden';
		if (document.documentElement)
		{
			this._documentElementOverflow = document.documentElement.style.overflow;
			document.documentElement.style.overflow = 'hidden';
		}
	},

	_enableScrolling: function()
	{
		document.body.style.overflow = this._bodyOverflow;
		if (document.documentElement)
		{
			document.documentElement.style.overflow = this._documentElementOverflow;
		}
	},

	_onWindowResize: function()
	{
		this._setPosition();
		this._setDimensions();
	},

	_setPosition: function()
	{
		if (document.documentElement)
		{
			this._div.style.top = document.documentElement.scrollTop + 'px';
			this._div.style.left = document.documentElement.scrollLeft + 'px';
		}
	},

	_setDimensions: function()
	{
		this._div.style.width = Dialog.Viewport.getWidth() + 'px';
		this._div.style.height = Dialog.Viewport.getHeight() + 'px';
	}
}

Dialog.Viewport =
{
	getWidth: function()
	{
		if (window.innerWidth)
		{
			return window.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientWidth > 0)
		{
			return document.documentElement.clientWidth;
		}
		else if (document.body.clientWidth)
		{
			return document.body.clientWidth;
		}

		return null;
	},

	getHeight: function()
	{
		if (window.innerHeight)
		{
			return window.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientHeight > 0)
		{
			return document.documentElement.clientHeight;
		}
		else if (document.body.clientHeight)
		{
			return document.body.clientHeight;
		}

		return null;
	}
}

Dialog.Event =
{
	attachHandler: function(obj, event, method)
	{
		if (obj.addEventListener)
			obj.addEventListener(event, method, true);
		else if (obj.attachEvent)
			obj.attachEvent('on' + event, method);
	}
}
