<script type="text/javascript">
 // <!--
  window.onload=createFormDialog();
  function createFormDialog()
  {
    $('#add-table-elem-dialog').dialog({ autoOpen: false, 
    									 modal: 'true',
    									 width: 400});
    create_example_opinion_dialog();
  }
  
  function draw_filtered_new_frames()
  {
    window.filter_schema_type_val = $('#frame_filter #schema_type_filter').val();
    window.filter_aspect_val = $('#frame_filter #aspect_filter').val();
    window.filter_reflex_val = $('#frame_filter #reflex_filter').val();
    window.filter_neg_val = $('#frame_filter #neg_filter').val();
    window.filter_pred_val = $('#frame_filter #pred_filter').val();
    window.filter_opinion_val = $('#frame_filter #opinion_filter').val();
    window.filter_position_val = $('#frame_filter #position_filter').val();
    window.filter_argument_val = $('#frame_filter #argument_filter').val();
    
    frame_class = 'InactiveFrameTable';
    {% if can_modify %}
      frame_class = 'ActiveFrameTable';
    {% endif %}
    draw_filtered_frames(window.schemas, 'new-frame-tables', 'new-frame-table', 'frame_filter', window.nkjp_examples, frame_class, window.lemma_entry);
  }
  
  function getParentInstance(id, schemas)
  {
    if(id != -1 && id.startsWith('frame'))
    {
      var ids = parseId(id);
      var frame_id = ids['frame_id'];
      var pos_id = ids['pos_id'];
      var arg_id = ids['arg_id'];
    
      for(var i=0; i<schemas.length; i++)
      {
        if(schemas[i].id == frame_id && pos_id)
        {
          for(var j=0; j<schemas[i].positions.length; j++)
          {
            if(schemas[i].positions[j].id == pos_id && arg_id)
            {
          	  for(var k=0; k<schemas[i].positions[j].arguments.length; k++)
          	  {
          	    if(schemas[i].positions[j].arguments[k].id == arg_id)
          	    {
          	      return {'element': schemas[i].positions[j],
          	              'type': 'position'};
          	    }
          	  }
            }
            else if(schemas[i].positions[j].id == pos_id)
            { // position
              return {'element': schemas[i],
          	          'type': 'frame'};
            }
          }
        }
        else if(schemas[i].id == frame_id)
        { // frame
          return {'element': schemas[i],
          	      'type': 'lemma'};
        }
      }
    }
    return {};
  }
  
  $("button#remove").click(function(){removeElement(); return false;});  
  $("button#add").click(function(){addElement(); return false;});
  $("button#copy").click(function(){copyElement(); return false;}); 
  $("button#paste").click(function(){pasteElement(); return false;});  
  $("button#cut").click(function(){cutElement(); return false;});  
  $("button#duplicate").click(function(){duplicateElement(); return false;});  
  $("button#add-phraseologic-frame").click(function(){addPhraseologicFrame(); return false;});
  $("button#assign-phraseologic-frame").click(function(){openAssignPhraseologicFrameDialog(); return false;});
  $("button#back").click(function(){backOneModification(); return false;});  
  $("button#forward").click(function(){forwardOneModification(); return false;}); 
  $("button#save").click(function(){save_new_frames(); return false;});
  $("button#validate").click(function(){validateSchemata(); return false;});

  $("button#delete_example").click(function(){
    delete_nkjp_example(selected_example_id)});
  $("button#delete_semantic_example").click(function(){
    remove_semantic_example(selected_example_id)});
  $("button#delete_all_examples").click(function(){
    delete_all_nkjp_examples(selectedFrameIdNkjp)});
  $("button#modify_example").click(function(){
    modify_nkjp_example(selected_example_id)});  
  $("button#confirm_example").click(function(){
    confirm_nkjp_example(selected_example_id)});  
  $("button#unpin_example").click(function(){
    unpin_nkjp_example(selected_example_id)}); 
  
  // -->
</script>

