{% extends "projects/project_menu.html" %}
{% load i18n %}
{% load cache %}
{% load statistics_resources %}
{% load permissions %}
{% load upload_manager_tags %}

{% block extra_head %}
{% get_permission "project_perm.maintain" for request.user and resource.project as "is_maintainer" %}
<link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}css/ui.tabs.outside.css" />
<link media="screen" href="{{ STATIC_URL }}css/tablesorter.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="{{ STATIC_URL }}js/tablesorted.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".tipsy_enable").tipsy({'html':true, 'gravity':'s'});

        {% if request.user.is_authenticated %}
        $("#new_translation1").click( function (){
            $("#new_translation_box1").toggle();
        });

        $("#start_new_translation").click( function() {
            var target_lang_code = $(this).siblings('select').val();
            if ( target_lang_code != "" ) {
                request_url = window.location+'l/'+target_lang_code;

                window.location = request_url;
            } else {
                alert('{% trans "Please select a target language first."|escapejs %}');
            }
        });
        {% endif %}

        $(".tablesorter_resource").tablesorter({
            sortList: [[1,1]],
            widgets: ['zebra'],
            headers: {
                1: { sorter: 'percent' },
                2: { sorter: 'number' }
            },
            textExtraction: function(node) {
                switch (node.cellIndex){
                    // Stats column
                    case 1:
                        return $(".stats_string_resource", node).text();
                    // Last update column
                    case 2:
                        return $("span", node).attr('unixdate');
                    default: return node.innerHTML;
                }
            }
        });

        {% if is_maintainer %}
        var resource_priority_cycle_url = '{% url cycle_resource_priority resource.project.slug resource.slug %}';
        $("a.resource_priority_trigger").click(function(){
            $(this).load(resource_priority_cycle_url, function(response, status, xhr) {
                if (status == "error") {
                    var msg = "Sorry but there was an error: ";
                    alert(msg + xhr.status + " " + xhr.statusText);
                }
            });
        });
        {% endif %}

    });
</script>
{% endblock %}

{% block breadcrumb %}

{{block.super}} »
<a href="{% url project_detail project.slug %}">{{project}}</a> »
<a href="{% url resource_detail project.slug resource.slug %}">{{resource}}</a>
{% endblock %}

{% block title %}
{{ block.super }} |
{% trans "Projects" %} |
{{project}}
{{resource}}
{% endblock %}

{% block body_class %}project_list{% endblock %}

{% block header_menu %}{% endblock %}

{% block project_header %}
{{block.super}}<h2 class="pagetitle{% if short_desc %} with_description{% endif %}">&nbsp;/&nbsp;</h2><h2 class="pagetitle with_description">{{ resource.name }}</h2>
{% endblock%}

{% block content_main %}
  {% get_permission "project_perm.maintain" for request.user and resource.project as "is_maintainer" %}

  <div class="obj_bigdetails ui-tabs-panel">
 {% comment %}
  <!--{% if resource.project.private %}
  <span id="private_project_banner" class="ui-corner-all">
    {% trans "Private Project"%}
    <img style="vertical-align:top" src="{{ STATIC_URL }}images/icons/lock.png" title="Private Project">
  </span>
  {% endif %}-->
 {% endcomment %}
 
  {% if perms.projects.edit_resource or is_maintainer or request.user.is_superuser %}
    <div class="editlinks">
      <p>
        <a class="i16 edit buttonized" href="{% url resource_edit project_slug=resource.project.slug resource_slug=resource.slug %}">{% trans "Edit resource" %}</a>
      </p>
    </div>
  {% endif %}

