(function ($){
$.fn.theiaStickySidebar=function (options){
var defaults={
'containerSelector': '',
'additionalMarginTop': 0,
'additionalMarginBottom': 0,
'updateSidebarHeight': true,
'minWidth': 0,
'disableOnResponsiveLayouts': true,
'sidebarBehavior': 'modern',
'defaultPosition': 'relative',
'namespace': 'TSS'
};
options=$.extend(defaults, options);
options.additionalMarginTop=parseInt(options.additionalMarginTop)||0;
options.additionalMarginBottom=parseInt(options.additionalMarginBottom)||0;
tryInitOrHookIntoEvents(options, this);
function tryInitOrHookIntoEvents(options, $that){
var success=tryInit(options, $that);
if(!success){
console.log('TSS: Body width smaller than options.minWidth. Init is delayed.');
$(document).on('scroll.' + options.namespace, function (options, $that){
return function (evt){
var success=tryInit(options, $that);
if(success){
$(this).unbind(evt);
}};}(options, $that));
$(window).on('resize.' + options.namespace, function (options, $that){
return function (evt){
var success=tryInit(options, $that);
if(success){
$(this).unbind(evt);
}};}(options, $that))
}}
function tryInit(options, $that){
if(options.initialized===true){
return true;
}
if($('body').width() < options.minWidth){
return false;
}
init(options, $that);
return true;
}
function init(options, $that){
options.initialized=true;
var existingStylesheet=$('#theia-sticky-sidebar-stylesheet-' + options.namespace);
if(existingStylesheet.length===0){
$('head').append($('<style id="theia-sticky-sidebar-stylesheet-' + options.namespace + '">.theiaStickySidebar:after {content: ""; display: table; clear: both;}</style>'));
}
$that.each(function (){
var o={};
o.sidebar=$(this);
o.options=options||{};
o.container=$(o.options.containerSelector);
if(o.container.length==0){
o.container=o.sidebar.parent();
}
o.sidebar.parents().css('-webkit-transform', 'none'); // Fix for WebKit bug - https://code.google.com/p/chromium/issues/detail?id=20574
o.sidebar.css({
'position': o.options.defaultPosition,
'overflow': 'visible',
'-webkit-box-sizing': 'border-box',
'-moz-box-sizing': 'border-box',
'box-sizing': 'border-box'
});
if(o.sidebar.hasClass('rh-sticky-container')){
o.stickySidebar=o.sidebar.find('.rh-sticky-wrap-column');
if(o.stickySidebar.length==0){
o.sidebar.find('script').remove();
o.stickySidebar=$('<div>').addClass('rh-sticky-wrap-column').append(o.sidebar.children());
o.sidebar.append(o.stickySidebar);
}}else if(o.sidebar.hasClass('wpb_column')){
o.stickySidebar=o.sidebar.find('.vc_column-inner');
if(o.stickySidebar.length==0){
o.sidebar.find('script').remove();
o.stickySidebar=$('<div>').addClass('vc_column-inner').append(o.sidebar.children());
o.sidebar.append(o.stickySidebar);
}}else if(o.sidebar.hasClass('elementor-top-column')){
o.stickySidebar=o.sidebar.find('.elementor-column');
if(o.stickySidebar.length==0){
o.sidebar.find('script').remove();
o.stickySidebar=$('<div>').addClass('elementor-column pr10 pl10 pt10 pb10').append(o.sidebar.children());
o.sidebar.append(o.stickySidebar);
}
var topoffset=parseInt(o.sidebar.closest('.rh-elementor-sticky-true').data('sticky-top-offset'));
var botoffset=parseInt(o.sidebar.closest('.rh-elementor-sticky-true').data('sticky-bottom-offset'));
if(topoffset){
o.options.additionalMarginTop=topoffset;
}
if(botoffset){
o.options.additionalMarginBottom=botoffset;
}}
o.marginBottom=parseInt(o.sidebar.css('margin-bottom'));
o.paddingTop=parseInt(o.sidebar.css('padding-top'));
o.paddingBottom=parseInt(o.sidebar.css('padding-bottom'));
var collapsedTopHeight=o.stickySidebar.offset().top;
var collapsedBottomHeight=o.stickySidebar.outerHeight();
o.stickySidebar.css('padding-top', 1);
o.stickySidebar.css('padding-bottom', 1);
collapsedTopHeight -=o.stickySidebar.offset().top;
collapsedBottomHeight=o.stickySidebar.outerHeight() - collapsedBottomHeight - collapsedTopHeight;
if(collapsedTopHeight==0){
o.stickySidebar.css('padding-top', 0);
o.stickySidebarPaddingTop=0;
}else{
o.stickySidebarPaddingTop=1;
}
if(collapsedBottomHeight==0){
o.stickySidebar.css('padding-bottom', 0);
o.stickySidebarPaddingBottom=0;
}else{
o.stickySidebarPaddingBottom=1;
}
o.previousScrollTop=null;
o.fixedScrollTop=0;
resetSidebar();
o.onScroll=function (o){
if(!o.stickySidebar.is(":visible")){
return;
}
if($('body').width() < o.options.minWidth){
resetSidebar();
return;
}
if(o.options.disableOnResponsiveLayouts){
var sidebarWidth=o.sidebar.outerWidth(o.sidebar.css('float')=='none');
if(sidebarWidth + 50 > o.container.width()){
resetSidebar();
return;
}}
var scrollTop=$(document).scrollTop();
var position='static';
if(scrollTop >=o.sidebar.offset().top + (o.paddingTop - o.options.additionalMarginTop)){
var offsetTop=o.paddingTop + options.additionalMarginTop;
var offsetBottom=o.paddingBottom + o.marginBottom + options.additionalMarginBottom;
var containerTop=o.sidebar.offset().top;
var containerBottom=o.sidebar.offset().top + getClearedHeight(o.container);
var windowOffsetTop=0 + options.additionalMarginTop;
var windowOffsetBottom;
var sidebarSmallerThanWindow=(o.stickySidebar.outerHeight() + offsetTop + offsetBottom) < $(window).height();
if(sidebarSmallerThanWindow){
windowOffsetBottom=windowOffsetTop + o.stickySidebar.outerHeight();
}else{
windowOffsetBottom=$(window).height() - o.marginBottom - o.paddingBottom - options.additionalMarginBottom;
}
var staticLimitTop=containerTop - scrollTop + o.paddingTop;
var staticLimitBottom=containerBottom - scrollTop - o.paddingBottom - o.marginBottom;
var top=o.stickySidebar.offset().top - scrollTop;
var scrollTopDiff=o.previousScrollTop - scrollTop;
if(o.stickySidebar.css('position')=='fixed'){
if(o.options.sidebarBehavior=='modern'){
top +=scrollTopDiff;
}}
if(o.options.sidebarBehavior=='stick-to-top'){
top=options.additionalMarginTop;
}
if(o.options.sidebarBehavior=='stick-to-bottom'){
top=windowOffsetBottom - o.stickySidebar.outerHeight();
}
if(scrollTopDiff > 0){
top=Math.min(top, windowOffsetTop);
}else{
top=Math.max(top, windowOffsetBottom - o.stickySidebar.outerHeight());
}
top=Math.max(top, staticLimitTop);
top=Math.min(top, staticLimitBottom - o.stickySidebar.outerHeight());
var sidebarSameHeightAsContainer=o.container.height()==o.stickySidebar.outerHeight();
if(!sidebarSameHeightAsContainer&&top==windowOffsetTop){
position='fixed';
}
else if(!sidebarSameHeightAsContainer&&top==windowOffsetBottom - o.stickySidebar.outerHeight()){
position='fixed';
}
else if(scrollTop + top - o.sidebar.offset().top - o.paddingTop <=options.additionalMarginTop){
position='static';
}else{
position='absolute';
}}
if(position=='fixed'){
var scrollLeft=$(document).scrollLeft();
o.stickySidebar.css({
'position': 'fixed',
'width': getWidthForObject(o.stickySidebar) + 'px',
'transform': 'translateY(' + top + 'px)',
'left': (o.sidebar.offset().left + parseInt(o.sidebar.css('padding-left')) - scrollLeft) + 'px',
'top': '0px'
});
}
else if(position=='absolute'){
var css={};
if(o.stickySidebar.css('position')!='absolute'){
css.position='absolute';
css.transform='translateY(' + (scrollTop + top - o.sidebar.offset().top - o.stickySidebarPaddingTop - o.stickySidebarPaddingBottom) + 'px)';
css.top='0px';
}
css.width=getWidthForObject(o.stickySidebar) + 'px';
css.left='';
o.stickySidebar.css(css);
}
else if(position=='static'){
resetSidebar();
}
if(position!='static'){
if(o.options.updateSidebarHeight==true){
o.sidebar.css({
'min-height': o.stickySidebar.outerHeight() + o.stickySidebar.offset().top - o.sidebar.offset().top + o.paddingBottom
});
}}
o.previousScrollTop=scrollTop;
};
o.onScroll(o);
$(document).on('scroll.' + o.options.namespace, function (o){
return function (){
o.onScroll(o);
};}(o));
$(window).on('resize.' + o.options.namespace, function (o){
return function (){
o.stickySidebar.css({'position': 'static'});
o.onScroll(o);
};}(o));
if(typeof ResizeSensor!=='undefined'){
new ResizeSensor(o.stickySidebar[0], function (o){
return function (){
o.onScroll(o);
};}(o));
}
function resetSidebar(){
o.fixedScrollTop=0;
o.sidebar.css({
'min-height': '1px'
});
o.stickySidebar.css({
'position': 'static',
'width': '',
'transform': 'none'
});
}
function getClearedHeight(e){
var height=e.height();
e.children().each(function (){
height=Math.max(height, $(this).height());
});
return height;
}});
}
function getWidthForObject(object){
var width;
try {
width=object[0].getBoundingClientRect().width;
}
catch (err){
}
if(typeof width==="undefined"){
width=object.width();
}
return width;
}
return this;
}})(jQuery);
jQuery(window).on('load', function(){
var additionalmarginforstickysidebar=(jQuery('.re-stickyheader').length > 0) ? jQuery('.re-stickyheader').height() + 30:30;
if(jQuery('.rh-float-panel').length > 0){
additionalmarginforstickysidebar=jQuery('.rh-float-panel').height() + 30;
}
if(jQuery('.stickyonfloatpanel').length > 0){
additionalmarginforstickysidebar=jQuery('.rh-float-panel').height();
}
jQuery('.stickysidebar-container > .wpb_column').theiaStickySidebar({
additionalMarginTop: additionalmarginforstickysidebar,
additionalMarginBottom: 30,
});
jQuery('.rh-stickysidebar-wrapper > .rh-sticky-container').theiaStickySidebar({
additionalMarginTop: additionalmarginforstickysidebar,
additionalMarginBottom: 30,
});
jQuery('.rh-elementor-sticky-true .elementor-top-column').theiaStickySidebar({
additionalMarginTop: additionalmarginforstickysidebar,
additionalMarginBottom: 30,
});
});