<div id="add-table-elem-dialog">
</div>
<div id="example-opinion-dialog">
</div>
<div id='framesSplit'>
  <div id="new-frame-tables">
  </div>  
  <div class="new_frame_tabs" id="tabs">
    <ul>
      {% if can_modify %}
        {% if perms.dictionary.add_syntactic_frames or perms.dictionary.add_phraseologic_frames %}
        	<li><a href="#frames_modif">Modyfikacja schematów</a></li>
        {% endif %}
        <li><a href="#add_nkjp_example">Dodawanie przykładów</a></li>
      {% endif %}
      <li><a href="#show_nkjp_example">Przegląd przykładów</a></li>
      <span id="cancel_frame_filter" style="float:right;">x</span>
      <span style="float:right;"> 
        <ul id="filter_frames_menu">
          <li id="filter_frames_options"><a href="#">Filtrowanie schematów</a>
            <ul id="options">
              <li id="frame_filter"><a href="#"></a></li>
            </ul>
          </li>
        </ul>
      </span>
    </ul>

    {% if can_modify %}
	  {% if perms.dictionary.add_syntactic_frames or perms.dictionary.add_phraseologic_frames %}
	      <div id="frames_modif">
	        <p id="new-frame-edition-buttons">
	          <button type="button" id="back" style="width:120px">Cofnij</button>
	          <button type="button" id="forward" style="width:120px">Powtórz</button>
	          <button type="button" id="save" style="width:120px">Zapisz zmiany</button>
	          {% if perms.dictionary.add_syntactic_frames or perms.dictionary.add_phraseologic_frames %}
	            <button type="button" id="validate" style="width:120px">Walidacja</button>
	          {% endif %}
	        </p> 
	        <p id="new-frame-modification-buttons">
	          <button type="button" id="remove" style="width:120px">Usuń</button>
	          <button type="button" id="add" style="width:120px">Dodaj</button>
	          <button type="button" id="duplicate" style="width:120px">Duplikuj</button>
	          <button type="button" id="copy" style="width:120px">Kopiuj</button>
	          <button type="button" id="paste" style="width:120px">Wklej</button>
	          {% if perms.dictionary.add_syntactic_frames %}
	            <button type="button" id="cut" style="width:120px">Wytnij</button>
	          {% endif %}
	          {% if perms.dictionary.add_phraseologic_frames %} 
	            <button type="button" id="add-phraseologic-frame" style="width:120px">Dodaj schemat frazeologiczny</button>
	            <button type="button" id="assign-phraseologic-frame" style="width:120px">Przypisz schemat frazeologiczny</button>
		      {% endif %}
	        </p>
	      </div>
	  {% endif %}
      <div id="add_nkjp_example">
        {% if perms.dictionary.add_syntactic_frames or perms.dictionary.add_phraseologic_frames %}
	        {% if skladnica_examples %}
	          <div>
	            Propozycje przykładów [Składnica]:
			    <select id='skladnica-example-propositions-select'>
	            </select>
	          </div>
	        {% endif %}
	        {% if xcp_examples %}
	          <div>
	            Propozycje przykładów [Xcp]:
			    <select id='xcp-example-propositions-select'>
	            </select> 
	          </div>
	        {% endif %}
	        <form class="nkjp-example-form" method="post"> {% csrf_token %}
	          {{ add_nkjp_form.as_p }}
	          <p class="lexeme-save">
	            <button type="submit" id="nkjp-example-form-submit">
	              Zapisz
	            </button>
	            <button type="button" id="nkjp-example-form-cancel">
	              Wyczyść
	            </button>
	          </p>
	        </form>
	    {% elif perms.dictionary.add_semantic_examples %}
	    	<form class="semantic-example-form" method="post"> {% csrf_token %}
	          {{ add_nkjp_form.as_p }}
	          <p class="example-save">
	            <button type="submit" id="semantic-example-form-submit">
	              Zapisz
	            </button>
	            <button type="button" id="semantic-example-form-cancel">
	              Wyczyść
	            </button>
	          </p>
	    	</form>
        {% endif %}
      </div>
    {% endif %}
    
    <div id="show_nkjp_example">
      <div id="show_nkjp_table">
        Wybierz schemat, aby zobaczyć dodane do niego przykłady.
      </div>
      {% if can_modify %}
        <div id='modify_example_buttons'>
          {% if perms.dictionary.add_syntactic_frames or perms.dictionary.add_phraseologic_frames %}
	          <button type="button" id="delete_example" style="width:140px">Usuń</button>
	          <button type="button" id="delete_all_examples" style="width:140px">Usuń wszystkie</button>
	          <button type="button" id="modify_example" style="width:140px">Modyfikuj</button>
	          <button type="button" id="unpin_example" style="width:140px">Odepnij</button>
	      {% else %}
	      	  {% if perms.dictionary.delete_semantic_examples %}
	      	  	<button type="button" id="delete_semantic_example" style="width:140px">Usuń</button>
	      	  {% endif %}
          {% endif %}
          <button type="button" id="confirm_example" style="width:140px; visibility:hidden;">Potwierdź</button>
        </div>
      {% endif %}
    </div>    
  </div>