{% cache 604800 resource_details resource.project.slug resource.slug LANGUAGE_CODE %}
<div id="resource_details" class="separate">
  <h3>{% trans "Translation Resource details" %}</h3>
  <table class="definition">
    <tr>
      <th class="i16 omega">{% trans "Source Language" %}</th>
      <td>
        {{ resource.source_language }}
      </td>
    </tr>
    <tr>
      <th class="i16 language">{% trans "Number of languages" %}</th>
      <td>
        {{ resource.available_languages|length }}
      </td>
    </tr>
    <tr>
      <th class="i16 stringnum">{% trans "Total Strings" %}</th>
      <td>
        {{ resource.total_entities }}
        <span class="words big_number">({{ resource.wordcount }} {% trans "words" %})</span>
      </td>
    </tr>
    <tr>
      <th class="i16 url_update">{% trans "Auto-update from URL?" %}</th>
      <td>
        {% if autofetch_url and autofetch_url.auto_update %}
		    <img class="noborder" src="{{ STATIC_URL }}images/icons/accept.png" /> &nbsp;
			<span><a href="{{ autofetch_url.source_file_url }}">[link to file]</a></span>
        {% else %}
            <img class="noborder" src="{{ STATIC_URL }}images/icons/delete.png" />
        {% endif %}
      </td>
    </tr>
    <tr>
      <th class="i16 accept_translation">{% trans "Accepting Translations?" %}</th>
      <td>
        {% if resource.accept_translations %}
            <img class="noborder" src="{{ STATIC_URL }}images/icons/accept.png" />
        {% else %}
            <img class="noborder" src="{{ STATIC_URL }}images/icons/delete.png" />
        {% endif %}
      </td>
    </tr>
    {% if resource.priority %}
    {% with resource.priority.level as priority_level %}
    {% with resource.priority.display_level as display_level %}
    <tr>
      <th class="i16 ruby">{% trans "Priority Level" %}</th>
      <td>
        {% if is_maintainer %}
            <a class="resource_priority_trigger" style="cursor:pointer">
            <img src="{{ STATIC_URL }}priorities/images/{{ display_level }}.png" class="noborder" title="{{ display_level }}"/>
            </a>
        {% else %}
            <img class="res_tipsy_enable" src="{{ STATIC_URL }}priorities/images/{{ display_level }}.png" class="noborder" title="{{ display_level }}"/>
        {% endif %}
      </td>
    </tr>
    {% endwith %}
    {% endwith %}
    {% endif %}
    {% with resource.releases.all as releases %}
    {% if releases %}
     <tr>
       <th class="i16 release">{% blocktrans count releases|length as counter %}Release:{% plural %}Releases:{% endblocktrans %}</th>
       <td class="compact">
         <ul class="simple links">
            {% for release in releases %}
                <li><a class="release" href="{% url release_detail project_slug=release.project.slug release_slug=release.slug %}">{{ release.full_name }}</a></li>
            {% endfor %}
        </ul>
       </td>
     </tr>
    {% endif %}
    {% endwith %}
  </table>
</div>

