function Ribbon(parent)
{
	this.ParentElement = parent;
	this.TabContainer = this.ParentElement.children[1];
	this.TabContentContainer = this.ParentElement.children[2];

	System.AddEventListener(parent, System.Events.MouseWheel, function(e)
	{
		if (e.Delta > 0)
		{
			parent.NativeObject.PreviousTab();
			e.Cancel = true;
		}
		else if (e.Delta < 0)
		{
			// forward
			parent.NativeObject.NextTab();
			e.Cancel = true;
		}
	});

	var applicationButton = parent.children[0];
	// var applicationMenu = document.getElementById("Ribbon_" + this.ID + "_ApplicationMenu");

	this.PreviousTab = function()
	{
		var tabs = this.TabContentContainer.children;
		for (var i = 1; i < tabs.length; i++)
		{
			if (System.ClassList.Contains(tabs[i], "uwt-selected"))
			{
				ribbon.ActivateTab(tabs[i - 1]);
				return;
			}
		}
	};
	this.NextTab = function()
	{
		var tabs = this.TabContentContainer.children;
		for (var i = 0; i < tabs.length - 1; i++)
		{
			if (System.ClassList.Contains(tabs[i], "uwt-selected"))
			{
				ribbon.ActivateTab(tabs[i + 1]);
				return;
			}
		}
	};

	this.GetActiveTab = function()
	{
		var tabs = this.TabContentContainer.children;
		for (var i = 0; i < tabs.length; i++)
		{
			if (System.ClassList.Contains(tabs[i], "uwt-selected"))
			{
				return tabs[i];
			}
		}
		return null;
	};
	this.ActivateTab = function (tab)
	{
		if (tab != null) this.SetOpened(true);
		var ribbonTabs = this.TabContainer.children;
		var tabs = this.TabContentContainer.children;
		for (var i = 0; i < tabs.length; i++)
		{
			if (tabs[i] === tab)
			{
				System.ClassList.Add(ribbonTabs[i], "uwt-selected");
				System.ClassList.Add(tabs[i], "uwt-selected");
			}
			else
			{
				System.ClassList.Remove(ribbonTabs[i], "uwt-selected");
				System.ClassList.Remove(tabs[i], "uwt-selected");
			}
		}

		if (tab == null)
		{
			// System.ClearClientProperty(this.ID, "ActiveTabID");
		}
		else
		{
			// System.SetClientProperty(this.ID, "ActiveTabID", tab.attributes["data-tab-id"].value);
		}
	};

	this.ToggleOpened = function ()
	{
		if (this.IsOpened())
		{
			this.SetOpened(false);
		}
		else
		{
			this.SetOpened(true);
		}
	};
	this.IsOpened = function ()
	{
		return !System.ClassList.Contains(this.ParentElement, "uwt-collapsed");
	}
	this.SetOpened = function (value)
	{
		switch (value)
		{
			case true:
			{
				System.ClassList.Remove(this.ParentElement, "uwt-collapsed");
				break;
			}
			case false:
			{
				System.ClassList.Add(this.ParentElement, "uwt-collapsed");
				this.ActivateTab(null);
				break;
			}
		}
	};

	this.ToggleCollapsed = function ()
	{
		if (this.IsCollapsed())
		{
			this.SetCollapsed(false);
		}
		else
		{
			this.SetCollapsed(true);
		}
	};
	this.IsCollapsed = function ()
	{
		// var ribbonSpacer = document.getElementById("Ribbon_" + ribbon.ID + "_Spacer");
		return System.ClassList.Contains(this.ParentElement, "uwt-collapsed"); // (ribbonSpacer.className == "RibbonSpacer Collapsed");
	};
	this.SetCollapsed = function (value)
	{
		/*
		var ribbonSpacer = document.getElementById("Ribbon_" + ribbon.ID + "_Spacer");
		ribbon.SetOpened(!value);
		switch (value)
		{
			case true:
			{
				ribbonSpacer.className = "RibbonSpacer Collapsed";
				ribbon.ActivateTab(null);
				break;
			}
			case false:
			{
				ribbonSpacer.className = "RibbonSpacer";
				break;
			}
		}

		System.SetClientProperty(this.ID, "Collapsed", value);
		*/
	};
	this.SetApplicationMenuVisible = function (value)
	{
		/*
		switch (value)
		{
			case true:
			{
				applicationButton.className = "ApplicationButton Selected";
				applicationMenu.className = "ApplicationMenu Visible";
				break;
			}
			case false:
			{
				applicationButton.className = "ApplicationButton";
				applicationMenu.className = "ApplicationMenu";
				break;
			}
		}
		*/
	};

	this.GetTabByIndex = function (i)
	{
		return this.TabContentContainer.children[i];
	};
	this.GetTabByName = function (name)
	{
		for (var i = 0; i < this.TabContentContainer.children.length; i++)
		{
			if (this.TabContentContainer.children[i].attributes["data-tab-id"].value == name)
			{
				return this.TabContentContainer.children[i];
			}
		}
		return null;
	};

	/*
	System.AddEventListener(applicationButton, System.Events.MouseClick, function (e)
	{
		if (applicationButton.className == "ApplicationButton")
		{
			if (ribbon.IsCollapsed() && ribbon.IsOpened())
			{
				ribbon.SetOpened(false);
			}
			ribbon.SetApplicationMenuVisible(true);
		}
		else
		{
			ribbon.SetApplicationMenuVisible(false);
		}
	});
	*/

	var ribbonTabs = this.TabContainer.children;
	for (var i = 0; i < ribbonTabs.length; i++)
	{
		var tab = ribbonTabs[i];
		tab.ni = this;
		tab.index = i;
		tab.addEventListener("click", function (e)
		{
			if (true) // (this.attributes["href"].value == "#")
			{
				// only activate tab if the tab is not a "navigational" tab
				tab.ni.ActivateTab(tab.ni.GetTabByIndex(this.index));
				e.preventDefault();
				e.stopPropagation();
				return false;
			}
		});
		tab.addEventListener("dblclick", function (e)
		{
			tab.ni.ToggleCollapsed();
			e.preventDefault();
			e.stopPropagation();
			return false;
		});
	}
}

