function McxMoniker(parentElement)
{
	this.ParentElement = parentElement;
	if (this.ParentElement.children[0].tagName == "IMG")
	{
		this.IconElement = this.ParentElement.children[0];
		this.LabelElement = this.ParentElement.children[1];
		this.ButtonElement = this.ParentElement.children[2];
		this.PopupElement = this.ParentElement.children[3];
	}
	else
	{
		this.IconElement = null;
		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, document.body);
		

		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.PreviewContentElement = this.PopupElement.children[1].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.OnOpening = function()
	{
		System.ClassList.Add(this.PopupElement, "uwt-loading");
		
		var url = System.ExpandRelativePath("~/" + System.TenantName + "/inst/" + this.InstanceID + "/rel-tasks.htmld");
		var xhr = new XMLHttpRequest();
		xhr.timeout = 5000;
		xhr.open("GET", url);
		xhr.thiss = this;
		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");
						}
					}

					if (json.preview)
					{
						xhr.thiss.PreviewContentElement.innerHTML = json.preview;
					}
		
					System.ClassList.Remove(xhr.thiss.PopupElement, "uwt-loading");
					xhr.thiss.ParentElement.NativeObject.UpdatePopupLocation();
				}
				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);
	};
}

McxMoniker.create = function(instanceKey, title, viewTask)
{
	var div = document.createElement("div");
	div.setAttribute("data-instance-id", instanceKey);
	div.className = "mcx-moniker uwt-actionpreviewbutton apb-show-text apb-style-ellipsis";

	var img = document.createElement("img");
	img.className = "apb-icon";
	img.addEventListener("error", function()
	{
		this.style.display="none";
	});
	img.src = "/madi/asset/uic-assets/1.1.5/zq/icons/1$" + instanceKey.split("$")[0];
	img.setAttribute("alt", "");
	div.appendChild(img);
	
	var a = document.createElement("a");
	if (viewTask != null)
	{
		a.className = "apb-text";
		a.href = System.ExpandRelativePath("~/" + System.TenantName + "/d/inst/" + instanceKey + ".htmld");
	}
	else
	{
		a.className = "apb-text apb-empty";
	}
	if (title == "")
	{
		a.innerHTML = "&nbsp;";
	}
	else
	{
		a.innerText = title;
	}
	div.appendChild(a);
	
	a = document.createElement("a");
	a.className = "apb-button";
	a.href = System.ExpandRelativePath("~/" + System.TenantName + "/d/inst/" + instanceKey + "/rel-tasks.htmld");
	a.innerHTML = "&nbsp;";
	div.appendChild(a);
	
	var apbPreview = document.createElement("div");
	apbPreview.className = "apb-preview uwt-popup";

	var apbActions = document.createElement("div");
	apbActions.className = "apb-actions uwt-empty";

	var h2 = document.createElement("h2");
	h2.innerText = "Available Actions";
	apbActions.appendChild(h2);

	var ul = document.createElement("ul");
	ul.className = "uwt-menu";
	
	apbActions.appendChild(ul);

	apbPreview.appendChild(apbActions);



	var apbPreviewContent = document.createElement("div");
	apbPreviewContent.className = "apb-preview-content";

	var apbHeader = document.createElement("div");
	apbHeader.className = "apb-header";

	var h2 = document.createElement("h2");
	var spanClassTitle = document.createElement("span");
	spanClassTitle.className = "apb-class-title";
	spanClassTitle.innerText = "Relationship";
	h2.appendChild(spanClassTitle);
	
	var img = document.createElement("img");
	img.className = "apb-icon";
	img.src = "/madi/asset/uic-assets/1.1.5/zq/icons/1$" + instanceKey.split("$")[0];
	img.setAttribute("alt", "");
	h2.appendChild(img);
	
	var text = document.createElement("a");
	text.className = "apb-text apb-empty";
	text.href=  "";
	text.innerText = title;
	h2.appendChild(text);

	apbHeader.appendChild(h2);

	apbPreviewContent.appendChild(apbHeader);
	apbPreview.appendChild(apbPreviewContent);

	var apbSpinner = document.createElement("div");
	apbSpinner.className = "uwt-spinner";
	apbPreview.appendChild(apbSpinner);


	div.appendChild(apbPreview);
	//</div><div class="apb-preview-content"><div class="apb-header"><h2><span class="apb-class-title">Work Set</span><img class="apb-icon" src="/madi/asset/uic-assets/1.1.5/zq/icons/1$15.png"><a class="apb-text" href="/super/d/inst/1$15/15$12.htmld">Conditional Select Attribute Case [Nonsingular]</a></h2></div><div class="apb-content"></div></div><div class="uwt-spinner">&nbsp;</div></div></div>"
	
	
	div.NativeObject = new AdditionalDetailWidget(div);
	div.McxNativeObject = new McxMoniker(div);
	div.NativeObject.OnOpening = function()
	{
		this.Parent.McxNativeObject.OnOpening();
	};
	return div;
};

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]);
		items[i].NativeObject.OnOpening = function()
		{
			this.Parent.McxNativeObject.OnOpening();
		};
	}
});