//
// To fake scrolling in tests, do:
//
//     $container.scrollTop(TARGET_HEIGHT * 20); xsy.process();
//
// to bypass event handling and callback throttling.

var $container,
    TARGET_HEIGHT = 20,
    n_changeFired = 0,  // number of times `change` was fired if set up
    countChange = function() { ++n_changeFired; };

test('basic properties are set after init', function() {
  var xsy = new XScrollY({
    targets: '#qunit-fixture li'
  });

  ok(xsy.$scrollElement.length);
  ok(xsy.$targets.length);
  equal(xsy.$targets.length, $('#qunit-fixture li').length);
  equal(xsy.$targets.length, xsy.offsets.length);
});


module('todo', {
  setup: function() {
    n_changeFired = 0;
    $container = $('#qunit-fixture ul');
  }
});

test('change event fires', function() {
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange
  });

  equal(xsy.$scrollElement.scrollTop(), 0,
    "container should start scrolled to the top");
  equal(xsy.active, xsy.$targets[0],
    "the first target should be active");
  $container.scrollTop(TARGET_HEIGHT * 3);
  equal(xsy.$scrollElement.scrollTop(), TARGET_HEIGHT * 3);
  equal(n_changeFired, 1);
});

test('change event fires twice', function() {
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange
  });

  $container.scrollTop(TARGET_HEIGHT * 20); xsy.process();
  equal(n_changeFired, 2);
});

test('active property is updated on scroll', function() {
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange
  });

  equal(xsy.$scrollElement.scrollTop(), 0,
    "container should start scrolled to the top");
  equal(xsy.active, xsy.$targets[0],
    "the first target should be active");
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(xsy.active.innerHTML, xsy.$targets[3].innerHTML,
    "the third target should be active");
  equal(n_changeFired, 2);
});

test('offset option works', function() {
  var xsy = new XScrollY({
    container: $container,
    offset: TARGET_HEIGHT * 3,
    targets: '#qunit-fixture li',
    change: countChange
  });

  equal($container.find('li').height(), TARGET_HEIGHT,
    "TARGET_HEIGHT variable should be the same as <li> height for testing");
  equal(xsy.$scrollElement.scrollTop(), 0,
    "container should start scrolled to the top");
  equal(xsy.active.innerHTML, xsy.$targets[3].innerHTML,
    "the third target should be active");
  equal(n_changeFired, 1);
});

test('unveil is only called once', function() {
  var n_unveilFired = 0;
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li',
    change: countChange,
    unveil: function($e) {
      ++n_unveilFired;
    }
  });

  equal(n_changeFired, 1);
  equal(n_unveilFired, 1);
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(n_changeFired, 2);
  equal(n_unveilFired, 2);
  $container.scrollTop(TARGET_HEIGHT * 6); xsy.process();
  equal(n_changeFired, 3);
  equal(n_unveilFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 0); xsy.process();
  equal(n_changeFired, 4);
  equal(n_unveilFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(n_changeFired, 5);
  equal(n_unveilFired, 3);
  $container.scrollTop(TARGET_HEIGHT * 6); xsy.process();
  equal(n_changeFired, 6);
  equal(n_unveilFired, 3);
});

test('visible', function() {
  var xsy = new XScrollY({
    container: $container,
    targets: '#qunit-fixture li'
  });

  equal(xsy.visible().text(), '0123456789');
  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();
  equal(xsy.visible().text(), '3456789101112');
});

@@ -167,24 +167,33 @@
 $container,%0A
+    xsy,%0A
     TARGET_H
@@ -3423,42 +3423,104 @@
 );%0A%0A
-test('visible', function() %7B%0A  var
+%0Amodule('target selection', %7B%0A  setup: function() %7B%0A    $container = $('#qunit-fixture ul');%0A   
  xsy
@@ -3529,32 +3529,34 @@
  new XScrollY(%7B%0A
+  
     container: $
@@ -3562,32 +3562,34 @@
 $container,%0A    
+  
 targets: '#qunit
@@ -3601,22 +3601,61 @@
 ure li'%0A
+  
   %7D);%0A
+  %7D%0A%7D);%0A%0Atest('visible', function() %7B
 %0A  equal
@@ -3798,12 +3798,424 @@
 1112');%0A%7D);%0A
+%0Atest('above', function() %7B%0A  equal(xsy.above().text(), '');%0A  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();%0A  equal(xsy.above().text(), '012');%0A%7D);%0A%0Atest('below', function() %7B%0A  equal(xsy.below().text(), '01234567891011121314151617181920212223242526272829');%0A  $container.scrollTop(TARGET_HEIGHT * 3); xsy.process();%0A  equal(xsy.below().text(), '34567891011121314151617181920212223242526272829');%0A%7D);%0A

add tests for the other target selection methods