function Popup(idOrParentElement)
{
	if (typeof(idOrParentElement) === "string")
	{
		this.ID = id;
	}
	else
	{
		this.ParentElement = idOrParentElement;
	}

	this.Show = function()
	{
		Popup.HideAll();
		
		if (this.ParentElement)
		{
			System.ClassList.Add(this.ParentElement, "uwt-visible");
		}
		else
		{
			var obj = document.getElementById("Popup_" + this.ID);
			obj.classList.add("Visible");
		}
	};
	this.Hide = function()
	{
		if (this.ParentElement)
		{
			System.ClassList.Remove(this.ParentElement, "uwt-visible");
		}
		else
		{
			var obj = document.getElementById("Popup_" + this.ID);
			obj.classList.remove("Visible");
		}
	};
}
Popup.HideAll = function()
{
	var elems = document.getElementsByClassName("uwt-popup");
	for (var i = 0; i < elems.length; i++)
	{
		if (!System.ClassList.Contains(elems[i], "uwt-visible-always"))
		{
			System.ClassList.Remove(elems[i], "uwt-visible");
		}
	}
};
Popup.create = function(container)
{
	console.log(container);

	var parentElement = document.createElement("div");
	parentElement.className = "uwt-popup";

	container.appendChild(parentElement);

	var popup = new Popup(parentElement);
	return popup;
};

window.addEventListener("mousedown", function(e)
{
	var sender;
	if (!e)
	{
		e = window.event;
	}
	if (e.target)
	{
		sender = e.target;
	}
	else if (e.srcElement)
	{
		sender = e.srcElement;
	}
	
	while (sender != null)
	{
		if (System.ClassList.Contains(sender, "Popup"))
		{
			// do not close the popup when we click inside itself
			e.preventDefault();
			e.stopPropagation();
			return;
		}
		sender = sender.parentNode;
		if (sender == null) break;
	}
	Popup.HideAll();
});