I have written a script to have a slide in top bar (branding) and a menu that site above and below a slider (or banner). When they scroll back in it's just the branding and the menu.. no slider or banner.

It works but there is a bug I can't seem to fix.

The page has 4 main elements but the SLIDER OR BANNER are option elements (not always present):
Code:
<div id="branding">BRANDING</div>
<div id="header">
    <div id="slider">SLIDER OR BANNER</div>
    <div id="menu">MENU</div>
</div>
<div id="content">CONTENT</div>
And here is my script so far:

Code:
 var sticky_navigation = function () {

         $lH = ($('#branding').length) ? $('#branding').height() : 0,
         $sH = ($('#slider').length) ? $('#slider').height() : 0,
         $bH = ($('#banner').length) ? $('#banner').height() : 0,
         $mH = ($('#menu').length) ? $('#menu').height() : 0;

         var $content = $('#content'), // Main content area
             $branding = $('#branding'),
             $header = $('#header'),
             $menu = $('#menu');

         // HEADER
         if ($(window).scrollTop() > $lH) {
             $header.css({
                 marginTop: $lH + "px"
             });
             if ($branding.css('position').toString() != "fixed") {
                 $branding.css({
                     position: "fixed",
                     top: "-" + $lH + "px",
                     left: 0,
                     zIndex: 500,
                 }).animate({
                     top: 0
                 }, 700);
             }
         } else {
             $branding.css({
                 position: "relative",
                 marginTop: "0px",
             });
             $header.css({
                 marginTop: "0px"
             });
         }

         // MENU
         if ($(window).scrollTop() > ($bH + $sH + $mH)) {
             $branding.css({
                 boxShadow: "none",
             });
             $header.css({
                 marginTop: ($lH + $mH) + "px"
             });
             if ($menu.css('position').toString() != "fixed") {
                 $menu.css({
                     position: "fixed",
                     top: "-" + $lH + "px",
                     left: 0,
                     zIndex: 490,
                 }).animate({
                     top: $lH
                 }, 700);
             }
         } else {
             $menu.css({
                 position: "relative",
                 marginTop: "0px",
                 top: 0,
             });
             if ($('#branding').length || $('#slider').length) {
                 $branding.css({
                     boxShadow: "0 0 16px rgba(0, 0, 0, 0.5)",
                 })
             }
         }
     };
     // run our function on load
     sticky_navigation();

     // and run it again every time you scroll
     $(window).scroll(function () {
         sticky_navigation();
     });

     // and run it again every time you resize
     $(window).resize(function () {
         sticky_navigation();
     });
Here is a jsfiddle for it as I have it now... to see the bug scroll up and down quickly.. you should see the menu sits lower than it should.

http://jsfiddle.net/hC423/1/

Screen-Shot-2013-11-11-at-07.54.33.jpg

Any help with this is very much appreciated. I'm open to suggestions if there's a better way to do this.

C