{% load url from future %}
{% load i18n %}
{% load agon_ratings_tags %}
{% load layers_tags %}
{% load base_tags %}
{% load geonode_auth %}

{% overall_rating layer "layer" as layer_rating %}
{% num_ratings layer as num_votes %}

{% has_obj_perm user layer "layers.view_layer" as can_view_layer %}

{% if can_view_layer %}
<article>
  <div class="content">
    <div class="item-header">
      <a href="{% url "layer_detail" layer.typename %}">
      {% if layer.get_thumbnail_url %}
      <img class="thumb" src="{{ layer.get_thumbnail_url }}" />
      {% else %}
      <img class="thumb" src="{{  STATIC_URL }}/geonode/img/missing_thumb.png" />
      {% endif %}
      </a>
      <h3><i class="icon-unchecked icon-rotate-45"></i> <a href="{% url "layer_detail" layer.typename %}">{{ layer.title }}</a></h3>   
    </div>
    <div class="details">
      <div class="meta">
        <p>Layer {% trans 'from' %} <a href="{% url "profile_detail" layer.owner.username %}">{{ layer.owner }}</a>, {{ layer.date|timesince }} {% trans 'ago' %}</p>
        <p class="abstract">{{ layer.abstract }}</p>
        <ul class="nav stats">
          <li><a href="{% url "layer_detail" layer.typename %}"><strong>{{ layer.popular_count }}</strong> {% trans "view" %}{{ layer.popular_count|pluralize }}</a></li>
          <li><a href="{% url "layer_detail" layer.typename %}#rate"><strong><span class="avg_rating" data-rating="{{ layer_rating }}"> <span class="overall_rating"></span></span></strong> {% trans "Average rating" %} ({{num_votes}} {% trans "votes" %})</a></li>
        </ul>
      </div> <!-- /.meta -->
      <div class="tools">
        <p>
          <a href="#download-{{layer.id}}" data-toggle="modal" class="btn btn-mini">{% trans "Download" %}</a>
          <a href="{% url "new_map" %}?layer={{ layer.typename }}" class="btn btn-mini">{% trans "Create a map" %}</a>
        </p>
      </div>
    </div> <!-- /.details -->  
  </div> <!-- /.content -->

  
  <div class="modal custom hide" id="download-{{layer.id}}">
    <div class="modal-header">
      <button class="close" data-dismiss="modal">×</button>
      <h2><i class="icon-download-alt"></i>  {% trans "Download Layer" %} "{{layer.title}}"</h2>
    </div>
    <div class="modal-body">
    </div>
    <div class="modal-footer">
      {% if layer.link_set.download|length > 0 %}
        {% for link in layer.link_set.download %}
        {% if forloop.counter0 == 0 %}
        <div class="span2 offset1">
          <ul class="unstyled">
            <li><a href="{{ link.url }}">{{ link.name }}</a></li>
        {% elif forloop.counter0 >= forloop.revcounter0 and forloop.revcounter0|add:1 >= forloop.counter0 %}
          </ul>
        </div>
        <div class="span2">
          <ul class="unstyled">
            <li><a href="{{ link.url }}">{{ link.name }}</a></li>
        {% else %}
            <li><a href="{{ link.url }}">{{ link.name }}</a></li>
        {% endif %}
        {% endfor %} 
        </ul>
      </div>
    {% endif %} 
    </div>
  </div>
</article>
{%  else %}
<article>
    <div class="content">
        <div class="item-header">
            <img class="thumb" src="{{  STATIC_URL }}/geonode/img/missing_thumb.png" />
            <h3><i class="icon-unchecked icon-rotate-45"></i> {{ layer.title }}</h3>
        </div>
        <div class="details">
            <div class="meta">
                <p>Layer {% trans 'from' %} <a href="{% url "profile_detail" layer.owner.username %}">{{ layer.owner }}</a>, {{ layer.date|timesince }} {% trans 'ago' %}</p>
                <p class="abstract">{%  trans "You do not have permission to view this layer" %}</p>
                <ul class="nav stats">
                    <li><a><strong>{{ layer.popular_count }}</strong> {% trans "view" %}{{ layer.popular_count|pluralize }}</a></li>
                    <li><a><strong><span class="avg_rating" data-rating="{{ layer_rating }}"> <span class="overall_rating"></span></span></strong> {% trans "Average rating" %} ({{num_votes}} {% trans "votes" %})</a></li>
                </ul>
            </div> <!-- /.meta -->
            <div class="tools">
            </div>
        </div> <!-- /.details -->
    </div> <!-- /.content -->
</article>
{%  endif %}
