<script type="text/javascript">
	(function() {
		function get_children(node) {
			var children = [];

			for (var k in node.children) {
				if (!node.children.hasOwnProperty(k))
					continue;

				if (!node.children[k].satisfied)
					continue;

				if (!node.children[k].hasOwnProperty('title'))
					continue;

				children.push(Object.assign(node.children[k], { name: k }));
			}

			return children.sort(function(a, b) {
				return ((a.order || 1000) - (b.order || 1000));
			});
		}

		function render_tabmenu(tree, url, level) {
			var container = document.querySelector('#tabmenu'),
			    ul = E('ul', { 'class': 'tabs' }),
			    children = get_children(tree),
			    activeNode = null;

			for (var i = 0; i < children.length; i++) {
				var isActive = (L.env.dispatchpath[3 + (level || 0)] == children[i].name),
				    activeClass = isActive ? ' active' : '',
				    className = 'tabmenu-item-%s %s'.format(children[i].name, activeClass);

				ul.appendChild(E('li', { 'class': className }, [
					E('a', { 'href': L.url(url, children[i].name) }, [ _(children[i].title) ] )]));

				if (isActive)
					activeNode = children[i];
			}

			if (ul.children.length == 0)
				return E([]);

			container.appendChild(ul);
			container.style.display = '';

			if (activeNode)
				render_tabmenu(activeNode, url + '/' + activeNode.name, (level || 0) + 1);

			return ul;
		}

		function render_mainmenu(tree, url, level) {
			var ul = level ? E('ul', { 'class': 'dropdown-menu' }) : document.querySelector('#topmenu'),
			    children = get_children(tree);

			if (children.length == 0 || level > 1)
				return E([]);

			for (var i = 0; i < children.length; i++) {
				var submenu = render_mainmenu(children[i], url + '/' + children[i].name, (level || 0) + 1),
				    subclass = (!level && submenu.firstElementChild) ? 'dropdown' : null,
				    linkclass = (!level && submenu.firstElementChild) ? 'menu' : null,
				    linkurl = submenu.firstElementChild ? '#' : L.url(url, children[i].name);

				var li = E('li', { 'class': subclass }, [
					E('a', { 'class': linkclass, 'href': linkurl }, [ _(children[i].title) ]),
					submenu
				]);

				ul.appendChild(li);
			}

			ul.style.display = '';

			return ul;
		}

		function render_modemenu(tree) {
			var ul = document.querySelector('#modemenu'),
			    children = get_children(tree);

			for (var i = 0; i < children.length; i++) {
				var isActive = (L.env.requestpath.length ? children[i].name == L.env.requestpath[0] : i == 0);

				ul.appendChild(E('li', { 'class': isActive ? 'active' : null }, [
					E('a', { 'href': L.url(children[i].name) }, [ _(children[i].title) ]),
					' ',
					E('span', { 'class': 'divider' }, [ '|' ])
				]));

				if (isActive)
					render_mainmenu(children[i], children[i].name);
			}

			if (ul.children.length > 1)
				ul.style.display = '';
		}

		document.addEventListener('luci-loaded', function(ev) {
			var tree = <%= luci.http.write_json(luci.dispatcher.context.authsession and luci.dispatcher.menu_json() or {}) %>,
			    node = tree,
			    url = '';

			render_modemenu(tree);

			if (L.env.dispatchpath.length >= 3) {
				for (var i = 0; i < 3 && node; i++) {
					node = node.children[L.env.dispatchpath[i]];
					url = url + (url ? '/' : '') + L.env.dispatchpath[i];
				}

				if (node)
					render_tabmenu(node, url);
			}
		});
	})();
</script>
