//
// The OpenDiamond Platform for Interactive Search
//
// Copyright (c) 2012 Carnegie Mellon University
// All rights reserved.
//
// This software is distributed under the terms of the Eclipse Public
// License, Version 1.0 which can be found in the file named LICENSE.
// ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
// RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
//

function AutoPause(blasters, max_unexposed, tag_class) {
  // Make sure "new" was used
  if (!(this instanceof arguments.callee)) {
    throw new Error('Constructor called as a function');
  }

  // Normalize a single blaster to an array
  if (!(blasters instanceof Array)) {
    blasters = [blasters];
  }

  // Default parameters
  if (typeof max_unexposed === 'undefined') {
    max_unexposed = 50;
  }
  if (typeof tag_class === 'undefined') {
    tag_class = 'unexposed';
  }

  // Private members
  var selector = '.' + tag_class;

  // Private methods
  function is_unexposed(el) {
    return !$(el).is(':visible') ||
        $(el).offset().top > $(document).scrollTop() + $(window).height();
  }

  function update_exposed() {
    $(selector).each(function(i, el) {
      if (!is_unexposed(el)) {
        $(el).removeClass(tag_class);
      }
    });
    if ($(selector).length < max_unexposed) {
      $.each(blasters, function(i, blaster) {
        blaster.resume();
      });
    }
  }

  // Connect to window events
  $(window).resize(update_exposed);
  $(window).scroll(update_exposed);

  // Public methods
  this.element_added = function(el) {
    if (is_unexposed(el)) {
      $(el).addClass(tag_class);
    }
    if ($(selector).length >= max_unexposed) {
      $.each(blasters, function(i, blaster) {
        blaster.pause();
      });
    }
  };

  this.refresh = function() {
    update_exposed();
  };
}

@@ -920,16 +920,39 @@
 g_class;
+%0A  var change_callback;
 %0A%0A  // P
@@ -1138,24 +1138,49 @@
 exposed() %7B%0A
+    var changed = false;%0A
     $(select
@@ -1271,24 +1271,48 @@
 tag_class);%0A
+        changed = true;%0A
       %7D%0A    
@@ -1315,24 +1315,33 @@
     %7D);%0A
+%0A
     
-if (
+var count = 
 $(select
@@ -1350,16 +1350,105 @@
 ).length
+;%0A    if (changed && change_callback) %7B%0A      change_callback(count);%0A    %7D%0A    if (count
  %3C max_u
@@ -1709,24 +1709,49 @@
 ction(el) %7B%0A
+    var changed = false;%0A
     if (is_u
@@ -1807,36 +1807,156 @@
     
-%7D%0A    if ($(selector).length
+  changed = true;%0A    %7D%0A%0A    var count = $(selector).length;%0A    if (changed && change_callback) %7B%0A      change_callback(count);%0A    %7D%0A    if (count
  %3E= 
@@ -2121,11 +2121,139 @@
 ();%0A  %7D;
+%0A%0A  // Callback takes the number of unviewed results%0A  this.onchange = function(callback) %7B%0A    change_callback = callback;%0A  %7D;
 %0A%7D%0A

Add callback on changes to unexposed count