<nav id="org-navbar" class="navbar navbar-default" role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header" >
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#org-navbar-menu" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <% if user_signed_in? && !current_user.org.nil? %>
        <% if current_user.org.logo.present? %>
          <%= link_to(image_tag(logo_url_for_org(current_user.org),
                                alt: current_user.org.name,
                                class: "org-logo",
                                title: current_user.org.name),
                      current_user.org.target_url) %>
        <% else %>
          <h4 id="banner-org-name"><%= current_user.org.name %></h4>
        <% end %>
      <% end %>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="org-navbar-menu">
      <!-- Organisational links -->
      <% if user_signed_in? and !current_user.org.links.nil? and !current_user.org.links['org'].blank? %>
        <ul class="nav navbar-nav">
          <% i=1; current_user.org.links['org'].each do |link| %>
            <% if !link.blank? && i <= max_number_links %>
              <% if i.odd? %>
                </li><li class="org-links">
              <% end %>
              <%= link_to link['link'], :target=>'_blank', :class => 'org-a' do %>
                <i class="fa fa-globe" aria-hidden="true">&nbsp;</i>
                <%= link['text'].blank? ? link['link'] : link['text'] %>
                <em class="sr-only"> (new window)</em>
              <% end; i+=1 %>
            <% end %>
          <% end %>
          <% if i.odd? %>
            </li><li class="org-links">
          <% end %>
          <% if !current_user.org.contact_email.blank? %>
            <%= mail_to current_user.org.contact_email, :class => 'org-a' do %>
              <i class="fa fa-envelope" aria-hidden="true">&nbsp;</i>
              <%= current_user.org.contact_name ? current_user.org.contact_name : current_user.org.contact_email %>
            <% end %>
          <% end %>
        </ul>
        <div class="org-banner-text">
        <%  if !current_user.org.banner_text.nil?  %>
          <%= current_user.org.banner_text.html_safe %>
        <% end %>
        </div>
      <% end %>

      <!-- Navigation for organisation admin -->
      <ul class="nav navbar-nav navbar-right">
        <!-- if org admin -->
        <% if current_user.can_org_admin? %>
          <li class="dropdown navbar-default" id="admin-dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" id="admin-menu" href="#">
              <i class="fa fa-lock" aria-hidden="true">&nbsp;</i>
              <%= _('Admin') %>
              <span class="caret"></span>
            </a>
            <ul class="dropdown-menu" aria-labelledby="admin-menu">
              <% if current_user.can_org_admin? %>
                <li <%= 'class=active' if active_page?(org_admin_plans_path) %>>
                  <%= link_to _('Plans'), org_admin_plans_path %>
                </li>
              <% end %>
              <% if current_user.can_modify_templates? %>
                <% template_path = current_user.can_super_admin? ? org_admin_templates_path : organisational_org_admin_templates_path %>
                  <li <%= 'class=active' if active_page?(template_path) %>>
                    <%= link_to _('Templates'), template_path %>
                  </li>
              <% end %>
              <% if current_user.can_modify_guidance? %>
                  <li <%= 'class=active' if active_page?(admin_index_guidance_path(current_user.org_id)) %>>
                    <%= link_to _('Guidance'), admin_index_guidance_path(current_user.org_id) %>
                  </li>
              <% end %>
              <% if current_user.can_super_admin? %>
                  <li <%= 'class=active' if active_page?(admin_edit_org_path(current_user.org_id)) %>>
                    <%= link_to _('Organisations'), super_admin_orgs_path %>
                  </li>
              <% else %>
                <% if current_user.can_modify_org_details? %>
                    <li <%= 'class=active' if active_page?(admin_edit_org_path(current_user.org_id)) %>>
                      <%= link_to _('Organisation details'), admin_edit_org_path(current_user.org_id) %>
                    </li>
                <% end %>
              <% end %>
              <% if current_user.can_grant_permissions? %>
                  <li <%= 'class=active' if active_page?(admin_index_users_path) %>>
                    <%= link_to _('Users'), admin_index_users_path, class: 'main_nav_last_li' %>
                  </li>
              <% end %>
              <% if current_user.can_super_admin? %>
                <li <%= 'class=active' if active_page?(super_admin_themes_path) %>>
                  <%= link_to(_('Themes'), super_admin_themes_path) %>
                </li>
              <% end %>
              <% if current_user.can_org_admin? || current_user.can_super_admin? %>
                <li <%= 'class=active' if active_page?(usage_index_path) %>>
                  <%= link_to(_('Usage'), usage_index_path) %>
                </li>
              <% end %>
              <% if current_user.can_super_admin? %>
                <li <%= 'class=active' if active_page?(super_admin_notifications_path) %>>
                  <%= link_to _('Notifications'), super_admin_notifications_path %>
                </li>
              <% end %>
            </ul>
          </li>
        <% end %> <!-- if org admin -->
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
