(function(d){d.isScrollToFixed=function(f){return!!d(f).data("ScrollToFixed")};d.ScrollToFixed=function(f,F){function z(){var c=b.options.limit;return c?"function"===typeof c?c.apply(a):c:0}function p(){return"fixed"===g}function n(){return!(p()||"absolute"===g)}function v(){p()||(e.css({display:a.css("display"),width:a.outerWidth(!0),height:a.outerHeight(!0),"float":a.css("float")}),e.addClass("toRemoveScrollToFixed"),cssOptions={"z-index":b.options.zIndex,position:"fixed",top:-1==b.options.bottom?
r():"",bottom:-1==b.options.bottom?"":b.options.bottom,"margin-left":"0px"},b.options.dontSetWidth||(cssOptions.width=a.width()),a.css(cssOptions),a.addClass(b.options.baseClassName),b.options.className&&a.addClass(b.options.className),g="fixed")}function A(){var c=z(),d=k;b.options.removeOffsets&&(d="",c-=s);cssOptions={position:"absolute",top:c,left:d,"margin-left":"0px",bottom:""};b.options.dontSetWidth||(cssOptions.width=a.width());a.css(cssOptions);g="absolute"}function l(){n()||(q=-1,e.css("display",
"none"),a.css({"z-index":B,width:"",position:w,left:"",top:C,"margin-left":""}),a.removeClass("scroll-to-fixed-fixed"),b.options.className&&a.removeClass(b.options.className),g=null)}function x(b){b!=q&&(a.css("left",k-b),q=b)}function r(){var c=b.options.marginTop;return c?"function"===typeof c?c.apply(a):c:0}function t(){if(d.isScrollToFixed(a)){var c=u;u?n()&&(s=a.offset().top,k=a.offset().left):(a.trigger("preUnfixed.ScrollToFixed"),l(),a.trigger("unfixed.ScrollToFixed"),q=-1,s=a.offset().top,
k=a.offset().left,b.options.offsets&&(k+=a.offset().left-a.position().left),-1==D&&(D=k),g=a.css("position"),u=!0,-1!=b.options.bottom&&(a.trigger("preFixed.ScrollToFixed"),v(),a.trigger("fixed.ScrollToFixed")));var e=d(window).scrollLeft(),h=d(window).scrollTop(),f=z();b.options.minWidth&&d(window).width()<b.options.minWidth?n()&&c||(m(),a.trigger("preUnfixed.ScrollToFixed"),l(),a.trigger("unfixed.ScrollToFixed")):b.options.maxWidth&&d(window).width()>b.options.maxWidth?n()&&c||(m(),a.trigger("preUnfixed.ScrollToFixed"),
l(),a.trigger("unfixed.ScrollToFixed")):-1==b.options.bottom?0<f&&h>=f-r()?"absolute"===g&&c||(m(),a.trigger("preAbsolute.ScrollToFixed"),A(),a.trigger("unfixed.ScrollToFixed")):h>=s-r()?(p()&&c||(m(),a.trigger("preFixed.ScrollToFixed"),v(),q=-1,a.trigger("fixed.ScrollToFixed")),x(e)):n()&&c||(m(),a.trigger("preUnfixed.ScrollToFixed"),l(),a.trigger("unfixed.ScrollToFixed")):0<f?(c=h+d(window).height()-a.outerHeight(!0),(h=r())||(h=b.options.bottom?b.options.bottom:0,h=-h),c>=f-h?p()&&(m(),a.trigger("preUnfixed.ScrollToFixed"),
"absolute"===w?A():l(),a.trigger("unfixed.ScrollToFixed")):(p()||(m(),a.trigger("preFixed.ScrollToFixed"),v()),x(e),a.trigger("fixed.ScrollToFixed"))):x(e)}}function m(){var b=a.css("position");"absolute"==b?a.trigger("postAbsolute.ScrollToFixed"):"fixed"==b?a.trigger("postFixed.ScrollToFixed"):a.trigger("postUnfixed.ScrollToFixed")}var b=this;b.$el=d(f);b.el=f;b.$el.data("ScrollToFixed",b);var u=!1,a=b.$el,g,w,C,B,s=0,k=0,D=-1,q=-1,e=null,y=function(b){a.is(":visible")&&(u=!1,t())},E=function(a){window.requestAnimationFrame?
requestAnimationFrame(t):t()};b.init=function(){b.options=d.extend({},d.ScrollToFixed.defaultOptions,F);B=a.css("z-index");b.$el.css("z-index",b.options.zIndex);e=d("<div />");g=a.css("position");w=a.css("position");C=a.css("top");n()&&b.$el.after(e);d(window).bind("resize.ScrollToFixed",y);d(window).bind("scroll.ScrollToFixed",E);b.options.preFixed&&a.bind("preFixed.ScrollToFixed",b.options.preFixed);b.options.postFixed&&a.bind("postFixed.ScrollToFixed",b.options.postFixed);b.options.preUnfixed&&
a.bind("preUnfixed.ScrollToFixed",b.options.preUnfixed);b.options.postUnfixed&&a.bind("postUnfixed.ScrollToFixed",b.options.postUnfixed);b.options.preAbsolute&&a.bind("preAbsolute.ScrollToFixed",b.options.preAbsolute);b.options.postAbsolute&&a.bind("postAbsolute.ScrollToFixed",b.options.postAbsolute);b.options.fixed&&a.bind("fixed.ScrollToFixed",b.options.fixed);b.options.unfixed&&a.bind("unfixed.ScrollToFixed",b.options.unfixed);b.options.spacerClass&&e.addClass(b.options.spacerClass);a.bind("resize.ScrollToFixed",
function(){e.height(a.height())});a.bind("scroll.ScrollToFixed",function(){a.trigger("preUnfixed.ScrollToFixed");l();a.trigger("unfixed.ScrollToFixed");t()});a.bind("detach.ScrollToFixed",function(c){c=c||window.event;c.preventDefault&&c.preventDefault();c.returnValue=!1;a.trigger("preUnfixed.ScrollToFixed");l();a.trigger("unfixed.ScrollToFixed");d(window).unbind("resize.ScrollToFixed",y);d(window).unbind("scroll.ScrollToFixed",E);a.unbind(".ScrollToFixed");e.remove();b.$el.removeData("ScrollToFixed")});
y()};b.init()};d.ScrollToFixed.defaultOptions={marginTop:0,limit:0,bottom:-1,zIndex:1E3,baseClassName:"scroll-to-fixed-fixed"};d.fn.scrollToFixed=function(f){return this.each(function(){new d.ScrollToFixed(this,f)})}})(jQuery);
