!function(a){a.fn.theiaStickySidebar=function(b){var c={containerSelector:"",additionalMarginTop:0,additionalMarginBottom:0,updateSidebarHeight:!0,minWidth:0};b=a.extend(c,b),b.additionalMarginTop=parseInt(b.additionalMarginTop)||0,b.additionalMarginBottom=parseInt(b.additionalMarginBottom)||0,a("head").append(a('<style>.theiaStickySidebar:after {content: ""; display: table; clear: both;}</style>')),this.each(function(){function f(){c.fixedScrollTop=0,c.sidebar.css({"min-height":"1px"}),c.stickySidebar.css({position:"static",width:""})}function g(b){var c=b.height();return b.children().each(function(){c=Math.max(c,a(this).height())}),c}var c={};c.sidebar=a(this),c.options=b||{},c.container=a(c.options.containerSelector),0==c.container.size()&&(c.container=c.sidebar.parent()),c.sidebar.parents().css("-webkit-transform","none"),c.sidebar.css({position:"relative",overflow:"visible","-webkit-box-sizing":"border-box","-moz-box-sizing":"border-box","box-sizing":"border-box"}),c.stickySidebar=c.sidebar.find(".theiaStickySidebar"),0==c.stickySidebar.length&&(c.sidebar.find("script").remove(),c.stickySidebar=a("<div>").addClass("theiaStickySidebar").append(c.sidebar.children()),c.sidebar.append(c.stickySidebar)),c.marginTop=parseInt(c.sidebar.css("margin-top")),c.marginBottom=parseInt(c.sidebar.css("margin-bottom")),c.paddingTop=parseInt(c.sidebar.css("padding-top")),c.paddingBottom=parseInt(c.sidebar.css("padding-bottom"));var d=c.stickySidebar.offset().top,e=c.stickySidebar.outerHeight();c.stickySidebar.css("padding-top",1),c.stickySidebar.css("padding-bottom",1),d-=c.stickySidebar.offset().top,e=c.stickySidebar.outerHeight()-e-d,0==d?(c.stickySidebar.css("padding-top",0),c.stickySidebarPaddingTop=0):c.stickySidebarPaddingTop=1,0==e?(c.stickySidebar.css("padding-bottom",0),c.stickySidebarPaddingBottom=0):c.stickySidebarPaddingBottom=1,c.previousScrollTop=null,c.fixedScrollTop=0,f(),c.onScroll=function(c){if(c.stickySidebar.is(":visible")){if(a("body").width()<c.options.minWidth)return f(),void 0;if(c.sidebar.outerWidth(!0)+50>c.container.width())return f(),void 0;var d=a(document).scrollTop(),e="static";if(d>=c.container.offset().top+(c.paddingTop+c.marginTop-c.options.additionalMarginTop)){var m,h=c.paddingTop+c.marginTop+b.additionalMarginTop,i=c.paddingBottom+c.marginBottom+b.additionalMarginBottom,j=c.container.offset().top,k=c.container.offset().top+g(c.container),l=0+b.additionalMarginTop,n=c.stickySidebar.outerHeight()+h+i<a(window).height();m=n?l+c.stickySidebar.outerHeight():a(window).height()-c.marginBottom-c.paddingBottom-b.additionalMarginBottom;var o=j-d+c.paddingTop+c.marginTop,p=k-d-c.paddingBottom-c.marginBottom,q=c.stickySidebar.offset().top-d,r=c.previousScrollTop-d;"fixed"==c.stickySidebar.css("position")&&(q+=r),q=r>0?Math.min(q,l):Math.max(q,m-c.stickySidebar.outerHeight()),q=Math.max(q,o),q=Math.min(q,p-c.stickySidebar.outerHeight());var s=c.container.height()==c.stickySidebar.outerHeight();e=s||q!=l?s||q!=m-c.stickySidebar.outerHeight()?d+q-c.sidebar.offset().top-c.paddingTop<=b.additionalMarginTop?"static":"absolute":"fixed":"fixed"}if("fixed"==e)c.stickySidebar.css({position:"fixed",width:c.sidebar.width(),top:q,left:c.sidebar.offset().left+parseInt(c.sidebar.css("padding-left"))});else if("absolute"==e){var t={};"absolute"!=c.stickySidebar.css("position")&&(t.position="absolute",t.top=d+q-c.sidebar.offset().top-c.stickySidebarPaddingTop-c.stickySidebarPaddingBottom),t.width=c.sidebar.width(),t.left="",c.stickySidebar.css(t)}else"static"==e&&f();"static"!=e&&1==c.options.updateSidebarHeight&&c.sidebar.css({"min-height":c.stickySidebar.outerHeight()+c.stickySidebar.offset().top-c.sidebar.offset().top+c.paddingBottom}),c.previousScrollTop=d}},c.onScroll(c),a(document).scroll(function(a){return function(){a.onScroll(a)}}(c)),a(window).resize(function(a){return function(){a.stickySidebar.css({position:"static"}),a.onScroll(a)}}(c))})}}(jQuery);