{% load i18n %}
<input type="text" id="lookup_{{ name }}" value="{{ label }}" style="display:none;" />
<a href="{{ related_url }}{{ url }}" class="related-lookup" id="lookup_id_{{ name }}" onclick="return showRelatedObjectLookupPopup(this);">
  <img src="{{ admin_media_prefix }}img/admin/selector-search.gif" width="16" height="16" alt="{% trans "Lookup" %}" />
</a>
<script type="text/javascript">
//$(document).ready(function() {
(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());
              };
          };
      });
      $('#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();
          };
      });
      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>