{% load assets %}
<h2><span><a href="/references">Références</a></span></h2>
<div class="carrousel-references-content">
 {% for menuitem in menuitems %}
  <input type="radio" name="carrousel-{{cell.id}}"
     class="carrousel-radio-{{forloop.counter}}"
     id="carrousel-{{cell.id}}-{{forloop.counter}}"
     {% if forloop.first %}checked{% endif %}>
  <div class="slide">
     {% block carrousel-item %}
     <div class="carrousel-item"
          style="background-image: url({% asset_url menuitem.page.picture size="1000" crop="center" %})">
      <div class="carrousel-item-content">
       {% block carrousel-item-content %}
       <a href="{{menuitem.page.get_online_url}}">
       <strong>{{menuitem.page.title}}</strong>
       {% if menuitem.page.description %}<p>{{ menuitem.page.description }}</p>{% endif %}
       </a>
       {% endblock %}
      </div>
     </div>
     {% endblock %}
  </div>
 {% endfor %}
 <a class="carrousel-references-previous"></a>
 {% block carrousel-navigation %}
 <div class="carrousel-nav">
 {% for menuitem in menuitems %}
 <label class="carrousel-label-{{forloop.counter}}" for="carrousel-{{cell.id}}-{{forloop.counter}}"></label>
 {% endfor %}
 </div>
 {% endblock %}
 <a class="carrousel-references-next"></a>
</div>
<script>
$(function() {
  function next() {
    if ($('.carrousel-references-content input:checked ~ input').length) {
      $('.carrousel-references-content input:checked ~ input').first().click();
    } else {
      $('.carrousel-references-content input').first().click();
    }
  }
  function previous() {
    if ($('.carrousel-references-content input ~ input:checked').length) {
      $('.carrousel-references-content input:checked').prev().prev().click();
    } else {
      $('.carrousel-references-content input').last().click();
    }
  }
  var timeout_references_id = setInterval(next, 5000);
  $('.carrousel-references-content label').on('click', function() {
    clearInterval(timeout_references_id);
  });
  $('a.carrousel-references-previous').on('click', function() {
    clearInterval(timeout_references_id);
    previous();
  });
  $('a.carrousel-references-next').on('click', function() {
    clearInterval(timeout_references_id);
    next();
  });

  var touch_start = null;
  $('.carrousel-item').on('touchstart', function(ev) {
    touch_start = ev.originalEvent.touches[0].pageX;
  });
  $('.carrousel-item').on('touchend', function(ev) {
    var touch_end = ev.originalEvent.changedTouches[0].pageX;
    var diff = touch_start - touch_end;
    touch_start = null;
    clearInterval(timeout_references_id);
    var width = $('body').width();
    if (diff*2 > width) {
      previous();
    }
    if (diff*2 < -width) {
      next();
    }
  });
});
</script>
