{% load i18n sekizai_tags aldryn_events %}
{% block calendar_tag_content %}
{% addtoblock "js" %}
<script type="text/javascript">
  $( ".events-calendar .controls a" ).click(function(event) {
    event.preventDefault();
    var wrapper = $(this).closest("div.events-calendar");
    var title = $(wrapper).find("h3");
    var direction = $(this).attr("class");
    var table = $(wrapper).find("table.table-calendar");
    var month = $(table).data('month-numeric');
    var year = $(table).data('year');

    if (direction == 'next' || direction == 'back') {
      if (direction == 'next') {
        if (month == 12) { month = 1; year += 1; } else { month += 1; }

      } else if (direction == 'back') {
        if (month == 1) { month = 12; year -= 1; } else { month -= 1; }
      }
      $.get("{% fallback_aware_namespace_url 'get-calendar-dates' namespace=calendar_tag.namespace %}" + year + "/" + month + "/?plugin_pk={{ plugin.instance.pk }}" ,
        function(data) {
          $(table).replaceWith(data);
          $(title).html($(data).data('month') + ' ' + $(data).data('year'));
        }
      );
    }
  });
</script>
{% endaddtoblock %}
<div class="events-calendar">
    {% if namespace_error %}
        {% if request.user.is_staff or request.user.is_superuser %}
            <div class="events-calendar events-calendar-error">
                <p class="config-errors">{{ namespace_error }}</p>
            </div>
        {% endif %}
    {% else %}
        <h3>{{ calendar_tag.label }}</h3>
        <p class="controls">
            <a href="" class="back">{% trans "Previous month" %}</a>
            <a href="" class="next">{% trans "Next month" %}</a>
        </p>
    {% include "aldryn_events/includes/calendar_table.html" %}
  {% endif %}
</div>
{% endblock %}
