
// Définition de la classe 'MessageBox'
// NB: L'ordre des méthodes est important
MessageBox.Instances = new Array();

MessageBox.Get = function(id)
{
	var box = MessageBox.Instances[id];
	if (box != null)
		return box;
	return new MessageBox(id);
}

function MessageBox(id)
{
	// Outil pour récupérer une instance par son identifiant
	MessageBox.Instances[id] = this;

	var messageMask = document.getElementById(id + "_messageMask");
	var messageText = document.getElementById(id + "_messageText");
	var messageBox = document.getElementById(id + "_messageBox");
	this.OnHide = new Array();

	var GetWindowSize = function()
	{
		var width = 0, height = 0;

		// Non-IE
		if (typeof (window.innerWidth) == 'number')
		{
			width = window.innerWidth;
			height = window.innerHeight;
		}
		// IE 6+ in 'standards compliant mode'
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		}
		// IE 4 compatible
		else if (document.body && document.body.clientWidth)
		{
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}

		return { Width: width, Height: height };
 	}

 	var MoveToRoot = function()
 	{
 		if (messageBox.parentNode.tagName.toLowerCase() != "body")
 		{
 			var parent = messageBox.parentNode;
 			parent.removeChild(messageBox);
 			document.body.appendChild(messageBox);
 			parent.removeChild(messageMask);
 			document.body.appendChild(messageMask);
 		}
 	}

 	this.Show = function(text)
	{
		MoveToRoot();

		Tools_EnableScrollBars(false);

		var windowSize = GetWindowSize();

		if (messageMask != null)
		{
			messageMask.style.width = windowSize.Width + "px";
			messageMask.style.height = windowSize.Height + "px";
			messageMask.style.display = "block";
		}

		if (messageText != null)
			messageText.innerHTML = text;

		if (messageBox != null)
		{
			messageBox.style.display = "block";
		 	messageBox.style.top = ((windowSize.Height / 2 - messageBox.offsetHeight)) + "px";
		 	messageBox.style.left = ((windowSize.Width - messageBox.offsetWidth) / 2) + "px";
		}
	}

	this.Hide = function()
	{
		Tools_EnableScrollBars(true);

		if (messageMask != null)
			messageMask.style.display = "none";

		if (messageBox != null)
			messageBox.style.display = "none";

		Tools_DispatchEvent(this.OnHide, this, null);
	}
}

