{% extends "admin/change_form.html" %}

<!-- LOADING -->
{% load url from future %}
{% load admin_static i18n admin_modify grp_tags %}

<!-- JAVASCRIPTS -->
{% block javascripts %}
    {{ block.super }}
    <script type="text/javascript" charset="utf-8">
        (function($) {
            $(document).ready(function() {
                // nodeshot specific
                var external = $('#id_is_external');
                var external_info = $('#external-group')
                if(!external.is(':checked')){
                    external_info.hide();
                }
                external.change(function(e){
                    external_info.fadeToggle(250);
                });
                // when synchronizer_path is changed
                {% if object_id %}
                var reload_schema_url = "{% url 'layer_external_reload_schema' object_id %}",
                    csrftoken = $('#layer_form input[name=csrfmiddlewaretoken]').val();
                $('#external-group').delegate(
                    '#id_external-0-synchronizer_path',
                    'change',
                    function(e){
                        // temporarily disable inputs
                        $('input,select,textarea', '#external0 .grp-module').attr('disabled', 'disabled')
                        // reload config
                        $.post(reload_schema_url, {
                            "synchronizer_path": $(this).val(),
                            "csrfmiddlewaretoken": csrftoken
                        }).done(function(response){
                            // load new fragment
                            var html = $.parseHTML(response),
                                fragment = $(html).find('#external-group').html();
                            $('#external-group').html(fragment);
                            // focus on select
                            $('#id_external-0-synchronizer_path').trigger('focus');
                        });
                    }
                )
                {% endif %}
            });
        })(grp.jQuery);
    </script>
{% endblock %}
