{% 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' %}
window.onload = function () {
    var img = $('#image-view');
    var img_h = img.outerHeight();
    var file_view_h = parseInt($('#file-view').css('min-height'));
    {% if img_prev or img_next %}
    var prev_next_h = $('#img-prev-next').outerHeight(true);
    {% else %}
    var prev_next_h = 0;
    {% endif %}
    if (img_h < file_view_h - prev_next_h) {
        img.css({'margin-top':(file_view_h - img_h - prev_next_h)/2});
    }
    if (img.width() > 946) {
        img.width(946);
    }
    img.removeClass('vh');
}
    {% if img_prev or img_next %}
    var input_focus = false;
    $('input, textarea').focus(function() {
        input_focus = true;
    }).blur(function() {
        input_focus = false;
    });
    $('body').bind('keydown', function(e) {
        if (!input_focus) { // so cursor move in form input element can work normally
            {% if img_prev %}
            if (e.keyCode == 37) { // press '<-'
                location.href = $('#img-prev').attr('href');
            }
            {% endif %}
            {% if img_next %}
            if (e.keyCode == 39) { // press '->'
                location.href = $('#img-next').attr('href');
            }
            {% endif %}
        }
    })
    {% endif %}
{% endif %}

{% if filetype == 'SVG' %}
    if (!$.browser.mozilla && !$.browser.safari && !($.browser.msie && parseInt($.browser.version) > 8)) {
        $('#file-view').html('<div id="file-view-tip"><p>' + "{% trans "To view it online, you can use firefox, chrome or IE 9." %}" + '</p></div>');
    } else {
        $('#file-view').html('<iframe src="{{ raw_path }}" frameborder="0" id="svg-view"></iframe>');
    }
{% endif %}

{% if filetype == 'Markdown' %}
    {% ifnotequal file_content None %}
    var converter = new Markdown.Converter();
    {% include 'snippets/md_xss.html' %}
    Markdown.Extra.init(converter, {extensions: ["fenced_code_gfm", "tables", "def_list"]});
    $('#md-view').html(converter.makeHtml('{{ file_content|escapejs }}')).children(':first').css('margin-top', '0');
    {% endifnotequal %}
{% endif %}

{% if filetype == 'Document' or filetype == 'PDF' and not use_pdfjs %}
    {% include 'snippets/office_convert_js.html' %}
{% endif %}

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

{% if filetype == 'PDF' and use_pdfjs %}
if (!($.browser.msie && parseInt($.browser.version) < 10)) {
    PDFJS.workerSrc = '{{MEDIA_URL}}js/pdf.js';
    $('#file-view').html('<div id="pdf"><div id="pdf-op-bar" class="vh"><span id="pdf-page-left"><button id="prev">{% trans "Previous" %}</button></span>{% blocktrans %}<span id="pdf-page"><label for="page-number">Page</label> <input type="number" id="page-number" value="1" min="1"></input> / <span id="page-nums"></span></span>{% endblocktrans %}<span id="pdf-page-right"><button id="next" style="margin-right:15px;">{% trans "Next" %}</button><button id="full-screen">{% trans "Full Screen" %}</button></span></div><img src="{{ MEDIA_URL }}pdf_full_view/images/loading-icon.gif" alt="{% trans "loading..." %}" id="pdf-loading" style="margin:20px 0;" /><canvas data="{{ raw_path }}" id="pdf-view" class="hide"></canvas></div>').css({'text-align':'center'});
    $('#pdf-page-left, #pdf-page-right').css('display', 'inline-block');
    $('#pdf-page-left').css({'text-align':'right', 'width': $('#pdf-page-right').width()});
    $('#pdf-op-bar').removeClass('vh');
    var seahub_getPage = function (pdf, page_number) {
        pdf.getPage(page_number).then(function(page) {
            var scale = 1.5;
            var viewport = page.getViewport(scale);
            var canvas = $('#pdf-view')[0];
            var context = canvas.getContext('2d');
            canvas.height = viewport.height;
            canvas.width = viewport.width;
            var renderContext = { 
                canvasContext: context,
                viewport: viewport
            };  
            page.render(renderContext);
        }); 
    };
    PDFJS.getDocument($('#pdf-view').attr('data')).then(function(pdf) {
        $('#page-nums').html(pdf.numPages);
        $('#page-number').attr('max', pdf.numPages).css('width', String(pdf.numPages).length * 6 + 10);
        seahub_getPage(pdf, 1);
        $('#pdf-loading').addClass('hide');
        $('#pdf-view').removeClass('hide');
        $('#page-number').change(function() {
            seahub_getPage(pdf, $(this).val());
        });
        $('#prev').click(function() {
            var current = $('#page-number').val();
            if (current > 1) {
                seahub_getPage(pdf, --current);
                $('#page-number').val(current);
            }
        });
        $('#next').click(function() {
            var current = $('#page-number').val();
            if (current < pdf.numPages) {
                seahub_getPage(pdf, ++current);
                $('#page-number').val(current);
            }
        });
        $('#full-screen').click(function() {
            window.open('{{ SITE_ROOT }}pdf_full_view/?repo_id={{ repo.id }}&obj_id={{obj_id}}&file_name=' + e('{{ file_name }}'));
        });
    });
} else {
    $('#file-view').html('<div id="file-view-tip"><p>{% trans "You can use IE 10 or other browsers, for example, firefox, to view it online." %}</p></div>');
}
{% endif %}

{% if filetype == 'Video' %}
      $('#file-view').html('<div class="video-container"><video src="{{raw_path}}" width="800" height="400"></video></div>');
$('video').mediaelementplayer(/* Options */);
{% 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"><p>{% trans "This type of file cannot be viewed online." %}</p></div>');
{% endif %}

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

{% if filetype == 'Text' or filetype == 'Sf' or filetype == 'Markdown' %}
    $('#file-enc').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 %}
