<div class="edit-row">
  <? if ( is_array( $this -> _pages ) ):?>
    <? foreach ( $this -> _pages as $page ):?>
      <div class="menu_sortable">    
        <ol class="sortable" id="sortable_<?= $page['id'];?>">
          <li id="list_<?= $page['id'];?>" class="page_<?= $page['id'];?>" page="<?= $page['id'];?>">
            <div class="content">
              <span class="disclose"><span></span></span>
              Strona: <b><?= $page['title'];?></b>
            </div>
            <? if ( is_array( $page['articles'] ) ):?>
              <ol>
                <? foreach ( $page['articles'] as $article ):?>
                <li id="list_<?= $article['id'];?>" idk="<?= $article['id'];?>" page="<?= $page['id'];?>">
                  <div class="content content_article">
                    <span class="disclose"><span></span></span>
                    <?= $article['title'];?>
                  </div>
                </li>
                <? endforeach;?>
              </ol>
            <? endif;?>
          </li>
        </ol>
      </div>
    <? endforeach;?>
  <? endif;?>
</div>

<script type="text/javascript">  
  var page_id   = 0;
  
  $( document ).ready( function() {    
    $('ol.sortable').nestedSortable({
			forcePlaceholderSize: true,
			handle: 'div',
			helper:	'clone',
			items: 'li',
			opacity: .6,
			placeholder: 'placeholder',
			revert: 250,
			tabSize: 25,
			tolerance: 'pointer',
			toleranceElement: '> div',
			maxLevels: 2,
			isTree: true,
			expandOnHover: 700,
      saveArticlesOrder: true
		});

		$('.disclose').on('click', function() {
			$(this).closest('li').toggleClass('mjs-nestedSortable-collapsed').toggleClass('mjs-nestedSortable-expanded');
		});

    $( ".content_article" ).mousedown(function(e) {
      if (e.which === 3 || e.which == 1 ) {
        page_id = $( this ).parent( 'li' ).attr( 'page' );
      }
    });
            
    $( '.disclose' ).mousedown(function(e) {
      if ( e.which === 1 ) {
        xajax_savePageCookie( $( this ).parent( 'div' ).parent( 'li' ).attr( 'page' ) );
      }
    });
    
    <?
      $array = unserialize( $_COOKIE[ 'savePageCookie' ] );
      if ( is_array( $array ) ): foreach ( $array as $key => $val ):
        if ( $val ):
          ?>$( '.page_<?= $key;?>' ).children( 'div' ).children( 'span.disclose' ).click();<?
        endif;
      endforeach; endif;
    ?>
  });
  
  function saveArticlesOrder( ) {
    arraied = $( '#sortable_' + page_id ).nestedSortable( 'toArray', { startDepthCount: 0 } );
    $.prompt( 'Trwa zapisywanie...', { title: 'Proszę czekać' } );
    xajax_saveArticlesOrder( arraied, page_id );
  }
</script>