text: function editorAction(json) {
      var cm = $('.CodeMirror')[0].CodeMirror;
      var doc = cm.getDoc();
      doc.replaceSelections(["![](/" + json.file.url + ")"]);
  }
  
  function filesAction(json) {
      var $fileLine = $('<tr id="file-' + json.file.name + '">' 
          + '<td class="mdl-data-table__cell--non-numeric">' + json.file.time + '</td>'
          + '<td class="mdl-data-table__cell--non-numeric">' + json.file.size + '</td>'
          + '<td class="mdl-data-table__cell--non-numeric">' + json.file.name + '</td>'
          + '<td class="mdl-data-table__cell--non-numeric">' + json.file.type + '</td>'
          + '<td class="mdl-data-table__cell--non-numeric">'
            + '<a class="btn btn-small blue" href="/'+ json.file.url +'" target="_blank" title="/' + json.file.name + '">View</a>&nbsp;'
            + '<a class="btn btn-small red delete-file" href="#" name="' + json.file.name + '" rel="' + json.file.url + '" title="Delete">Delete</a>'
          + '</td></tr>');
      $('tbody').append($fileLine);
  }
  
  function initUpload(p) {
      $('#attach-show').on("click", function() {
          $('#attach-upload').trigger("click");
      });
      $('#attach-upload').on("change", function () {
          alertify.confirm("Upload now?", function() {
              var bar = $('<p class="file-progress inline-block">0%</p>');
              $('#attach-form').ajaxSubmit({
                  "beforeSubmit": function () {
                      $(p).before(bar);
                  },
                  "uploadProgress": function (event, position, total, percentComplete) {
                      var percentVal = percentComplete + '%';
                      bar.css("width", percentVal).html(percentVal);
                  },
                  "success": function (json) {
                      $('#attach-upload').val("");
                      if (json.status === "error") {
                          bar.html(json.msg).addClass("err");
                          setTimeout(function () {
                              bar.remove();
                          }, 5000);
                          return
                      }
                      
                      alertify.success("File has been uploaded.")
                      bar.html("/" + json.file.url + "&nbsp;&nbsp;&nbsp;(@" + json.file.name + ")");
                      
                      if ($('.CodeMirror').length == 0) {
                          filesAction(json);
                      } else {
                          editorAction(json);
                      }
                  }
              });
          }, function() {
              $(this).val("");
          });
      });
  }
  