Problem jQuery scroller script

Problem jQuery scroller script

Hey,
I'm quite new to jQuery, and i have a question about a script I'm using.

I'm using the following script to make a div stay on top of the page when it hits the top. This works.

<script>function moveScroller() {
  var a = function() {
    var b = $(window).scrollTop();
    var d = $("#scroller-anchor").offset().top;
    var c=$("#scroller");
        if (b>d) {
      c.css({position:"fixed",top:"0px"})
    } else {
      if (b<=d) {
        c.css({position:"relative",top:""})
      }
    }
 };
  $(window).scroll(a);a()
}</script>


<script type="text/javascript"> 
  $(function() {
    moveScroller();
  });
</script> 



I also use a html div: <div id="scroller-anchor"></div>, to determine the point from where the div should stay on top. 

Now the problem is:
There is a footer on the bottom of the site, and in want the div to stop staying in place when it hits the footer, in stead of scrolling over the footer.

I want to use a similar div as the scroller-anchor, but now to make the div stay on place just above the footer. The div also has to go up again when you scroll up, so it should not stay just above the footer when you scroll up again.

I was thinking to fix this by adding a new varriant:

var e = $("scroller-stop")

But when I started trying this, the whole script stopped working.

I hope you understand what I mean, and I hope that anyone can help me out!

Thanks a lot,

Greetings Joe Cast.