{% load thumbnail %}
<h2><span><a href="/references">Références</a></span></h2>
<div class="carrousel-references-content">
  {% for e in json.data %}
  <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">
      <div class="carrousel-item-image">
          <span class="helper"></span>
        {% thumbnail e.logo "240x160" as im %}
        <img src="{{ im.url }}">
        {% endthumbnail %}
      </div>
     </div>
     {% endblock %}
  </div>
 {% endfor %}
 <a class="carrousel-references-previous"></a>
 {% block carrousel-navigation %}
 <div class="carrousel-nav">

 {% for e in json.data %}
 <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>
