vertical sliding navigation - how to improve my code?

vertical sliding navigation - how to improve my code?

hi folks!
i have been coding on this vertical sliding navigation quite a while. its tested on all relevant browsers and works like a dream.

id loved to show you an example of it, but the site is under development, and i, as a little coder, am not allowed to grant access to it, additionally i do not have enough time to create a testcase for everybody to see.
to give you a short insight:

the site is quite long (vertically) and we wanted a small navigation (very subtle) to follow the user when scrolling. the problem was: the footer is quite long too, and we didnt want the sliding navigation to reach into it, when scrolling down the footer. in other words: i had to stop the animation when the sliding nav reached the end of the main content.
so far so good, i had NO idea how to tell the animation to stop at certain conditions, other than telling it that from the start.


my solution is totally workaround and completely selfmade. but i am SURE that there is some kind of "stop animation when condition is met"-command i was totally overlooking. so please dear jquery community: tell me what i am doing wrong, and how i could improve this function.

here is the code:
  1. function slide_nav() {
  2. var scrollingDiv = jQuery("div#slide_nav");
  3. if(scrollingDiv.length > 0)
  4.     {
  5.         // check for main div lower border to set the navigation so, that it
  6.         // doesnt exceeds this value when scrolling
  7.         
  8.         // save the main divs lower border position from top
  9.         var main_div = jQuery('div#main');
  10.         var main_div_position = main_div.position();
  11.         var main_div_lower_reach = main_div_position.top + main_div.height();
  12.         
  13.         var scrollingDiv_offset = scrollingDiv.offset();
  14.         var initial_offset = scrollingDiv_offset.top;
  15.         
  16.         // Create small helper dog to look up position when finished animating
  17.         var wrapper = jQuery('div#wrapper');
  18.         wrapper.before('<div id="watchdog"></div>');
  19.         
  20.         var watchdog = jQuery('div#watchdog');
  21.         
  22.         watchdog.css({
  23.                 'background': 'transparent',
  24.                 'height': scrollingDiv.height(),
  25.                 'width': scrollingDiv.width(),
  26.                 'position': 'absolute',
  27.                 'top': scrollingDiv_offset.top,
  28.                 'left': scrollingDiv_offset.left
  29.         });
  30.         
  31.         jQuery(window).scroll(function() {
  32.             setTimeout(function() {
  33.                 scroll_this_dog(scrollingDiv)
  34.             }, 500);
  35.         });
  36.         
  37.         function scroll_this_dog(element) 
  38.         {
  39.             watchdog.css({
  40.                 "marginTop" : (jQuery(window).scrollTop() + 1) + "px"
  41.             });
  42.             
  43.             var watchdog_offset = watchdog.offset();
  44.             var watchdog_lowerend = watchdog_offset.top+watchdog.height();
  45.             
  46.             //console.log("BARK! thats my lower end: "+watchdog_lowerend);
  47.             //console.log("maindiv lower end: "+main_div_lower_reach);
  48.             
  49.             
  50.             if((watchdog.height()+watchdog_offset.top) > main_div_lower_reach)
  51.             {
  52.                 //console.log("BARK! im lower!");
  53.                 element.stop().animate({
  54.                     "marginTop" : main_div_lower_reach - element.height() - initial_offset + "px"
  55.                 }, 1000, "easeOutExpo");
  56.             }
  57.             else
  58.             {
  59.                 //console.log("BARK! im above!");
  60.                 element.stop().animate({
  61.                     "marginTop" : (jQuery(window).scrollTop() + 1) + "px"
  62.                 }, 1000, "easeOutExpo");
  63.             }
  64.         }
  65.     }
  66. }
thanks to everyone willing to have a look into this, its really appreciated!
thanks and gbye