Can't find the quirk in my jQuery - need help debugging!

Can't find the quirk in my jQuery - need help debugging!

I've been working on some jQuery for a site's navbar and it works for the most part: on the home page the menu icon is clicked to reveal the menu items and once the menu is scrolled out of view, the site's "main" menu becomes fixed to the top of the page. On the rest of the site, the main menu is just fixed to the top of the page. 


Where things get tricky is that the menu at the top of the home page only triggers to open the menu so once the menu scrolls down the rest of the page, the menu icon gets replaced with the logo and *should* become wrapped with a home link - this "main" menu should always be fixed at the top of the page for the rest of the site with the home link. This kind of works - there are some bugs and I can't figure out what to do. 

On the internal pages, the jQuery is set to wrap html around a div for the logo with the home link - it works initially, but then disappears when scrolling down for a little while (I assume, it's taking on the same behavior as the home page) but then sometimes the html may not wrap at all when the page loads, but then appears when scrolling immediately begins. 

Also, sometimes on maybe a page refresh (when I'm just testing the home page) the menu icon will actually have the home link wrapped around the div - this causes the page to refresh if I just want to open the menu on page load.

Here is the jQuery I have for this so far:

  1. jQuery(document).ready(function($) { 
  2.     
  3.     // immediately remove home page link from home when as a menu
  4.     if ($('body.home .siteBrand').parent().hasClass('home-link')) {
  5.         $('.home a .siteBrand').unwrap();
  6.     }
  7.        
  8.    
  9. // navbar fade effect for home page
  10.      $('.home .x-btn-navbar').removeClass("collapsed");
  11.    $('.home .siteBrand').click(function(){
  12.         $('.home .x-navbar').toggleClass("x-navbarOpen");
  13.         $('.home .x-navbar').addClass("ani");
  14.         $('.home .x-nav-wrap.desktop').toggleClass("siteBrandOpen");
  15.         $('.home .x-nav-wrap.desktop').addClass("ani");
  16.         $('.home .x-navbar .desktop .x-nav > li > a').toggleClass("menuOpen");
  17.         $('.home .x-navbar .desktop .x-nav > li > a').addClass("ani");
  18.         $('.home .x-btn-navbar').toggleClass("collapsed");
  19.         $('.home .x-nav-wrap').toggleClass("in");
  20.         $('.home .x-nav-wrap').addClass("ani");
  21. });

  22. });


  23. ;(function($){

  24. // adds fixed top nav bar & link to all pages other than homepage
  25.   $("body:not(.homeGrid) .x-navbar").addClass("x-navbar-fixed-top");
  26.   $('body:not(.homeGrid) .siteBrand').wrap('<a href="http://sites.camwebdev.com/xplayground" class="home-link"></a>');
  27.     
  28. })(jQuery);


  29. // fixed top nav after scrolling navbar out of view on home page
  30. ;(function($){
  31. $(window).scroll(function() {   
  32. var scroll = $(window).scrollTop();
  33.  
  34. if (scroll >= 350) {
  35. $(".home .x-navbar").addClass("x-navbar-fixed-top ani");
  36. $("body.homeGrid").removeClass("home");
  37.            
  38.             
  39.             //adds site link after scrolling navbar out of view
  40.     if (!$('.siteBrand').parent().hasClass('home-link')) {
  41.         $('.siteBrand').wrap('<a href="http://sites.camwebdev.com/xplayground" class="home-link"></a>');
  42.     }
  43.             
  44.             // go back to normal at the top of homepage
  45. } else {
  46.              $("a .siteBrand").unwrap(); 
  47. $("body.homeGrid").addClass("home");
  48. $(".home .x-navbar").removeClass("ani");
  49. $('.home .x-nav-wrap.desktop').removeClass("ani");
  50. $(".home .x-navbar").removeClass("x-navbar-fixed-top");      
  51. }
  52. });

  53. })(jQuery);

I'm still kinda new to all this so is there something I'm overlooking or maybe not including? Thanks much for the help! :)