<h3>{% trans "Available translations" %}</h3>
<div class="list separate clear">
<table class="stat_table_font stats_table highlighted tablesorter_resource" style="clear:both;margin-top:0.5em;width:100%">
  <thead>
  <tr>
    <th><span>{% trans "Language" %}</span></th>
    <th style="width:320px"><span>{% trans "Completion" %}</span></th>
    <th><span>{% trans "Last Updated" %}</span></th>
   </tr>
{% for stat in statslist %}
    {% ifequal forloop.counter 2 %}
        </thead>
      <tbody>
    {% endifequal %}
    <tr id="stat_row_{{forloop.counter}}" title="{% trans 'click for translation' %}">
        {% cache 604800 resource_details_lang resource.project.slug resource.slug stat.language.code LANGUAGE_CODE %}
        <td>
          <span class="clickable">{{ stat.language.name }}</span>
          <span class="locale">({{ stat.language.code }})</span>
          {% ifequal resource.source_language stat.language %}
             <span style="padding-left:1em"><em>{% trans "source language" %}</em></span>
          {% endifequal %}
          {% if stat.lock.valid %}
            <img class="tipsy_enable bullet" src="{{ STATIC_URL }}images/icons/bullet_lock.png" title="{% trans "Locked" %}"/>
          {% endif %}
        </td>
        <td>
          {% with 200 as barwidth %}
            {% stats_bar_simple stat barwidth %}
          {% endwith %}
        </td>
        {% endcache %}
        <td>
        {% with stat.last_update as last_update %}
          <span style="border:0" unixdate="{{ last_update|date:'U' }}">
          {% with stat.last_committer as last_committer %}
          {% if last_update %}
              {{ last_update|date:"M d, h:ia" }}
              {% if last_committer %}
              by {{ last_committer }}
              {% endif %}
          {% else %}
              {% trans "no translations yet" %}
          {% endif %}
          {% endwith %}
        </span>
        {% endwith %}
        </td>
    </tr>
    <script>
        $(".highlighted tr#stat_row_{{forloop.counter}}").click(function(){
           /* FancyBox Using custom settings */
           $.fancybox(
               {
                'href': '{% url resource_actions resource.project.slug resource.slug stat.language.code %}',
                'hideOnContentClick': false,
                'onStart':function(){
                    // IE8 fix
                    if(jQuery.browser.msie) {
                      $('#fancybox-wrap').css('max-width','716px');
                      $('#fancybox-inner').css('max-width','716px');
                      $('#fancybox-outer').css('left','50%');
                    }
                },
                'onComplete':function() {
                    // IE8 fix
                    if(jQuery.browser.msie){
                        $('.fancybox-title-outside').css('max-width','716px');
                    }
                    $(".tipsy_enable").tipsy({'html':true, 'gravity':'s'});
                    $("a.language_chooser_trigger").toggle(function(){
                        $("div.language_chooser").css({'visibility':'visible'});
                    },function(){
                        $("div.language_chooser").css({'visibility':'hidden'});
                    });
                    $("div.language_chooser a").click( function(){
                        var target_lang_code = $("div.language_chooser select").val();
                        if ( target_lang_code != "" ) {
                            request_url = window.location+'l/{{ stat.language.code }}/clone/'+target_lang_code+'/';
                            window.location = request_url;
                        } else {
                            alert('{% trans "Please select a target language first."|escapejs %}');
                        }
                    });
                    if(!$("#download_for_translation a").hasClass('disabled')){
                    $("#download_for_translation").click(function(){
                        $.ajax({
                            url : '{% url lock_and_download_translation resource.project.slug resource.slug stat.language.code %}',
                            contentType : 'application/json',
                            type : 'POST',
                            beforeSend: function(){
                            $("div#notification-container div").html('{% trans "Trying to lock resource..."|escapejs %}');
                            $("div#notification-container").fadeIn("fast");
                            },
                            complete : function(xmlhttpreq, textStatus) {
                                if (textStatus=='success'){
                                    response = JSON.parse(xmlhttpreq.responseText);
                                    if(response['status']=='FAILED'){
                                        $("#download_for_translation").siblings("div.error_notes").text(response['message']);
                                        $("#download_for_translation").siblings("div.error_notes").show();
                                    }else{
                                        if(response['redirect']){
                                            window.location = response['redirect'];
                                        }
                                    }
                                    $("div#notification-container").fadeOut("fast");
                                }
                            }
                        });
                    });
                    }
                },
                'titleShow': true,
                'titlePosition': 'float',
                'title': '{% trans "Translation Details"|escapejs %}'
           });
        });
    </script>
{% endfor %}
  </tbody>
</table>



{% endcache %}


{% get_permission "project_perm.submit_translations" for request.user and resource.project,1 as "can_submit_translation" %}
{% get_permission "project_perm.maintain" for request.user and resource.project as "is_maintainer" %}
{% if can_submit_translation and resource.accept_translations or is_maintainer %}

<p class="i16 tip" style="font-size:85%">
{% blocktrans %}If you cannot see your language, click the button below to translate.<span class="note">( You must belong to a language team of this project, to be able to translate! )</span>{% endblocktrans %}
</p>

<div id="new_translation_box1">

{% comment %}
    I kept this in order to remember to fix the languages in the templatetags.
    The change should respect the below permissions!!!

    <select name="translation_languages" class="language">
        <option value="">{% trans "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>
{% endcomment %}

{% upload_resource_translation_button request resource '' 'button-global' 'True' %}
</div>
<p style="text-align:center;padding-top:1em">
  <a id="new_translation1" class="buttonized i16 add">{% trans "Add Translation" %}</a>
</p>
{% endif %}

</div>

</div>


<h3>{% trans 'History' %}</h3>
{% load tx_action_log %}
{% get_log 5 as action_log for_object resource %}
{% if not action_log %}
<p>{% trans 'No actions recorded yet :)' %}</p>
{% else %}
<ul class="actionlist simple">
{% for entry in action_log %}
    <li class="i16 actionlog">
    {{ entry.message|safe }} by {{ entry.user }} <span class="timestamp">{{ entry.action_time|timesince }} ago.</span>
{% endfor %}
</ul>
{% endif %}

{% endblock %}

{% block content_footer %}
  <div id="content_footer_center">

  </div>
{% endblock %}