var Ribbons = new Array();
// window.addEventListener('load', function (e)
// {
// 	var ribbons = document.getElementsByClassName("Ribbon");
// 	for (var i = 0; i < ribbons.length; i++)
// 	{
// 		var parent = ribbons[i];
// 		if (parent.tagName != "DIV") continue;
// 		var ribbon = new Ribbon(parent.attributes["data-id"].value, parent);
// 		parent.Ribbon = ribbon;

// 		parent.addEventListener("contextmenu", function(e)
// 		{
// 			e.preventDefault();
// 			e.stopPropagation();
// 			return false;
// 		});

// 		Ribbons.push(ribbon);
// 	}
// });
window.addEventListener("keydown", function (e)
{
	switch (e.keyCode)
	{
		case 27: /* ESC */
		{
			// ESCAPE was pressed; hide all application menus
			for (var i = 0; i < Ribbons.length; i++)
			{
				Ribbons[i].SetApplicationMenuVisible(false);
			}
			break;
		}
	}
});


function TerminateIfSenderIs(sender, compareTo)
{
	while (sender != null)
	{
		if (sender.classList)
		{
			for (var i = 0; i < compareTo.length; i++)
			{
				if (System.ClassList.Contains(sender, compareTo[i]))
				{
					// do not close the popup when we click inside itself
					// e.preventDefault();
					// e.stopPropagation();
					// alert(compareTo[i] + " = " + sender.className + " ? true ");
					return true;
				}
			}
		}
		sender = sender.parentNode;
		if (sender == null) break;
	}
	return false;
}

