var userId=$("#userId").val();
var uploader = new AliyunUpload.Vod({
    userId: userId,
    region:"cn-shanghai",
    partSize: 1048576,
    parallel: 5,
    retryCount: 3,
    retryDuration: 2,
    'onUploadstarted': function (uploadInfo) {
        var ajaxtoken=ajaxgettoken(uploadInfo.file.name);
        uploader.setUploadAuthAndAddress(uploadInfo, ajaxtoken.uploadAuth, ajaxtoken.uploadAddress,ajaxtoken.VideoId);
    },
    'onUploadSucceed': function (uploadInfo) {
        result(uploadInfo.file.name);
    },
    'onUploadFailed': function (uploadInfo, code, message) {
    },
    'onUploadProgress': function (uploadInfo, totalSize, loadedPercent) {
        addinfo(uploadInfo.file.name,totalSize,loadedPercent);
    },
    'onUploadTokenExpired': function (uploadInfo) {
        uploader.resumeUploadWithAuth(uploadAuth);
    },
    'onUploadEnd':function(uploadInfo){
        layer.msg('上传成功');

    }
});
document.getElementById("files").addEventListener('change', function (event) {
        for(var i=0; i<event.target.files.length; i++) {
            $("#video-list").prepend("<div class='upload-data-video-content' id='uploadBox"
                + event.target.files[i].name
                + "'> "
                + "<li class='upload-video-name' title='"+event.target.files[i].name+"' tips='"+event.target.files[i].name+"'>"
                + event.target.files[i].name
                + "</li>"
                + "<li class='upload-video-size' id='size"
                + hex_md5(event.target.files[i].name)
                + "'>"
                + "计算中"
                + "</li>"
                + " <li class='upload-video-state'>"
                + "<span>"
                + "<u id='bfb"
                + hex_md5(event.target.files[i].name)
                + "'></u>"
                + "<i id='"
                + hex_md5(event.target.files[i].name)
                + "'>0%</i>"
                + " </span>"
                + " </li>"
                + "<li class='upload-video-operation' id='state"
                + hex_md5(event.target.files[i].name)
                + "'>"
                + "等待中"
                + "</li>"
                + "</div>");
            $("#upload-data-video").css("display","block");
            $("#button-start").show();
            uploader.addFile(event.target.files[i], null, null, null);
        }
    });

function ToSelectFile() {
    $("#files").click();
}

function addinfo(name,totalSize,loadedPercent){
    var filename=hex_md5(name);
    var bfb='bfb'+filename;
    var state='state'+filename;
    var size='size'+filename;
    var percent=Math.ceil(loadedPercent * 100) + "%";
    var sizem = (totalSize/1048576).toFixed(2);
    $("#"+filename).html(percent);
    $("#"+state).html('上传中');
    $("#"+size).html(sizem+'M');
    $("#"+bfb).width(percent);
}

function result(name){
    var filename=hex_md5(name);
    var state='state'+filename;
    $("#"+state).html("<font color='green'>完成</font>");
}
function start() {
    uploader.startUpload();
}
function shut(){
    var index = parent.layer.getFrameIndex(window.name);
    window.parent.location.reload()
    parent.layer.close(index);
}
$("#myModal").on("hidden.bs.modal", function() {
    $(this).removeData("bs.modal");
    location.replace(location.href);
});