function McxTypeaheadSearch(parentElement)
{
	this.ParentElement = parentElement;
	this.FormElement = this.ParentElement.children[0];
	this.TextBoxElement = this.FormElement.children[0];
	this.PopupElement = this.ParentElement.children[1];
	this.MenuElement = this.PopupElement.children[0];

	this.clearSearchResults = function()
	{
		while (this.MenuElement.children.length > 0)
		{
			this.MenuElement.children[0].remove();
		}
	};
	this.activateSearchResult = function(searchResult)
	{
		window.location.href = searchResult.uri;
	};

	this.search = function(query)
	{
		System.ClassList.Add(this.PopupElement, "uwt-visible");
		System.ClassList.Add(this.PopupElement, "uwt-loading");

		var xhr = new XMLHttpRequest();
		var searchUrl = System.ExpandRelativePath("~/" + System.TenantName + "/search.htmld?q=" + encodeURIComponent(query));
		// var searchUrl = System.ExpandRelativePath("~/madi/pex/fs/" + System.TenantName + "typeahead?q=" + encodeURIComponent(query) + "&contextualsearch=true&autocomplete=true&clientRequestId=");

		/*
		if (this.ParentElement.hasAttribute("data-valid-class-ids"))
		{
			searchUrl += "&validClassIds=" + this.ParentElement.getAttribute("data-valid-class-ids");
		}
		*/
		xhr.open("GET", searchUrl, true);
		xhr.NativeObject = this;
		xhr.onreadystatechange = function()
		{
			if (this.readyState === 4)
			{
				var resultsText = this.responseText;
				var json = JSON.parse(resultsText);
				System.ClassList.Remove(this.NativeObject.PopupElement, "uwt-loading");
				if (json.result === "success")
				{
					this.NativeObject.clearSearchResults();

					for (var i = 0; i < json.items.length; i++)
					{
						var li = document.createElement("li");
						li.className = "uwt-menu-item uwt-menu-item-command uwt-visible";
						li.searchResult = json.items[i];
						li.NativeObject = this.NativeObject;
						li.addEventListener("click", function()
						{
							if (this.NativeObject.activateSearchResult(this.searchResult))
							{
								this.NativeObject.setEditing(false);
							}
						});

						var span = document.createElement("span");
						span.className = "uwt-title";
						span.innerText = json.items[i].label;
						li.appendChild(span);
						
						span = document.createElement("span");
						span.className = "uwt-description";
						span.innerText = json.items[i].summaryDescription;
						li.appendChild(span);

						this.NativeObject.MenuElement.appendChild(li);
					}

					System.ClassList.Remove(this.NativeObject.PopupElement, "uwt-loading");
				}
				else
				{
					if (json.responseCode === 403)
					{
						Alert.show("Please log in to continue", "Not Authorized", "uwt-color-danger");
					}
					System.ClassList.Remove(this.NativeObject.PopupElement, "uwt-loading");
				}
			}
		};
		xhr.send(null);
	};

	this.__timeout = null;
	this.TextBoxElement.NativeObject = this;
	this.TextBoxElement.addEventListener("keyup", function(e)
	{
		if (this.NativeObject.__timeout !== null)
		{
			window.clearTimeout(this.NativeObject.__timeout);
		}
		this.NativeObject.__timeout = window.setTimeout(function(thiss)
		{
			thiss.search(thiss.TextBoxElement.value);
		}, 500, this.NativeObject);
	});
}

window.addEventListener("load", function()
{
	var primarySearch = document.getElementsByClassName("uwt-searchbar");
	primarySearch[0].NativeObject = new McxTypeaheadSearch(primarySearch[0]);
});
