<script type="text/javascript">
  function getPage(currentPage) {
    document.location.href = '?current_page='+currentPage+'&activity=<%= Rack::Utils.escape(params[:activity]) %>'
  }
</script>
<script src="/js/news/template.js"></script>
<script src="/js/news/like.js"></script>
<script src="/js/news/comment.js"></script>
<script src="/js/news/profile_comment.js"></script>
<script src="/js/news/event.js"></script>
<script src="/js/news/site.js"></script>

<% events.each do |event| %>
  <% if event.profile_comment_id %>
    <div class="news-item comment for-me" id="event_<%= event.id %>">
      <%== erb :'_news_profile_comment', layout: false, locals: {profile_comment: event.profile_comment} %>
  <% elsif event.follow_id %>
    <div class="news-item follow">
      <div class="title">
      <div class="icon"></div>
      <% actioning_site = event.actioning_site_dataset.select(:id, :username, :title, :domain, :stripe_customer_id).first %>
      <% event_site = event.site_dataset.select(:id, :username, :title, :domain, :stripe_customer_id).first %>

      <% if current_site && current_site.id == actioning_site.id %>
        You
      <% else %>
        <i class="fa fa-user"><% if actioning_site.supporter? && !actioning_site.ended_supporter? %><i class="fa fa-heart"></i><% end %></i><a href="/site/<%= actioning_site.username %>" class="user" title="<%= actioning_site.title %>"><%= actioning_site.username %></a>
      <% end %>

      started following

      <% if current_site && event_site.id == current_site.id %>
        your site!
      <% else %>
        <a href="/site/<%= event_site.username %>" class="user" title="<%= event_site.title %>"><i class="fa fa-user"><% if event_site.supporter? && !event_site.ended_supporter? %><i class="fa fa-heart"></i><% end %></i><%= event_site.username %></a>
      <% end %>

      <span class="date">
        <a href="?event_id=<%= event.id %>"><%= event.created_at.ago %></a>
      </span>
    </div>
  <% elsif event.site_change_id %>
    <div class="news-item update">
      <div class="title">
        <div class="icon"></div>
        <% event_site = event.site_dataset.select(:id, :username, :title, :domain).first %>
        <% if current_site && event_site.id == current_site.id %>
          Your site was updated.
        <% else %>
          <a href="/site/<%= event_site.username %>" class="user" title="<%= event_site.title %>"><%= event_site.title.shorten(45) %></a> has been updated.
        <% end %>
        <span class="date">
          <a href="?event_id=<%= event.id %>"><%= event.created_at.ago %></a>
        </span>
      </div>
      <% site_change_filenames = event.site_change.site_change_filenames %>
      <% unless site_change_filenames.empty? %>
        <div class="content">
          <div class="files">
            <% site_change_filenames.each do |f| %>
              <div class="file">
                <div class="html-thumbnail <%= site_change_file_display_class f %>">
                  <a href="//<%= event_site.host %>/<%= f %>">
                    <% if site_change_file_display_class(f) == 'html' %>
                      <img src="<%= event_site.screenshot_url(f, '210x158') %>">
                    <% elsif site_change_file_display_class(f) == 'image' %>
                      <img src="<%= event_site.thumbnail_url(f, '210x158') %>">
                    <% elsif site_change_file_display_class(f) == 'misc' %>
                      <span class="misc-icon">
                        <%= File.extname(f).sub('.', '') %>
                      </span>
                    <% end %>
                    <span class="title" title="<%= f %>"><%= File.basename f %></span>
                  </a>
                </div>
              </div>
            <% end %>
          </div>
        </div>
      <% end %>
  <% end %>

  <% if !event.follow_id %>
    <%== erb :'_news_actions', layout: false, locals: {event: event} %>
  <% end %>

  <% if event.comments_dataset.count > 0 %>
    <div class="content">
      <div class="comments">
        <% event.comments.each do |comment| %>
          <% comment_actioning_site = comment.actioning_site_dataset.select(:id, :title, :domain, :username, :stripe_customer_id).first %>
          <div class="comment" id="comment_<%= comment.id %>">
            <img class="avatar" src="<%= comment_actioning_site.screenshot_url('index.html', '50x50') %>">
            <i class="fa fa-user"><% if comment_actioning_site.supporter? && !comment_actioning_site.ended_supporter? %><i class="fa fa-heart"></i><% end %></i><a href="/site/<%= comment_actioning_site.username %>" class="user" title="<%= comment_actioning_site.title %>"><%= comment_actioning_site.username %></a>
            <span class="date">
              <a href="?event_id=<%= event.id %>"><%= comment.created_at.ago %></a>
            </span>
            <p><%= comment.message %></p>
          </div>
          <div class="actions">
            <% comment_likes_count = comment.comment_likes_dataset.count %>
            <% if current_site %>
              <a href="#" class="comment_like" id="comment_<%= comment.id %>_like" data-placement="bottom" data-toggle="tooltip" data-original-title="<%= comment.liking_site_titles.join('<br>') %>" onclick="Comment.toggleLike(<%= comment.id %>, '<%= csrf_token %>'); return false"><%= comment.site_likes?(current_site) ? 'Unlike' : 'Like' %><%= comment_likes_count > 0 ? " (#{comment_likes_count})" : '' %></a>
            <% else %>
              <% if comment_likes_count > 0 %>
                <%= comment_likes_count %>&nbsp;<%= comment_likes_count == 1 ? 'like' : 'likes' %>
              <% end %>
            <% end %>

            <% if current_site %>
              <% if event.site_id == current_site.id || comment.actioning_site_id == current_site.id %>
                <a href="#" onclick="Comment.delete(<%= comment.id %>, '<%= csrf_token %>'); return false">Delete</a>
              <% end %>
            <% end %>
          </div>
        <% end %>
      </div>
    </div>
  <% end %>

  </div>

<% end %>

<div class="content">
  <% if @page_count > 1 %>
    <div class="txt-Center content eps pagination">
      <% if @current_page != 1 %>
        <a href="#" onclick="getPage(<%= @current_page - 1 %>); return false"><i class="fa fa-arrow-left arrow"></i></a>&nbsp;
      <% end %>

      <% 1.upto(@page_count) do |num| %>
        <% if num == @current_page %>
          <%= num %>&nbsp;
        <% else %>
          <a href="#" onclick="getPage(<%= num %>); return false"><%= num %></a>&nbsp;
        <% end %>

      <% end %>

      <% if @current_page != @page_count %>
        <a href="#" onclick="getPage(<%= @current_page + 1 %>); return false"><i class="fa fa-arrow-right arrow"></i></a>
      <% end %>
    </div>
  <% end %>
</div>

<%== erb :'_news_templates', layout: false %>
