!function(t){"undefined"!=typeof define&&define.amd?define([],t):"undefined"!=typeof module&&module.exports?module.exports=t():window.scrollMonitor=t()}(function(){function t(){if(r.viewportTop=l(),r.viewportBottom=r.viewportTop+r.viewportHeight,r.documentHeight=I(),r.documentHeight!==k){for(E=a.length;E--;)a[E].recalculateLocation();k=r.documentHeight}}function i(){r.viewportHeight=V(),t(),e()}function o(){clearTimeout(H),H=setTimeout(i,100)}function e(){for(T=a.length;T--;)a[T].update();for(T=a.length;T--;)a[T].triggerCallbacks()}function s(t,i){function o(t){if(0!==t.length)for(V=t.length;V--;)(I=t[V]).callback.call(e,y),I.isOne&&t.splice(V,1)}var e=this;this.watchItem=t,this.offsets=i?i===+i?{top:i,bottom:i}:{top:i.top||g.top,bottom:i.bottom||g.bottom}:g,this.callbacks={};for(var s=0,n=v.length;s<n;s++)e.callbacks[v[s]]=[];this.locked=!1;var h,c,l,a,V,I;this.triggerCallbacks=function(){switch(this.isInViewport&&!h&&o(this.callbacks[w]),this.isFullyInViewport&&!c&&o(this.callbacks[u]),this.isAboveViewport!==l&&this.isBelowViewport!==a&&(o(this.callbacks[p]),c||this.isFullyInViewport||(o(this.callbacks[u]),o(this.callbacks[f])),h||this.isInViewport||(o(this.callbacks[w]),o(this.callbacks[m]))),!this.isFullyInViewport&&c&&o(this.callbacks[f]),!this.isInViewport&&h&&o(this.callbacks[m]),this.isInViewport!==h&&o(this.callbacks[p]),!0){case h!==this.isInViewport:case c!==this.isFullyInViewport:case l!==this.isAboveViewport:case a!==this.isBelowViewport:o(this.callbacks[b])}h=this.isInViewport,c=this.isFullyInViewport,l=this.isAboveViewport,a=this.isBelowViewport},this.recalculateLocation=function(){if(!this.locked){var t=this.top,i=this.bottom;if(this.watchItem.nodeName){var e=this.watchItem.style.display;"none"===e&&(this.watchItem.style.display="");var s=this.watchItem.getBoundingClientRect();this.top=s.top+r.viewportTop,this.bottom=s.bottom+r.viewportTop,"none"===e&&(this.watchItem.style.display=e)}else this.watchItem===+this.watchItem?this.watchItem>0?this.top=this.bottom=this.watchItem:this.top=this.bottom=r.documentHeight-this.watchItem:(this.top=this.watchItem.top,this.bottom=this.watchItem.bottom);this.top-=this.offsets.top,this.bottom+=this.offsets.bottom,this.height=this.bottom-this.top,void 0===t&&void 0===i||this.top===t&&this.bottom===i||o(this.callbacks[d])}},this.recalculateLocation(),this.update(),h=this.isInViewport,c=this.isFullyInViewport,l=this.isAboveViewport,a=this.isBelowViewport}function n(i){y=i,t(),e()}var h="undefined"==typeof window,c=!h,l=function(){return h?0:window.pageYOffset||document.documentElement&&document.documentElement.scrollTop},r={},a=[],p="visibilityChange",w="enterViewport",u="fullyEnterViewport",m="exitViewport",f="partiallyExitViewport",d="locationChange",b="stateChange",v=[p,w,u,m,f,d,b],g={top:0,bottom:0},V=function(){return h?0:window.innerHeight||document.documentElement.clientHeight},I=function(){return h?0:Math.max(document.documentElement.scrollHeight,document.documentElement.offsetHeight,document.documentElement.clientHeight)};r.viewportTop=null,r.viewportBottom=null,r.documentHeight=null,r.viewportHeight=V();var k,y,E,H,T;s.prototype={on:function(t,i,o){switch(!0){case t===p&&!this.isInViewport&&this.isAboveViewport:case t===w&&this.isInViewport:case t===u&&this.isFullyInViewport:case t===m&&this.isAboveViewport&&!this.isInViewport:case t===f&&this.isAboveViewport:if(i.call(this,y),o)return}if(!this.callbacks[t])throw new Error("Tried to add a scroll monitor listener of type "+t+". Your options are: "+v.join(", "));this.callbacks[t].push({callback:i,isOne:o||!1})},off:function(t,i){if(!this.callbacks[t])throw new Error("Tried to remove a scroll monitor listener of type "+t+". Your options are: "+v.join(", "));for(var o,e=0;o=this.callbacks[t][e];e++)if(o.callback===i){this.callbacks[t].splice(e,1);break}},one:function(t,i){this.on(t,i,!0)},recalculateSize:function(){this.height=this.watchItem.offsetHeight+this.offsets.top+this.offsets.bottom,this.bottom=this.top+this.height},update:function(){this.isAboveViewport=this.top<r.viewportTop,this.isBelowViewport=this.bottom>r.viewportBottom,this.isInViewport=this.top<=r.viewportBottom&&this.bottom>=r.viewportTop,this.isFullyInViewport=this.top>=r.viewportTop&&this.bottom<=r.viewportBottom||this.isAboveViewport&&this.isBelowViewport},destroy:function(){var t=a.indexOf(this),i=this;a.splice(t,1);for(var o=0,e=v.length;o<e;o++)i.callbacks[v[o]].length=0},lock:function(){this.locked=!0},unlock:function(){this.locked=!1}};for(var B=0,A=v.length;B<A;B++){var F=v[B];s.prototype[F]=function(t){return function(i,o){this.on.call(this,t,i,o)}}(F)}if(c)try{t()}catch(i){try{window.$(t)}catch(t){throw new Error("If you must put scrollMonitor in the <head>, you must use jQuery.")}}return c&&(window.addEventListener?(window.addEventListener("scroll",n),window.addEventListener("resize",o)):(window.attachEvent("onscroll",n),window.attachEvent("onresize",o))),r.beget=r.create=function(t,i){"string"==typeof t?t=document.querySelector(t):t&&t.length>0&&(t=t[0]);var o=new s(t,i);return a.push(o),o.update(),o},r.update=function(){y=null,t(),e()},r.recalculateLocations=function(){r.documentHeight=0,r.update()},r});