<% if Merb.env != "test" %>
<script type="text/javascript">
  //<![CDATA[
    (function() {
      els = document.getElementsByTagName('td');
      var forEach = function(arr, fn) {
        for(var i=0; i<arr.length; i++) {
          var res = fn(arr[i]);
          if(res === false) break;
        }
      }
      var toggleClasses = function(node, first, second) {
        var classes = node.className.split(" ");
        var newClasses = [];
        forEach(classes, function(k) {
          if(k == first) newClasses.push(second);
          else if(k == second) newClasses.push(first);
          else newClasses.push(k);
        });
        node.className = newClasses.join(" ");
      }
      forEach(els, function(el) {
        // swap the open & closed classes       
        if(hasClass(el, "expand") || hasClass(el, "collapse")) {
          el.onclick = function(e){
            tbody = this.parentNode.parentNode;
            toggleClasses(tbody, "open", "close");
          }
        }        
      })
      forEach(document.getElementsByTagName("h3"), function(el) {
        el.onclick = function(e) {
          var tag = this.nextSibling;
          while(tag.nodeType != 1) tag = tag.nextSibling;
          tag.style.display = tag.style.display == "none" ? "" : "none";
        }        
      })
      function hasClass(node, matchClass) {
        var classes = node.className.split(" ");
        for(var i=0,className;className=classes[i];i++)
          if(className == matchClass) return true;
        return false;
      }
      var els = document.getElementsByTagName("p");
      forEach(els, function(tag) {
        if(tag.className != "options") return true;
        var checkboxes = tag.getElementsByTagName("input");
        forEach(checkboxes, function(box) {
          if(window.navigator.userAgent.match(/Firefox/)) {
            box.style.top = "3px";
          }
        });
        tag.getElementsByTagName("input")[0].onclick = function(e) {
          forEach(checkboxes, function(box) {
            if(box == e.target) return true;
            box.checked = e.target.checked;
            toggleTraces(box, box);
          })
        };
        var toggleTraces = function(box, target) {
          var tbodies = tag.parentNode.getElementsByTagName("tbody");
          forEach(tbodies, function(tbody) {
            if(hasClass(tbody, target.parentNode.className)) {
              if(target.checked) tbody.style.display = "";
              else tbody.style.display = "none";
            }
          })
        }
        forEach(checkboxes, function(box) {
          if(box == checkboxes[0]) return true;
          box.onchange = function(e) { toggleTraces(box, e.target) }
        })
      })
    })();
  //]]>
</script>
<% end %>