<?php
        if($_GET['modal_view']) {
            echo '<div class="modal-dialog"><div class="modal-content">';         
            echo '<div class="modal-body">';            
        } else {
            require_once('_backend/templates/header.php');        
        }
?>            
<?php require_once('_backend/internals/file_manager/views/custom_header.php'); ?>
        
     
    <div class="left" style="overflow-x: scroll">
        <div id="dirTree"><ul><li id="node_root"><a href="#" path="/">/</a><?php echo listDirectories($cfg['admin']['file_manager_path'])?></li></ul></div>        
    </div>
        
        
    <div class="right" style="overflow: scroll" id="filesList">
     
        <?php require_once('_backend/internals/file_manager/views/files_list.php'); ?>
    


    </div>
    <div class="panel-footer">

        <form id="selected_files">
            
        </form>
 
        <div id="upload_progress" class="progress progress-striped active">
            <div class="progress-bar progress-bar-default"></div>
        </div>
    
        <span class="btn btn-success fileinput-button" style="float: right;">
            <i class="glyphicon glyphicon-plus"></i>
            <span><?php echo _t('SelectFiles') ?></span>
            <!-- The file input field used as target for the file upload widget -->
            <input id="fileupload" type="file" name="files[]" multiple>
        </span>
        
        <a style="float: right; margin-right: 5px;" type="button" class="btn btn-success ajaxModal" id="create_dir" href="/<?php echo $_url[0]; ?>/file_manager/create_dir?dir=<?php echo $_SESSION['file_manager']['path'] ?>" mode="post"><span class="glyphicon glyphicon-folder-open"></span> Nowy katalog </a>  		
        <a style="float: right; margin-right: 5px;" type="button" class="btn btn-danger disabled ajaxModal" id="remove_files" href="/<?php echo $_url[0]; ?>/file_manager/remove_files" mode="post" formdata="#selected_files"><span class="glyphicon glyphicon-trash"></span> Usuń </a>            
        <?php if($_GET['modal_view']) { ?><a style="float: right; margin-right: 5px;" type="button" class="btn btn-primary disabled" id="select_files"><span class="glyphicon glyphicon-trash"></span> Wybierz </a><?php } ?>            
        


    
  
    <!-- The container for the uploaded files --> 
        <div class="clear"></div>
    </div>  
    
    <input type="hidden" name="current_upload_dir" value="<?php echo $dir ?>"/>
    
    <script type="text/javascript">
        function check_thumbnails() {
            $('.preview[generate_thumb]').each(function() {
                var obj=$(this);
                $.get(url_panel+"/file_manager/generate_thumb?file="+$(this).attr('generate_thumb'), function(data) {
                    if(data=='ERROR') {
                        spl=obj.attr('generate_thumb').split('.');    
                        obj.css('background', 'none');                    
                        obj.html('<span style="font-size: 90px; color: #D43F3A;" class="glyphicon glyphicon-file"></span><div class="nothumb"><span class="glyphicon glyphicon-warning-sign"></span> '+spl[spl.length-1]+'</div>')    
                    } else {
                        obj.css('background', 'url('+data+') no-repeat center center');
                    }
                });
            }); 
        }
        
        $(document).ready(function() {
            check_thumbnails();
            $('#dirTree').jstree({ "plugins" : ["themes","html_data","ui"],
                    'themes': {
                        'theme' : "default",
                        'dots' : false,
                        'icons' : true,
                    },
                    'core': {
                        'animation' : 0,
                    }     
                 
                 })
                 .bind("select_node.jstree", function(event, data) {
                    $('#selected_files').html('');
                    $('#remove_files').addClass('disabled');      
                    $('#select_files').addClass('disabled');                                  
                    $.get(url_panel+"/file_manager?ajax=1&dir="+$('.jstree-clicked').attr('path'), function(data) {
                        $('#filesList').html(data);
                        upload_url = url_panel + "/file_manager/upload?dir="+$('.jstree-clicked').attr('path');                     
                        $('#fileupload').fileupload({     url: upload_url});                          
                        check_thumbnails();
                    });
        
                    //alert(data);
                 })
                 .on("loaded.jstree", function() {
                    $(this).jstree('open_node', $('#node_root'));
                    //$(this).jstree('open_node', $('li[path=\\/_config]'));
                    //$(this).jstree('open_node', $('li[path=<?php echo str_replace('/', '\\\\/', $_SESSION['file_manager']['path'])?>]'));
                   // $('li[path=<?php echo str_replace('/', '\\\\/', $_SESSION['file_manager']['path'])?>]').addClass('jstree-clicked');
                   $('a[path=<?php echo str_replace('/', '\\\\/', $_SESSION['file_manager']['path'])?>]').trigger('click');             
                   
                 })
        		// 1) the loaded event fires as soon as data is parsed and inserted
        		.bind("loaded.jstree", function (event, data) { })
        		// 2) but if you are using the cookie plugin or the core `initially_open` option:
        		.one("reopen.jstree", function (event, data) { })
        		// 3) but if you are using the cookie plugin or the UI `initially_select` option:
        		.one("reselect.jstree", function (event, data) { });
                
                
                
            // file upload support
                upload_url = url_panel+ "/file_manager/upload";        
                $('#fileupload').fileupload({
                    url: upload_url,
                    dataType: 'json',
                    done: function (e, data) {
                        $.each(data.result.files, function (index, file) {
                            if($('input[name=current_upload_dir]').val()==$('.jstree-clicked').attr('path')) {
                                $.get(url_panel+"/file_manager?ajax=1&dir="+$('.jstree-clicked').attr('path'), function(data) {
                                    $('#filesList').html(data);                      
                                    check_thumbnails();  
                                });                          
                            }

                        });
                    },
                    add: function(e ,data) {
                        $('#upload_progress').css('display', 'block');
                        $('#upload_progress').animate({'width':'300px'}, 200, function() {
                            $('input[name=current_upload_dir]').val($('.jstree-clicked').attr('path'));                          
                            data.submit();
                        });
                        
                    },                  
                    progressall: function (e, data) {
                        var progress = parseInt(data.loaded / data.total * 100, 10);
                        $('#upload_progress .progress-bar').css(
                            'width',
                            progress + '%'
                        );
                        if(progress==100) {
                        /*
                          $('#upload_progress .progress-bar').css(
                              'width','0%'
                          );*/            
                          $('#upload_progress').animate({'width':'0'}, 200, function() {
                                $('#upload_progress .progress-bar').css('width','0%');                          
                                $('#upload_progress').css('display', 'none');                          
                          });                
                        }
                    }
                }).prop('disabled', !$.support.fileInput)
                    .parent().addClass($.support.fileInput ? undefined : 'disabled');
                    
                    
                $('#filesList').selectable({
                    stop: function( event, ui ) {
                        $('#selected_files').html('');
                        $('#remove_files').addClass('disabled');
                        $('#select_files').addClass('disabled');                        
                        $('#selected_files').append('<input type="hidden" name="dir" value="'+$('.jstree-clicked').attr('path')+'"/>');
                        $('#filesList .file_item.ui-selected').each(function() {
                            $('#remove_files').removeClass('disabled');
                            $('#select_files').removeClass('disabled');                            
                            //alert($(this).find('.filename').html());
                            $('#selected_files').append('<input type="hidden" name="selected_files[]" value="'+$(this).find('.filename').html()+'"/>')
                        });
                        //alert('lucyphere');
                    },
                    <?php if($_GET['ui_single_select']) { ?>
                    selected: function(event, ui) { 
                        $(ui.selected).addClass("ui-selected").siblings().removeClass("ui-selected");           
                    }  
                    <?php } ?>  
                });
               //alert($('#fileupload').fileupload('url'));
            
                                 
                $('body').on('click','#select_files', function(event) {
                    event.stopImmediatePropagation();
                    if($('.file_item.ui-selected').length>1) {
                        alert('Możesz wybrać tylko jeden plik');
                    } else {
                        <?php if($_GET['editor']) { ?>
                            var editor;  
                            for(var i in CKEDITOR.instances) {                          
                                if($(CKEDITOR.instances[i]).attr('id')=='<?php echo $_GET['editor']?>') {
                                  editor=CKEDITOR.instances[i];
                                } 
                                
                             }
                             var path=$('.jstree-clicked').attr('path');                             
                             if(path=='/') path='';                             
                             editor.insertHtml('<img src="/media/'+path+'/'+$('.file_item.ui-selected').find('.filename').html()+'"/>');
                             $('#modalFullscreen').modal('hide')
                        <?php } else { ?>
                            var path=$('.jstree-clicked').attr('path');
                            if(path=='/') path='';
                            $('.imageCurrentField').val('/media'+path+'/'+$('.file_item.ui-selected').find('.filename').html());
                            //$('.imageCurrentField').next().val(path+'/'+$('.file_item.ui-selected').find('.filename').html());
                            $('#modalFullscreen').modal('hide')
                        <?php } ?>   
                    }
                });

                              
        });
        
        
    </script>


          
<?php      
    if($_GET['modal_view']) {
        echo '</div></div></div>';
    } else {                    
        require_once('_backend/templates/footer.php');
    }
?>   