Calculate element's numeric position in a tree of elements
<p>I'm preparing a set of presentation slides, and each slide can have a 
number of sections of code on them.  I'm making a jQuery plugin to let
me highlight individual lines of code, and move the highlight up and
down by line. </p>

<p>Here's an example of the markup:</p>

<pre><code>&lt;div class='slide'&gt;
    &lt;div class='section'&gt;
        &lt;span class='line'&gt;Line 1&lt;/span&gt;
        &lt;span class='line'&gt;Line 2&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class='section'&gt;
        &lt;span class='line'&gt;Line 3&lt;/span&gt;
        &lt;span class='line'&gt;Line 4&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class='section'&gt;
        &lt;span class='line'&gt;Line 5&lt;/span&gt;
        &lt;span class='line'&gt;Line 6&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;div class='slide active'&gt;
    &lt;div class='section'&gt;
        &lt;span class='line'&gt;Line 1&lt;/span&gt;
        &lt;span class='line'&gt;Line 2&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class='section'&gt;
        &lt;span class='line'&gt;Line 3&lt;/span&gt;
        &lt;span class='line selected'&gt;Line 4&lt;/span&gt;
        &lt;span class='line'&gt;Line 5&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class='section'&gt;
        &lt;span class='line'&gt;Line 6&lt;/span&gt;
        &lt;span class='line'&gt;Line 7&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;div class='slide'&gt;
    &lt;div class='section'&gt;
        &lt;span class='line'&gt;Line 1&lt;/span&gt;
        &lt;span class='line'&gt;Line 2&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class='section'&gt;
        &lt;span class='line'&gt;Line 3&lt;/span&gt;
        &lt;span class='line'&gt;Line 4&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>The second slide (of three) is the active visible slide, and in that slide,
the middle line of the second code section is selected (highlighted).</p>

<p>I can select a line by number using jQuery like this:</p>

<pre><code>var the_line = $(".slide.active").find("span.line")[lineno-1];
</code></pre>

<p>What I can't seem to do is calculate the line number of the currently selected line.
I tried this to compute how many lines were following the selected one, but it only
works within a single section:</p>

<pre><code>var following_lines = $(".slide.active").find("span.line.selected").nextAll("span.line");
</code></pre>

<p>How can I get the selected line's line number?</p>
