	/**
    *  Plupload 上传
	*/

	// 图片上传
	var settings = {
		runtimes : 'html5',
		browse_button : 'pickfile',
		container: document.getElementById('fileupload'),
		url : url_upload,
		dragdrop: true,
		multi_selection: true,
		multipart_params: {'type': 1},
		headers: {
			'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		},
		init: {
			PostInit: function(up) {
				up._container = $(up.getOption('container'));
				up._btn   = $(up.getOption('browse_button'));
				up._multi_selection  = $(up.getOption('multi_selection'))[0];
				var type  = up._container.find('.upload').data('type');
				var thumb = up._container.find('.upload').data('thumb');
				if (type) {
					up.setOption('multipart_params', {'type': type, 'thumb': thumb});
				}
			},
			FilesAdded: function(up, files) {
				up._container.siblings('.upload-message').html('<span>正在上传...</span>');
				up.start();
			},
			UploadProgress: function(up, file) {},
            FileUploaded: function(up, file, data) {
				var response = $.parseJSON(data.response);
                var upload   = up._container.find('.upload');
				var name     = upload.data('name');
				if (!response.error && response.state == "SUCCESS") {
					var html = "<li class='thumbnail' data-img='"
							+ response.path +"'>"
							+ "<input type='hidden' name='" + name + "' value='" + response.path + "'>"
							+ "<a class='thumbnail-img'><img src='" 
							+ response.path + '?x-oss-process=image/resize,m_fixed,h_80,w_80' 
							+ "'></a><a href='javascript:;' data-id='"+ up._container[0].id +"' class='thumbnail-remove'>删除</a></li>";
                    if (up._multi_selection) {
                        up._container.siblings('.attachments').append(html).show();
                    } else {
                        up._container.siblings('.attachments').html(html).show();
                        up._container.hide();
                    }
				} else {
					swal({
						title: "", 
						text: response.state,
						type: "error",
						confirmButtonText: "确定",
						timer: 4000,
					});
				}
            },		
			UploadComplete: function(up, file) {
				up._container.siblings('.upload-message').html('');
			},
			Error: function(up, err) {
				alert('文件上传失败');
			},
		},
	};
	
	var uploader = function(config) {
		var options = {};
		$.extend(options, settings, config);

		var up = new plupload.Uploader(options);
		up.init();
	}

	// 广告图片
	var imgedit = {
		runtimes : 'html5',
		browse_button : 'imgedit',
		container: document.getElementById('fileupload'),
		url : url_upload,
		dragdrop: true,
		multipart_params: {'type': 1},
		headers: {
			'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		},
		init: {
			PostInit: function(up) {
				up._container = $(up.getOption('container'));
				up._btn  = $(up.getOption('browse_button'));
			},
			FilesAdded: function(up, files) {
				up.start();
			},
			UploadProgress: function(up, file) {},
            FileUploaded: function(up, file, data) {
				var response = $.parseJSON(data.response);
				if (!response.error && response.state == "SUCCESS") {
					up._container.find('img').attr('data-original-title', '修改图片');
					up._container.find('img').attr('src', response.path);
					up._container.find('.input-image').val(response.path);
				} else {
					swal({
						title: "", 
						text: response.state,
						type: "error",
						confirmButtonText: "确定",
						timer: 4000,
					});
				}
            },		
			Error: function(up, err) {
				alert(JSON.stringify(err));
			},
		},
	};
	
	var adUploader = function(config) {
		var options = {};
		$.extend(options, imgedit, config);
		var upEdit = new plupload.Uploader(options);
		upEdit.init();
	}

	var fileupload = new plupload.Uploader({
		runtimes : 'html5',
		browse_button : 'fileuploader',
		container: document.getElementById('filecontainer'),
		url : url_upload,
		multi_selection: false,
		chunk_size: '2mb',
		multipart_params: {'type': 1},
		headers: {
			'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		},
		init: {
			PostInit: function(up) {
				up._container = $(up.getOption('container'));
				up._btn  = $(up.getOption('browse_button'));
				var type = up._container.find('.filepath').data('type');
				if (type) {
					up.setOption('multipart_params', {'type': type});
				}
			},
			FilesAdded: function(up, files) {
				var filesize = '';
				plupload.each(files, function(file) {
					filesize += file.size;
			    });
			    up._container.find('.filesize').val(plupload.formatSize(filesize));
				up.start();
			},
			UploadProgress: function(up, file) {
				up._container.find('b')[0].innerHTML = '<span>正在上传...</span>';
			},
            FileUploaded: function(up, file, data) {
				var response = $.parseJSON(data.response);

				if (!response.error && response.state == "SUCCESS") {
					up._container.find('.fileupload').val(response.path);
				} else {
					swal({
						title: "", 
						text: response.state,
						type: "error",
						confirmButtonText: "确定",
						timer: 4000,
					});
				}
            },		
			UploadComplete: function(up, file) {
				up._container.find('b')[0].innerHTML = '';
			},
			Error: function(up, err) {
				alert('文件上传失败');
				// alert(JSON.stringify(err));
			},
		},
	});

	fileupload.init();

    // 图片上传
    var mediaSettings = {
        runtimes : 'html5',
        browse_button : 'pickmedia',
        container: document.getElementById('mediaupload'),
        url : url_upload,
        dragdrop: true,
        multi_selection: true,
        multipart_params: {'type': 4},
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        init: {
            PostInit: function(up) {
                up._container = $(up.getOption('container'));
                up._btn   = $(up.getOption('browse_button'));
                up._multi_selection  = $(up.getOption('multi_selection'))[0];
                var type  = up._container.find('.upload').data('type');
                var thumb = up._container.find('.upload').data('thumb');
                if (type) {
                    up.setOption('multipart_params', {'type': type, 'thumb': thumb});
                }
            },
            FilesAdded: function(up, files) {
                up._container.siblings('.upload-message').html('<span>正在上传...</span>');
                up.start();
            },
            UploadProgress: function(up, file) {},
            FileUploaded: function(up, file, data) {
                var response = $.parseJSON(data.response);
                var upload   = up._container.find('.upload');
                var name     = upload.data('name');
                if (!response.error && response.state == "SUCCESS") {
                    if (['ogg', 'mp4', 'webm'].indexOf(response.extension) !== -1) {
                        var html = "<li class='thumbnail' data-img='"
                            + response.path +"'>"
                            + "<input type='hidden' name='" + name + "' value='" + response.path + '?x-oss-process=video/snapshot,t_1000,f_jpg,w_400,m_fast,ar_auto' + "'>"
                            + "<a class='thumbnail-img'><video src='"
                            + response.path + "' poster='" + response.path + '?x-oss-process=video/snapshot,t_1000,f_jpg,w_400,m_fast,ar_auto' + "' controls='controls' width='80' height='80'></video></a><a href='javascript:;' data-id='"+ up._container[0].id +"' class='thumbnail-remove'>删除</a></li>";
                    } else {
                        var html = "<li class='thumbnail' data-img='"
                            + response.path +"'>"
                            + "<input type='hidden' name='" + name + "' value='" + response.path + "'>"
                            + "<a class='thumbnail-img'><img src='"
                            + response.path + '?x-oss-process=image/resize,m_fixed,h_80,w_80'
                            + "'></a><a href='javascript:;' data-id='"+ up._container[0].id +"' class='thumbnail-remove'>删除</a></li>";
                    }

                    if (up._multi_selection) {
                        up._container.siblings('.attachments').append(html).show();
                    } else {
                        up._container.siblings('.attachments').html(html).show();
                        up._container.hide();
                    }
                } else {
                    swal({
                        title: "",
                        text: response.state,
                        type: "error",
                        confirmButtonText: "确定",
                        timer: 4000,
                    });
                }
            },
            UploadComplete: function(up, file) {
                up._container.siblings('.upload-message').html('');
            },
            Error: function(up, err) {
                alert('文件上传失败');
            },
        },
    };

    var mediaUploader = function(config) {
        var options = {};
        $.extend(options, mediaSettings, config);

        var up = new plupload.Uploader(options);
        up.init();
    }