!function(i){i.isScrollToFixed=function(o){return void 0!==i(o).data("ScrollToFixed")},i.ScrollToFixed=function(o,t){var e=this;e.$el=i(o),e.el=o,e.$el.data("ScrollToFixed",e);var n,r=!1,d=e.$el,s=0,l=0,p=-1,f=-1,x=null;function c(){var i=e.options.limit;return i?"function"==typeof i?i():i:0}function u(){return"fixed"===n}function g(){return"absolute"===n}function a(){return!(u()||g())}function b(){u()||(x.css({display:d.css("display"),width:d.outerWidth(!0),height:d.outerHeight(!0),float:d.css("float")}),d.css({width:d.width(),position:"fixed",top:-1==e.options.bottom?S():"",bottom:-1==e.options.bottom?"":e.options.bottom}),n="fixed")}function F(){a()||(f=-1,x.css("display","none"),d.css({width:"",position:"",left:"",top:""}),n=null)}function T(i){i!=f&&(d.css("left",l-i),f=i)}function S(){return e.options.marginTop}function h(){if(i.isScrollToFixed(d)){var o=r;r||(d.trigger("preUnfixed"),F(),d.trigger("unfixed"),f=-1,s=d.offset().top,l=d.offset().left,e.options.offsets&&(l+=d.offset().left-d.position().left),-1==p&&(p=l),n=d.css("position"),r=!0,-1!=e.options.bottom&&(d.trigger("preFixed"),b(),d.trigger("fixed")));var t=i(window).scrollLeft(),x=i(window).scrollTop(),h=c();e.options.minWidth&&i(window).width()<e.options.minWidth?a()&&o||(w(),d.trigger("preUnfixed"),F(),d.trigger("unfixed")):-1==e.options.bottom?h>0&&x>=h-S()?g()&&o||(w(),d.trigger("preAbsolute"),d.css({width:d.width(),position:"absolute",top:c(),left:l}),n="absolute",d.trigger("unfixed")):x>=s-S()?(u()&&o||(w(),d.trigger("preFixed"),b(),f=-1,d.trigger("fixed")),T(t)):a()&&o||(w(),d.trigger("preUnfixed"),F(),d.trigger("unfixed")):h>0?x+i(window).height()-d.outerHeight(!0)>=h-S()?u()&&(w(),d.trigger("preUnfixed"),F(),d.trigger("unfixed")):(u()||(w(),d.trigger("preFixed"),b()),T(t),d.trigger("fixed")):T(t)}}function w(){var i=d.css("position");"absolute"==i?d.trigger("postAbsolute"):"fixed"==i?d.trigger("postFixed"):d.trigger("postUnfixed")}var m=function(i){d.is(":visible")&&(r=!1,h())},v=function(i){h()};e.init=function(){e.options=i.extend({},i.ScrollToFixed.defaultOptions,t),("iPad"!==navigator.platform&&"iPhone"!==navigator.platform&&"iPod"!==navigator.platform||navigator.userAgent.match(/OS 5_.*\ like Mac OS X/i))&&(e.$el.css("z-index",e.options.zIndex),x=i("<div />"),n=d.css("position"),a()&&e.$el.after(x),i(window).bind("resize.ScrollToFixed",m),i(window).bind("scroll.ScrollToFixed",v),e.options.preFixed&&d.bind("preFixed.ScrollToFixed",e.options.preFixed),e.options.postFixed&&d.bind("postFixed.ScrollToFixed",e.options.postFixed),e.options.preUnfixed&&d.bind("preUnfixed.ScrollToFixed",e.options.preUnfixed),e.options.postUnfixed&&d.bind("postUnfixed.ScrollToFixed",e.options.postUnfixed),e.options.preAbsolute&&d.bind("preAbsolute.ScrollToFixed",e.options.preAbsolute),e.options.postAbsolute&&d.bind("postAbsolute.ScrollToFixed",e.options.postAbsolute),e.options.fixed&&d.bind("fixed.ScrollToFixed",e.options.fixed),e.options.unfixed&&d.bind("unfixed.ScrollToFixed",e.options.unfixed),e.options.spacerClass&&x.addClass(e.options.spacerClass),d.bind("resize.ScrollToFixed",function(){x.height(d.height())}),d.bind("scroll.ScrollToFixed",function(){d.trigger("preUnfixed"),F(),d.trigger("unfixed"),h()}),d.bind("remove.ScrollToFixed",function(){d.trigger("preUnfixed"),F(),d.trigger("unfixed"),i(window).unbind("resize.ScrollToFixed",m),i(window).unbind("scroll.ScrollToFixed",v),d.unbind(".ScrollToFixed"),e.$el.removeData("ScrollToFixed")}),m())},e.init()},i.ScrollToFixed.defaultOptions={marginTop:0,limit:0,bottom:-1,zIndex:1e3},i.fn.scrollToFixed=function(o){return this.each(function(){new i.ScrollToFixed(this,o)})}}(jQuery);