(function(){var t=function(t){if("undefined"==typeof $)return void alert("InfiniteScroller require jQuery");var e={container:null,tail:null,autoStart:!0,initCallback:function(){},nextCallback:function(t){},doneCallback:function(){},resetCallback:function(){},pullToRefreshCallback:function(){},pullToRefresh:!1,pullToRefreshDom:null};this.opt=$.extend(e,t),null===this.opt.container&&(this.opt.container=window),this.dom={container:null,tail:null},this.runtime={processNext:!1,done:!1,page:1,started:!1};var i=this;if(this.dom.container=$(this.opt.container),this.dom.tail=$(this.opt.tail),this.dom.pullToRefresh=null,this.dom.container.on("scroll",function(){i._nextCheck()}),this.opt.initCallback(),this.opt.autoStart&&(this.runtime.started=!0,i._nextCheck()),this.opt.pullToRefresh){i.dom.pullToRefresh=$(this.opt.pullToRefreshDom);var n=i.dom.pullToRefresh.height(),o=null,s={x:0,y:0},h=0,l=0;i.dom.container.on("touchstart",function(t){t=t.originalEvent,l=0,h=0,i.dom.container.scrollTop()<=0&&(o="up"),"up"==o&&(s.x=t.changedTouches[0].pageX,s.y=t.changedTouches[0].pageY,i.dom.pullToRefresh.find(".status").hide(),i.dom.pullToRefresh.find(".status.pull").show())}),i.dom.container.on("touchmove",function(t){if(t=t.originalEvent,t.preventDefault(),"up"==o){if(Math.abs(t.touches[0].pageX-s.x)-Math.abs(t.touches[0].pageY-s.y)>0)return!1;var e=t.changedTouches[0].pageY-s.y;e>l?(l=Math.max(l,e),e>n&&(e=n),h=e-n):(h-=l-e,l=e,h<-n&&(h=-n)),e>0&&i.dom.pullToRefresh.css({marginTop:h+"px"})}}),i.dom.container.on("touchend",function(t){t=t.originalEvent,"up"==o&&(o=null,0==h?(i.dom.pullToRefresh.find(".status").hide(),i.dom.pullToRefresh.find(".status.refreshing").show(),i.runtime.page=1,i.opt.pullToRefreshCallback(i.runtime.page)):i.dom.pullToRefresh.css({marginTop:-n+"px"}))})}};t.prototype={_isTailOnScreen:function(){if(!this.dom.tail.is(":visible"))return!1;var t=$(window),e={top:t.scrollTop(),left:t.scrollLeft()};e.width=t.width(),e.height=t.height(),e.x=e.left+e.width/2,e.y=e.top+e.height/2;var i=this.dom.tail.offset();if(i.width=this.dom.tail.outerWidth(),i.height=this.dom.tail.outerHeight(),i.x=i.left+i.width/2,i.y=i.top+i.height/2,0==i.left&&0==i.right)return!1;var n=!0;return n&&2*Math.abs(e.x-i.x)>=e.width+i.width&&(n=!1),n&&2*Math.abs(e.y-i.y)>=e.height+i.height&&(n=!1),n},_nextCheck:function(){this.runtime.started&&!this.runtime.done&&!this.runtime.processNext&&this._isTailOnScreen()&&this._next()},_next:function(){this.runtime.processNext=!0,this.opt.nextCallback(this.runtime.page)},check:function(){this._nextCheck()},nextDone:function(){this.runtime.processNext=!1,this.runtime.page++,this._nextCheck()},done:function(){this.runtime.done=!0,this.opt.doneCallback()},reset:function(){this.opt.resetCallback(),this.runtime.processNext=!1,this.runtime.done=!1,this.runtime.page=1,this._nextCheck()},start:function(){this.runtime.started=!0,this._nextCheck()},setPage:function(t){this.runtime.page=t},pullToRefreshDone:function(){this.dom.pullToRefresh.css({marginTop:-this.dom.pullToRefresh.height()+"px"}),this.reset()}},"undefined"!=typeof module&&"object"==typeof exports&&define.cmd?module.exports=t:"function"==typeof define&&define.amd?define(function(){return t}):this.InfiniteScroller=t}).call(function(){return this||("undefined"!=typeof window?window:global)}());