/**
 * ng-scrollbar
 * @version v0.0.7 - 2015-06-19
 * @link https://github.com/asafdav/ng-scrollbar
 * @author Asaf David <asafdav@gmail.com>
 * @license MIT License, http://www.opensource.org/licenses/MIT
 */
"use strict";angular.module("ngScrollbar",[]).directive("ngScrollbar",["$parse","$window",function(a,b){return{restrict:"A",replace:!0,transclude:!0,scope:{showYScrollbar:"=?isBarShown"},link:function(a,c,d){var e,g,h,i,j,k,l,m,n,o,p,q={bottom:d.hasOwnProperty("bottom")},r=angular.element(b),s=!!r[0].addEventListener,t=!!r[0].removeEventListener,u={top:0},v={top:0},w=function(){l={position:"relative",overflow:"hidden","max-width":"100%",height:"100%"},v.height&&(l.height=v.height+"px"),m={position:"absolute",height:u.height+"px",top:u.top+"px"},n={position:"relative","line-height":u.height+"px"},o={position:"relative",top:v.top+"px",overflow:"hidden"}},x=function(){i.css("top",u.top+"px");var a=u.top/v.height;v.top=-Math.round(v.scrollHeight*a),g.css("top",v.top+"px")},y=function(a){var b=a.hasOwnProperty("offsetY")?a.offsetY:a.layerY,c=Math.max(0,Math.min(parseInt(u.trackHeight,10)-parseInt(u.height,10),b));u.top=c,x(),a.stopPropagation()},z=function(a){var b=40,c=a,d=c.detail,e=c.wheelDelta,g=225,h=g-1;return d=d?e&&(f=e/d)?d/f:-d/1.35:e/120,d=1>d?-1>d?(-Math.pow(d,2)-h)/g:d:(Math.pow(d,2)+h)/g,a.delta=Math.min(Math.max(d/2,-1),1),a.delta=a.delta*b,u.top=Math.max(0,Math.min(parseInt(v.height,10)-parseInt(u.height,10),parseInt(u.top,10)-a.delta)),x(),a.preventDefault?void a.preventDefault():!1},A=0,B=function(a,b,c){u.top=Math.max(0,Math.min(parseInt(u.trackHeight,10)-parseInt(u.height,10),c)),a.stopPropagation()},C=function(a){var b=0,c=a.pageY-i[0].scrollTop-A;B(a,b,c),x()},D=function(a){r.off("mousemove",C),r.off("mouseup",D),a.stopPropagation()},E=function(a){var b=0,c=a.originalEvent.changedTouches[0].pageY-i[0].scrollTop-A;B(a,b,c),x()},F=function(a){r.off("touchmove",E),r.off("touchend",F),a.stopPropagation()},G=function(a){var b=void 0!==r[0].onmousewheel?"mousewheel":"DOMMouseScroll";s?a.addEventListener(b,z,!1):a.attachEvent("onmousewheel",z)},H=function(a){var b=void 0!==r[0].onmousewheel?"mousewheel":"DOMMouseScroll";t?a.removeEventListener(b,z,!1):a.detachEvent("onmousewheel",z)},I=function(b){b=q.bottom||b,e=angular.element(c.children()[0]),g=angular.element(e.children()[0]),h=angular.element(e.children()[1]),i=angular.element(angular.element(h.children()[0]).children()[0]),j=angular.element(i.children()[0]),k=angular.element(angular.element(h.children()[0]).children()[1]),v.height=c[0].offsetHeight,v.scrollHeight=g[0].scrollHeight,v.height<v.scrollHeight?(a.showYScrollbar=!0,a.$emit("scrollbar.show"),u.height=Math.round(v.height/v.scrollHeight*v.height),u.trackHeight=v.height,w(),c.css({overflow:"hidden"}),e.css(l),g.css(o),i.css(m),j.css(n),k.bind("click",y),G(g[0]),i.on("mousedown",function(a){A=a.pageY-i[0].offsetTop,r.on("mouseup",D),r.on("mousemove",C),a.preventDefault()}),i.on("touchstart",function(a){A=a.originalEvent.changedTouches[0].pageY-i[0].offsetTop,r.on("touchend",F),r.on("touchmove",E),a.preventDefault()}),b?(q.bottom=!1,u.top=parseInt(v.height,10)-parseInt(u.height,10)):u.top=Math.max(0,Math.min(parseInt(v.height,10)-parseInt(u.height,10),parseInt(u.top,10))),x()):(a.showYScrollbar=!1,a.$emit("scrollbar.hide"),i.off("mousedown"),H(g[0]),g.attr("style","position:relative;top:0"),e.css({height:"100%"}))},J=function(b,c){null!=p&&clearTimeout(p);var d=!!c&&!!c.rollToBottom;p=setTimeout(function(){v.height=null,I(d),a.$$phase||a.$digest(),a.$parent.$$phase||a.$parent.$digest()},72)};I(),d.rebuildOn&&d.rebuildOn.split(" ").forEach(function(b){a.$on(b,J)}),d.hasOwnProperty("rebuildOnResize")&&r.on("resize",J)},template:'<div><div class="ngsb-wrap"><div class="ngsb-container" ng-transclude></div><div class="ngsb-scrollbar" style="position: absolute; display: block;" ng-show="showYScrollbar"><div class="ngsb-thumb-container"><div class="ngsb-thumb-pos" oncontextmenu="return false;"><div class="ngsb-thumb" ></div></div><div class="ngsb-track"></div></div></div></div></div>'}}]);