Compare offset().top with object properties
<p>I am trying to build a 'scrollSpy' type function. I don't know how to compare an argument against some values in an object, and return the (numerically) highest value's name.</p>

<p>I have some markup:</p>

<pre><code>&lt;ul class="nav"&gt;
    &lt;li&gt;&lt;a href="a"&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="b"&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="c"&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="d"&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="e"&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="f"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;section id="a" class="scrollspy"&gt;&lt;/section&gt;
&lt;section id="b" class="scrollspy"&gt;&lt;/section&gt;
&lt;section id="c" class="scrollspy"&gt;&lt;/section&gt;
&lt;section id="d" class="scrollspy"&gt;&lt;/section&gt;
&lt;section id="e" class="scrollspy"&gt;&lt;/section&gt;
&lt;section id="f" class="scrollspy"&gt;&lt;/section&gt;
</code></pre>

<p>And some script that creates an object consisting of each <code>section</code> and its distance in px from top:</p>

<pre><code>var obj = {
    sectionOffset: {},
    scrollSpy: function (scrolled) {
        // This should look in the object with the offset values, and from all
        // the values that (scrolled) is higher than, take the largest, and
        // return its name.
    }
}


$(function () {

    $(window).scroll(function () {
        obj.scrollSpy($(document).scrollTop()); 
    });

    // Create an object consisting of all elements I want to 'spy' on.
    // Names and values are made of element ID and top offset respectively.
    $('.scrollspy').each(function () {
        obj.sectionOffset[$(this).attr('id')] = (parseInt($(this).offset().top));
    });

});
</code></pre>

<p>After I loop through the elements I want, it produces an object like:</p>

<pre><code>{
    d: 5195,
    b: 3245,
    a: 1319,
    f: 5682,
    c: 2139,
    e: 3343
}
</code></pre>

<p>Just to be clear, if the user scrolls 3000px down the page, the function should return <code>c</code>.</p>
