<script>
(function($) {
    $(function() {

        $('form').submit(function() {
            if($(this).hasClass('{{ name }}_disabled')) {
                return false;
            }
        });

        if (!(new Resumable().support)) {
            alert("No uploader support");
        }
        var r = new Resumable({
            target: '{% url 'admin_resumable' %}',
            chunkSize: {{ chunkSize }},
            query: {
                csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val(),
            },
        });
        r.assignBrowse($('#{{ id }}_input_file'));
        r.on('fileAdded', function(file) {
            r.upload();
            $("form").addClass("{{ name }}_disabled");
        });
        r.on('fileSuccess', function(file, message) {
            $('#{{ id }}').val(message);
            $("#{{ id }}_uploaded_status").html(message + ' Uploaded');
            $("form").removeClass("{{ name }}_disabled");
        });
        r.on('fileError', function(file, message) {
            $("#{{ id }}_uploaded_status").html(message);
        });
        r.on('progress', function(file, message) {
            $('#{{ id }}_progress').val(r.progress());
        });
        
    });

})(django.jQuery);


</script>
<div id="container">
    <p class="file-upload">
        {% if value %}
            Currently: 
            {% if value.url %}
                <a id="{{ id }}_link" target="_new" href="{{ value.url }}">{{ value }}</a>
                {% if show_thumb %}
                    <img src="{{ value.url }}" style="width:250px;">
                {% endif %}
            {% else %}
                {{ value }}
            {% endif %}
            {{ clear_checkbox }}
            <br>
            Change:
        {% endif %}

        <span id="{{ id }}_uploaded_status"></span>
        <input type="file" id="{{ id }}_input_file">
    </p>
    <progress id="{{ id }}_progress" value="0" max="1" style="width:500px"></progress>
</div>

<input type="hidden" name="{{ name }}" id="{{ id }}" value="{{ value }}">
