<%= provide :content_class, 'no-body' %>

<div id="topic">
  <%= render partial: 'breadcrumbs', locals: { topic: @topic } %>

  <h2><%= namelink @topic %></h2>

  <% if @posts.count > Topic::POSTS_PAGE %>
    <%= will_paginate @posts %>
  <% end %>

  <div class="topic-controls">
    <% if @newpost.can_create? cuser %>
      <%= link_to 'Reply', new_post_path(@newpost, id: @topic), class: 'button' %>
    <% end %>
    <% if @topic.can_update? cuser %>
      <%= link_to 'Edit', edit_topic_path(@topic), class: 'button' %>
    <% end %>
  </div>

  <div id="posts">
    <% @posts.each_with_index do |post, n| %>
      <%= render partial: "posts/post", locals: { post: post, n: n } %>
    <% end %>

    <% if @newpost.errors.count > 0 %>
      <div class="flash warning">
        <ul>
          <% @newpost.error_messages.each do |m| %>
            <li><%= h m %></li>
          <% end %>
        </ul>
      </div>
    <% end %>
  </div>

  <div id="reply">
    <h5>New Reply</h5>
    
    <%= form_for(@newpost, remote: true, html: { class: 'square' }) do |f| %>
      <%= f.error_messages %>
      <%= f.hidden_field :topic_id %>
      <div class="fields horizontal">
        <%= f.label "Enter your post text:" %>
        <%= f.text_area :text, rows: 10 %>
      </div>
      <div class="controls horizontal">
        <%= f.submit 'Post Message' %>
      </div>
    <% end %>
  </div>

  <%= will_paginate @posts %>

  <div>
    <% if @newpost.can_create? cuser %>
      <%= link_to_function 'Fast Reply', "$('#reply').fadeIn('slow')", class: 'button' %>
      <%= link_to 'Reply', new_post_path(@newpost, id: @topic), class: 'button' %>
    <% end %>
    <% if @topic.can_update? cuser %>
      <%= link_to 'Edit', edit_topic_path(@topic), class: 'button' %>
    <% end %>

    <% if @lock.new_record? and @lock.can_create? cuser %>
      <%= form_for @lock do |f| %>
        <%= f.hidden_field :lockable_type %>
        <%= f.hidden_field :lockable_id %>
        <%= link_to_function "Lock", "this.parentNode.submit()", class: 'button' %>
      <% end %>
    <% elsif @lock.can_destroy? cuser %>
      <%= link_to 'Unlock', @lock, class: 'button', confirm: 'Are you sure?', method: :delete %>
    <% end %>
  </div>

  <%= render partial: 'breadcrumbs', locals: { topic: @topic } %>
</div>