{% load i18n admin_modify adminmedia filermedia %}

<div style="margin-left: -9px;"><span id="spanButtonPlaceHolder"></span></div>

<script type="text/javascript">
//<![CDATA[
var swfu;
$(document).ready(function() {
    // Check to see if SWFUpload is available
    if (typeof(SWFUpload) === "undefined") {
        return;
    }
    var QUEUE_CONTAINER_ID = "fileInputQueue";
    var handlers = {
        file_dialog_complete: function(numFilesSelected, numFilesQueued) {
            this.startUpload();
        },
        fileQueued: function(file) {
            // handle file info stuff
            var byteSize = Math.round(file.size / 1024 * 100) * 0.01;
            var suffix = 'KB';
            if (byteSize > 1000) {
                byteSize = Math.round(byteSize *0.001 * 100) * 0.01;
                suffix = 'MB';
            }
            var sizeParts = byteSize.toString().split('.');
            if (sizeParts.length > 1) {
                byteSize = sizeParts[0] + '.' + sizeParts[1].substr(0,2);
            } else {
                byteSize = sizeParts[0];
            }
            if (file.name.length > 20) {
                fileName = file.name.substr(0,20) + '...';
            } else {
                fileName = file.name;
            }
            
            var queueString = file.id + ":  0%:" + file.name;
            $('#' + QUEUE_CONTAINER_ID + ' .noItemsRow').hide();
            $('#' + QUEUE_CONTAINER_ID).append('<tr id="' + file.id + '" class="fileUploadQueueItem">\
                    <td><div class="loadingThumb" /></td>\
                    <td>\
                        <div><span class="fileName">' + fileName + ' (' + byteSize + suffix + ')</span><span class="percentage">&nbsp;</span><span class="serveraction">&nbsp;</span></div>\
                        <div class="fileUploadProgress" style="width: 100%;">\
                            <div id="' + file.id + 'ProgressBar" class="fileUploadProgressBar" style="width: 1px; height: 3px;"></div>\
                        </div>\
                    </td>\
                    <td>\
                        ...\
                    </td>\
                ');//.show(250);
        },
        uploadProgress: function(file, bytesLoaded, totalBytes) {
            try {
                var percent = Math.ceil((bytesLoaded / file.size) * 100);
                $("#" + file.id + "ProgressBar").css('width', percent + '%');
                var queueString = file.id + ":" + percent + "%:" + file.name;
                if (parseInt(percent)==100){// && fileObj.type.toLowerCase()=='.zip') {
                    if ($.inArray('zip',file.name.toLowerCase().split('.')) == 1) {
                        $("#" + file.id + " .serveraction").text(' unpacking zip...');
                        $("#" + file.id).effect("highlight",{},4000).effect("highlight",{},4000).effect("highlight",{},4000).effect("highlight",{},4000).effect("highlight",{},4000).effect("highlight",{},4000).effect("highlight",{},4000).effect("highlight",{},4000).effect("highlight",{},4000);
                    }
                }
            } catch (ex) {
                this.debug(ex);
            }
    
        },
        uploadSuccess: function(file, serverData, receivedResponse) {
            try {
                if (receivedResponse) {
                    var responsetext = unescape(serverData);
                    var newElements = $(responsetext);
                    $("#" + file.id).before(newElements).remove();
                    newElements.effect("highlight", {}, 1000);
                } else {
                    this.debug("repsonse timeout for " + file.id)
                }
            } catch (ex) {
                this.debug(ex);
            }
        },
        uploadError : function (file, errorCode, message) {
            var errorNotification = "";
            try {
                var errorName = "";
                switch (errorCode) {
                case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
                    errorNotification = file.id + ":HTTP:" + file.name;
                    errorName = "HTTP ERROR";
                    break;
                case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
                    errorName = "MISSING UPLOAD URL";
                    break;
                case SWFUpload.UPLOAD_ERROR.IO_ERROR:
                    errorNotification = file.id + ":IO  :" + file.name;
                    errorName = "IO ERROR";
                    break;
                case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
                    errorNotification = file.id + ":SEC :" + file.name;
                    errorName = "SECURITY ERROR";
                    break;
                case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                    errorName = "UPLOAD LIMIT EXCEEDED";
                    break;
                case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
                    errorName = "UPLOAD FAILED";
                    break;
                case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
                    errorName = "SPECIFIED FILE ID NOT FOUND";
                    break;
                case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
                    errorName = "FILE VALIDATION FAILED";
                    break;
                case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
                    errorName = "FILE CANCELLED";
                    errorNotification = file.id + ":----:" + file.name;
                    break;
                case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
                    errorName = "FILE STOPPED";
                    errorNotification = file.id + ":  0%:" + file.name;
                    break;
                default:
                    errorName = "UNKNOWN";
                    break;
                }
    
                var errorString = errorName + ":File ID: " + (typeof(file) === "object" && file !== null ? file.id : "na") + ":" + message;
                $('#' + file.id + ' .serveraction').text(errorName);
            } catch (ex) {
                this.debug(ex);
            }
        }
    };
    var settings = {
        flash_url : "{% filer_staticmedia_prefix %}flash/swfupload.swf",
        upload_url: "{% url admin:filer-ajax_upload %}",
        post_params: {'jsessionid':$.cookie('sessionid')},
        file_size_limit : "100 MB",
        file_types : "*.*",
        file_types_description : "All Files",
        file_upload_limit : 200,
        file_queue_limit : 0,
        custom_settings : {
            progressTarget : "fsUploadProgress",
            cancelButtonId : "btnCancel"
        },
        debug: false,

        // Button settings
        button_image_url: "{% filer_staticmedia_prefix %}img/upload_button.png",
        button_width: "79",
        button_height: "16",
        button_placeholder_id: "spanButtonPlaceHolder",
        button_text: '<span class="theFont"></span>',
        button_text_style: ".theFont { font-size: 16; }",
        button_text_left_padding: 12,
        button_text_top_padding: 3,
        
        // Event Handler Settings
        //swfupload_loaded_handler : FeaturesDemoHandlers.swfUploadLoaded,
        //file_dialog_start_handler : FeaturesDemoHandlers.fileDialogStart,
        file_queued_handler : handlers.fileQueued,
        //file_queue_error_handler : FeaturesDemoHandlers.fileQueueError,
        file_dialog_complete_handler : handlers.file_dialog_complete,
        //upload_start_handler : FeaturesDemoHandlers.uploadStart,
        upload_progress_handler : handlers.uploadProgress,
        upload_error_handler : handlers.uploadError,
        upload_success_handler : handlers.uploadSuccess
        //upload_complete_handler : handlers.uploadComplete,
        //debug_handler : FeaturesDemoHandlers.debug,
    };

    swfu = new SWFUpload(settings);
 });
//]]>
</script>
