<% provide :title, (@user == @current_user ? t('.own_title') : t('.title', :name => @user.name)) %>
<% if @user.has_avatar? then %>
  <div style="width: 25em; height: <%= [@user.avatar_height, 80].max %>px; position: relative;">
    <div style="position: absolute; bottom: 0;">
      <%= avatar(@user, 1) %>
    </div>
    <div style="position: absolute; bottom: 0; margin-bottom: 15px; left: <%= @user.avatar_width+5 %>px; ">
      <% if @current_user.has_permission?(@user) then %>
        &nbsp;<%= link_to t(:user_edit), :controller => "user", :action => "set_avatar", :id => @user.avatar_post.id.to_s, :user_id => @user.id %>
        <%= link_to t('.avatar.remove.link'), { :controller => '/user', :action => :remove_avatar, :id => @user.id }, :method => :post, :data => { :confirm => t('.avatar.remove.confirm') } %>
      <% end %>
      <h2><%= h(@user.pretty_name) %></h2>
    </div>
  </div>
<% else %>
  <h2><%= h(@user.pretty_name) %></h2>
<% end %>

<div style="float: left; width: 25em; clear: left;">
  <table width="100%">
    <tr>
      <td width="40%"><strong><%=t :user_join %></strong></td>
      <td width="60%"><%= @user.created_at.strftime("%Y-%m-%d") %>
      </td>

    </tr>
    <% if @user.level < 20 or @user.level > 33 or @current_user.is_mod_or_higher? then %>
    <tr>
      <td><strong><%=t :user_level %></strong></td>
      <td>
        <%= @user.pretty_level %>
        <% if @user.is_blocked? && @user.ban %>
          <%=t :user_reason2 %><%= h @user.ban.reason %><%=t :user_expire2 %><%= @user.ban.expires_at.strftime("%Y-%m-%d") %><%=t :user_reason3 %>
        <% end %>
      </td>
    </tr>
    <% end %>
    <tr>
      <td><strong><%=t :user_subs %></strong></td>
      <td class="large-text">
        <%= render :partial => "tag_subscription/user_listing", :locals => {:user => @user} %>
      </td>
    </tr>
    <tr>
      <td><strong><%=t :user_posts %></strong></td>
      <td><%= link_to Post.where(:user_id => @user.id).count, :controller => "post", :action => "index", :tags => "user:#{@user.name}" %></td>
    </tr>
    <tr>
      <td><strong><%=t :user_deleted_posts %></strong></td>
      <td><%= link_to Post.where(:user_id => @user.id).where("status = ?", "deleted").count, :controller => "post", :action => "deleted_index", :user_id => @user.id %></td>
    </tr>
    <tr>
      <th><%=t :user_votes %></th>
      <td>
        <span class="stars">
          <% (1..3).each do |i| %>
            <a class="star" data-star="<%= i %>" href="<%= url_for :controller => "post", :action => "index", :tags => "vote:>=#{i}:#{@user.name} order:vote" %>">
              <%= @user.post_votes.where(:score => i).count %>
              <span class="score-on score-voted score-visible">★</span>
            </a>
          <% end %>
        </span>
      </td>
    </tr>
    <tr>
      <td><strong><%=t :user_comments %></strong></td>
      <td><%= link_to Comment.where(:user_id => @user.id).count, :controller => "comment", :action => "search", :query => "user:#{@user.name}" %></td>
    </tr>
    <tr>
      <td><strong><%=t :user_edits %></strong></td>
      <td><%= link_to History.where(:user_id => @user.id).count, :controller => "history", :action => "index", :search => "user:#{@user.name}" %></td>
    </tr>
    <tr>
      <td><strong><%=t :user_tag_edits %></strong></td>
      <td><%= link_to History.where(:user_id => @user.id, :group_by_table => "posts").count, :controller => "history", :action => "index", :search => "type:post user:#{@user.name}" %></td>
    </tr>
    <tr>
      <td><strong><%=t :user_note_edits %></strong></td>
      <td><%= link_to NoteVersion.where(:user_id => @user.id).count, :controller => "note", :action => "history", :user_id => @user.id %></td>
    </tr>
    <tr>
      <td><strong><%=t :user_wiki_edits %></strong></td>
      <td><%= link_to WikiPageVersion.where(:user_id => @user.id).count, :controller => "wiki", :action => "recent_changes", :user_id => @user.id %></td>
    </tr>
    <tr>
      <td><strong><%=t :user_forum_posts %></strong></td>
      <td><%= ForumPost.where(:creator_id => @user.id).count %></td>
    </tr>
    <% if @user.invited_by %>
      <tr>
        <td><strong><%=t :user_invited_by %></strong></td>
        <td><%= link_to h(User.find(@user.invited_by).name), :action => "show", :id => @user.invited_by %></td>
      </tr>
    <% end %>
    <% if CONFIG["starting_level"] < 30 %>
    <tr>
      <td><strong><%=t :user_rec_invites %></strong></td>
      <td><%= User.where(:invited_by => @user.id).order(:id => :desc).select(:name, :id).limit(5).map {|x| link_to(h(x.pretty_name), :action => "show", :id => x.id)}.join(", ").html_safe %></td>
    </tr>
    <% end %>
    <tr>
      <td><strong><%=t :user_record %></strong></td>
      <td>
        <% if UserRecord.where(:user_id => @user.id).any? %>
          <%= UserRecord.where(:user_id => @user.id, :is_positive => true).count - UserRecord.where(:user_id => @user.id, :is_positive => false).count %>
        <% else %>
          <%=t :user_record_none %>
        <% end %>
        (<%= link_to t(:user_record_add), :controller => "user_record", :action => "index", :user_id => @user.id %>)
      </td>
    </tr>
    <% if @current_user.is_mod_or_higher? %>
      <tr>
        <td><strong><%=t :user_ip %></strong></td>
        <td>
          <% @user_ips[0,5].each do |ip| %>
          <%= ip %>
          <% end %>
          <% if @user_ips.length > 5 %>(more)<% end %>
        </td>
      </tr>
    <% end %>
  </table>
