var Addon_Id = "mouse";

if (window.Addon == 1) {
	Addons.Mouse =
	{
		Menus: [],

		OpenMenu: function (Ctrl, pt, nIndex)
		{
			var ar = this.Menus[nIndex];
			var items = ar[0];
			if (items) {
				var arMenu = ar[1];
				var hMenu = api.CreatePopupMenu();
				MakeMenus(hMenu, null, arMenu, items, Ctrl, pt);
				AdjustMenuBreak(hMenu);
				window.g_menu_click = 2;
				var nVerb = api.TrackPopupMenuEx(hMenu, TPM_RIGHTBUTTON | TPM_RETURNCMD, pt.x, pt.y, te.hwnd, null);
				api.DestroyMenu(hMenu);
				if (nVerb == 0) {
					return S_OK;
				}
				item = items[nVerb - 1];
				var s = item.getAttribute("Type");
				Exec(Ctrl, item.text, window.g_menu_button == 3 && s == "Open" ? "Open in New Tab" : s, Ctrl.hwnd, pt);
			}
			return S_OK;
		}
	}

	var xml = OpenXml("mouse.xml", false, true);
	for (var mode in eventTE.Mouse) {
		var items = xml.getElementsByTagName(mode);
		for (var i = 0; i < items.length; i++) {
			var item = items[i];
			var strMouse = String(item.getAttribute("Mouse"));
			var strType = item.getAttribute("Type");
			var strOpt = item.text;
			if (String(strType).toLowerCase() == "menus" && String(strOpt.toLowerCase()) == "open") {
				var arMenu = [];
				var nLevel = 0;
				while (++i < items.length) {
					item = items[i];
					strType = String(item.getAttribute("Type")).toLowerCase();
					strOpt = String(item.text);
					if (strType == "menus") {
						if (strOpt.toLowerCase() == "open") {
							nLevel++;
						}
						if (strOpt.toLowerCase() == "close") {
							if (--nLevel < 0) {
								break;
							}
						}
						continue;
					}
					arMenu.push(i);
				}
				strType = "JScript";
				strOpt = ["Addons.Mouse.OpenMenu(Ctrl, pt,", Addons.Mouse.Menus.length, ");"].join("");
				Addons.Mouse.Menus.push([items, arMenu]);
			}
			SetGestureExec(mode, strMouse, strOpt, strType, false, item.getAttribute("Name"));
		}
	}
} else {
	importScript("addons\\" + Addon_Id + "\\options.js");
}