(function($) {
    wp.customize.bind('preview-ready', function() {
        // Listen for the scroll message from the customizer controls
        wp.customize.preview.bind('blog-over-scroll-to', function(selector) {
            var $element = $(selector);
            
            if ($element.length) {
                var isMobile = window.innerWidth <= 782;
                var offset = isMobile ? 20 : 50; 
                
                // Account for wpadminbar height if present
                if ($('#wpadminbar').length) {
                    offset += $('#wpadminbar').height();
                }

                $('html, body').stop().animate({
                    scrollTop: $element.offset().top - offset
                }, 600);
            }
        });
    });
})(jQuery);
