<!--
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">
 // <!-- 
  var prev_frames = new Array();
  var prev_nkjp_examples = new Array();
  var activePrevPanel = 'prev_frames_modif';
  var selectedPrevFrameIdNkjp = -1;
  var prev_selected_example_id = -1;
 
  frame_chars_str = '{{frame_char_list}}'
  frame_chars_str = frame_chars_str.replace(/&#39;/g, '\'');
  frame_chars_str = frame_chars_str.replace(/&quot;/g, '\"');
  window.frame_char_order = 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; 
  window.prev_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.prev_nkjp_examples = serializedNkjpToObj(nkjp_examples_str);
  
  // drawing initial tables
 // var parent = document.getElementById("prev-lemma-tables");
 // draw_frames(window.prev_frames, parent, 'prev-lemma-table');

function selectPrevNkjpTr(id)
{  	  
  if(window.prev_selected_example_id != -1)
  {
  	var recent_row = document.getElementById(window.prev_selected_example_id);
  	var recent_descendants = getDescendantsNodes(recent_row);
  	for(var i = 0; i<recent_descendants.length; i++)
  	  $(recent_descendants[i]).css("background-color", "");
  }
  	
  if(window.prev_selected_example_id == id)
  {
  	  window.prev_selected_example_id = -1;
  }
  else
  {
  	window.prev_selected_example_id = id;
  	var row = document.getElementById(id);
  	var descendants = getDescendantsNodes(row);
  	for(var i = 0; i<descendants.length; i++)
  	  $(descendants[i]).css("background-color", "LightSteelBlue");  	  
  	nkjpInstance = getNkjpExampleInstance(window.prev_nkjp_examples, id);
 
 // wyswietlanie komentarza jesli istnieje
  	if(nkjpInstance.comment)
  	{
  	   var nkjp_table_container = document.getElementById("show_prev_nkjp_table");
  	   var last_shown_comm_elem = document.getElementById('prev_example_comment');
  	   $(last_shown_comm_elem).remove();
  	   
  	   var outer_div = document.createElement('div');
  	   outer_div.setAttribute('id', 'prev_example_comment');
  	   var strong = document.createElement('strong');
  	   strong.appendChild(document.createTextNode("Komentarz do przykładu:"));
  	   outer_div.appendChild(strong);
  	   div = document.createElement('div');
  	   pre = document.createElement('pre');
  	   pre.appendChild(document.createTextNode(nkjpInstance.comment));
  	   div.appendChild(pre);
  	   outer_div.appendChild(div);
  	   nkjp_table_container.appendChild(outer_div);
  	}	 
  	
    //selectTd(nkjpInstance.frame_id);
    var parent = document.getElementById("prev-lemma-tables");
    var matching_elements = getMatchingElements(parent, nkjpInstance.frame_id);
	for(var i=0; i<matching_elements.length; i++)
      $(matching_elements[i]).css("background-color", "LightSteelBlue");
      
    for(var i=0; i<nkjpInstance.arguments_ids.length; i++)
    {
      unselectPrevTd(nkjpInstance.arguments_ids[i]);
    }
  }
}
 
function selectPrevTd(id)
{     
  	var parent = document.getElementById("prev-lemma-tables");
  	var recent_sel_elements = new Array();
  	
  	if(window.activePrevPanel == 'prev_frames_modif')
  	{
  	  if(selected_id != -1)
  	  {
  	    recent_sel_elements = getMatchingElements(parent, selected_id);
  	    for(var i=0; i<recent_sel_elements.length; i++)
		  $(recent_sel_elements[i]).css("background-color", "");
  	  }
  	
  	  if(selected_id == id)
  	    selected_id = -1;
  	  else
  	  {
  	    selected_id = id;
  	    var matching_elements = getMatchingElements(parent, id);
	    for(var i=0; i<matching_elements.length; i++)
          $(matching_elements[i]).css("background-color", "LightSteelBlue");
  	  }
  	}
  	else if(window.activePrevPanel == 'show_prev_nkjp_example')
  	{
  	  elementInstance = getElementInstance(id, window.prev_frames);
  	  if(elementInstance['type'] == 'frame')
  	  {
  	    prev_selected_example_id = -1;
  	    if(id != window.selectedPrevFrameIdNkjp)
  	      unselectPrevTd(window.selectedPrevFrameIdNkjp);
  	      
  	    window.selectedPrevFrameIdNkjp = id;
  	    var parent = document.getElementById("prev-lemma-tables");
  	    var matching_elements = getMatchingElements(parent, id);
	    for(var i=0; i<matching_elements.length; i++)
          $(matching_elements[i]).css("background-color", "LightSteelBlue");
          
        var table_container = document.getElementById("show_prev_nkjp_table");
        draw_nkjp_table(table_container, id, prev_nkjp_examples, 'NkjpTableRow', 'nkjp_');
        $("tr.NkjpTableRow").click(function(){
          selectPrevNkjpTr(this.id)});
  	  }
  	}  	
}

function unselectPrevTd(id)
{ 
  	var parent = document.getElementById("prev-lemma-tables");
  	var recent_sel_elements = getMatchingElements(parent, id);
  	for(var i=0; i<recent_sel_elements.length; i++)
	  $(recent_sel_elements[i]).css("background-color", "");
}

  function draw_filtered_prev_frames()
  {
    draw_filtered_frames(window.prev_frames, 'prev-lemma-tables', 'prev-lemma-table', 'prev_frame_filter', window.prev_nkjp_examples, 'OthersTable', window.prev_lemma_entry);
    $("#prev-lemma-tables td").click(function(){
      selectPrevTd(this.id)});
  }

  $("button#prev_copy").click(function(){copyElement()}); 
  $("#prev-lemma-tables td").click(function(){
    selectPrevTd(this.id)});  
  
  // -->
</script>
<div id='prevFramesSplit'>
  <div id="prev-lemma-tables">
  </div>  
  <div id="prev_tabs" class="prev_tabs">
    <ul>
      <li><a href="#prev_frames_modif">Modyfikacja ramek</a></li>
      <li><a href="#show_prev_nkjp_example">Przegląd przykładów</a></li>
      <li><a href="#similar_lemmas_old_frames">Czasowniki podobne (stare ramki)</a></li>
      <li><a href="#similar_lemmas_new_frames">Czasowniki podobne (nowe ramki)</a></li>
      <li id='prev_lemma_info' style="float:right;">{{selected_lemma.entry}} ({{selected_lemma.vocabulary.name}})</li>
      <li id="prev_frame_filter" style="float:right;"></li>
      <li style="float:right;">Filtrowanie ramek:</li>
    </ul>

    <div id="prev_frames_modif">
      <button type="button" id="prev_copy" style="width:120px">Kopiuj</button>
    </div>
    
    <div id="show_prev_nkjp_example">
      <div id="show_prev_nkjp_table">
        Wybierz ramkę aby zobaczyć dodane do niej przykłady.
      </div>
    </div>
    <div id="similar_lemmas_new_frames">
      <form class="similar-lemmas-new-form" method="post"> {% csrf_token %}
        {{ similarLemmasNewForm.as_p }}
        <p class="lexeme-save">
          <button type="submit" id="similar-lemmas-new-form-submit">
            Pokaż podobne
          </button>
          <button type="reset" id="similar-lemmas-new-form-cancel">
            Wyczyść
          </button>
        </p>
      </form>
    </div>
    <div id="similar_lemmas_old_frames">
      <form class="similar-lemmas-old-form" method="post"> {% csrf_token %}
        {{ similarLemmasOldForm.as_p }}
        <p class="lexeme-save">
          <button type="submit" id="similar-lemmas-old-form-submit">
            Pokaż podobne
          </button>
          <button type="reset" id="similar-lemmas-old-form-cancel">
            Wyczyść
          </button>
        </p>
      </form>
    </div>
  </div>
</div>


<script type="text/javascript">
$(document).ready(function() {
    window.prev_lemma_entry = '{{selected_lemma.entry}}';
    window.activePrevPanel = 'prev_frames_modif';
    $(".prev_tabs").tabs().bind('tabsselect', function(event, ui) {
         window.activePrevPanel = ui.panel.id;
       // czyszczenie zaznaczen elementow w tablicach (trzeba to jeszcze fizycznie zrobic)
         unselectPrevTd(selected_id);
         window.selected_id = -1;
         unselectPrevTd(window.selectedPrevFrameIdNkjp);
         //$('#show_prev_nkjp_table').empty();
         window.selectedPrevFrameIdNkjp = -1;
         prev_selected_example_id = -1;
    });
    draw_frames_filter('prev_frame_filter');
    
    draw_filtered_prev_frames();
    $('#prev_frame_filter #aspect_filter').live('change', draw_filtered_prev_frames);
    $('#prev_frame_filter #reflex_filter').live('change', draw_filtered_prev_frames);
});
</script>
