<html xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      py:strip="True">
  <?python from allura.lib.security import has_neighborhood_access ?>

  <ul py:def="project_tree(project)">
    <li py:for="p in project.direct_subprojects">
      <a href="${p.url()}">$p.shortname</a>
      <py:if test="p.direct_subprojects">
        ${project_tree(p)}
      </py:if>
    </li>
  </ul>

  <py:def function="nav(s)">
    <li>
      <a py:if="s.url" href="$s.url" class="$s.className">$s.label</a>
      <span py:if="not s.url" class="$s.className">$s.label</span>
      <ul py:if="s.children">
        <py:for each="ss in s.children">${nav(ss)}</py:for>
      </ul>
    </li>
  </py:def>

  <py:def function="main_menu()">
    <div class="nav">
      <div id="mainmenu" class="row">
        <div class="column grid_2"><a href="/" class="sf_logo"><img src="${g.forge_static('images/sf10.png')}" alt="SourceForge" width="110" height="14"/></a></div>
        <div class="column grid_10 tright">
          <a href="/account/" py:if="c.user and c.user._id">Account</a>
          <a href="/users/${c.user.username}" py:if="c.user and c.user._id">$c.user.display_name</a>
          <a href="/auth/create_account" py:if="not c.user or not c.user._id">Register</a>
          <a href="/auth/logout" py:if="c.user and c.user._id">Log Out</a>
          <a href="${g.login_url}" py:if="not c.user or not c.user._id">Log In</a>
        </div>
      </div>
    </div>
  </py:def>

  <py:def function="nav_menu()">
    <div id="nav_menu_missing" py:if="not c.project"/>
    <py:if test="c.project">
	<div class="column grid_1">
        <img py:if="c.project.icon" src="${c.project.url()}/icon" class="project_icon"/>
        <img py:if="not c.project.icon" src="${g.forge_static('images/project_default.png')}" class="project_icon"/>
	</div>
	<div class="column grid_9">
		<h1>
		<a href="${c.project.url()}" py:if="c.project.shortname.endswith('--init--')">$c.project.neighborhood.name</a>
		<a py:if="not c.project.shortname.endswith('--init--')" href="${c.project.url()}">$c.project.name</a>
		</h1>
	</div>
	<div class="column grid_2">
		<p class="neighborhood tright" py:if="c.project.neighborhood.name not in ['Projects', 'Users']">
			<a href="${c.project.neighborhood.url()}">
				<small>Project by</small><br/>
				$c.project.neighborhood.name
            <img py:if="c.project.neighborhood.icon" src="${c.project.neighborhood.url()}/icon" alt="$c.project.neighborhood.name Project"/>
            <img py:if="not c.project.neighborhood.icon" src="${g.forge_static('images/project_default.png')}" alt="$c.project.neighborhood.name Project"/>
			</a>
		</p>
	</div>
	</py:if>
  </py:def>

  <py:def function="top_nav()">
		<div id="nav_menu" class="row project" py:if="c.project">
			<div class="column grid_9">
				<ul class="ui-tab">
					<li py:for="s in c.project.sitemap()" class="$s.className ${s.url in request.url and 'active' or ''}">
						<a py:if="s.url" href="$s.url" class="ico-l"><b class="ui-icon ui-icon-${s.ui_icon or 'admin'}"></b> <span>$s.label</span></a>
            <span py:if="not s.url">$s.label</span>
					</li>
				</ul>
			</div>
		</div>
  </py:def>

  <py:def function="sidebar_item(s)">
    <li py:if="s.url" class="${request.url.find(s.url,-len(s.url)) != -1 and 'active' or None}">
      <a href="$s.url" class="$s.className${s.ui_icon and ' ico-l' or ''}"><b py:if="s.ui_icon" class="ui-icon ui-icon-$s.ui_icon"></b> <span>$s.label</span><small py:if="s.small is not None">$s.small</small></a>
    </li>
    <py:if test="not s.url">
      ${Markup('&lt;/ul>')}
        <span py:if="s.label" class="$s.className nav_head"><span py:if="s.ui_icon" class="ui-icon ui-icon-$s.ui_icon"></span>$s.label</span>
      ${Markup('&lt;ul class="sidebarmenu">')}
    </py:if>
  </py:def>

  <py:def function="sidebar_menu()">
		<div id="sidebar" class="column grid_2 menu">
			<form id="search" py:if="c.app and c.app.searchable" method="GET" action="${c.app.url + 'search/'}">
				<input name="q" class="border defaultText" type="text" title="Search ${c.app.config.options.mount_point.title()}"/>
			</form>
      <div py:if="not c.app or not c.app.searchable">&nbsp;</div>
			<ul class="sidebarmenu">
			  <py:if test="getattr(c, 'custom_sidebar_menu', None)">
			    <py:for each="s in c.custom_sidebar_menu">${sidebar_item(s)}</py:for>
			  </py:if>
			  <py:if test="getattr(c, 'app', None)">
			    <py:for each="s in c.app.sidebar_menu()">${sidebar_item(s)}</py:for>
			  </py:if>
			  <py:if test="getattr(c, 'project', None) and not getattr(c, 'app', None)">
			    <py:for each="s in c.project.sidebar_menu()">${sidebar_item(s)}</py:for>
			  </py:if>
			</ul>
		</div>
  </py:def>

  <py:def function="crumbs(trail)">
    <py:for each="i, (label, url) in enumerate(trail)">
      <li class="${i == 0 and 'first' or None}">
        <a py:if="url" href="$url">$label</a>
        <span py:if="not url">$label</span>
      </li>
    </py:for>
  </py:def>

  <py:def function="path_links(parts)">
    <py:for each="i, part in enumerate(parts)">/
      <a href="${'./' + '../' * (len(parts)-i-1)}">$part
      </a>
    </py:for>/
  </py:def>

  <py:def function="text_field(name, label, value=None)">
    <label for="$name">$label</label><br/>
    <input type="text" id="$name" name="$name" class="text" value="$value"/><br/>
  </py:def>

  <py:def function="file_field(name, label)">
    <label for="$name" py:if="label">$label</label>
    <input type="file" id="$name" name="$name" class="text" /><br/>
  </py:def>

  <py:def function="text_area(name, label, value=None, **kw)">
    <label for="$name">$label</label><br/>
    <textarea id="$name" name="$name" py:attrs="kw">$value</textarea><br/>
  </py:def>

  <py:def function="select_field(name, label, options, value=None)">
    <label for="$name">$label</label><br/>
    <select id="$name" name="$name">
      <py:for each="olbl, oval in options">
        <option value="$oval" selected="${oval == value or None}">$olbl</option>
      </py:for>
    </select>
    <br/>
  </py:def>

  <py:def function="radio_button(name, label, option, value=None)">
    <py:if test="label">
      <label for="$name">$label</label><br/>
    </py:if>
    <input type="radio" id="$name" name="$name" class="text" value="$option" 
           checked="${option==value and 'on' or None}"/><br/>
  </py:def>

  <py:def function="checkbox(name, label, checked)">
    <input type="checkbox" id="$name" name="$name" 
           checked="${checked or None}"/>
    <py:if test="label">
      <label for="$name">$label</label>
    </py:if>
    <br/>
  </py:def>

  <py:def function="submit_button(value, name=None)">
    <input type="submit" name="$name" value="$value"/><br/>
  </py:def>

  <py:def function="gravatar(user, **kw)">
    <img py:if="user.preferences.email_address and 'size' in kw"
         src="${g.gravatar(user.preferences.email_address, default=g.url('/u/'+user.username.replace('_', '-')+'/user_icon'), **kw)}"
         alt="${user.display_name}"
         title="${user.display_name}"
         height="${kw['size']}"
         width="${kw['size']}"/>
    <img py:if="user.preferences.email_address and 'size' not in kw"
         src="${g.gravatar(user.preferences.email_address, default=g.url('/u/'+user.username.replace('_', '-')+'/user_icon'), **kw)}"
         alt="${user.display_name}"
         title="${user.display_name}"/>
    <img py:if="not user.preferences.email_address and 'size' in kw"
         src="${'/u/'+user.username.replace('_', '-')+'/user_icon'}"
         alt="${user.display_name}"
         title="${user.display_name}"
         height="${kw['size']}"
         width="${kw['size']}"/>
    <img py:if="not user.preferences.email_address and 'size' not in kw"
         src="${'/u/'+user.username.replace('_', '-')+'/user_icon'}"
         alt="${user.display_name}"
         title="${user.display_name}"/>
  </py:def>

  <py:def function="email_gravatar(email, title=None, **kw)">
    <img py:if="email" src="${g.gravatar(email, **kw)}" alt="${title or email}" title="${title or email}"/>
    <img py:if="not email and 'size' not in kw" src="${g.forge_static('images/user.png')}" alt="${title}" title="${title}"/>
    <img py:if="not email and 'size' in kw" src="${g.forge_static('images/user.png')}" alt="${title}" title="${title}" width="${kw['size']}" height="${kw['size']}"/>
  </py:def>
</html>
