<!--
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.selected_ctrl_row_id = -1;
  ax_change_id = -1;
 
  function selectTr(id)
  {  	  
  	if(selected_ctrl_row_id != -1)
  	{
  	  var recent_row = document.getElementById(selected_ctrl_row_id);
  	  var recent_descendants = getDescendantsNodes(recent_row);
  	  for(var i = 0; i<recent_descendants.length; i++)
  	    $(recent_descendants[i]).css("background-color", "");
  	}
  	
  	if(selected_ctrl_row_id == id)
  	  selected_ctrl_row_id = -1;
  	else
  	{
  	  selected_ctrl_row_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");
  	  change_id = id.replace('change_', '');
  	  
  	  $.get(ajax_get_ctrl_preview, {id: change_id}, function(resp) {
        draw_filtered_frames(resp['serialized_frames'], 'change-ctrl-preview', 'ctrl-frame-table', '', new Array(), 'OthersTable', window.lemma_entry);
      }); 	  
  	}
  }
  
  function restore_lemma() {
      ShowProgressAnimation();
      change = false;
    
      if(selected_ctrl_row_id != -1)
  	  {
  	      change_id = selected_ctrl_row_id.replace('change_', '');
  	      $.ajaxJSON({
            method: 'post',
            url: ajax_restore_lemma,
            data: {
              change_id: change_id, // haslo przechowywane w systemie kontroli zmian
              lemma_id: lemma_id    // obecnie ustawione haslo
            },

            callback: function(result) {
              window.lastSelectedId = result['id'];
              grid.trigger("reloadGrid");

              data = grid.jqGrid('getGridParam', 'postData');
              data['mask'] = '';
              data['id'] = result['id'];
              window.lemma_id = result['id'];

              $.get(ajax_location, data, function(resp) {
                var selectedRow = resp['selected_id'] + "";
                var rowIndex = resp['rowIndex'];
                var recordsCount = resp['records'];
                if (recordsCount > 0 && !resp['lemma_nfound']){
               // grid.jqGrid('scrollAndSet', selectedRow, rowIndex, recordsCount);
                  grid.jqGrid('scrollToRow', rowIndex, recordsCount);
                }
                else if(resp['lemma_nfound'])
                {
                  load_content(window.lemma_id);
                }
              });
              HideProgressAnimation();	
              alert('Przywracanie hasła zakończono pomyślnie.');	
            },
    
            error_callback: function(xhr, status, error) {
              HideProgressAnimation();
              error_alert(status + ': ' + error);
      //if (ctrl)
      //  ctrl.remove();
            },
          });
  	  }
      return false;
  }

  $("tr.ChangeControlTableRow").click(function(){
    selectTr(this.id)});
    
  $("button#restore").click(function(){
    restore_lemma();
  });

</script>

<div class="change-ctrl-container">

  <table class='ChangeControlTable' id='change-ctrl-table'>
	<tr> 
	  <td class='ColumnHeader'>Czas modyfikacji:</td> 
	  <td class='ColumnHeader'>Właściciel:</td>
	  <td class='ColumnHeader'>Osoba dokonująca zmiany:</td>
	</tr>
	{% for version in old_versions %}
	  <tr class='ChangeControlTableRow' id="change_{{version.id}}"> 
		<td>{{version.time}}</td> 
		<td>{{version.act_owner.username}}</td>
		<td>{{version.user.username}}</td>
	  </tr>
	{% endfor %}
  </table>	
</div>

{% if can_modify %}
<div id="change-ctrl-buttons">
  <button type="button" id="restore" style="width:120px">Przywróć</button>
</div>
{% endif %}
</br>
</br>
<strong>Podgląd:</strong>
<div id="change-ctrl-preview">
</div>
