        function vpWidth() {
          return document.documentElement.clientWidth || document.body.clientWidth || window.innerWidth;
        }
        function vpHeight() {
          return document.documentElement.clientHeight || document.body.clientHeight || window.innerHeight;
        }

       var oldWidth = layoutWidth;
       var oldHeight = -1;
       
       var borderWidth = 3 * 2;
       var dividerWidth = 3;
       var sidePaneSize = 200;
       var maxContentWithBorder = 1024;
       var knittingEdging = 64;
       var minWidthForEdging = 640;
       var omitSideMenuSize = 480;

       // To reduce flashing, don't shift the margin etc due to small width
       // shifts that are likely due to layout deciding a scroll bar is or
       // is not needed:
       var widthSlop = 20;
       
       var savedSideMenu = null;
       var sideMenuInDOM = true;
       
       function DoResize()
       {
           var newHeight = vpHeight();
           var newWidth = vpWidth();
           if ((newHeight != oldHeight) ||
               (newWidth < oldWidth - widthSlop) || 
               (newWidth > oldWidth + widthSlop))
           {
               oldHeight = newHeight;
               if ((oldWidth > 0) &&
                   (newWidth >= oldWidth - widthSlop) &&
                   (newWidth <= oldWidth + widthSlop))
               {
                  newWidth = oldWidth;
               }
               oldWidth = newWidth;
               
               if (savedSideMenu == null)
               {
                   savedSideMenu = $("#sideMenuPane");
                   if (savedSideMenu.length == 0) {
                       savedSideMenu = null;
                   }
               }
               var sideMenu = savedSideMenu;               
               
               var contentPane = $("#contentPane");
               var titleAndContent = $("#titleAndContentPane");
               var topMenu = $("#topMenuPane");
               var titleAndLogo = $("#companyLogoTitlePane");
               var contentRow = $("#contentTableRow");
               
               var widthInsideBorder = newWidth - borderWidth;

               if ((newWidth <= omitSideMenuSize) || isMobileDevice)
               {
                   if (sideMenuInDOM && titleAndContent)
                   {
                       titleAndContent.removeClass("titleAndContentPane");
                       titleAndContent.addClass("titleAndContentPaneOnly");
                   }
                   if (/*sideMenuInDOM && */sideMenu)
                   {
                       sideMenu.detach();
                   }
                   sideMenuInDOM = false;
                   if (topMenu)
                   {
                     topMenu.css("display", "block");
                     topMenu.css("width", widthInsideBorder);
                     topMenu.css("height", "auto");
                   }
                   if (contentPane)
                   {
                       contentPane.css("width", widthInsideBorder);
                       contentPane.css("margin-left", 0);
                   }
                   if (titleAndContent)
                   {
                     titleAndContent.css("width", widthInsideBorder);
                   }
                   if (titleAndLogo)
                   {
                     titleAndLogo.css("width", widthInsideBorder);
                   }
               }
               else
               {
                   if (titleAndContent && !sideMenuInDOM)
                   {
                       titleAndContent.removeClass("titleAndContentPaneOnly");
                       titleAndContent.addClass("titleAndContentPane");
                   }

                   var showTopMenu = false;
                   if (sideMenu)
                   {
                     if (!sideMenuInDOM)
                     {
                         titleAndContent.before(sideMenu);
                     }
                     sideMenu.css("width", sidePaneSize.toString() + "px");
                     if (topMenu) {
                         topMenu.css("display", "none");
                     }
                   }
                   else if (topMenu) {
                      topMenu.css("display", "block");
                      showTopMenu = true;
                   }

                   sideMenuInDOM = true;
                   var maxToUse = maxContentWithBorder;
                   if ((newWidth >= minWidthForEdging) && (newWidth <= maxToUse + knittingEdging)) 
                   {
                       maxToUse = newWidth - knittingEdging;
                   }

                   var sideMenuAllowance = sidePaneSize + dividerWidth;
                   if ((layoutWidth > 0) && (layoutWidth < omitSideMenuSize)) {
                       // we don't send down a side menu so omit it
                       sideMenuAllowance = 0;
                   }
                   if (newWidth <= maxToUse)
                   {
                     var mainWidth = (widthInsideBorder - sideMenuAllowance).toString() + "px";
                     if (titleAndContent)
                     {
                       titleAndContent.css("width", mainWidth);
                     }
                     if (titleAndLogo)
                     {
                         titleAndLogo.css("width", mainWidth);
                     }
                     if (contentPane)
                     {
                         contentPane.css("width", widthInsideBorder);
                         contentPane.css("margin-left", (newWidth - (widthInsideBorder + borderWidth)) / 2);
                     }
                     if (showTopMenu) {
                         topMenu.css("width", widthInsideBorder);
                     }
                   }
                   else
                   {
                     var contentPaneInsideBorder = maxToUse - borderWidth;
                     var contentExcludingSideMenuAndDivider = contentPaneInsideBorder - sideMenuAllowance;

                     if (showTopMenu) {
                         topMenu.css("width", contentPaneInsideBorder.toString() + "px");
                     }
                     if (contentPane)
                     {
                       contentPane.css("width", contentPaneInsideBorder.toString() + "px");
                       contentPane.css("margin-left", (newWidth - maxToUse) / 2);
                     }
                     if (titleAndContent)
                     {
                        titleAndContent.css("width", contentExcludingSideMenuAndDivider.toString() + "px");
                     }
                     if (titleAndLogo)
                     {
                        titleAndLogo.css("width", contentExcludingSideMenuAndDivider.toString() + "px");
                     }
                   }
               }
               
               var fullHeightInsideBorders = newHeight - borderWidth;
               if (contentPane)
               {
                 contentPane.css("min-height", fullHeightInsideBorders);
               }
               if (titleAndContent)
               {
                   titleAndContent.css("min-height", fullHeightInsideBorders);
               }
               if (sideMenu)
               {
                   sideMenu.css("min-height", fullHeightInsideBorders);
               }

               var menuAndContentHeight = fullHeightInsideBorders - sideMenuAllowance;
               var sideMenuBottom = $("#sideMenuFrame");
               if (sideMenuBottom)
               {
                   sideMenuBottom.css("min-height", menuAndContentHeight);
               }
               var contentBottom = $("#menusAndContentPane");
               if (contentBottom)
               {
                 contentBottom.css("min-height", menuAndContentHeight);
               }
           }
       }

       var topMenuIsExpanded = false;

       function ExpandTopMenu() {
           if (topMenuIsExpanded)
               return;

           $("#topMenuExpandPane").removeClass("topMenuExpanded");
           $("#topMenuExpandPane").addClass("topMenuRetracted");

           $("#topMenuExpandIconPane").removeClass("topMenuExpanded");
           $("#topMenuExpandIconPane").addClass("topMenuRetracted");

           $("#topMenuRetractPane").removeClass("topMenuRetracted");
           $("#topMenuRetractPane").addClass("topMenuExpanded");

           $("#retractableTopMenuPane").removeClass("retractableTopMenuPaneHidden");
           $("#retractableTopMenuPane").addClass("retractableTopMenuPaneVisible");

           topMenuIsExpanded = true;
       }

       function RetractTopMenu() {
           if (!topMenuIsExpanded)
               return;

           $("#topMenuExpandPane").removeClass("topMenuRetracted");
           $("#topMenuExpandPane").addClass("topMenuExpanded");

           $("#topMenuExpandIconPane").removeClass("topMenuRetracted");
           $("#topMenuExpandIconPane").addClass("topMenuExpanded");

           $("#topMenuRetractPane").removeClass("topMenuExpanded");
           $("#topMenuRetractPane").addClass("topMenuRetracted");

           $("#retractableTopMenuPane").removeClass("retractableTopMenuPaneVisible");
           $("#retractableTopMenuPane").addClass("retractableTopMenuPaneHidden");

           topMenuIsExpanded = false;
       }

       function DoInitialSize() {
           DoResize();
           RetractTopMenu();
       }

       function AddBrowserWidth(aLink) {
            if (oldWidth > 0)
            {
                var theException;

                try
                {
                    var theUrl = aLink.href;
                    if (theUrl.indexOf("?") < 0) {
                        theUrl = theUrl + "?BrowserWidth=" + oldWidth.toString();
                    }
                    else {
                        theUrl = theUrl + "&BrowserWidth=" + oldWidth.toString();
                    }
                    aLink.href = theUrl;
                } 
                catch (theException)
                {
                };
            }
       }

       window.onresize = DoResize;

