<h1 class="title">Files</h1>

<% 
  active = 1
  n = 1
%>

<div id="files" class="tabbed">
  <ul id="files-nav" class="tabs">
    <% @directories.each do |dir| %>
      <li><a href="#dir_<%= dir.id %>"><%= h dir.name %></a></li>
    <% end %>
  </ul>

  <div class="tabbed-contents">
    <% @directories.each do |dir| %>
      <div class="tab" id="dir_<%= dir.id %>">
      
        <%
          if @directory.path.include?(dir.path)
            dir = @directory
            active = n
          end
          n = n + 1
        %>

        <div class="directories">
          <% if !dir.subdirs.ordered.empty? or dir.parent.id != Directory::ROOT %>
            <h3>Directories</h3>
          <% end %>

          <div class="subdirectories">
            <ul class="disc">
              <% if dir.parent.id != Directory::ROOT %>
                <li class="parent">
                  <%= link_to "Parent", dir.parent %>
                </li>
              <% end %>
              <% dir.subdirs.ordered.each do |subdir| %>
                <li><%= namelink subdir %></li>
              <% end %>
            </ul>
          </div>
        </div>
      
        <h3>Files</h3>

        <% dir.files.unrelated.each do |file| %>
          <div class="file">
            <div class="actions">
              <% if file.can_update? cuser %>
                <%= link_to icon('pencil'), edit_data_file_path(file) %>
              <% end %>

              <% if file.can_destroy? cuser %>
                <%= link_to icon('times'), file, confirm: 'Are you sure?', method: :delete %>
              <% end %>
            </div>

            <h4><%= namelink file %></h4>
            <p><strong>Size:</strong> <%= file.size_s %>MB</p>

            <div class="comments">
              <%= link_to file do %>
                <%= icon('comments') %> Comments (<%= file.comments.count %>)
              <% end %>
            </div>

            <%= link_to 'Download', file.url, class: 'button tiny' %>
          </div>
        <% end %>

        <% if dir.files.empty? %>
          <p>Empty directory.</p>
        <% end %>

        <% if cuser and cuser.admin? %>
          <div class="controls">
            <%= link_to "Edit Directory", edit_directory_path(dir), class: 'button' %>
            <%= link_to "Delete Directory", dir, confirm: "Are you REALLY sure?", method: :delete, class: 'button' %>
            <%= link_to "New Directory", { controller: "directories", action: "new", id: dir }, { class: 'button' } %>
            <%= link_to "New File", { controller: "data_files", action: "new", id: dir }, { class: 'button' } %>
          </div>
        <% end %>
      </div>
    <% end %>
  </div>
</div>

<script type="text/javascript">
  new Yetii({
    id: 'files',
    active: <%= active %>
  });
</script>
