$("#tijiao_button").on("click", function(){
  var title = $("#space_form").find("input[name='title']");
  var filepath = $("#space_form").find("input[name='filepath']");
  if($(title).val()==""){
    $.toast("请填写标题");
    return false;
  }
  if($(filepath).val()==""){
    $.toast("请上传文件");
    return false;
  }
  $.ajax({
    url: '/yidong/spaceadd/basicok', 
    type: 'POST',
    dataType: "json", 
    data: $("#space_form").serialize(),
    beforeSend: function(){
      $.showIndicator();
    },
    success: function(data){
      if(data.code=="success"){
        $.toast("提交成功");
        setTimeout(function(){window.location = "/yidong/space/index?ketangid="+ ketangid;}, 2000);
      }else{
        $.toast(data.msg);
      }
    },
    error: function(){
      $.toast("系统错误，请联系管理员");
    },
    complete: function(){
      $.hideIndicator();
    }
  });
  return false;
});

$("#cancel_button").on("click", function(){
  window.location = "/yidong/space/index?ketangid="+ ketangid;
});

//上传文件
var uploader = new plupload.Uploader({
  runtimes : 'html5,flash,silverlight,html4',
  browse_button : 'pickfiles', // you can pass an id...
  container: document.getElementById('uploadcontainer'), // ... or DOM Element itself
  url : '/yidong/upload/space',
  flash_swf_url : '/plugins/plupload/js/Moxie.swf',
  silverlight_xap_url : '/plugins/plupload/js/Moxie.xap',
  multi_selection: false,
  
  filters : {
    max_file_size : '1000mb',
    /*mime_types: [ //去掉这个条件，否则手机无法上传
      {title : "Image files", extensions : "jpg,gif,png,jpeg,bmp,mp3,mp4"}
    ]*/
  },

  init: {
    PostInit: function() {
      document.getElementById('filelist').innerHTML = '';
      document.getElementById('uploadfiles').onclick = function() {
        uploader.start();
        return false;
      };
    },

    FilesAdded: function(up, files) {
      plupload.each(files, function(file) {
        document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b><div class="progress"><div class="progress-bar" style="width: 0%"></div></div></div>';
      });
      uploader.start(); //自动开始上传
      return false;
    },

    UploadProgress: function(up, file) {
      var d = document.getElementById(file.id);
      d.getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
            var prog = d.getElementsByTagName('div')[0];
      var progBar = prog.getElementsByTagName('div')[0]
      progBar.style.width= 2*file.percent+'px';
      progBar.setAttribute('aria-valuenow', file.percent);
    },

    FileUploaded: function(up, file, info) {
      //console.log(info.response);
      if (info.status == 200){
        var json = $.parseJSON(info.response);
        $("#space_form").find("input[name='filepath']").val(json.info.path);
        $("#space_form").find("input[name='filesize']").val(json.info.filesize);
        $("#space_form").find("input[name='fileext']").val(json.info.fileext);
        $("#space_form").find("input[name='filemd5']").val(json.info.filemd5);
      }else{
        document.getElementById("console").innerHTML += file.name +'上传失败<br>';
      }
    },

    UploadComplete: function(up, files) {
      console.log('complete');
    },

    Error: function(up, err) {
      document.getElementById('console').appendChild(document.createTextNode("\nError #" + err.code + ": " + err.message));
    }
  }
});

uploader.init();