{% extends "happenings/base.html" %}

{% load markup typogrify_tags cache social_tags event_tags humanize %}

{% block extra_title %}Events: {{ object.name }}{% endblock %}

{% block bodyclass %}detail{% endblock %}

{% block content %}
  <article class="vcalendar">
    <section class="content-body">
      {% include "includes/top_assets.html" %}
      {% if object.recap %}
        <h1>{{ object.name }}</h1>
        {{ object.recap|markdown|typogrify }}
      {% else %}
        {% include "happenings/includes/event_info.html" %}
      {% endif %}
      {% social_links object user authenticated_request %}
    </section>

    <hr class="fancy">

    {% include "happenings/includes/latest_updates.html" %}

    {% if object.has_started %}
      {% with object.memories_set.count as memory_count %}
        {% if memory_count %}
          <hr class="fancy">
        {% endif %}
        {% if user in object.attending.all %}
          {% if memory_count == 0 %}
            <h2>
              <a href="{% url 'add_memory' object.slug %}" class="icon-add">
                Share your memories
              </a>
            </h2>
            <p>No one has shared any memories yet. You can be the first.</p>
          {% endif %}

          {% if memory_count > 0 %}
            <a href="{% url 'add_memory' object.slug %}" class="right" class="icon-add">
              Add yours
            </a>
          {% endif %}
        {% endif %} 

        {% if memory_count > 0 %}
          <h2>
            <a href="{% url 'event_memories' object.slug %}">Memories</a>
          </h2>
          <div class="columned two-col">
            {% for m in object.memories_set.all|dictsortreversed:'id'|slice:"2" %}
              <a href="{{ m.get_absolute_url }}">
                {% if m.thoughts %}
                  <p>{{ m.thoughts|truncatewords:"30" }}<br>
                    <cite class="right">&ndash; {{ m.author.display_name }}</cite>
                  </p>
                {% else %}
                  <small>{{ m.author.display_name }} shared some photos</small>
                {% endif %}
              </a>
            {% endfor %}
          </div>
          <a href="{% url 'event_memories' object.slug %}">
            View all {{ memory_count }} memories
          </a>
        {% endif %}
      {% endwith %}
    {% endif %}

    <hr class="fancy">

    {% block comments %}
      {% if object.comment_set.count %}
        <a href="{% url 'event_comments' object.slug %}" class="right">
          <span class="right">{{ count }}</span>
          View all comments
        </a>
      {% endif %}
      {% include 'includes/comments/comments.html' %}
    {% endblock %}
  </article>
  <hr class="clear">
{% endblock %}

{% block aside %}
  <ul class="fancy">
    {% load_event_subnav event user %}
  </ul>

  {% if event.ended %}
    {% if user.is_staff or user.id == event.submitted_by.id %}
      <p>
        <a href="{{ event.get_absolute_url }}add-recap/" class="{% if event.recap %}button icon-edit{% else %}icon-add{% endif %}">
          Add or edit an event recap</a>
      </p>
    {% endif %}
  {% else %}
    {% if user.is_staff or user.id == event.submitted_by.id %}
      <p>
        <a href="{{ event.get_absolute_url }}edit-event/" class="button icon-edit">
          Edit event info
        </a>
      </p>
    {% endif %}
  {% endif %}

  {% if user in event.attending.all %}
    <p>
      <a href="{{ event.get_absolute_url }}memories/add/" title="Share your memories" class="icon-add">
        Add your memories
      </a>
    </p>
  {% endif %}

  {% if event.schedule.count %}
    <h2>Schedule</h2>
    <ul id="event_schedule">
      {% for ev in event.schedule.all|dictsort:"start" %}
        <li>
          {% ifchanged ev.start.day %}<h4>{{ ev.start|date:"N j"}}</h4>{% endifchanged %}
          {% if ev.show_time %}
            <strong>
              {{ ev.start.time }}{% if ev.end %} - {{ ev.end.time }}{% endif %}:
            </strong>
          {% endif %}
          {{ ev.event }}
          <span class="meta">{{ ev.description }}</span>
        </li>
      {% endfor %}
    </ul>
  {% endif %}
  {% block event_sidebars %}
    {% with event.get_sidebars as sidebars %}
      {% if sidebars %}
        {% for side in sidebars %}
          <div>
            <h3>{{side.title}}</h3>
            {{ side.text|markdown|typogrify }}
          </div>
        {% endfor %}
      {% endif %}
    {% endwith %}
  {% endblock %}

  {% with event.related_events.all as related_events %}
    {% if related_events %}
      <h2>Related</h2>
      <ul class="fancy">
        {% for rel in related_events %}
          <li><a href="{{ rel.get_absolute_url }}">{{ rel.name }}</a></li>
        {% endfor %}
      </ul>
    {% endif %}
  {% endwith %}
{% endblock %}
