{% load i18n %}
{% if not err %}

{% if filetype == 'Text' %}
    {% ifnotequal file_content None %}
    var editor = CodeMirror.fromTextArea($('#docu-view')[0], {
        {% include 'snippets/editor_set_mode.html' %}
        theme: 'default',
        indentUnit: 4,
        {% if fileext != 'txt' and fileext != 'text' %}
        lineNumbers: true,
        {% endif %}
        lineWrapping: true,
        readOnly: true
    });
    {% endifnotequal %}
{% endif %}

{% if filetype == 'Image' %}
{% include 'snippets/image_file_view_js.html' %}
{% endif %}

{% if filetype == 'SVG' %}
function setFileViewAreaHeight() {
    var $fileView = $('#file-view');
    $fileView.outerHeight($(window).height() - $fileView.offset().top);
}
setFileViewAreaHeight();
$(window).on('resize', setFileViewAreaHeight);
$('#svg-view').prev('.loading-icon').hide();
$('#svg-view').show();
{% endif %}

{% if filetype == 'Markdown' %}
    {% ifnotequal file_content None %}
    var renderer = new marked.Renderer();
    // remove 'id' attribute for headings
    renderer.heading = function (text, level) {
        return '<h' + level + '>' + text + '</h' + level + '>';
    };
    marked.setOptions({
        renderer: renderer,
        breaks: true, // keep linebreak
        smartLists: true,
        sanitize: true // Ignore any HTML that has been input
    });
    $('#md-view').html(marked('{{ file_content|escapejs }}')).children(':first').css('margin-top', '0');
    {% endifnotequal %}
{% endif %}

{% if filetype == 'Document' %}
    {% include 'snippets/document_file_viewer.html' %}
{% endif %}

{% if filetype == 'SpreadSheet' %}
    {% include 'snippets/spreadsheet_convert_js.html' %}
{% endif %}

{% if filetype == 'PDF' %}
    var raw_path = "{{ raw_path|escapejs }}";
    {% include 'snippets/pdfjs_file_viewer.html' %}
{% endif %}

{% if filetype == 'Video' %}
$('#file-view').html('<div class="video-container"><video id="sf-video" src="{{raw_path}}" width="800" height="400" class="video-js vjs-default-skin"></video></div>');
videojs.options.flash.swf = "{{MEDIA_URL}}js/video-js.swf";
videojs('sf-video', {
    'controls': true,
    'autoplay': false,
    'preload': 'auto'
});
{% endif %}

{% if filetype == 'Audio' %}
      $('#file-view').html('<div class="audio-container"><audio src="{{raw_path}}" width="500" height="30"></audio></div>');
$('audio').mediaelementplayer(/* Options */);
{% endif %}

{% if filetype == 'Unknown' %}
$('#file-view').html('<div id="file-view-tip"></div>');
{% endif %}

{% endif %}{# 'if not err' ends here. #}

{% if filetype == 'Text' or filetype == 'Markdown' %}
    $('#file-enc').on('change', function() {
        var file_enc = $(this).val();
        var s = location.search;
        if (s.indexOf('?') == -1) {
            location.search = '?file_enc=' + file_enc;
        } else {
            if (s.indexOf('file_enc') == -1) {
                location.search += '&file_enc=' + file_enc; 
            } else {
                var params = s.substr(1).split('&');
                var param;
                for (var i = 0, len = params.length; i < len; i++) {
                    param = params[i].split('=');
                    if (param[0] == 'file_enc') {
                        param[1] = file_enc;
                        params[i] = param.join('=');
                        break;
                    }
                }
                location.search = '?' + params.join('&');
            }
        }
    })
{% endif %}
