/*
 * angular-sticky-plugin
 * https://github.com/harm-less/angular-sticky

 * Version: 0.5.0 - 2018-10-25
 * License: MIT
 */
"use strict";angular.module("hl.sticky",[]).factory("mediaQuery",function(){return{matches:function(t){return t&&(matchMedia("("+t+")").matches||matchMedia(t).matches)}}}).factory("hlStickyStack",["$document","StickyStackDefaults",function(t,e){function n(t){var n=(t=t||{}).name||e.defaultStack;if(r[n])return r[n];var a=t.zIndex||e.zIndex,i=[],c={};return c.options=t,c.stackName=n,c.add=function(t,e){return angular.isString(t)&&""!==t||(t=c.length()),e.id=t,e.zIndex=a,i.push(e),a-=1,e},c.get=function(t){for(var e=0;e<i.length;e++)if(t==i[e].id)return i[e];return!1},c.index=function(t){for(var e=0;e<i.length;e++)if(t==i[e].id)return e;return-1},c.range=function(t,e){return i.slice(t,e)},c.all=function(){return i},c.keys=function(){for(var t=[],e=0;e<i.length;e++)t.push(i[e].id);return t},c.top=function(){return i[i.length-1]},c.remove=function(t){for(var e=0;e<i.length;e++)if(t==i[e].id)return a+=1,i.splice(e,1)[0];return!1},c.removeTop=function(){return a+=1,i.splice(i.length-1,1)[0]},c.length=function(){return i.length},c.height=function(t){var e={top:0,bottom:0};return angular.forEach(i,function(n){e[n.anchor()]+=n.computedHeight(t)}),e[t]},c.heightAt=function(t,e){for(var n,o,r=e-1,a={top:0,bottom:0},c=0;c<i.length;c++)if((n=i[c]).sticksAtPosition(t,r)){var l=n.anchor();o=n.computedHeight(t,r-a[l]),a[l]+=o}return a[t]},c.heightCurrent=function(t){return c.heightAt(t,window.pageYOffset||o.scrollTop)},r[n]=c,c}var o=t[0].documentElement,r={};return n}]).factory("hlStickyElement",["$document","$log","hlStickyStack","throttle","mediaQuery","StickyStackDefaults",function(t,e,n,o,r,a){return function(o,i){function c(){return T?D:D=b(B)-L-x()}function l(){return T?P:P=w(B)+R+E()}function u(){return!angular.isDefined(i.enable)||i.enable}function s(){return u()&&T||i.alwaysSticky}function f(t,n){if(!m())return!1;switch(t){case"top":return d(n);case"bottom":return h(n);default:e.error('Unknown anchor "'+t+'"')}return!1}function d(t){return(t=void 0!==t?t:window.pageYOffset||M.scrollTop)-(A.clientTop||0)>=c()}function h(t){return(t=void 0!==t?t:window.pageYOffset||M.scrollTop)+window.innerHeight<=l()}function m(){return!1===Q||r.matches(Q)}function g(){var t=f(Y);angular.isDefined(i.enable)&&!i.enable&&(t=!1),angular.isDefined(i.alwaysSticky)&&i.alwaysSticky&&(t=!0),t&&!T?(k(),j({event:"stick"})):!t&&T&&(p(),j({event:"unstick"})),y(),T&&("top"===Y?o.css("top",L+C(Y)-I()+"px"):"bottom"===Y&&o.css("bottom",R+C(Y)-z()+"px"),o.css("width",v()+"px"))}function y(){var t=null;switch(o.removeClass(i.beforeStickyClass),o.removeClass(i.afterStickyClass),Y){case"top":T?I()>0&&(o.addClass(i.afterStickyClass),t="after"):(o.addClass(i.beforeStickyClass),t="before");break;case"bottom":z()>0?(o.addClass(i.beforeStickyClass),t="before"):T||(o.addClass(i.afterStickyClass),t="after")}"before"!==t&&o.removeClass(i.beforeStickyClass),"after"!==t&&o.removeClass(i.afterStickyClass)}function k(){T=!0,o.addClass(O),i.usePlaceholder&&((H=H||angular.element("<div>")).css("height",S()+"px"),o.after(H));var t=B.getBoundingClientRect(),e={width:v()+"px",position:"fixed",left:t.left+"px","z-index":q?q.get(N).zIndex-(U.zIndex||0):null};e["margin-"+Y]=0,o.css(e)}function p(){T=!1,o.removeClass(O),o.attr("style",W.style),H&&H.remove()}function v(){return B.offsetWidth}function S(){return B.offsetHeight}function b(t){var e=0;if(t&&t.offsetParent)do{e+=t.offsetTop,t=t.offsetParent}while(t);return e}function w(t){return b(t)+t.clientHeight}function C(t){var e=0;if("top"===t&&U.top>0&&(e+=U.top),"bottom"===t&&U.bottom>0&&(e+=U.bottom),q){var n=q.index(N);"top"===t&&n>0&&q.range(0,n).forEach(function(n){n.isSticky()&&(e+=n.computedHeight(t))}),"bottom"===t&&n!==q.length()-1&&q.range(n+1,q.length()).forEach(function(n){n.isSticky()&&(e+=n.computedHeight(t))})}return e}function x(){return C("top")}function E(){return C("bottom")}function $(t,e){return"top"===t?Math.max(0,S()-I(e)+L):"bottom"===t?Math.max(0,S()-z(e)+R):0}function z(t){if(null===F&&(F=void 0!==i.container&&(angular.isString(i.container)?angular.element(A.querySelector("#"+i.container))[0]:i.container)),F){var e=!(null===t||void 0===t),n=F.getBoundingClientRect(),o=e?b(F)+n.height-t:n.top-window.innerHeight+S();return Math.max(0,o+L+R-C(Y))}return 0}function I(t){if(null===F&&(F=void 0!==i.container&&(angular.isString(i.container)?angular.element(A.querySelector("#"+i.container))[0]:i.container)),F){var e=!(null===t||void 0===t),n=F.getBoundingClientRect(),o=e?b(F)+n.height-t:n.bottom;return Math.max(0,L+C(Y)+S()+R-o)}return 0}i=i||{};var D,P,H,T=!1,M=t[0].body,B=o[0],A=t[0].documentElement;angular.isFunction(i.event)||delete i.event,angular.forEach(a,function(t,e){angular.isUndefined(i[e])||""===i[e]?i[e]=t:i[e]&&!isNaN(i[e])&&(i[e]*=1)});var N=i.id,Q=i.mediaQuery,O=i.stickyClass,L=(i.usePlaceholder,i.offsetTop),R=i.offsetBottom,Y=i.anchor.toLowerCase().trim(),j=i.event,q=!1===i.stack?null:i.stack||n({zIndex:i.zIndex}),F=null,U={top:0,bottom:0},W={style:o.attr("style")||""};o.addClass("sticky-"+Y);var G={};if(q){var J=q.add(N,G);N=J.id}return G.draw=function(t){var e=(t=t||{}).offset;e&&(U.top=e.top||0,U.bottom=e.bottom||0,U.zIndex=e.zIndex),!0===t.force&&p(),g()},G.anchor=function(){return Y},G.isSticky=s,G.isEnabled=u,G.computedHeight=$,G.sticksAtPosition=f,G.destroy=function(){p(),q&&q.remove(N)},G}}]).constant("StickyStackDefaults",{id:null,enable:!0,mediaQuery:!1,stickyClass:"is-sticky",beforeStickyClass:"sticky-before",afterStickyClass:"sticky-after",usePlaceholder:!0,offsetTop:0,offsetBottom:0,anchor:"top",container:null,event:angular.noop,stack:null,defaultStack:"default-stack",collection:null,collectionParent:null,zIndex:1039}).provider("hlStickyElementCollection",function(){var t=0,e={collections:{},defaults:{checkDelay:20},elementsDefaults:{},$get:["$rootScope","$window","$document","$log","StickyStackDefaults","hlStickyElement","hlStickyStack","throttle",function(n,o,r,a,i,c,l,u){function s(){t++,h(),t>1||(p=u(h,e.defaults.checkDelay),v.on("resize",p),v.on("scroll",d),y=n.$on("$viewContentLoaded",p),k=n.$on("$includeContentLoaded",p),p())}function f(){--t>0||(v.off("resize",p),v.off("scroll",d),y(),k())}function d(){m()}function h(){m({force:!0})}function m(t){angular.forEach(e.collections,function(e){e.draw(t)})}function g(t){t&&angular.isObject(t)||(a.warn("Must supply an options object"),t={});var n=(t=angular.extend({},e.elementsDefaults,t)).name||i.defaultStack;if(e.collections[n])return e.collections[n];var o=l({name:n,zIndex:t.zIndex}),r=[],u={};return u.addElement=function(t,e){(e=e||{}).stack=o;var n=c(t,e);return r.push({stickyElement:n,element:t}),n},u.removeElement=function(t){for(var e,n=r.length;n--;)if(angular.isString(t)&&"#"+r[n].element.id===t||r[n].element===t){e=n;break}var o=r.splice(e,1)[0];return o&&o.stickyElement.destroy(),o},u.draw=function(e){var n={};if(t.parent){var o=l({name:t.parent,zIndex:t.zIndex});n.offset={top:o.heightCurrent("top"),zIndex:o.length()}}angular.extend(n,e||{}),angular.forEach(r,function(t){t.stickyElement.draw(n)})},u.destroy=function(){angular.forEach(angular.copy(r),function(t){u.removeElement(t)}),delete e.collections[n],f()},u.trackedElements=function(){return r},e.collections[n]=u,s(),u}var y,k,p,v=angular.element(o);return g}]};return e}).directive("hlSticky",["$log","$window","$document","hlStickyElementCollection",function(t,e,n,o){return{restrict:"A",scope:{container:"@",anchor:"@",stickyClass:"@",mediaQuery:"@",collection:"@",collectionParent:"@",offsetTop:"@",offsetBottom:"@",zIndex:"@",event:"&",usePlaceholder:"=?",enable:"=?",alwaysSticky:"=?",options:"=?"},link:function(t,e,n){e.addClass("hl-sticky");var r;t.options?r=t.options:(r={id:n.hlSticky,event:function(){var e=t.event();angular.isFunction(e)&&e.apply(null,arguments)}},angular.forEach(t,function(e,n){"event"!==n&&"$"!==n[0]&&angular.isDefined(e)&&(r[n]=t[n],r[n]&&!isNaN(r[n])&&(r[n]*=1))}));var a=o({name:r.collection,parent:r.collectionParent,zIndex:r.zIndex});a.addElement(e,r),t.$watch("options",function(t,e){t&&t!==e&&a.draw({force:!0})},!0),t.$watch("enable",function(e,n){e!==n&&(r.enable=t.enable,a.draw({force:!0}))}),t.$watch("alwaysSticky",function(e,n){e!==n&&(r.alwaysSticky=t.alwaysSticky,a.draw({force:!0}))}),t.$on("$destroy",function(){a.removeElement(e),a.trackedElements().length||a.destroy()})}}}]).factory("throttle",["$timeout",function(t){return function(e,n,o){var r=null;return o=o||{},function(){var o=this,a=arguments;t.cancel(r),r=t(function(){e.apply(o,a)},n,!1)}}}]);