//
//  application.js
//

function prepareCategories () {
  $$("#categories li").each(function (element) {
    Event.observe(element.down("input"), "change", function () {
      if (this.checked) enableCategory(this.up("li"));
      else              disableCategory(this.up("li"))
    });
  });
}

function enableCategory (element) {
  var children = element.down("ul");
  children.removeClassName("disabled");
  
  children.select("li").each(function (child) {
    child.down("input").disabled = false;
  });
}

function disableCategory (element) {
  var children = element.down("ul");
  children.addClassName("disabled");
  
  children.select("li").each(function (child) {
    child.down("input").disabled = true;
    child.down("input").checked  = false;
  });
}

Event.observe(window, "load", function () {
  if ($("categories")) prepareCategories();
});
