(function($){
  
  $.fn.addAnchor = function(title) {
    title = title || "Link here";
    return this.filter("*[@id]").each(function() {
      $("<a class='anchor'> \u00B6</a>").attr("href", "#" + this.id)
        .attr("title", title).appendTo(this);
    });
  }
  
  $.fn.checked = function(checked) {
    if (checked == undefined) { // getter
      if (!this.length) return false;
      return this.get(0).checked;
    } else { // setter
      return this.each(function() {
        this.checked = checked;
      });
    }
  }
  
  $.fn.enable = function(enabled) {
    if (enabled == undefined) enabled = true;
    return this.each(function() {
      this.disabled = !enabled;
      var label = $(this).parents("label");
      if (!label.length && this.id) {
        label = $("label[@for='" + this.id + "']");
      }
      if (!enabled) {
        label.addClass("disabled");
      } else {
        label.removeClass("disabled");
      }
    });
  }
  
  $.loadStyleSheet = function(href, type) {
    type = type || "text/css";
    $(document).ready(function() {
      if (document.createStyleSheet) { // MSIE
        document.createStyleSheet(href);
      } else {
        $("<link rel='stylesheet' type='" + type + "' href='" + href + "' />")
          .appendTo("head");
      }
    });
  }
  
  // Used for dynamically updating the height of a textarea
  window.resizeTextArea = function (id, rows) {
    var textarea = $("#" + id).get(0);
    if (!textarea || textarea.rows == undefined) return;
    textarea.rows = rows;
  }
  
  // The following are defined for backwards compatibility with releases prior
  // to Trac 0.11
  
  window.addEvent = function(elem, type, func) {
    $(elem).bind(type, func);
  }
  window.addHeadingLinks = function(container, title) {
    $.each(["h1", "h2", "h3", "h4", "h5", "h6"], function() {
      $(this, container).addAnchor(title);
    });
  }
  window.enableControl = function(id, enabled) {
    $("#" + id).enable(enabled);
  }
  window.getAncestorByTagName = function(elem, tagName) {
    return $(elem).parents(tagName).get(0);
  }
})(jQuery);

var ATTACHFILE_COUNTER=0;
function manageMultipleAttachFields(_obj){
    if (_obj.value == '') {
        if($("#multiAttach_tbody").get(0).rows.length == 1) return;
      $("#multiAttach_tbody").get(0).deleteRow($(_obj).attr("attachnum")*1);
      ATTACHFILE_COUNTER=0;
      $("#multiAttach_tbody tr").each(function(index, element){
          $("input", $("td", element).get(0))
              .attr("attachnum",ATTACHFILE_COUNTER)
              .get(0).name = "attachment-"+ATTACHFILE_COUNTER;
          $("input", $("td", element).get(1))
              .get(0).name = "description-"+ATTACHFILE_COUNTER;
          ATTACHFILE_COUNTER++;
      });
      $("#multiAttach_count").get(0).value = $("#multiAttach_count").val()*1-1;
    } else {
      if ($(_obj).attr("attachnum") != $("#multiAttach_count").val()-1) return;
      var tr = $("#multiAttach_tbody").get(0).insertRow(-1);
      var td = tr.insertCell(-1);
      $('<input type="file" onchange="manageMultipleAttachFields(this)">')
         .attr("attachnum",$("#multiAttach_count").val())
         .appendTo(td)
         .get(0).name = "attachment-"+$("#multiAttach_count").val();

      td = tr.insertCell(-1);
      $('<input type="text">')
          .attr("size",60)
          .appendTo(td)
          .get(0).name = "description-"+$("#multiAttach_count").val();
      $("#multiAttach_count").get(0).value = $("#multiAttach_count").val()*1+1;
    }
}

