function removeLibraryValue(fieldName) {
  // Hide [fieldName]Div field - contains thumbnail or filename
  $("div#" + fieldName + "Div").hide();

  // Set input#[fieldName] value to ""
  $("input#" + fieldName).val('');
}

$(document).ready(function() {
  $(".radioGroup").each(function(i) {
    html = '';
    html = $(this).html();
    if ( html != '' ) {
      html = html.replace(/(<input[^>]*>&nbsp;[^<]+<br[^>]*>)/gi, "<span>$1</span>");
      $(this).html(html);
    }
  });
  $(".hidden input").each(function() {
    var d = new Date();
    $(this).val('cb-' + d.getTime());
  });
  $(".editForm input[type=text]:first").focus();
});

$(window).load(function() {
  $(".editForm img").each(function(i) {
    if ($(this).attr("src") == '/') {
      $(this).hide();
    } else {
      if ($(this).attr("src") != '') {
        var imgWidth = $(this).width();
        var imgHeight = $(this).height();

        if (imgWidth > 100 || imgHeight > 100) {
          if (imgWidth > imgHeight) {
            var factor = imgWidth / 100;
          } else {
            var factor = imgHeight / 100;
          }
          factor = Math.ceil(factor * 100) / 100;

          var newWidth = imgWidth / factor;
          var newHeight = imgHeight / factor;

          $(this).width(newWidth);
          $(this).height(newHeight);
        }
      }
    }
  });
  // Adding "delete" option to Library field types
  var html = '<img src="/item-delete-red.gif" width="9" height="9" alt="Delete" /></a>';

  // Look for image/file/media DIVs
  $(".editForm div[id*='Div']").each(function() {
    var id = $(this).attr('id').replace('Div', '');
    if ( $("input#" + id).length ) {
      if ($("input#" + id).val()) {
        // Append delete button to end of image...
        $(this).append(' <a href="#" onclick="removeLibraryValue(\'' + id + '\'); return false;">' +html);
      }
    }
  });
  
  /*
  var fields = [
    'Image'
  ];
  for (var i = 0, j = fields.length; i < j; i++) {
    if ($("#" + fields[i]).val()) {
      // Append delete button to end of image...
      $("div#" + fields[i] + "Div").append(' <a href="#" onclick="removeLibraryValue(\'' + fields[i] + '\'); return false;">' +html);
    }
  }
  */
});
