define(["dom","layoutManager","apphost","events","Modernizr","css!./headroom"],function(dom,layoutManager,appHost,events,Modernizr){"use strict";var windowInsetTop=0;function onWindowInsetsChanged(e,insets){windowInsetTop=insets.top}function Headroom(options){options=options||{},this.lastScrollY=0,this.lastTransformScrollY=0,this.element=options.element,this.scrollElementForEvents=options.scroller||window,this.scroller=this.scrollElementForEvents,this.updateFn=this.update.bind(this),this.offset=options.offset,this.initialised=!1}return Headroom.prototype={constructor:Headroom,init:function(){return appHost.getWindowInsets&&(windowInsetTop=appHost.getWindowInsets().top),events.on(appHost,"windowinsetschanged",onWindowInsetsChanged),Modernizr.csstransforms&&this.attachEvent(),this},pause:function(){this.paused=!0},resume:function(){this.paused&&(this.paused=!1,this.transform=null,this.isAtTop=null,this.update({},!0))},destroy:function(){events.off(appHost,"windowinsetschanged",onWindowInsetsChanged),this.initialised=!1,this.lastScrollY=null,this.lastTransformScrollY=null;var scroller=this.scrollElementForEvents;scroller&&(scroller.removeScrollEventListener?scroller.removeScrollEventListener(this.updateFn,{capture:!1,passive:!0}):dom.removeEventListener(scroller,"scroll",this.updateFn,{capture:!1,passive:!0})),this.scrollElementForEvents=null,this.scroller=null,this.element=null},attachEvent:function(){var scroller;this.initialised||(this.lastScrollY=this.getScrollY(),this.lastTransformScrollY=this.lastScrollY,this.initialised=!0,(scroller=this.scrollElementForEvents)&&(scroller.addScrollEventListener?scroller.addScrollEventListener(this.updateFn,{capture:!1,passive:!0}):dom.addEventListener(scroller,"scroll",this.updateFn,{capture:!1,passive:!0})),this.update())},setTransform:function(value,top,forceRefresh){var isActive,elem,classList;value===this.transform&&!forceRefresh||(0===(this.transform=value)?value="none":isActive=(value=1===value?windowInsetTop?"translateY(calc(-100% + "+windowInsetTop+"px))":"translateY(-100%)":"translateY(-"+(value+windowInsetTop)+"px)",!0),(elem=this.element)&&(classList=elem.classList,windowInsetTop&&(isActive?classList.add("headroom-active"):classList.remove("headroom-active")),elem.style.transform=value))},getScrollY:function(){var scroller=this.scroller;if(scroller.getScrollPosition)return scroller.getScrollPosition();var scrollTop=scroller.scrollTop;return void 0!==scrollTop?scrollTop:(document.scrollingElement||document.documentElement||document.body).scrollTop},update:function(e,force){var currentScrollY,lastScrollY,isTv,scrollingDirection,lastTransformScrollY,scrollThreshold,top,forceRefresh,toleranceExceeded;this.paused||((currentScrollY=this.getScrollY())<0||(lastScrollY=this.lastScrollY,isTv=layoutManager.tv,scrollThreshold=10,-1==(scrollingDirection=lastScrollY<currentScrollY?1:currentScrollY<lastScrollY?-1:0)?(lastTransformScrollY=this.lastTransformScrollY,scrollThreshold=40):lastTransformScrollY=lastScrollY,top=currentScrollY<=(isTv?130:0),toleranceExceeded=(forceRefresh=!0===force)||Math.abs(currentScrollY-lastTransformScrollY)>scrollThreshold,1==scrollingDirection&&!top&&toleranceExceeded?(this.lastTransformScrollY=currentScrollY,this.setTransform(1,top,forceRefresh)):-1==scrollingDirection&&!isTv&&toleranceExceeded||top?(this.lastTransformScrollY=currentScrollY,this.setTransform(0,top,forceRefresh||top)):!scrollingDirection&&forceRefresh&&(this.lastTransformScrollY=currentScrollY,this.setTransform(top?0:1,top,forceRefresh||top))),this.lastScrollY=currentScrollY)}},Headroom});