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


{% block after_related_objects %}
{{ block.super }}

{# CYCLOPE: chainedSelect for Layout #}

<script type="text/javascript">
//<![CDATA[

  function setup_chainedSelect_for_layout(i) {
      $("#id_regionview_set-" + i + "-region").chainedSelect({
          parent: '#id_template',
          url: '/{{CYCLOPE_PREFIX}}layout_regions_json',
          value: 'region_name',
          label: 'verbose_name'
      });

      $("#id_regionview_set-" + i + "-content_view").chainedSelect({
          parent: '#id_regionview_set-' + i + '-content_type',
          url: '/{{CYCLOPE_PREFIX}}registered_region_views_json',
          value: 'view_name',
          label: 'verbose_name'
      });

      $("#id_regionview_set-" + i + "-object_id").chainedSelect({
          parent: '#id_regionview_set-' + i + '-content_type',
          url: '/{{CYCLOPE_PREFIX}}objects_for_ctype_json',
          value: 'object_id',
          label: 'verbose_name'
      });
  }

  jQuery(document).ready(function($){
      $("select[id^='id_regionview_set-'][id$='-region']").each(function(i, sel){
          setup_chainedSelect_for_layout(i);
      });

      $("#regionview_set-group .add-row a").click(function() {
        setup_chainedSelect_for_layout($("select[id^='id_regionview_set-'][id$='-region']").length - 2);
      });

      // View options

      function update_to_default_view_options(view_options_combo, prefix, div_container, i){
          var view_name = view_options_combo.val();
          var content_type_id = $(prefix+"content_type  option:selected").val()

          if (view_name && content_type_id){
              $.get("/{{CYCLOPE_PREFIX}}options_view_widget_html",
                 {
                    content_type_id: content_type_id,
                    view_name: view_name,
                    prefix_name: "regionview_set-"+i+"-"
                 },
                 function(data){
                     if (data){
                          $("#regionview_set-" + i + "-view_options_multiple").replaceWith(data);
                          div_container.show();
                      }
                      else{
                          $("#regionview_set-" + i + "-view_options_multiple").html("");
                          div_container.hide();
                      }
                 });
          }
      }

      $.cyclope = {access_number: [], initial_content: []};

      $("select[id^='id_regionview_set-'][id$='-content_view']").each(function(i, sel){
          $.cyclope.access_number[i] = 0;

          var prefix = "#id_regionview_set-" + i + "-"
          var div_container = $("#regionview_set-"+i+"-view_options_multiple").parent().parent();

          if ($("#regionview_set-" + i + "-view_options_multiple").html().indexOf("form-row") == -1){
              div_container.hide();
              $.cyclope.initial_content[i] = false;
          }
          else{
              $.cyclope.initial_content[i] = true;
          }

          $(this).change(function(){
              $.cyclope.access_number[i] += 1;

              if ($.cyclope.access_number[i] == 2){
                if ($.cyclope.initial_content[i] === false){
                  update_to_default_view_options($(this), prefix, div_container, i);
                }
              }
              else if ($.cyclope.access_number[i] >= 3){
                update_to_default_view_options($(this), prefix, div_container, i);
              }
          });
      });
  });
//]]>
</script>


{% endblock %}
