<%def name="nav_link(href, text, li_class='', a_class='',  a_rel='', id_='', title='', condition=True)">
%if condition:
<li class="${li_class}" id="${id_}">
    <a href="${href}" class="${a_class}" rel="${a_rel}" title="${title}">${text}</a>
</li>
%endif
</%def>


<%def name="global_nav(active)">

<%
   active_nav = {}
   active_nav[active] = 'current'

   def _class(identifier):
       return active_nav.get(identifier, '')
%>

<!-- begin: main navi -->
<nav>
    <div id="nav">
        <!-- skiplink anchor: navigation -->
        <a id="navigation"></a>
        <!-- main navigation: horizontal list -->
        <div class="menu hlist">
            <ul>
                ${nav_link(href=h.base_url('/', None),
                text=_("Start"),
                li_class=_class('home'),
                id_="nav_home")}

                %if not h.config.get_bool('adhocracy.hide_instance_list_in_navigation'):
                %if app_globals.single_instance is None:
                ${nav_link(href=h.base_url('/instance', None),
                           a_rel='#overlay-default',
                           li_class=_class('instances'),
                           text=_("Instances"),
                           id_="nav_instances")}
                %endif
                %endif

                %if not h.config.get_bool('adhocracy.put_watchlist_in_user_menu') and c.user:
                ${nav_link(href=h.entity_url(c.user, member='watchlist'),
                           li_class=_class('watchlist'),
                           text=_("Watchlist"),
                           id_="nav_watchlist")}
                %endif
                ${nav_link(href=h.base_url('/static/about.html', None),
                           text=_('About'),
                           a_class='staticlink_about',
                           li_class=_class('about'),
                           id_="nav_about")}
                <!--TODO: remove dummy link to merge in new links with diazo themeing-->
                <li id="nav_dummy" style="display: none;"></li>
                ${nav_link(href=h.base_url('/static/help.html', None),
                           text=_("Help"),
                           a_class='staticlink_help',
                           li_class=_class('help'),
                           id_="nav_help")}

                %if c.user is None:
                ${nav_link(href=h.login_redirect_url(),
                           a_rel='#overlay-default',
                           text=_("Login"),
                           li_class=_class('login'),
                           id_="nav_login")}
                %else:
                <li id="user_menu" class="${_class('user')}">
                    <a class="icon_link show_me"
                       href="${h.base_url(u'/user/dashboard')}">${c.user.name}</a>
                    <ul class="children">
                        <li>
                            <a href="${h.base_url(u'/user/dashboard')}">
                                ${_('Dashboard')}
                            </a>
                        </li>

                        %if h.config.get_bool('adhocracy.put_watchlist_in_user_menu'):
                        <li>
                            <a href="${h.entity_url(c.user, member='watchlist')}">
                                ${_('Watchlist')}
                            </a>
                        </li>
                        %endif

                        %if can.user.edit(c.user):
                        <li>
                            <a href="${h.entity_url(c.user, instance=c.instance, member='settings')}">
                                ${_('Edit Profile')}
                            </a>
                        </li>
                        %endif
                        %if can.user.show(c.user):
                        <li>
                            <a href="${h.entity_url(c.user, instance=c.instance)}">
                                ${_('View Profile')}
                            </a>
                        </li>
                        %endif
                        %if h.has_permission("global.admin"):
                        <li>
                            <a href="${h.base_url('/admin', None)}">
                                ${_('Administrate Site')}
                            </a>
                        </li>
                        %endif
                        <li><a href="/logout">${_("Logout")}</a></li>
                    </ul>
                </li>
                %endif

            </ul>
        </div>
    </div>
</nav>
</%def>


<%def name="subheader_navigation(logo, label, search_action=None, search_label='',
                                 search_button_text=None, url=None)">

  <div id="subheader" class="${'logo' if logo else ''}">
      <div class="page_wrapper">
        <div id="logo_row">
            <div id="organisation_logo">
                %if url:
                <a href="${url}">
                %endif
                %if logo:
                <img src="${logo}" alt=""/>
                %endif
                <h2>${label}</h2>
                %if url:
                </a>
                %endif
            </div>
            %if can.instance.edit(c.instance):
            <div id="instance_settings">
              <a href="${h.entity_url(c.instance, member='settings/overview')}" class="button settings">${_("Settings")}</a>
            </div>
            %endif
        </div>

        <div id="nav_row">
            %if search_action:
            <div id="searchbox">
                <form action="${search_action}"
                      id="searchform" method="get">
                    <input type="text" id="s" name="serp_q" placeholder="${search_label|n}"
                           class="search_text" />
                    <input type="submit" value="${search_button_text or _('Search')}"
                           id="searchsubmit" class="search_button" />
                </form>
            </div>
            %endif

            <!-- begin: sub navi -->
            <nav>
                <div class="hlist">
                    <ul id="subnav">
                        ${caller.body()}
                    </ul>
                </div>
            </nav>
        </div>
      </div>
  </div>

</%def>

<%def name="subheader_instance_navigation(active)">

<%
   import mako
   from adhocracy.lib.tiles.instance_tiles import InstanceTile
   active_subnav = {}
   active_subnav[active or 'instance'] = 'current'
   search_label = _('Search in &ldquo;%s&rdquo;&hellip;') % mako.filters.html_escape(c.instance.label)
   logo = ''
   url = None
   if c.instance:
       if InstanceTile(c.instance).show_icon():
           logo = h.logo_url(c.instance, 48)
           url = h.entity_url(c.instance)

   def _class(identifier):
       return active_subnav.get(identifier, '')
%>

<%call expr="subheader_navigation(logo, c.instance.label,
                                  search_action=h.base_url('/search'),
                                  search_label=search_label,
                                  url=url)">

  ${nav_link(href=h.entity_url(c.instance),
             text=h.instance.area_title('instance'),
             li_class=_class('instance'),
             id_='subnav-overview')}

  ${nav_link(href=h.base_url('/proposal'),
             text=h.instance.area_title('proposals'),
             li_class=_class('proposals'),
             condition=c.instance.show_proposals_navigation,
             id_='subnav-proposals')}

  ${nav_link(href=h.base_url('/milestone'),
             text=h.instance.area_title('milestones'),
             li_class=_class('milestones'),
             condition=c.instance.milestones,
             id_='subnav-milestones')}

  ${nav_link(href=h.base_url('/page'),
             text=h.instance.area_title('norms'),
             li_class=_class('norms'),
             condition=c.instance.use_norms and c.instance.show_norms_navigation,
             id_='subnav-norms')}

  ${nav_link(href=h.base_url('/category'),
             text=h.instance.area_title('category'),
             li_class=_class('category'),
             condition=c.instance.display_category_pages,
             id_='subnav-category')}

  ${nav_link(href=h.base_url('/user'),
             text=h.instance.area_title('members'),
             li_class=_class('members'),
             id_='subnav-members')}

</%call>
</%def>

