<% new_record = form_file.new_record? && form_file["file"].blank? %>
<% if new_record %>
  <div class="fileupload fileupload-new start-line" data-provides="fileupload">
<% else %>
  <div class="fileupload fileupload-exists start-line" data-provides="fileupload">
    <% if form_file.file.blank? && form_file["file"].blank? %>
      <%= t(:no_file) %>
    <% else %>
      <% belongs_to_record = form_file.relations.select{|k,v| v.macro == :belongs_to}.first[0] rescue nil %>
      <% url = form_file.file.url %>
      <% if belongs_to_record.present? && (form_file.send(belongs_to_record).copy_id.present? rescue false) %>
        <% begin
         belongs_to_record = belongs_to_record.classify.constantize.find(form_file.send(belongs_to_record).copy_id)
         target_form_files = belongs_to_record.send(form_file.class.to_s.underscore+"s")
         target_form_file = target_form_files.select{|ff| ff["file"] == form_file["file"]}.first
         url = target_form_file.file.url
         rescue
          nil
         end %>
      <% end %>
      <%= link_to content_tag(:i) + form_file.file_identifier, url, {:class => 'file-link file-type', :target => '_blank', :title => form_file.file_identifier} %>
    <% end %>
<% end %>
  <div class="input-prepend input-append">
    <label>
      <span class="add-on btn btn-file" title='<%= t(:file_) %>'>
        <i class="icons-paperclip"></i>
        <%= f.file_field :file %>
      </span>
      <div class="uneditable-input input-medium">
        <i class="icon-file fileupload-exists"></i>
        <span class="fileupload-preview"><%= (new_record) ? t(:select_file) : t(:change_file) %></span>
      </div>
    </label>
    <span class="add-on icons-pencil" title='<%= t(:alternative) %>'></span>
    <span class="tab-content">
      <% @site_in_use_locales.each_with_index do |locale, i| %>
        <span class="tab-pane fade <%= ( i == 0 ) ? "in active" : '' %> <%= locale %>">
          <%= f.fields_for :title_translations do |f| %>
            <%= f.text_field locale, :class => "input-medium", placeholder: t(:alternative), :value => (form_file.title_translations[locale] rescue nil) %>
          <% end %>
        </span>
      <% end %>
    </span>
    <span class="add-on icons-pencil" title='<%= t(:description) %>'></span>
    <span class="tab-content">
      <% @site_in_use_locales.each_with_index do |locale, i| %>
        <span class="tab-pane fade <%= ( i == 0 ) ? "in active" : '' %> <%= locale %>">
          <%= f.fields_for :description_translations do |f| %>
            <%= f.text_field locale, :class => "input-medium", placeholder: t(:description), :value => (form_file.description_translations[locale] rescue nil) %>
          <% end %>
        </span>
      <% end %>
    </span>
    </span>
    <% if new_record %>
      <span class="delete_file add-on btn" title="<%= t(:delete_) %>">
        <a class="icon-trash"></a>
      </span>
    <% else %> 
      <span class="remove_existing_record add-on btn" title="<%= t(:remove) %>">
        <%= f.hidden_field :id unless form_file.new_record?%>
        <a class="icon-remove"></a>
        <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %>
      </span>
    <% end %>
  </div>
</div>