text: /*global Qiniu */
  /*global plupload */
  /*global FileProgress */
  /*global hljs */
  
  /*
  id:上传按钮
  container：容器id
  uptoken:七牛token值
  autostart：是否自动上传 true自动上传
  multiseleted:上传个数
  */
  function InitQiniuUpload(option) {
      var uploader = Qiniu.uploader({
          runtimes: 'html5,flash,html4',
          browse_button: option.id,
          container: option.container,
          drop_element: option.container,
          max_file_size: '2mb',
          flash_swf_url: 'js/plupload/Moxie.swf',
          dragdrop: true,
          chunk_size: '1mb',
          uptoken: option.uptoken,
          domain: option.domain,
          auto_start: option.autostart,
          mime_types: [
                          { title: "Image files", extensions: "jpg,gif,png" }
                      ],
          init: {
              'FilesAdded': function (up, files) {
                  $('table').show();
                  $('#success').hide();
                  plupload.each(files, function (file) {
                      var filemaxsize = plupload.parseSize(up.getOption('max_file_size'));
                      var filesize = file.size;
                      if (filemaxsize >= filesize)
                      {
                          var progress = new FileProgress(file, 'fsUploadProgress');
                          progress.setStatus("等待...");
                      }
                  });
              },
              'BeforeUpload': function (up, file) {
                  var progress = new FileProgress(file, 'fsUploadProgress');
                  var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
                  if (up.runtime === 'html5' && chunk_size) {
                      progress.setChunkProgess(chunk_size);
                  }
              },
              'UploadProgress': function (up, file) {
                  var progress = new FileProgress(file, 'fsUploadProgress');
                  var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
  
                  progress.setProgress(file.percent + "%", file.speed, chunk_size);
              },
              'UploadComplete': function () {
                  $('#success').show();
              },
              'FileUploaded': function (up, file, info) {
                  var progress = new FileProgress(file, 'fsUploadProgress');
                  progress.setComplete(up, info);
              },
              'Error': function (up, err, errTip) {
                  var filemaxsize = plupload.parseSize(up.getOption('max_file_size'));
                  var filesize = err.file.size;
                  if (filemaxsize >= filesize) {
                      $('table').show();
                      var progress = new FileProgress(err.file, 'fsUploadProgress');
                      progress.setError();
                      progress.setStatus(errTip);
                  }
              }
          }
      });
  
      $('#container').on(
          'dragenter',
          function (e) {
              e.preventDefault();
              $('#container').addClass('draging');
              e.stopPropagation();
          }
      ).on('drop', function (e) {
          e.preventDefault();
          $('#container').removeClass('draging');
          e.stopPropagation();
      }).on('dragleave', function (e) {
          e.preventDefault();
          $('#container').removeClass('draging');
          e.stopPropagation();
      }).on('dragover', function (e) {
          e.preventDefault();
          $('#container').addClass('draging');
          e.stopPropagation();
      });
  
  
  
      $('#show_code').on('click', function () {
          $('#myModal-code').modal();
          $('pre code').each(function (i, e) {
              hljs.highlightBlock(e);
          });
      });
  
  
      $('body').on('click', 'table button.btn', function () {
          $(this).parents('tr').next().toggle();
      });
  
  
      var getRotate = function (url) {
          if (!url) {
              return 0;
          }
          var arr = url.split('/');
          for (var i = 0, len = arr.length; i < len; i++) {
              if (arr[i] === 'rotate') {
                  return parseInt(arr[i + 1], 10);
              }
          }
          return 0;
      };
  
      $('#myModal-img .modal-body-footer').find('a').on('click', function () {
          var img = $('#myModal-img').find('.modal-body img');
          var key = img.data('key');
          var oldUrl = img.attr('src');
          var originHeight = parseInt(img.data('h'), 10);
          var fopArr = [];
          var rotate = getRotate(oldUrl);
          if (!$(this).hasClass('no-disable-click')) {
              $(this).addClass('disabled').siblings().removeClass('disabled');
              if ($(this).data('imagemogr') !== 'no-rotate') {
                  fopArr.push({
                      'fop': 'imageMogr2',
                      'auto-orient': true,
                      'strip': true,
                      'rotate': rotate,
                      'format': 'png'
                  });
              }
          } else {
              $(this).siblings().removeClass('disabled');
              var imageMogr = $(this).data('imagemogr');
              if (imageMogr === 'left') {
                  rotate = rotate - 90 < 0 ? rotate + 270 : rotate - 90;
              } else if (imageMogr === 'right') {
                  rotate = rotate + 90 > 360 ? rotate - 270 : rotate + 90;
              }
              fopArr.push({
                  'fop': 'imageMogr2',
                  'auto-orient': true,
                  'strip': true,
                  'rotate': rotate,
                  'format': 'png'
              });
          }
  
          $('#myModal-img .modal-body-footer').find('a.disabled').each(function () {
  
              var watermark = $(this).data('watermark');
              var imageView = $(this).data('imageview');
              var imageMogr = $(this).data('imagemogr');
  
              if (watermark) {
                  fopArr.push({
                      fop: 'watermark',
                      mode: 1,
                      image: 'http://www.b1.qiniudn.com/images/logo-2.png',
                      dissolve: 100,
                      gravity: watermark,
                      dx: 100,
                      dy: 100
                  });
              }
  
              if (imageView) {
                  var height;
                  switch (imageView) {
                      case 'large':
                          height = originHeight;
                          break;
                      case 'middle':
                          height = originHeight * 0.5;
                          break;
                      case 'small':
                          height = originHeight * 0.1;
                          break;
                      default:
                          height = originHeight;
                          break;
                  }
                  fopArr.push({
                      fop: 'imageView2',
                      mode: 3,
                      h: parseInt(height, 10),
                      q: 100,
                      format: 'png'
                  });
              }
  
              if (imageMogr === 'no-rotate') {
                  fopArr.push({
                      'fop': 'imageMogr2',
                      'auto-orient': true,
                      'strip': true,
                      'rotate': 0,
                      'format': 'png'
                  });
              }
          });
  
          var newUrl = Qiniu.pipeline(fopArr, key);
  
          var newImg = new Image();
          img.attr('src', 'loading.gif');
          newImg.onload = function () {
              img.attr('src', newUrl);
              img.parent('a').attr('href', newUrl);
          };
          newImg.src = newUrl;
          return false;
      });
  
      return uploader;
  
  }
  
  function getImgUrl(key) {
      var imgLink = Qiniu.watermark({
          mode: 1,  // 图片水印
          image: getQiniuUrl('watermark_ccn.png'), // 图片水印的Url，mode = 1 时 **必需**
          gravity: 'SouthEast',  // 水印位置，为以下参数[NorthWest、North、NorthEast、West、Center、East、SouthWest、South、SouthEast]之一
          dx: 50,  // 横轴边距，单位:像素(px)
          dy: 50   // 纵轴边距，单位:像素(px)
      }, key);      // key 为非必需参数，下同
      return imgLink;
  }
  