(function(n,t){typeof define=="function"&&define.amd?define([],t):typeof module=="object"&&module.exports?module.exports=t():n.WhenInViewport=t()})(this,function(){function t(t,r,u){i.setup();this.registryItem=n.addItem(t,typeof r=="function"?s(u||{},{callback:r}):r);n.processItem(this.registryItem)}function o(){return"innerHeight"in window?window.innerHeight:document.documentElement.offsetHeight}function r(){return"pageYOffset"in window?window.pageYOffset:document.documentElement.scrollTop||document.body.scrollTop}function h(n){return n.getBoundingClientRect().top+r()}function u(n,t,i){for(var r in n)if(n.hasOwnProperty(r)&&t.call(i,n[r],r)===!1)break}function s(n){for(var t=1;t<arguments.length;t++)u(arguments[t],function(t,i){n[i]=t});return n}var f,e;t.prototype.stopListening=function(){n.removeItem(this.registryItem);i.removeIfStoreEmpty()};t.defaults={threshold:0,context:null};s(t,{setRateLimiter:function(n,t){return i.rateLimiter=n,t&&(i.rateLimitDelay=t),this},checkAll:function(){return e=r(),f=o(),n.adjustPositions(n.processItem),i.removeIfStoreEmpty(),this},destroy:function(){return n.store={},i.remove(),delete i.scrollHandler,delete i.resizeHandler,this},registerAsJqueryPlugin:function(n){return n.fn.whenInViewport=function(i,r){var u,f=function(t){return function(i){t.call(this,n(i))}};return u=typeof i=="function"?n.extend({},r,{callback:f(i)}):n.extend(i,{callback:f(i.callback)}),this.each(function(){u.setupOnce?n.data(this,"whenInViewport")||n.data(this,"whenInViewport",new t(this,u)):n.data(this,"whenInViewport",new t(this,u))})},this}});var n={store:{},counter:0,addItem:function(n,i){var r="whenInViewport"+ ++this.counter,u=s({},t.defaults,i,{storeKey:r,element:n,topOffset:h(n)});return this.store[r]=u},adjustPositions:function(t){u(this.store,function(i){i.topOffset=h(i.element);t&&t.call(n,i)})},processAll:function(){u(this.store,this.processItem,this)},processItem:function(n){e+f>=n.topOffset-n.threshold&&(this.removeItem(n),n.callback.call(n.context||window,n.element))},removeItem:function(n){delete this.store[n.storeKey]},isEmpty:function(){var n=!0;return u(this.store,function(){return n=!1}),n}},i={setuped:!1,rateLimiter:function(n){return n},rateLimitDelay:100,on:function(n,t){return window.addEventListener?window.addEventListener(n,t,!1):window.attachEvent&&window.attachEvent(n,t),this},off:function(n,t){return window.removeEventListener?window.removeEventListener(n,t,!1):window.detachEvent&&window.detachEvent("on"+n,t),this},setup:function(){var t=this;if(!this.setuped){e=r();f=o();this.scrollHandler=this.scrollHandler||this.rateLimiter(function(){e=r();n.processAll();t.removeIfStoreEmpty()},this.rateLimitDelay);this.resizeHandler=this.resizeHandler||this.rateLimiter(function(){f=o();n.adjustPositions(n.processItem);t.removeIfStoreEmpty()},this.rateLimitDelay);this.on("scroll",this.scrollHandler).on("resize",this.resizeHandler);this.setuped=!0}},removeIfStoreEmpty:function(){n.isEmpty()&&this.remove()},remove:function(){this.setuped&&(this.off("scroll",this.scrollHandler).off("resize",this.resizeHandler),this.setuped=!1)}},c=window.jQuery||window.$;return c&&t.registerAsJqueryPlugin(c),t})