(function($){$.ScrollToFixed=function(el,options){var base=this;base.$el=$(el);base.el=el;base.$el.data("ScrollToFixed",base);var isReset=false;var target=base.$el;var offsetTop=0;var offsetLeft=0;var originalOffsetLeft=-1;var lastOffsetLeft=-1;var spacer=null;function resetScroll(){setUnfixed();lastOffsetLeft=-1;offsetTop=target.offset().top;offsetLeft=target.offset().left;if(originalOffsetLeft==-1){orginalOffsetLeft=offsetLeft}isReset=true;if(base.options.bottom!=-1){setFixed()}}function isFixed(){return target.css('position')=='fixed'}function isAbsolute(){return target.css('position')=='absolute'}function setFixed(){if(!isFixed()){spacer.css({'display':target.css('display'),'width':target.outerWidth(true),'height':target.outerHeight(true),'float':target.css('float')});target.css({'width':target.width(),'position':'fixed','top':base.options.bottom==-1?base.options.marginTop:'','bottom':base.options.bottom==-1?'':base.options.bottom,})}}function setUnfixed(){if(isFixed()||isAbsolute()){lastOffsetLeft=-1;spacer.css('display','none');target.css({'width':'','position':'','left':'','top':''})}}function setLeft(x){if(x!=lastOffsetLeft){target.css('left',offsetLeft-x);lastOffsetLeft=x}}function checkScroll(){if(!isReset){resetScroll()}var x=$(window).scrollLeft();var y=$(window).scrollTop();if(base.options.bottom==-1){if(base.options.limit>0&&y>=base.options.limit-base.options.marginTop){target.css({'width':target.width(),'position':'absolute','top':base.options.limit,'left':offsetLeft})}else if(y>=offsetTop-base.options.marginTop){setFixed();lastOffsetLeft=-1;setLeft(x)}else{setUnfixed()}}else{if(base.options.limit>0){if(y+$(window).height()-target.outerHeight()>=base.options.limit-base.options.marginTop){setUnfixed()}else{setFixed();setLeft(x)}}else{setLeft(x)}}}base.init=function(){base.options=$.extend({},$.ScrollToFixed.defaultOptions,options);if(navigator.platform=='iPad'||navigator.platform=='iPhone'||navigator.platform=="iPod"){return}base.$el.css('z-index',base.options.zIndex);spacer=$('<div/>');base.$el.after(spacer);$(window).bind('resize',function(event){resetScroll();checkScroll()});$(window).bind('scroll',function(event){checkScroll()});if(base.options.bottom!=-1){setFixed()}};base.init()};$.ScrollToFixed.defaultOptions={marginTop:0,limit:0,bottom:-1,zIndex:1000};$.fn.scrollToFixed=function(options){return this.each(function(){(new $.ScrollToFixed(this,options))})}})(jQuery);
