<div class="attachments">

<div class="contextual">
  <%= link_to(l(:label_edit_attachments),
              container_attachments_edit_path(container),
              :title => l(:label_edit_attachments),
              :class => 'icon-only icon-edit'
             ) if options[:editable] %>
</div>

<table>
<% for attachment in attachments %>
<% caption = "#{attachment.filename}#{ (' - ' + attachment.description) unless attachment.description.blank? }" %>
<tr>
  <td>
    <% if attachment.is_image? %>
      <%= link_to_attachment attachment, :class => 'icon icon-attachment lightbox_preview', :download => true -%>
      </td>
      <td class="alignrigth">
      <%= link_to l(:button_view),
                  { :controller => 'attachments', :action => 'download', :id => attachment, :filename => attachment.filename },
                  :class => 'icon-only icon-magnifier lightbox_preview ' + attachment.filename.split('.').last.downcase,
                  :rel => 'attachments', :title => l(:label_preview),
                  :data  => {
                    :type => 'image',
                    :fancybox => 'attachments',
                    :caption => caption
                  } %>

    <% elsif attachment.is_pdf? %>
      <%= link_to_attachment attachment, :class => 'icon icon-attachment lightbox_preview pdf', :download => true -%>
      </td>
      <td class="alignrigth">
      <%= link_to l(:button_view),
                  { :controller => 'attachments', :action => 'download_inline', :id => attachment, :filename => attachment.filename },
                  :class => 'icon-only icon-magnifier lightbox_preview pdf',
                  :rel => 'attachments', :title => l(:label_preview),
                  :data  => {
                    :type => 'iframe',
                    :fancybox => 'attachments',
                    :caption => caption
                  } %>

    <% else %>
      <%= link_to_attachment attachment, :class => 'icon icon-attachment' -%>
      </td>
      <td class="alignrigth">
    <% end %>
    <%= link_to_attachment attachment, class: 'icon-only icon-download', title: l(:button_download), download: true -%>
  </td>
  <td class="alignrigth">
    <span class="size"><%= number_to_human_size attachment.filesize %></span>
  </td>
  <td><%= attachment.description unless attachment.description.blank? %></td>
  <td>
    <% if options[:author] %>
      <span class="author"><%= attachment.author %>, <%= format_time(attachment.created_on) %></span>
    <% end %>
  </td>
  <td>
    <% if options[:deletable] %>
      <%= link_to l(:button_delete), attachment_path(attachment),
                  :data => {:confirm => l(:text_are_you_sure)},
                  :method => :delete,
                  :class => 'delete icon-only icon-del',
                  :title => l(:button_delete) %>
    <% end %>
  </td>
</tr>
<% end %>
</table>

<% if defined?(thumbnails) && thumbnails %>
  <% images = attachments.select(&:thumbnailable?) %>
  <% if images.any? %>
  <div class="thumbnails images">
    <% images.each do |attachment| %>
      <% caption = "#{attachment.filename}#{ (' - ' + attachment.description) unless attachment.description.blank? }" %>
      <div>
        <%=
          link_to image_tag(url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment)),
          { :controller => 'attachments', :action => 'download', :id => attachment, :filename => attachment.filename },
          :class => ('lightbox' + (attachment.is_pdf? ? ' pdf' : '')) , :rel => 'thumbnails', :title => l(:label_preview) + ': ' + caption,
          :data  => {
            :type => attachment.is_pdf? ? 'iframe' : 'image',
            :fancybox => 'thumbnails',
            :caption => caption
          }
        %>
      </div>
    <% end %>
  </div>
  <% end %>
<% else %>
  <% images = attachments.select { |a| a.image? } %>
  <% unless images.empty? %>
    <div class='images'>
      <% images.each do |attachment| %>
        <% caption = "#{attachment.filename}#{ (' - ' + attachment.description) unless attachment.description.blank? }" %>
        <%= link_to image_tag(url_for({:controller => 'attachments', :action => 'download', :id => attachment, :filename => attachment.filename }), :width => Setting.thumbnails_size),
          { :controller => 'attachments', :action => 'download', :id => attachment, :filename => attachment.filename },
          :class => 'lightbox', :rel => 'thumbnails', :title => l(:label_preview) + ': ' + caption,
          :data  => {
            :type => attachment.is_pdf? ? 'iframe' : 'image',
            :fancybox => 'thumbnails',
            :caption => caption
          }
        %>
      <% end -%>
    </div>
  <% end -%>
<% end %>

</div>
