Index: modules/simpletest/simpletest.js
===================================================================
--- modules/simpletest/simpletest.js	(revision 89)
+++ modules/simpletest/simpletest.js	(working copy)
@@ -10,27 +10,36 @@
     $(this).html(Drupal.settings.simpleTest.images[direction]);
   });
   $('div.simpletest-image').click(function() {
-    // Toggle all of the trs.
+    // Toggle all of the test rows.
     if (!Drupal.settings.simpleTest[$(this).attr('id')].clickActive) {
-      Drupal.settings.simpleTest[$(this).attr('id')].clickActive = true;
-      var trs = $(this).parents('tbody').children().filter('.' + Drupal.settings.simpleTest[$(this).attr('id')].testClass), trs_formatted = [], direction = Drupal.settings.simpleTest[$(this).attr('id')].imageDirection, self = $(this);
-      for (var i = 0; i < trs.length; i++) {
-        trs_formatted.push(trs[i]);
+      // $(this) is different within jQuery selector contexts other than the
+      // current 'div.simpletest-image'. Keeping track of it with a new
+      // variable named self will allow us to handle this context change in the
+      // toggleTestRows() function.
+      var self = $(this);
+      var testRows = self.parents('tbody').children().filter('.' + Drupal.settings.simpleTest[self.attr('id')].testClass);
+      var testRowsFormatted = [];
+      var direction = Drupal.settings.simpleTest[self.attr('id')].imageDirection;
+
+      Drupal.settings.simpleTest[self.attr('id')].clickActive = true;
+
+      for (var i = 0; i < testRows.length; i++) {
+        testRowsFormatted.push(testRows[i]);
       }
-      var toggleTrs = function(trs, action, action2) {
-        tr = trs[action]();
-        if (tr) {
-          $(tr)[action2](1, function() {
-            toggleTrs(trs, action, action2);
+      var toggleTestRows = function(testRows, action, action2) {
+        var testRow = testRows[action]();
+        if (testRow) {
+          $(testRow)[action2](1, function() {
+            toggleTestRows(testRows, action, action2);
           });
         }
         else {
           Drupal.settings.simpleTest[self.attr('id')].clickActive = false;
         }
       }
-      toggleTrs(trs_formatted, (direction ? 'pop' : 'shift'), (direction ? 'fadeOut' : 'fadeIn'));
-      Drupal.settings.simpleTest[$(this).attr('id')].imageDirection = !direction;
-      $(this).html(Drupal.settings.simpleTest.images[(direction? 0 : 1)]);
+      toggleTestRows(testRowsFormatted, (direction ? 'pop' : 'shift'), (direction ? 'fadeOut' : 'fadeIn'));
+      Drupal.settings.simpleTest[self.attr('id')].imageDirection = !direction;
+      self.html(Drupal.settings.simpleTest.images[(direction? 0 : 1)]);
     }
   });
 }
@@ -41,35 +50,45 @@
  */
 Drupal.behaviors.simpleTestSelectAll = function() {
   $('td.simpletest-select-all').each(function() {
-    var checkboxes = Drupal.settings.simpleTest['simpletest-test-group-'+ $(this).attr('id')].testNames, totalCheckboxes = 0,
-      checkbox = $('<input type="checkbox" class="form-checkbox" id="'+ $(this).attr('id') +'-select-all" />').change(function() {
+      // $(this) is different within jQuery selector contexts other than the
+      // current 'td.simpletest-select-all'. Keeping track of it with a new
+      // variable named self will allow us to handle this context change in the
+      // various functions that apply to the checkboxes themselves.
+    var self = $(this);
+    var checkboxes = Drupal.settings.simpleTest['simpletest-test-group-' + self.attr('id')].testNames;
+    var checkbox = $('<input type="checkbox" class="form-checkbox" id="' + self.attr('id') + '-select-all" />');
+
+    // The number of checked tests, initially 0.
+    self.data('simpletest-checked-tests', 0);
+
+    // Have the single-test checkboxes follow the group checkbox.
+    checkbox.change(function() {
       var checked = !!($(this).attr('checked'));
       for (var i = 0; i < checkboxes.length; i++) {
         $('#'+ checkboxes[i]).attr('checked', checked);
       }
       self.data('simpletest-checked-tests', (checked ? checkboxes.length : 0));
-    }).data('simpletest-checked-tests', 0);
-    var self = $(this);
+    });
+
     for (var i = 0; i < checkboxes.length; i++) {
-      if ($('#' + checkboxes[i]).change(function() {
-        if (checkbox.attr('checked') == 'checked') {
-          checkbox.attr('checked', '');
+      // Initialize the checked test count correctly.
+      $('#' + checkboxes[i]).each(function() {
+        if (!!($(this).attr('checked'))) {
+          self.data('simpletest-checked-tests', self.data('simpletest-checked-tests') + 1);
         }
-        var data = (!self.data('simpletest-checked-tests') ? 0 : self.data('simpletest-checked-tests')) + (!!($(this).attr('checked')) ? 1 : -1);
-        self.data('simpletest-checked-tests', data);
-        if (data == checkboxes.length) {
-          checkbox.attr('checked', 'checked');
-        }
-        else {
-          checkbox.removeAttr('checked');
-        }
-      }).attr('checked') == 'checked') {
-        totalCheckboxes++;
-      }
+      })
+      // Have the group checkbox follow the single-test checkboxes.
+      .change(function() {
+        var newCount = self.data('simpletest-checked-tests') + (!!($(this).attr('checked')) ? 1 : -1);
+        self.data('simpletest-checked-tests', newCount);
+        checkbox.attr('checked', newCount == checkboxes.length);
+      });
     }
-    if (totalCheckboxes == checkboxes.length) {
-      $(checkbox).attr('checked', 'checked');
+
+    // Initialize status for the group checkbox correctly.
+    if (self.data('simpletest-checked-tests') == checkboxes.length) {
+      $(checkbox).attr('checked', true);
     }
-    $(this).append(checkbox);
+    self.append(checkbox);
   });
-};
\ No newline at end of file
+};
