<?
global $gdb;

ob_start();
  if ( is_array( $this -> menus ) ) foreach ( $this -> menus as $menu )
  {
    ?>
      <div class="menu_sortable">    
        <ol class="sortable" id="sortable_<?= $menu['id'];?>" menu-id="<?= $menu['id'];?>">
          <li id="list_<?= $menu['id'];?>" class="menu_<?= $menu['id'];?>" menu="<?= $menu['id'];?>">
            <div class="context_0 content content_menu">
              <span class="disclose"><span></span></span>
              <? if ( !$menu['status'] ) echo '<i class="fa fa-ban fa-lg text-danger" title="Menu nieaktywne"></i>';?> 
              Menu: <b><?= $menu['name'];?></b>
            </div>
            <?= \admin\view\Pages::subpages_browse_list( \admin\factory\Pages::menu_pages( $menu['id'] ) );?>
          </li>
        </ol>
      </div>
    <?
  }

$out = ob_get_clean();

$grid = new \gridEdit;
$grid -> id               = 'pages-list';
$grid -> gdb_opt          = $gdb;
$grid -> include_plugins  = true;
$grid -> title            = 'Lista stron';
$grid -> default_buttons  = false;
$grid -> external_code    = $out;

echo $grid -> draw();
?>
<style type="text/css">
  <? if ( $this -> modal ): echo '.mfp-container'; endif;?> body.sb-top.sb-top-sm .navbar.navbar-fixed-top + #sidebar_left + #content_wrapper {
    padding-top: 0;
  }
  <? if ( $this -> modal ): echo '.mfp-container'; endif;?> #content {
    padding: 27px 0 0;
  }
  <? if ( $this -> modal ): echo '.mfp-container'; endif;?> #content > .row {
    margin: 0;
  }
  <? if ( $this -> modal ): echo '.mfp-container'; endif;?> #content > .row > .col-lg-9 {
    padding: 0;
    width: 100%;
    float: none;
  }
</style>
<script type="text/javascript" src="/libraries/jquery-nested-sortable/jquery.mjs.nestedSortable.js"></script>
<script type="text/javascript">
  var menu_id   = 0;
  
  function getUrlParam(paramName) 
  {
    var reParam = new RegExp('(?:[\?&]|&amp;)' + paramName + '=([^&]+)', 'i');
    var match = window.location.search.match(reParam);

    return (match && match.length > 1) ? match[1] : '';
  }
  
  $( document ).ready( function() 
  {    
    <? if ( !$this -> modal ):?> 
      $( '#sidebar_left, .navbar-fixed-top' ).hide();
    <? endif;?>
    
    var funcNum = getUrlParam('CKEditorFuncNum');
    
    $( 'body' ).on( 'click', '.button', function()
    {
      if ( funcNum )
      {
        window.opener.CKEDITOR.tools.callFunction(funcNum, $( this ).attr( 'url' ) );
        window.close();
      }
    });
    
    $( 'ol.sortable' ).nestedSortable(
    {
			forcePlaceholderSize: true,
			handle: 'div',
			helper:	'clone',
			items: 'li',
			opacity: .9,
			placeholder: 'placeholder',
			revert: 250,
			tabSize: 45,
			tolerance: 'pointer',
			toleranceElement: '> div',
			maxLevels: 3,
			isTree: true,
			expandOnHover: 700,
      stop: function() { 
        save_pages_order(); 
      }
		});

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

    $( ".sortable *" ).mousedown( function(e) 
    {
      menu_id = $( this ).parents( 'ol.sortable' ).attr( 'menu-id' );
    });
            
    $( '.disclose' ).mousedown( function(e) 
    {
      if ( e.which === 1 ) {
        if ( $( this ).parent( 'div' ).hasClass( 'content_menu' ) ) 
        {
          var menu_id = $( this ).parent( 'div' ).parent( 'li' ).attr( 'menu' );
    
          $.ajax(
          {
            type: 'POST',
            cache: false,
            url: '/admin/ajax.php',
            data: {
              a: 'cookie_menus',
              menu_id:  menu_id
            }
          });
        }
        else
        {
          var page_id = $( this ).parent( 'div' ).parent( 'li' ).attr( 'id' );
    
          $.ajax(
          {
            type: 'POST',
            cache: false,
            url: '/admin/ajax.php',
            data: {
              a: 'cookie_pages',
              page_id:  page_id
            }
          });
        }
      }
    });
    
    <?php
      $array = unserialize( $_COOKIE[ 'cookie_pages' ] );
      if ( is_array( $array ) ): foreach ( $array as $key => $val ):
        if ( $val ):
          ?>$( '.<?= $key;?>' ).children( 'div' ).children( 'span.disclose' ).click();<?
        endif;
      endforeach; endif;
      
      $array = unserialize( $_COOKIE[ 'cookie_menus' ] );
      if ( is_array( $array ) ): foreach ( $array as $key => $val ):
        if ( $val ):
          ?>$( '.menu_<?= $key;?>' ).children( 'div' ).children( 'span.disclose' ).click();<?
        endif;
      endforeach; endif;
    ?>
  });
</script>