function Menu(parentElement)
{
	this.ParentElement = parentElement;
	this.set_Expanded = function(value)
	{
		switch (value)
		{
			case true:
			{
				System.ClassList.Add(this.ParentElement.parentNode, "Opened");
				break;
			}
			case false:
			{
				System.ClassList.Remove(this.ParentElement.parentNode, "Opened");
				break;
			}
		}
	};
	
	for (var i = 0; i < this.ParentElement.children.length; i++)
	{
		this.ParentElement.children[i].children[0].addEventListener("click", function(e)
		{
			if (this.parentNode.children.length > 1)
			{
				System.ClassList.Toggle(this.parentNode, "Opened");
			}
			
			this.blur();
			
			if (this.href == "" || this.href == window.location.href + "#")
			{
				e.preventDefault();
				e.stopPropagation();
				return false;
			}
		});
	}
}

window.addEventListener("load", function(e)
{
	var items = document.getElementsByClassName("uwt-menu");
	for (var i = 0; i < items.length; i++)
	{
		if (items[i].NativeObject) continue;
		items[i].NativeObject = new Menu(items[i]);
	}
});

function ContextMenu()
{
	this.Items = [];
	this.ParentElement = null;
	this.Show = function(x, y, parent)
	{
		ContextMenu.HideAll();
		if (this.ParentElement == null)
		{
			var elem = document.createElement("ul");
			elem.className = "uwt-menu uwt-popup";
			elem.addEventListener("contextmenu", function(e)
			{
				e.preventDefault();
				e.stopPropagation();
				return false;
			});
			elem.addEventListener("transitionend", function(e)
			{
				if (!System.ClassList.Contains(this, "uwt-visible"))
				{
					System.ClassList.Remove(document.body, "uwt-prevent-scrolling");
					this.remove();
				}
			});
			
			for (var i = 0; i < this.Items.length; i++)
			{
				this.Items[i].Menu = this;
				
				var li = document.createElement("li");
				System.ClassList.Add(li, "uwt-menu-item");

				var visible = false;
				if (typeof(this.Items[i].Visible) === "function")
				{
					visible = this.Items[i].Visible();
				}
				else if (typeof(this.Items[i].Visible) === "undefined")
				{
					visible = false;
				}
				else
				{
					visible = this.Items[i].Visible;
				}

				if (visible)
				{
					System.ClassList.Add(li, "uwt-visible");
				}
				else
				{
					System.ClassList.Remove(li, "uwt-visible");
				}
				
				if (this.Items[i].ClassName == "MenuItemCommand")
				{
					var elem1 = document.createElement("a");
					if (this.Items[i].TargetURL)
					{
						var targetUrl = this.Items[i].TargetURL;
						if (typeof(targetUrl) === 'function')
						{
							targetUrl = targetUrl();
						}
						elem1.setAttribute("href", targetUrl);
					}
					else
					{
						elem1.setAttribute("href", "#");
						elem1.addEventListener("click", function(e)
						{
							this.NativeObject.Hide();
							this.MenuItem.Execute();
							
							e.preventDefault();
							e.stopPropagation();
							return false;
						});
					}
					elem1.innerHTML = this.Items[i].Title;
					elem1.NativeObject = this;
					elem1.MenuItem = this.Items[i];
					li.appendChild(elem1);
					
					System.ClassList.Add(li, "uwt-menu-item-command");
				}
				else if (this.Items[i].ClassName == "MenuItemSeparator")
				{
					System.ClassList.Add(li, "uwt-separator");
				}
				else if (this.Items[i].ClassName == "MenuItemHeader")
				{
					System.ClassList.Add(li, "uwt-menu-item-header");
					li.innerHTML = this.Items[i].Title;
				}
				elem.appendChild(li);
			}
			
			elem.style.left = x + "px";
			elem.style.top = y + "px";
			
			if (parent == null) parent = document.body;
			parent.appendChild(elem);
			this.ParentElement = elem;
			System.ClassList.Add(document.body, "uwt-prevent-scrolling");
		}
		this.ParentElement.className = "uwt-menu uwt-popup";
		
		this.ParentElement.offsetWidth = this.ParentElement.offsetWidth; // thanks https://stackoverflow.com/a/24195559
		System.ClassList.Add(this.ParentElement, "uwt-visible");
		System.EnsureElementVisible(this.ParentElement, null);
	};
	this.Hide = function()
	{
		if (this.ParentElement == null) return;
		this.ParentElement.className = "uwt-menu uwt-popup";
	};
}

ContextMenu.HideAll = function()
{
	var items = document.getElementsByClassName("uwt-menu");
	for (var i = 0; i < items.length; i++)
	{
		if (System.ClassList.Contains(items[i], "uwt-popup"))
		{
			if (!System.ClassList.Contains(items[i], "uwt-visible-always"))
			{
				System.ClassList.Remove(items[i], "uwt-visible");
			}
		}
	}
};

function MenuItemHeader(id, title)
{
	this.ClassName = "MenuItemHeader";
	this.ID = id;
	this.Title = title;
	this.Visible = true;
}
function MenuItemSeparator(id)
{
	this.ClassName = "MenuItemSeparator";
	this.ID = id;
	this.Visible = true;
}
function MenuItemCommand(id, title, onclick)
{
	this.ClassName = "MenuItemCommand";
	this.ID = id;
	this.Title = title;
	this.OnClientClick = onclick;
	this.Visible = true;
	
	this.Execute = function()
	{
		if (this.OnClientClick != null) this.OnClientClick();
	};
}

window.addEventListener("contextmenu", function()
{
	ContextMenu.HideAll();
});
window.addEventListener("mousedown", function()
{
	ContextMenu.HideAll();
});