</div>
<div id="pinned-examples-dialog">
    Usuwany element posiada dowiązane przykłady. Co należy z nimi zrobić?
</div>

<script type="text/javascript">

$(document).ready(function() {

    window.lemma_entry = '{{selected_lemma.entry}}';
    {% if can_modify %}
      {% if perms.dictionary.add_syntactic_frames or perms.dictionary.add_phraseologic_frames %}
        window.activeNewFramePanel = 'frames_modif';
      {% else %}
      	window.activeNewFramePanel = 'add_nkjp_example';
      {% endif %}
    {% else %}
      window.activeNewFramePanel = 'show_nkjp_example';
    {% endif %}
    $(".new_frame_tabs").tabs().bind('tabsselect', function(event, ui) {
         window.activeNewFramePanel = ui.panel.id;
         $("button#confirm_example").css("visibility","hidden");
       // czyszczenie zaznaczen elementow w tablicach (trzeba to jeszcze fizycznie zrobic)
         unselectTd(window.selected_id);
         window.selected_id = -1;
         unselectTd(selectedFrameIdNkjp);
         window.selectedArgumentsIdNkjp = new Array();
         window.selected_example_id = -1;
         window.selected_lemma_example_id = -1;
         $('#show_nkjp_table').empty();
         selectedFrameIdNkjp = -1;
    });

    draw_frames_filter('frame_filter');
    $('#filter_frames_options #options').hide();
    $('#filter_frames_options').click(function(){
        $('#filter_frames_options #options').slideToggle('fast');
    });
    $('#frame_filter #schema_type_filter').change(draw_filtered_new_frames);
    $('#frame_filter #aspect_filter').change(draw_filtered_new_frames);
    $('#frame_filter #reflex_filter').change(draw_filtered_new_frames);
    $('#frame_filter #neg_filter').change(draw_filtered_new_frames);
    $('#frame_filter #pred_filter').change(draw_filtered_new_frames);
    $('#frame_filter #opinion_filter').change(draw_filtered_new_frames);
    $('#frame_filter #argument_filter').change(draw_filtered_new_frames);
    $('#frame_filter #position_filter').change(draw_filtered_new_frames);
    $('#filter_frames_options #options').click(function(event) {
        event.stopPropagation();
    }); 
    $('#cancel_frame_filter').click(cancel_schemata_filtering);
    
    {% if can_modify %}
        {% if perms.dictionary.add_syntactic_frames or perms.dictionary.add_phraseologic_frames %}
            $(document).unbind('keydown');
            $(document).bind('keydown', 'shift+s', saveHandle);
            $(document).bind('keydown', 'shift+z', function(evt){backOneModification(); return false; });
            $(document).bind('keydown', 'shift+y', function(evt){forwardOneModification(); return false; });
            $(document).bind('keydown', 'shift+a', function(evt){addElement(); return false; });
            $(document).bind('keydown', 'shift+r', function(evt){removeElement(); return false; });
            $(document).bind('keydown', 'shift+d', function(evt){duplicateElement(); return false; });
            $(document).bind('keydown', 'shift+c', function(evt){copyElement(); return false; });
            $(document).bind('keydown', 'shift+v', function(evt){pasteElement(); return false; });
            $(document).bind('keydown', 'shift+w', function(evt){validateSchemata(); return false; }); 
            {% if perms.dictionary.add_syntactic_frames %}
            	$(document).bind('keydown', 'shift+x', function(evt){cutElement(); return false; });
                $(document).bind('keydown', 'shift+m', function(evt){reserveLemma(); return false; });
            {% endif %}
            {% if perms.dictionary.add_phraseologic_frames %}
            	$(document).bind('keydown', 'shift+l', function(evt){addPhraseologicFrame(); return false; });
            	$(document).bind('keydown', 'shift+b', function(evt){openAssignPhraseologicFrameDialog(); return false; });
            {% endif %}
        {% endif %}
    {% else %}
        $(document).unbind('keydown');
        {% if perms.dictionary.own_lemmas %}
            $(document).bind('keydown', 'shift+m', function(evt){reserveLemma(); return false; });
        {% endif %}
    {% endif %}

});
</script>
