<%#
Copyright 2008 Steven Barth <steven@midlink.org>
Copyright 2008-2010 Jo-Philipp Wich <xm@subsignal.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0
-%>
<%
	local pretty_hostname = require "pretty_hostname"
	local uci  = require("simple-uci").cursor()
	local util = require "gluon.util"

	local hostname = pretty_hostname.get(uci)
	local release = util.readfile("/lib/gluon/release")

	local root = node()
	local rnode = node(unpack(request))

	local category = request[1]
	local cattree  = category and node(category)

	local function node_visible(node)
		return (
			node.title and
			node.target and
			(not node.hidden)
		)
	end

	local function node_children(node)
		if not node then return {} end

		local ret = {}
		for k, v in pairs(node.nodes) do
			if node_visible(v) then
				table.insert(ret, k)
			end
		end

		table.sort(ret,
			function(a, b)
				return (node.nodes[a].order or 100)
				     < (node.nodes[b].order or 100)
			end
		)
		return ret
	end

	local categories = node_children(root)

	local function append(xs, x)
		local r = {unpack(xs)}
		r[#r+1] = x
		return r
	end

	local function title(node)
		return i18n(node.pkg).translate(node.title)
	end

	local function subtree(prefix, node, name, ...)
		if not node then return end

		local children = node_children(node)
		if #children == 0 then return end

	%>
	<div class="tabmenu<%=#prefix%>">
		<ul class="tabmenu l<%=#prefix%>">
			<%
				for i, v in ipairs(children) do
					local child = node.nodes[v]
					local active = (v == name)
			%>
			<li class="tabmenu-item-<%=v%><% if active then %> active<% end %>">
				<a href="<%|url(append(prefix, v))%>"><%|title(child)%></a>
			</li>
			<%
				end
			%>
		</ul>
		<br style="clear:both" />
		<%
			subtree(append(prefix, name), node.nodes[name], ...)
		%>
	</div>
	<%
	end

	local function menutree(path, ...)
		subtree({path}, root.nodes[category], ...)
	end

	http:prepare_content("application/xhtml+xml")
-%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
	<head>
		<meta charset="UTF-8" />
		<link rel="stylesheet" type="text/css" media="screen" href="/static/gluon.css" />
		<title><%| hostname .. ((rnode and rnode.title) and ' - ' .. title(rnode) or '') %></title>
	</head>
	<body>

	<div id="menubar">
		<div class="hostinfo">
			<a href="<%|url({})%>">
				<%|hostname%>
				<% if release then %>
				/ <%|release%>
				<% end %>
			</a>
		</div>

		<% if #categories > 1 and not hidenav then %>
			<ul id="topmenu">
				<% for i, r in ipairs(categories) do %>
					<li><a class="topcat<% if request[1] == r then %> active<%end%>" href="<%|url({r})%>"><%|title(root.nodes[r])%></a></li>
				<% end %>
			</ul>
		<% end %>
	</div>

	<div id="maincontainer">
	  <%
			if not hidenav then
				menutree(unpack(request))
			end
		%>

		<div id="maincontent">
			<noscript>
				<div class="errorbox">
					<strong><%:JavaScript required!%></strong><br />
					<%:You must enable JavaScript in your browser or the web interface will not work properly.%>
				</div>
			</noscript>

			<%
				ok, err = pcall(renderer.render, content, scope, pkg)
				if not ok then
					renderer.render('error/500', {message = err}, 'gluon-web')
				end
			%>

			</div>
		</div>
	</body>
</html>
