{% load admin_urls admin_static i18n custom_autocomplete_tags %}

{% include 'admin/edit_inline/tabular.html' %}

<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        var prefix = "{{ inline_admin_formset.formset.prefix }}";
        {% get_custom_autocomplete_lookup_fields_fk inline_admin_formset.opts as fk_fields %};
        {% get_custom_autocomplete_lookup_fields_m2m inline_admin_formset.opts as m2m_fields %};
        {% get_custom_autocomplete_lookup_fields_generic inline_admin_formset.opts as generic_fields %};

        var custom_autocomplete_fields_fk = {{fk_fields|safe}}
        var custom_autocomplete_fields_m2m = {{m2m_fields|safe}}
        var custom_autocomplete_fields_generic = {{generic_fields|safe}}
        
        function initCustomTabularItems(){

            // console.log("TABULAR custom_autocomplete_fields_fk? "+custom_autocomplete_fields_fk)

            $.each(custom_autocomplete_fields_fk, function() {
                $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-table > div:not(.grp-empty-form)")
                .find("input[name^='" + prefix + "'][name$='-" + this + "']:not(.grp-autocomplete-hidden-field)")
                .each(function() {
                    $(this).grp_custom_autocomplete_fk({
                        lookup_url:"{% url 'custom_related_lookup' %}",
                        autocomplete_lookup_url:"{% url 'custom_autocomplete_lookup' %}"
                    });
                });
            });
            $.each(custom_autocomplete_fields_m2m, function() {
                $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-table > div:not(.grp-empty-form)")
                .find("input[name^='" + prefix + "'][name$='-" + this + "']:not(.grp-autocomplete-hidden-field)")
                .each(function() {
                    $(this).grp_custom_autocomplete_m2m({
                        lookup_url:"{% url 'custom_m2m_lookup' %}",
                        autocomplete_lookup_url:"{% url 'custom_autocomplete_lookup' %}"
                    });
                });
            });
            $.each(custom_autocomplete_fields_generic, function() {
                var content_type = this[0],
                    object_id = this[1];
                $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-table > div:not(.grp-empty-form)")
                .find("input[name^='" + prefix + "'][name$='-" + this[1] + "']:not(.grp-autocomplete-hidden-field)")
                .each(function() {
                    var i = $(this).attr("id").match(/(?:-\d+)?(-\d+-)/);
                    if (i) {
                        var ct_id = "#id_" + prefix + i[1] + content_type,
                            obj_id = "#id_" + prefix + i[1] + object_id;
                        $(this).grp_custom_autocomplete_generic({
                            content_type:ct_id,
                            object_id:obj_id,
                            lookup_url:"{% url 'custom_related_lookup' %}",
                            autocomplete_lookup_url:"{% url 'custom_autocomplete_lookup' %}"
                        });
                    }
                });
            });

        }

        initCustomTabularItems();

        $("#{{ inline_admin_formset.formset.prefix }}-group a.grp-add-handler").bind("click", function(event){
            setTimeout(function(){
                initCustomTabularItems();
            }, 250);
        });



       

    });
})(grp.jQuery);
</script>