$(document).ready(function () {
  Wick.OfferBanner = (function () {
    var offerBanner = $('.offer-banner')[0],
        offer;

    var init = function(){
      offer = new Waypoint.Sticky({
        element: offerBanner
      });
    };

    var toggle = function(){
      if (offer){
        offer.destroy();
        offer = null;
      } else {
        init();
      }
    };

    function handleResize(){o
      if (!offer){
        return;
      }

      var elHeight = offer.$element.outerHeight(true);
      if (elHeight !== offer.$wrapper.outerHeight(true)){
        offer.$wrapper.height(elHeight);
      }
    };

    var bindEvents = function(){
      $(window).on('resize', handleResize);
      $(window).on('orientationchange', handleResize);

      $(document).on('mobileNav.toggle', toggle);
    };

    if (!offerBanner) {
      return;
    }

    //Workaround for sticky banner functionality in IE
    function isIE() {
      ua = navigator.userAgent;
      /* MSIE used to detect old browsers and Trident used to newer ones*/
      var is_ie = ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident/") > -1;

      return is_ie;
    }

    if (isIE()) {
      init();
      bindEvents();
    }

    return offer;
  })();
});
