<!--
Copyright (c) 2012, Bartłomiej Nitoń
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

﻿<script type="text/javascript">
 // <!--
  window.onload=createFormDialog();
  function createFormDialog()
  {
    $('#add-table-elem-dialog').dialog({ autoOpen: false, modal: 'true'});
    {% if can_modify %}
      window.can_modify = true;
    {% else %}
      window.can_modify = false;
    {% endif %}
  }
 
  lemma_nkjp_examples_str = '{{lemma_nkjp_examples}}'
  lemma_nkjp_examples_str = lemma_nkjp_examples_str.replace(/&#39;/g, '\'');
  lemma_nkjp_examples_str = lemma_nkjp_examples_str.replace(/&quot;/g, '\"');
  window.nkjp_lemma_examples = serializedNkjpToObj(lemma_nkjp_examples_str);
 // window.curr_lemma_example_id = window.nkjp_lemma_examples.length+1;
 
  frame_chars_str = '{{aspect_val_list}}'
  frame_chars_str = frame_chars_str.replace(/&#39;/g, '\'');
  frame_chars_str = frame_chars_str.replace(/&quot;/g, '\"');
  window.aspect_vals = serializedObjToObj(frame_chars_str)
  
  frame_chars_str = '{{reflex_val_list}}'
  frame_chars_str = frame_chars_str.replace(/&#39;/g, '\'');
  frame_chars_str = frame_chars_str.replace(/&quot;/g, '\"');
  window.reflex_vals = serializedObjToObj(frame_chars_str)
 
  serialized_text = '{{serialized_frames}}';
  serialized_text = serialized_text.replace(/&#39;/g, '\'');
  serialized_text = serialized_text.replace(/&quot;/g, '\"');
  serialized_frames = serialized_text; 
  frames = serializedObjToObj(serialized_frames);
  
  nkjp_examples_str = '{{nkjp_examples}}'
  nkjp_examples_str = nkjp_examples_str.replace(/&#39;/g, '\'');
  nkjp_examples_str = nkjp_examples_str.replace(/&quot;/g, '\"');
  window.nkjp_examples = serializedNkjpToObj(nkjp_examples_str);
 // window.curr_example_id = window.nkjp_examples.length+1;
  
  /*window.frames_modif = new Array(); // UWAGA, przestawic do lemma-view
  window.frames_modif_idx = 0;
  var lemma_version = new Lemma_Version(window.frames, window.nkjp_examples, window.nkjp_lemma_examples);
  frames_modif.push(lemma_version);*/
  
  function draw_filtered_new_frames()
  {
    draw_filtered_frames(window.frames, 'new-frame-tables', 'new-frame-table', 'frame_filter', window.nkjp_examples, 'OthersTable', window.lemma_entry);
    {% if can_modify %}
      draw_filtered_frames(window.frames, 'new-frame-tables', 'new-frame-table', 'frame_filter', window.nkjp_examples, 'InflectionTable', window.lemma_entry);
      $("#new-frame-tables td").dblclick(function(e){
        e.stopPropagation();
        openEditForm(this.id)});
    {% endif %}
    $("#new-frame-tables td").click(function(e){
      e.stopPropagation();
      selectTd(this.id)});
  }
  
  function getParentInstance(id, frames)
  {
    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<frames.length; i++)
      {
        if(frames[i].id == frame_id && pos_id)
        {
          for(var j=0; j<frames[i].positions.length; j++)
          {
            if(frames[i].positions[j].id == pos_id && arg_id)
            {
          	  for(var k=0; k<frames[i].positions[j].arguments.length; k++)
          	  {
          	    if(frames[i].positions[j].arguments[k].id == arg_id)
          	    {
          	      return {'element': frames[i].positions[j],
          	              'type': 'position'};
          	    }
          	  }
            }
            else if(frames[i].positions[j].id == pos_id)
            { // position
              return {'element': frames[i],
          	          'type': 'frame'};
            }
          }
        }
        else if(frames[i].id == frame_id)
        { // frame
          return {'element': frames[i],
          	      'type': 'lemma'};
        }
      }
    }
    return {};
  }
  
  
  $("button#remove").click(function(){removeElement()});  
  $("button#add").click(function(){addElement()});
  $("button#copy").click(function(){copyElement();}); 
  $("button#paste").click(function(){pasteElement()});  
  $("button#cut").click(function(){cutElement()});  
  $("button#duplicate").click(function(){duplicateElement()});  
  $("button#back").click(function(){backOneModification()});  
  $("button#forward").click(function(){forwardOneModification()}); 
  $("button#save").click(function(){save_new_frames()});
  $("button#validate").click(function(){validate_new_frames(false)});
  
function delete_nkjp_example(example_id)
{
  if(example_id!=-1)
  {
    example_id = selected_example_id.replace('nkjp_', '');
    for(var i=0; i<nkjp_examples.length; i++)
    {
      if(nkjp_examples[i].example_id == example_id)
      {
        nkjp_examples.splice(i, 1);
        change = true;
        break;
      }  
    }
   
    window.frames_modif = window.frames_modif.slice(0, frames_modif_idx+1);     
    var lemma_version = new Lemma_Version(frames, window.nkjp_examples, window.nkjp_lemma_examples);
    frames_modif.push(lemma_version);
    frames_modif_idx++;   
   
    window.selected_example_id = -1;
    var parent = document.getElementById("show_nkjp_table");
    draw_nkjp_table(parent, selectedFrameIdNkjp, window.nkjp_examples, 'NkjpTableRow', 'nkjp_');  
    $("tr.NkjpTableRow").click(function(){
      selectNkjpTr(this.id)});
     
    draw_filtered_frames(window.frames, 'new-frame-tables', 'new-frame-table', 'frame_filter', window.nkjp_examples, 'InflectionTable', window.lemma_entry);  
    $("#new-frame-tables td").click(function(e){
      e.stopPropagation();
      selectTd(this.id)});
    $("#new-frame-tables td").dblclick(function(e){
      e.stopPropagation();
      openEditForm(this.id)});
  }
}
  
function delete_all_nkjp_examples(frame_id)
{
    var new_example_tab = new Array();
    for(var i=0; i<nkjp_examples.length; i++)
    {
      if(nkjp_examples[i].frame_id != frame_id)
      {
        new_example_tab.push(nkjp_examples[i]);
      }  
      else
        change = true;
    }
    window.nkjp_examples = new_example_tab;
    
    frames_modif = frames_modif.slice(0, frames_modif_idx+1);     
    var lemma_version = new Lemma_Version(window.frames, window.nkjp_examples, window.nkjp_lemma_examples);
    frames_modif.push(lemma_version);
    frames_modif_idx++;
    
    window.selected_example_id = -1;
    var parent = document.getElementById("show_nkjp_table");
    draw_nkjp_table(parent, frame_id, window.nkjp_examples, 'NkjpTableRow', 'nkjp_');
    $("tr.NkjpTableRow").click(function(){
      selectNkjpTr(this.id)});  
      
    draw_filtered_frames(window.frames, 'new-frame-tables', 'new-frame-table', 'frame_filter', window.nkjp_examples, 'InflectionTable', window.lemma_entry);  
    $("#new-frame-tables td").click(function(e){
      e.stopPropagation();
      selectTd(this.id)});
    $("#new-frame-tables td").dblclick(function(e){
      e.stopPropagation();
      openEditForm(this.id)});
}

function fill_nkjp_example_form(example, form_selector) {	
	
    $.ajaxJSON({
      method: 'post',
      url: ajax_get_nkjp_dict_ids,
      data: {
        source_str: example.source,
        opinion_str: example.opinion
      },

    callback: function(result) {      
		//$('#'+id).remove();
		//$('#lemma_note_text').empty();
		//selected_notes_row_id = -1;
		$(form_selector+' #id_source').val(result['source_id']);
		$(form_selector+' #id_opinion').val(result['opinion_id']);
		$(form_selector+' #id_sentence').val(example.sentence);
        $(form_selector+' #id_comment').val(example.comment);
    },
    error_callback: function(xhr, status, error) {
      error_alert(status + ': ' + error);
      //if (ctrl)
      //  ctrl.remove();
    },
    bad_data_callback: function(result) {
      //if (ctrl)
      //  ctrl.remove();
      return true;
    },
  });
  return false;
}

function modify_nkjp_example(example_id)
{
  if(window.selected_example_id != -1)
  {
  //  window.activeNewFramePanel = 'add_nkjp_example';
    $('.new_frame_tabs').tabs('select', 1);

    window.selected_example_id = example_id;
    var example = '';
    for(var i=0; i<window.nkjp_examples.length; i++)
    {
      example_tabId = 'nkjp_'+window.nkjp_examples[i].example_id;
      if(example_tabId == example_id)
      {
        example = window.nkjp_examples[i];
        break;
      }
    }
  
    fill_nkjp_example_form(example, '.nkjp-example-form');
    window.selectedFrameIdNkjp = example.frame_id;
    for(var i=0; i<example.arguments_ids.length; i++)
      selectTd(example.arguments_ids[i]);
  }
}

function confirm_nkjp_example(example_tabId)
{
  if(example_tabId != -1)
  {
    nkjpInstance = getNkjpExampleInstance(window.nkjp_examples, example_tabId);
    confirmNkjpExample(nkjpInstance);
  }
}

  $("button#delete_example").click(function(){
    delete_nkjp_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='framesSplit'>
  <div id="new-frame-tables">
  </div>  
  <div class="new_frame_tabs" id="tabs">
    <ul>
      {% if can_modify %}
        <li><a href="#frames_modif">Modyfikacja ramek</a></li>
        <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>
      <li id="frame_filter" style="float:right;"></li>
      <li style="float:right;">Filtrowanie ramek:</li>
    </ul>
    {% if can_modify %}
      <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>
          <button type="button" id="validate" style="width:120px">Walidacja</button>
        </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="copy" style="width:120px">Kopiuj</button>
          <button type="button" id="paste" style="width:120px">Wklej</button>
          <button type="button" id="cut" style="width:120px">Wytnij</button>
          <button type="button" id="duplicate" style="width:120px">Duplikuj</button>
        </p>
      </div>
      <div id="add_nkjp_example">
        <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="reset" id="nkjp-example-form-cancel">
              Wyczyść
            </button>
          </p>
        </form>
      </div>
    {% endif %}
    <div id="show_nkjp_example">
      <div id="show_nkjp_table">
        Wybierz ramkę, aby zobaczyć dodane do niej przykłady.
      </div>
      {% if can_modify %}
        <div id='modify_example_buttons'>
          <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>
          <button type="button" id="confirm_example" style="width:140px; visibility:hidden;">Potwierdź</button>
        </div>
      {% endif %}
    </div>    
  </div>
</div>

<script type="text/javascript">
$(document).ready(function() {
    window.lemma_entry = '{{selected_lemma.entry}}';
    {% if can_modify %}
      window.activeNewFramePanel = 'frames_modif';
    {% 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(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;
    });
    var lemma_desc = document.getElementById('lemma_desc');
    $(lemma_desc).empty();
    lemma_desc.appendChild(document.createTextNode('{{selected_lemma.entry}} ({{selected_lemma.vocabulary.name}})'));
    
    $('#frame_filter #aspect_filter').live('change', draw_filtered_new_frames);
    $('#frame_filter #reflex_filter').live('change', draw_filtered_new_frames);
    
    draw_frames_filter('frame_filter');
    //draw_filtered_frames(window.frames, 'new-frame-tables', 'new-frame-table', 'frame_filter'); 
  
    draw_filtered_frames(window.frames, 'new-frame-tables', 'new-frame-table', 'frame_filter', window.nkjp_examples, 'OthersTable', window.lemma_entry, window.lemma_entry);
    {% if can_modify %}
      draw_filtered_frames(window.frames, 'new-frame-tables', 'new-frame-table', 'frame_filter', window.nkjp_examples, 'InflectionTable', window.lemma_entry, window.lemma_entry);
      $("#new-frame-tables td").dblclick(function(e){
        e.stopPropagation();
        openEditForm(this.id)});
    {% endif %}
    $("#new-frame-tables td").click(function(e){
      e.stopPropagation();
      selectTd(this.id)}); 
      
    $('#new-frame-tables').bind('click', function(e) {
      e.stopPropagation();
      unselectTd(window.selected_id);
      window.selected_id = -1;
      unselectTd(selectedFrameIdNkjp);
      window.selectedArgumentsIdNkjp = new Array();
      $('#show_nkjp_table').empty();
      selectedFrameIdNkjp = -1;
      window.selected_example_id = -1;
    });
});
</script>