window.addEventListener("mousedown", function (e)
{
	var sender = null;
	if (!e) e = window.event;
	if (e.target)
	{
		sender = e.target;
	}
	else if (e.srcElement)
	{
		sender = e.srcElement;
	}

	if (!TerminateIfSenderIs(sender, ["ApplicationMenu"]))
	{
		for (var i = 0; i < Ribbons.length; i++)
		{
			Ribbons[i].SetApplicationMenuVisible(false);
		}
	}

	if (!TerminateIfSenderIs(sender, ["Ribbon"]))
	{
		for (var i = 0; i < Ribbons.length; i++)
		{
			if (Ribbons[i].IsCollapsed())
			{
				Ribbons[i].SetOpened(false);
			}
		}
	}

	if (!TerminateIfSenderIs(sender, ["RibbonDropDownCommand"]))
	{
		Ribbon.CloseAllRibbonDropDownMenus();
	}
});
Ribbon.FromID = function (id)
{
	for (var i = 0; i < Ribbons.length; i++)
	{
		if (Ribbons[i].ID == id) return Ribbons[i];
	}
	return null;
};
Ribbon.CloseAllRibbonDropDownMenus = function()
{
	var RibbonDropDownItems = document.getElementsByClassName("RibbonDropDownCommand");
	for (var i = 0; i < RibbonDropDownItems.length; i++)
	{
		RibbonDropDownItems[i].classList.remove("Selected");
	}
};
Ribbon.CurrentContextHelpURL = "";
Ribbon.CurrentContextHelpTargetName = "_blank";

function RibbonButtonCommand(id)
{
	this.ID = id;
	this.SetSelected = function(value)
	{
		switch (value)
		{
			case true:
			{
				var rbc_array = document.getElementsByClassName(this.ID);
				for (var i = 0; i < rbc_array.length; i++)
				{
					if (System.ClassList.Contains(rbc_array[i], "Disabled")) continue;
					System.ClassList.Add(rbc_array[i], "Selected");
				}
				break;
			}
			case false:
			{
				var rbc_array = document.getElementsByClassName(this.ID);
				for (var i = 0; i < rbc_array.length; i++)
				{
					if (System.ClassList.Contains(rbc_array[i], "Disabled")) continue;
					System.ClassList.Remove(rbc_array[i], "Selected");
				}
				break;
			}
		}
	};
	this.IsSelected = function()
	{
		var rbc_array = document.getElementsByClassName(this.ID);
		for (var i = 0; i < rbc_array.length; i++)
		{
			if (rbc_array[i].className == "RibbonCommand RibbonButtonCommand " + this.ID + " Selected") return true;
		}
		return false;
	};
	this.ToggleSelected = function()
	{
		this.SetSelected(!this.IsSelected());
		return false;
	};
}
function RibbonDropDownCommand(id)
{
	this.ID = id;
	this.SetSelected = function(value)
	{
		switch (value)
		{
			case true:
			{
				Ribbon.CloseAllRibbonDropDownMenus();
				var rbc_array = document.getElementsByClassName(this.ID);
				for (var i = 0; i < rbc_array.length; i++)
				{
					if (System.ClassList.Contains(rbc_array[i], "Disabled")) continue;
					System.ClassList.Add(rbc_array[i], "Selected");
				}
				break;
			}
			case false:
			{
				var rbc_array = document.getElementsByClassName(this.ID);
				for (var i = 0; i < rbc_array.length; i++)
				{
					if (System.ClassList.Contains(rbc_array[i], "Disabled")) continue;
					System.ClassList.Remove(rbc_array[i], "Selected");
				}
				break;
			}
		}
	};
	this.IsSelected = function()
	{
		var rbc_array = document.getElementsByClassName(this.ID);
		for (var i = 0; i < rbc_array.length; i++)
		{
			if (rbc_array[i].className == "RibbonCommand RibbonDropDownCommand " + this.ID + " Selected") return true;
		}
		return false;
	};
	this.ToggleSelected = function()
	{
		this.SetSelected(!this.IsSelected());
		return false;
	};
}

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