function McxMoniker(parentElement)
{
	this.ParentElement = parentElement;
	this.LabelElement = this.ParentElement.children[0];
	this.ButtonElement = this.ParentElement.children[1];
	this.PopupElement = this.ParentElement.children[2];

	this.ParentElement.addEventListener("contextmenu", function(e)
	{
		var cm = new ContextMenu();
		cm.Data = this.McxNativeObject;
		var ecid = this.NativeObject.ECID;
		var iid = this.McxNativeObject.ParentElement.getAttribute("data-instance-id");
		cm.Items = [
			{
				"ClassName": "MenuItemCommand",
				"Title": mochaMessages["MXRES.MONIKER.CONTEXTMENUITEM.SeeInNewWindow"],
				"Visible": function()
				{
					return this.Menu.Data.LabelElement.tagName == "A";
				},
				"Execute": function()
				{
					var ik = InstanceKey.parse(iid);
					var castClassId = "1$" + ik.ClassIndex;
					var relativeUrl = System.ExpandRelativePath("~/" + System.TenantName + "/d/inst/" + castClassId + "/" + iid + ".htmld");
					var url = new URL(relativeUrl, document.baseURI).href;
					window.open(relativeUrl);
				}
			},
			{
				"ClassName": "MenuItemSeparator",
				"Visible": function()
				{
					return this.Menu.Data.LabelElement.tagName == "A";
				}
			},
			{
				"ClassName": "MenuItemCommand",
				"Title": mochaMessages["MXRES.MONIKER.CONTEXTMENUITEM.CopyURL"],
				"Visible": true,
				"Execute": function()
				{
					var ik = InstanceKey.parse(iid);
					var castClassId = "1$" + ik.ClassIndex;
					var relativeUrl = System.ExpandRelativePath("~/" + System.TenantName + "/d/inst/" + castClassId + "/" + iid + ".htmld");
					var url = new URL(relativeUrl, document.baseURI).href;
					Clipboard.setText(url);
				}
			},
			{
				"ClassName": "MenuItemCommand",
				"Title": mochaMessages["MXRES.MONIKER.CONTEXTMENUITEM.CopyText"],
				"Visible": true,
				"Execute": function()
				{
					Clipboard.setText(this.Menu.Data.LabelElement.innerText);
				}
			},
			{
				"ClassName": "MenuItemSeparator",
				"Visible": true
			},
			{
				"ClassName": "MenuItemCommand",
				"Title": String.format(mochaMessages["MXRES.MONIKER.CONTEXTMENUITEM.CopyInstanceID"], iid),
				"Visible": true,
				"Execute": function()
				{
					Clipboard.setText(iid);
				}
			},
			{
				"ClassName": "MenuItemCommand",
				"Title": mochaMessages["MXRES.MONIKER.CONTEXTMENUITEM.CopyTextAndInstanceID"],
				"Visible": true,
				"Execute": function()
				{
					Clipboard.setText(this.Menu.Data.LabelElement.innerText);
				}
			},
			{
				"ClassName": "MenuItemSeparator",
				"Visible": true
			},
			{
				"ClassName": "MenuItemCommand",
				"Title": mochaMessages["MXRES.MONIKER.CONTEXTMENUITEM.EditInstance"],
				"Visible": true,
				"TargetURL": function()
				{
					var ik = InstanceKey.parse(iid);
					var castClassId = "1$" + ik.ClassIndex;
					var relativeUrl = System.ExpandRelativePath("~/" + System.TenantName + "/d/inst/" + castClassId + "/" + iid + ".htmld");
					var url = new URL(relativeUrl, document.baseURI).href;
					return url;
				}
			},
			{
				"ClassName": "MenuItemCommand",
				"Title": String.format(mochaMessages["MXRES.MONIKER.CONTEXTMENUITEM.SearchInstanceID"], iid),
				"Visible": true,
				"TargetURL": function()
				{
					return System.ExpandRelativePath("~/" + System.TenantName + "/d/search.htmld?q=" + iid + "&branch=true");
				}
			},
			{
				"ClassName": "MenuItemSeparator",
				"Visible": true
			},
			{
				"ClassName": "MenuItemCommand",
				"Title": mochaMessages["MXRES.CONTEXTMENUITEM.ViewPrintableVersion"],
				"Visible": true,
				"Execute": function()
				{
					Window.ShowDialog("The method or operation is not implemented", "Not Implemented");
				}
			},
			{
				"ClassName": "MenuItemCommand",
				"Title": mochaMessages["MXRES.CONTEXTMENUITEM.ExportToSpreadsheet"],
				"Visible": true,
				"Execute": function()
				{
					Window.ShowDialog("The method or operation is not implemented", "Not Implemented");
				}
			}
		];
		cm.Show(e.clientX, e.clientY, this);
		

		e.preventDefault();
		e.stopPropagation();
		return false;
	});

	this.ActionsMenuElement = this.PopupElement.children[0].children[1];
	this.ClassTitleElement = this.PopupElement.children[1].children[0].children[0].children[0];
	this.InstanceTitleElement = this.PopupElement.children[1].children[0].children[0].children[1];

	this.InstanceID = this.ParentElement.getAttribute("data-instance-id");

	this.setInstanceTitle = function(value)
	{
		this.InstanceTitleElement.innerText = value;
	};
	this.setClassTitle = function(value)
	{
		this.ClassTitleElement.innerText = value;
	};

	this.BuildMenuItem = function(title, url)
	{

	};

	this.ButtonElement.McxNativeObject = this;
	this.ButtonElement.addEventListener("click", function()
	{
		System.ClassList.Add(this.McxNativeObject.PopupElement, "uwt-loading");
		
		var url = System.ExpandRelativePath("~/" + System.TenantName + "/inst/" + this.McxNativeObject.InstanceID + "/rel-tasks.htmld");
		var xhr = new XMLHttpRequest();
		xhr.timeout = 5000;
		xhr.open("GET", url);
		xhr.thiss = this.McxNativeObject;
		xhr.onreadystatechange = function(e)
		{
			var xhr = e.target;
			if (xhr.readyState == 4)
			{
				if (xhr.status == 200)
				{
					var json = JSON.parse(xhr.responseText);
					console.log(json);

					if (json.result == "failure" && json.responseCode == 403)
					{
						Alert.show(json.message, json.title, "uwt-color-danger");
						return;
					}

					if (json.instance)
					{
						if (json.instance.label)
						{
							// xhr.thiss.setClassTitle("Return Attribute Method Binding");
							xhr.thiss.setClassTitle(json.instance.label);
						}
						if (json.instance.title)
						{
							// xhr.thiss.setInstanceTitle("Common Text@get Concatenated Date and Time Format (BA)*P*S(public)[ramb]");
							xhr.thiss.setInstanceTitle(json.instance.title);
						}
					}
					if (json.taskGroups)
					{
						System.ClassList.Remove(xhr.thiss.ActionsMenuElement.parentElement, "uwt-empty");

						xhr.thiss.ActionsMenuElement.innerHTML = "";
						for (var i = 0; i < json.taskGroups.length; i++)
						{
							var tgprimary = json.taskGroups[i].taskGroups[0];
							if (tgprimary.taskGroups.length === 0)
								continue;

							var label = tgprimary.label;
							
							var li = document.createElement("li");
							li.className = "uwt-menu-item-command uwt-menu-item-popup uwt-visible";
		
							var a = document.createElement("a");
							a.href = "#";
		
							var span = document.createElement("span");
							span.className = "uwt-title";
							span.innerText = label;
							a.appendChild(span);
		
							li.appendChild(a);

							var ulMenuChild = document.createElement("ul");
							ulMenuChild.className = "uwt-menu uwt-popup";
		
							for (var j = 0; j < tgprimary.taskGroups.length; j++)
							{
								var tgsecondary = tgprimary.taskGroups[j].tasks[0];
								var li2 = document.createElement("li");
								li2.className = "uwt-menu-item-command uwt-visible";
								
								var a2 = document.createElement("a");
								a2.href = tgsecondary.uri;
								
								var span2 = document.createElement("span");
								span2.className = "uwt-title";
								span2.innerText = tgsecondary.label;
								a2.appendChild(span2);
								
								li2.appendChild(a2);
								ulMenuChild.appendChild(li2);
							}
		
							li.appendChild(ulMenuChild);
		
							xhr.thiss.ActionsMenuElement.appendChild(li);
						}
					}

					if (json.messages)
					{
						for (var i = 0; i < json.messages.length; i++)
						{
							Alert.show(json.messages[i].content, json.messages[i].title, "uwt-color-danger");
						}
					}
		
					System.ClassList.Remove(xhr.thiss.PopupElement, "uwt-loading");
				}
				else if (xhr.status == 403)
				{

				}
				else if (xhr.status == 0)
				{
					Alert.show("Please check your connection and try again", "Connection lost", "uwt-color-danger", 5000);
				}
			}
		};
		xhr.send(null);
	});
}

window.addEventListener("load", function()
{
	var items = document.getElementsByClassName("mcx-moniker");
	for (var i = 0; i < items.length; i++)
	{
		items[i].McxNativeObject = new McxMoniker(items[i]);
	}
});