</div>

<div style="float: left; width: 60em;">
  <table width="100%">
    <% CONFIG["tag_types"].select {|k, v| k =~ /^[A-Z]/ && k != "General" && k != "Faults"}.each do |name, value| %>
      <tr>
        <th><%=t :user_fav2 %><%= name.pluralize %></th>
        <td><%= @user.voted_tags(:type => value).map {|tag| link_to tag["tag"].tr("_", " "), :controller => "post", :action => "index", :tags => "vote:3:#{@user.name} #{tag['tag']} order:vote"}.join(", ").html_safe %></td>
      </tr>
    <% end %>
    <tr>
      <th><%=t :user_uploaded_tags %></th>
      <td><%= @user.uploaded_tags.map {|tag| link_to tag["tag"].tr("_", " "), :controller => "post", :action => "index", :tags => "user:#{@user.name} #{tag['tag']}"}.join(", ").html_safe %></td>
    </tr>
    <% CONFIG["tag_types"].select {|k, v| k =~ /^[A-Z]/ && k != "General" && k != "Faults"}.each do |name, value| %>
      <tr>
        <th><%=t :user_uploaded %><%= name.pluralize %></th>
        <td><%= @user.uploaded_tags(:type => value).map {|tag| link_to tag["tag"].tr("_", " "), :controller => "post", :action => "index", :tags => "user:#{@user.name} #{tag['tag']}"}.join(", ").html_safe %></td>
      </tr>
    <% end %>
  </table>
</div>

<% @user.tag_subscriptions.visible.each do |tag_subscription| %>
  <div style="margin-bottom: 1em; float: left; clear: both;">
    <h4><%=t :user_sub2 %><%= tag_subscription.name %> <%= link_to "»", :controller => "post", :action => "index", :tags => "sub:#{@user.name}:#{tag_subscription.name}" %></h4>
    <%= render :partial => "post/posts", :locals => {:posts => @user.tag_subscription_posts(5, tag_subscription.name).select {|x| CONFIG["can_see_post"].call(@current_user, x)}} %>
  </div>
<% end %>

<div style="margin-bottom: 1em; float: left; clear: both;">
  <h4><%= link_to t(:user_fav3), :controller => "post", :action => "index", :tags => "vote:3:#{@user.name} order:vote" %></h4>
  <%= render :partial => "post/posts", :locals => {:posts => @user.recent_favorite_posts.select {|x| CONFIG["can_see_post"].call(@current_user, x)}} %>
</div>

<div style="margin-bottom: 1em;  float: left; clear: both;">
  <h4><%= link_to t(:user_uploads), :controller => "post", :action => "index", :tags => "user:#{@user.name}" %></h4>
  <%= render :partial => "post/posts", :locals => {:posts => @user.recent_uploaded_posts.select {|x| CONFIG["can_see_post"].call(@current_user, x)}} %>
</div>

<% content_for :footer do %>
  <li><%= link_to t(:user_list), :controller => "user", :action => "index" %></li>
  <% if @current_user.is_mod_or_higher? %>
    <li><%= link_to t(:user_ban), :controller => "user", :action => "block", :id => @user.id %></li>
  <% end %>
  <% if @current_user.is_janitor_or_higher? && @user.is_member_or_lower? %>
    <li><%= link_to t(:user_invite_link), :controller => "user", :action => "invites", :name => @user.name %></li>
  <% end %>
  <li><%= link_to t(:user_send_msg), :controller => "dmail", :action => "compose", :to => @user.name %></li>
<% end %>

<%= render :partial => "footer" %>
