{% extends "transhette/base.html" %}
{% load transhette_filters i18n %}

{% block extrahead %}
  <script src="{{ MEDIA_URL }}js/jquery-1.4.2.min.js"></script>
  <script type="text/javascript">
  // <![CDATA[
  (function($) {

  $(document).ready( function() {
      $("tr.fuzzy textarea").change(function () {
          $(this).parent().parent().removeClass("fuzzy");
      });

      $("div.submit-row input").click(function () {
          var not_empty_fuzzies = $("tr.fuzzy textarea").filter(function () {
              return this.value != ""
          });
          if (not_empty_fuzzies.length > 0) {
              confirmation = confirm("{% trans "Warning... There are fuzzy elements that will be translated as suggestion says. Do you want to continue?" %}");
              if(!confirmation) {
                  return false; // do not perform form submit
              }
          }
      });
  });

  })(jQuery);

  (function ($) {
    $(document).ready(function () {
        var ajax_url = $("input#transhette-ajax-url").val();
    $("input.update-by-ajax").click(function() {
            container = $(this).parents('td.translation');
            catalog = container.find('input.valid-catalog').val();
            textareas = container.find('textarea');
            if (!textareas) return;
            multiple=false;
            if (textareas.length > 1) {
                translation="";
                textareas.each(function() {
                    key = $(this).attr('name').split('_')[2];
                    translation += "&translation_" + key + "=" + $(this).val();
                });
                multiple=true;
            } else {
                translation = "&translation=" + textareas.val();
            }
            msgid = container.find('input.entry-id').val();

            $.ajax({
                data: "catalog=" + catalog + translation + '&msgid=' + msgid,
                url: ajax_url,
                type: "GET",
                async: true,
                dataType: "json",
                success: function(json_data, textstatus) {
                    if (json_data.saved) {
                        if (multiple) {
                            for (var item in json_data.translation) {
                                container.find('span.prioritary-translation-'+item).text(json_data.translation[item]);
                            }
                        } else {
                            container.find('span.prioritary-translation').text(json_data.translation);
                        }
                        container.find('.ajax-success').show();
                    } else {
                        container.find('.ajax-error').show();
                    }
                }});
        });
    });
  })(jQuery);

  // ]]>
  </script>
{% endblock %}
{% block header %}
    {{ block.super }}
    <div id="transhette-tools" style="float: right; font-size: 11px; padding-right: 10px;">
        <p>
            <span><a href="{% url transhette-restart-server %}">{% trans "Restart web server" %}</a> /
            <a href="{% url transhette-pick-file %}">{% trans "Pick another file" %}</a> / 
            <a href="{% url transhette-download-file %}">{% trans "Download this catalog" %}</a> /
            <a href="{% url transhette-update-catalogue %}">{% trans "Update this catalogue" %}</a>
            </span>
        </p>
    </div>
    <input type="hidden" id="transhette-ajax-url" name="transhette-ajax-url" value="{% url transhette-ajax %}" />
{% endblock %}
{% block breadcumbs %}
    <a href="{% url transhette-pick-file %}">{% trans "Home" %}</a> &rsaquo; 
    {{ transhette_i18n_lang_name }} &rsaquo; 
    {{ transhette_i18n_fn }} &rsaquo; 
    {% blocktrans with transhette_i18n_pofile.percent_translated|floatformat:2 as percent_translated  %}Progress: {{ percent_translated }}%{% endblocktrans %}
    {% if not transhette_i18n_write %}<span class="alert">{% trans "File is read-only: download the file when done editing!" %}</span>{% endif %}
{% endblock %}
{% block pagetitle %}{% trans "English" %} - {{transhette_i18n_lang_name}} ({{ transhette_i18n_pofile.percent_translated|floatformat:0 }}%){% endblock %}
{% block main %}
    <h1>{% blocktrans  %}Translate into {{transhette_i18n_lang_name}}{% endblocktrans %}</h1>
    
    <ul class="object-tools">
        <li class="nobubble">{% trans "Display:" %}</li>
        <li {% ifequal transhette_i18n_filter 'untranslated' %}class="active"{% endifequal %}><a href="?filter=untranslated">{% trans "Untranslated only" %}</a></li>
        <li {% ifequal transhette_i18n_filter 'translated' %}class="active"{% endifequal %}><a href="?filter=translated">{% trans "Translated only" %}</a></li>
        <li {% ifequal transhette_i18n_filter 'fuzzy' %}class="active"{% endifequal %}><a href="?filter=fuzzy">{% trans "Fuzzy" %}</a></li>
        <li {% ifequal transhette_i18n_filter 'both' %}class="active"{% endifequal %}><a href="?filter=both">{% trans "Both" %}</a></li>
    </ul>
    <div id="changelist" class="module{% if transhette_i18n_lang_bidi %} rtl{% endif %}">
        <div id="toolbar">
        <form id="changelist-search" action="" method="post">
            <div><!-- DIV needed for valid HTML -->
                <label for="searchbar"><img src="{{ADMIN_MEDIA_PREFIX}}img/admin/icon_searchbox.png" alt="{% trans "transhette_search" %}" /></label>
                <input type="text" size="40" name="query" value="{{query}}" id="searchbar" />
                <input type="submit" name="search" value="{% trans "Go" %}" />
            </div>
        </form>
        </div>
        
        <form method="post" action="">
        {% if format_errors %}
        <ul>
          {% for error in format_errors %}
            <li class="alert">{{ error }}</li>
          {% endfor %}
        </ul>
        {% endif %}
            <table>
                <thead>
                    <tr>
                        <th>{% trans "ID" %}</th>
                        {% if default_column_name %}
                        <th>{% trans transhette_i18n_native_lang_name %}</th>
                        {% endif %}
                        <th>{% trans transhette_i18n_lang_name %}</th>

                        <th>{% trans "Occurrences(s)" %}</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item_message in message_list %}
                    {% with item_message.message as message %}
                    <tr class="{% cycle row1,row2 %} {% for flag in message.flags %}{{flag}} {% endfor %}{% if not item_message.is_valid %} novalid{% endif %}">
                        {% if message.msgid_plural %}
                            <td class="original plural">
                                <span class="part">{{message.msgid|format_message|linebreaksbr}}</span>
                                <span class="part">{{message.msgid_plural|format_message|linebreaksbr}}</span>
                            </td>
                            {% if default_column_name %}
                            <td class="original plural">
                                <span class="part">{{item_message.native_message.msgid|format_message|linebreaksbr}}</span>
                                <span class="part">{{item_message.native_message.msgid_plural|format_message|linebreaksbr}}</span>
                            <td class="original plural">
                            {% endif %}
                            
                            <td class="translation">
                                {% for k, msgstr in message.msgstr_plural.items|dictsort:"0"  %}
                                    <label for="m_{{message.id}}_{{k}}">{{k}}:</label>
                                    <textarea rows="{{message.msgid|format_message|lines_count}}" cols="40" id="m_{{message.id}}_{{k}}" name="m_{{message.id}}_{{k}}">{{msgstr}}</textarea>
                                {% endfor %}
                                {% if not item_message.is_valid %}
                                  <p>
                                    <img src="{{ ADMIN_MEDIA_PREFIX }}img/admin/icon-no.gif" alt="{% trans "No" %}" />
                                    <a href="{% url transhette-change-catalogue %}?entry_id={{ message.msgid }}&catalog={{ item_message.valid_catalog.fpath }}">{% trans "There is a more prioritary catalog" %}</a>
                                  </p>
                                  <p>
                                    <label>{% trans "Prioritary translation:" %}</label>
                                    <ul>
                                    {% for k, msgstr in item_message.valid_entry.msgstr_plural.items|dictsort:"0"  %}
                                      <li>
                                        <label>{{k}}:</label>
                                        <span class="prioritary-translation-{{ k }}">{{ msgstr }}</span>
                                      </li>
                                    {% endfor %}
                                    </ul>
                                    <span class="hide ajax-notify ajax-success">
                                       <img src="{{ ADMIN_MEDIA_PREFIX }}img/admin/icon-yes.gif" alt="{% trans "Yes" %}" /> {% trans "Updated!" %}
                                    </span>
                                    <span class="hide ajax-notify ajax-error">
                                       <img src="{{ ADMIN_MEDIA_PREFIX }}img/admin/icon-no.gif" alt="{% trans "No" %}" /> {% trans "Error updating" %}
                                    </span>
                                  </p>
                                  <p>
                                    {% trans "Update prioritary catalog with the text from the input boxes?" %}
                                    <input type="hidden" class="valid-catalog" name="valid_catalog" value="{{ item_message.valid_catalog.fpath }}" />
                                    <input type="hidden" class="entry-id" name="entry_id" value="{{ message.msgid }}" />
                                    <input type="button" class="update-by-ajax" name="update_prioritary" value="{% trans "Yes, update" %}" />
                                  </p>
                                {% endif %}
                            </td>
                        {% else %}
                            <td class="original">{{ message.msgid|format_message|linebreaksbr }}</td>
                            {% if default_column_name %}
                            <td class="original">{{ item_message.native_message.msgstr }}</td>
                            {% endif %}
                            <td class="translation">
                                <textarea rows="{{message.msgid|format_message|lines_count}}" cols="40" name="m_{{message.id}}">{{message.msgstr}}</textarea>
                                {% if ENABLE_TRANSLATION_SUGGESTIONS %}<a href="#" class="suggest">{% trans "suggest" %}</a>{% endif %}
                                {% if not item_message.is_valid %}
                                  <p>
                                    <img src="{{ ADMIN_MEDIA_PREFIX }}img/admin/icon-no.gif" alt="{% trans "No" %}" />
                                    <a href="{% url transhette-change-catalogue %}?entry_id={{ message.msgid }}&catalog={{ item_message.valid_catalog.fpath }}">{% trans "There is a more prioritary catalog" %}</a>
                                  </p>
                                  <p>
                                    <label>{% trans "Prioritary translation:" %}</label>
                                    <span class="prioritary-translation">{{ item_message.valid_entry.msgstr }}</span>
                                    <span class="hide ajax-notify ajax-success">
                                       <img src="{{ ADMIN_MEDIA_PREFIX }}img/admin/icon-yes.gif" alt="{% trans "Yes" %}" /> {% trans "Updated!" %}
                                    </span>
                                    <span class="hide ajax-notify ajax-error">
                                       <img src="{{ ADMIN_MEDIA_PREFIX }}img/admin/icon-no.gif" alt="{% trans "No" %}" /> {% trans "Error updating" %}
                                    </span>
                                  </p>
                                  <p>
                                    {% trans "Update prioritary catalog with the text from the input box?" %}
                                    <input type="hidden" class="valid-catalog" name="valid_catalog" value="{{ item_message.valid_catalog.fpath }}" />
                                    <input type="hidden" class="entry-id" name="entry_id" value="{{ message.msgid }}" />
                                    <input type="button" class="update-by-ajax" name="update_prioritary" value="{% trans "Yes, update" %}" />
                                  </p>
                                {% endif %}
                            </td>
                        {% endif %}
                            <td class="location">
                                {% for fn,lineno in message.occurrences %}
                                    <code{% if forloop.counter|gt:"3" %} class="hide"{% endif %}>{{ fn }}:{{lineno}}</code> 
                                {% endfor %}
                                {% if message.occurrences and message.occurrences|length|gt:"3" %}
                                    <a href="#">... ({% blocktrans count message.occurrences|length|minus:"3" as more_count %}{{more_count}} more{% plural %}{{more_count}} more{% endblocktrans %})</a>
                                {% endif %}
                            </td>
                    </tr>
                    {% endwith %}
                    {% endfor %}
                </tbody>
            </table>
            <div class="submit-row">
                <p class="paginator">
                    {% if query %}
                        <input type="hidden" name="query" value="{{query}}"  />
                    {% endif %}
                    <input type="submit" class="default" name="_next" value="{% trans "Save and translate next block" %}"/>

                    {% for lid,language,position in languages %}
                        <a href="{% url transhette-language-selection lid,position %}{{ query_arg }}">{% trans "Continue" %} {{ lid|upper }}</a>
                    {% endfor %}

                    {% if needs_pagination %}
                    {% trans "Skip to page:" %}
                    {% for i in page_range %}
                        {% ifequal i '...' %}
                        <span class="space">{{ i }}</span>
                        {% else %}
                        {% ifequal i page %}
                            <span class="this-page">{{i}}</span>
                        {% else %}    
                            <a href="?page={{i}}{% if query %}&amp;query={{query}}{% endif %}">{{i}}</a>
                        {% endifequal %}
                        {% endifequal %}
                    {% endfor %}
                    {% else %}
                    {% trans "Displaying:" %}
                    {% endif %}
                    <strong>{% blocktrans count transhette_i18n_pofile|length as message_number and paginator.object_list|length as hits %}{{hits}}/{{message_number}} message{% plural %}{{hits}}/{{message_number}} messages{% endblocktrans %}</strong>

                    
                </p>
            </div>
        </form>
    </div>
{% endblock %}
