{% load i18n %}
{% load txcommontags %}
{% load permissions %}
{% load statistics_resources %}
{% load upload_manager_tags %}

<div id="resources" class="resources separate">
  <h3>{% blocktrans %}Project Resources{% endblocktrans %}</h3>

<script type="text/javascript">
    $(document).ready(function(){
        $(".res_tipsy_enable").tipsy({'html':true, 'gravity':'s'});
        $(".tablesorter_resource_list").tablesorter({
            widgets: ['zebra'],
            textExtraction: { // Take value inside an object for the columns
              0: function(node) {
                  return $("a", node).text();
              },
              1: function(node) {
                  return $(".origin_format", node).text();
              },
              2: function(node) {
                  return parseInt($(".priority_sort", node).text());
              }
            }
        });
        {% if request.user.is_authenticated %}
        $("#new_translation1").click( function (){ 
            $("#new_translation_box1").toggle();
        });
        $("#start_new_translation").click( function() {
            var target_lang_code = $(this).prev().val();
            if ( target_lang_code != "" ) {
                //request_url = window.location + target_lang_code;
                //This is _UGLY_. We need to find a way to do reverse url
                //lookups from JS code. #FIXME
                request_url ='/resources/project/{{project.slug}}/'+ target_lang_code;
                window.location = request_url;
            } else {
                alert("Please select a target language first and then click Go.");
            }
        });
        {% endif %}
        {% if is_maintainer %}
        var resource_priority_cycle_url = '{% url cycle_resource_priority project.slug "1111111111" %}';
        $("a.resource_priority_trigger").each(function(){
          $(this).click(function(){
            id_string = $(this).attr("id");
            var slug = id_string.substring(id_string.indexOf("_")+1);
            var pr_url = resource_priority_cycle_url.replace('1111111111', slug)
            $(this).load(pr_url, function(response, status, xhr) {
                if (status == "error") {
                    var msg = "Sorry but there was an error: ";
                    alert(msg + xhr.status + " " + xhr.statusText);
                }
            });
          });
        });
        {% endif %}
    });
</script>

  {% for stat in statslist.resource_stats %}
  {% if forloop.first %}
  <table class="stat_table_font stats_table tablesorter_resource_list" style="clear:both;margin-top:0.5em;width:100%"> 
  <thead>
   <tr>
    <th><span class="i16 tag">Resource Name</span</th>
    <th><span class="i16 clock">Last Updated</span></th>
    <th><span {% if is_maintainer %}class="res_tipsy_enable" title="Click the flags to modify the importance of a resource." style="border:0" {% endif %}>Importance</span></th>
   </tr>
  </thead>
  <tbody>
  {% endif %}
    <tr>
        <td>
          <a class="res_tipsy_enable" href="{% url resource_detail project.slug stat.object.slug %}" style="font-weight:bold" title="Total Strings: {{ stat.total_entities }}<br/>Available Languages: {{ stat.object.available_languages|length }}">{{ stat.object.name }}</a>
          <sup class="entry_metalink">
             {% if is_maintainer or request.user.is_superuser %}
               <a href="{% url resource_edit project.slug stat.object.slug %}">{% trans "edit" %}</a>
               , <a href="{% url resource_delete project.slug stat.object.slug %}">{% trans "del" %}</a>
             {% endif %}
          </sup>
        </td>
        <td class="last_updated">
          {% with stat.last_committer as last_committer %}
            <span class="res_tipsy_enable" style="border:0" title="{% if last_committer %}Committed by {{ last_committer }}<br/>{% else %}No committers yet{% endif %}">
          {% endwith %}
          {% with stat.last_update as last_update %}
            {% if last_update %}
                <span class="origin_format" style="display:none">{{ last_update|date:"M d,Y h:i A" }}</span>
                {{ last_update|timesince }}
            {% else %}
                {% trans "no translations yet" %}
            {% endif %}
          {% endwith %}
          </span>
        </td>
        <td class="priority_level" style="width:100px;text-align:center">
            {% with stat.object.priority.level as priority_level %}
            {% with stat.object.priority.display_level as display_level %}
            {% if is_maintainer %}
              <a id="priority_{{ stat.object.slug }}" class="resource_priority_trigger" style="cursor:pointer">
                <span class="priority_sort" style="display:none">{{ priority_level }}</span>
                <img src="{{ STATIC_URL }}priorities/images/{{ display_level }}.png" style="border:0" title="{{ display_level }}"/>
              </a>
            {% else %}
              <span class="priority_sort" style="display:none">{{ priority_level }}</span>
              {% ifnotequal priority_level "0" %}
              <img class="res_tipsy_enable" src="{{ STATIC_URL }}priorities/images/{{ display_level }}.png" style="border:0" title="{{ display_level }}"/>
              {% endifnotequal %}
            {% endif %}
            {% endwith %}
            {% endwith %}
        </td>
    </tr>
  {% if forloop.last %}
  </tbody>
  </table>
  {% endif %}
  {% empty %}
    <p>{% trans "No resources are registered for this project yet." %}</p>
  {% endfor %}

  {% comment %}
  {% get_permission "project_perm.submit_file" for request.user and project,1 as "can_submit_translation" %}
  {% if can_submit_translation or request.user.is_superuser %}
  <div id="new_translation_box1">
      <select name="translation_languages" class="language">
          <option value="">Select Language</option>
          {% if is_maintainer or request.user.is_superuser %}
            {% for language in languages %}
                <option value="{{ language.code }}">{{ language.name }}</option>
            {% endfor %}
          {% else %}
            {% for team in user_teams %}
                <option value="{{ team.language.code }}">{{ team.language.name }}</option>
            {% endfor %}
          {% endif %}
      </select>
      <a id="start_new_translation" class="i16 buttonized action">GO</a>
  </div>
  <p style="text-align:center;padding-top:1em">
   <a id="new_translation1" class="buttonized i16 add">{% trans "Translate All" %}</a>
  </p>
  {% endif %}
  {% endcomment %}

  <p class="i16 tip compact clear">
    {% blocktrans %}A <em>Resource</em> is mapped to master file and it's translated versions.
    For example Resource could be bound to nano.pot and de.po, it.po, fi.po, etc.{% endblocktrans %}
  </p>

  {% get_permission "project_perm.maintain" for request.user and project as "is_maintainer" %}
  {% if is_maintainer %}
    {% upload_create_resource_form request project %}
  {% endif %}

</div>
