{% load i18n %}
<input type="text" id="lookup_{{ name }}" class="inline_lookup" value="{{ label }}" style="display:none;" />
<a href="{{ related_url }}{{ url }}" class="related-lookup" id="lookup_id_{{ name }}" onclick="return showRelatedObjectLookupPopup(this);">
  <img src="/static/admin/img/admin/selector-search.gif" width="16" height="16" alt="{% trans "Lookup" %}" />
</a>
<script type="text/javascript">

(function($){    
    // Show lookup input  bla bla bla
    $("#lookup_{{ name }}").show();
    function reset() {
          $('#id_{{ name }}').val('');
          $('#lookup_{{ name }}').val('');
      };
    function lookup(query) {
          $.get('{{ search_path }}', {
              'search_fields': '{{ search_fields }}',
              'app_label': '{{ app_label }}',
              'model_name': '{{ model_name }}',
              'object_pk': query
          }, function(data){
              $('#lookup_{{ name }}').val(data);
              {{ name|cut:"-" }}_value = query;
          });
      };

    $('#id_{{ name }}').bind(($.browser.opera ? "keypress" : "keyup"), function(event) {
          if ($(this).val()) {
              if (event.keyCode == 27) {
                  reset();
              } else {
                  lookup($(this).val());
              };
          };
      });
   
      {% if not '__prefix__' in name %}
      
        $("#lookup_{{ name }}").autocomplete('{{ search_path }}', {
            extraParams: {
                'search_fields': '{{ search_fields }}',
                'app_label': '{{ app_label }}',
                'model_name': '{{ model_name }}'
            }, max: 100, scrollHeight: 400,   // mikele: added by me
        }).result(function(event, data, formatted) {
            if (data) {
                $("#id_{{name}}").val(data[1]);
            }
        }).keyup(function(event){
            if (event.keyCode == 27) {
                reset();
            };
        });

      {% endif %}


      django.jQuery(document).bind('formset_add.admin', function(event, row_element) {

          new_ac = $(row_element).find(".inline_lookup")
          //console.log(new_ac)
          value_field = $("#" + new_ac.attr('id').replace('lookup_','id_'))
          //console.log(value_field)

              new_ac.autocomplete('{{ search_path }}', {
                  extraParams: {
                      'search_fields': '{{ search_fields }}',
                      'app_label': '{{ app_label }}',
                      'model_name': '{{ model_name }}'
                  }, max: 100, scrollHeight: 400,   // mikele: added by me
              }).result(function(event, data, formatted) {
                  if (data) {
                      value_field.val(data[1]);
                  }
              }).keyup(function(event){
                  if (event.keyCode == 27) {
                      reset();
                  };
              });
      });


      var {{ name|cut:"-" }}_value = $('#id_{{ name }}').val();
      function check() {
          {{ name|cut:"-" }}_check = $('#id_{{ name }}').val();
          if ({{ name|cut:"-" }}_check) {
              if ({{ name|cut:"-" }}_check != {{ name|cut:"-" }}_value) {
                  lookup({{ name|cut:"-" }}_check);
              }
          }
      }
      timeout = window.setInterval(check, 300);
})(django.jQuery);
//}); // domguard : use django admin jQuery version
</script>