(function(){u8.Base.HotKeys=function(){this._hotKeys=[];this.id="hotkeys";u8.services.events.bind("u8.services.pages.onPageInit",this._onPageInit.bind(this),this.id)};u8.Base.HotKeys.makeClass("HotKeys");$.extend(u8.Base.HotKeys.prototype,{dispose:function(){if(this._isDisposed){return}u8.services.events.unbind("u8.services.pages.onPageInit",this.id);this._isDisposed=true},register:function(e,t,i){if(!t){return false}if("string"==typeof t){t=new u8.Base.HotKey(t);t.bind("Hit",i)}if(u8.debug.level){u8.log.debug(this.id,"Registering "+t.getDisplayString()+" to id '"+e+"'.")}this._hotKeys.push({hotKey:t,id:e});return true},registerButtons:function(e,t){if(!t){return}var i=this._onButtonHotKeyHit.bind(this);for(var n=0;n<t.length;n++){var s=t[n].hotKey;if(!s){continue}var o=new u8.Base.HotKey(s);if(!o.isValid()){continue}o._button=t[n];o.bind("Hit",i);this.register(e,o)}},unregister:function(e){var t=0;for(var i=0;i<this._hotKeys.length;i++){var n=this._hotKeys[i];if(n.id==e){if(u8.debug.level){u8.log.debug(this.id,"Unregistering "+n.hotKey.getDisplayString()+" from id '"+n.id+"'.")}n.hotKey.dispose();this._hotKeys.splice(i--,1);t++}}return t},_onPageInit:function(e,t){var i=t.elWindow;if(!i){return}$$(i.document).on("keydown",this._onKeyDown.bind(this))},_onKeyDown:function(e){if(!e){return}var t;var i=u8.debug.level?true:false;var n=e.target;var s=undefined;var o;var r=undefined;for(var a=0;a<this._hotKeys.length;a++){var u=this._hotKeys[a];t=u.hotKey;if(t.keyCode==e.keyCode&&t.ctrlKey==e.ctrlKey&&t.shiftKey==e.shiftKey&&t.altKey==e.altKey){if(!s){s=[]}var d=t.scopeId;if(d){var f=n.ownerDocument.getElementById(d);var h=u8.Base.Dom.getDistanceToParent(n,f);if(h>=0){s.push({distance:h,item:u})}else if($p(n).id==d){s.push({distance:Number.MAX_VALUE,item:u})}}else{var c={distance:0,item:u};if(t.useLastUnscoped){r=c}else{if(o){continue}s.unshift(c);o=true}}}}if(s&&r){s.unshift(r)}if(!s||s.length<=0){return}if(s.length>1){this._sortByDistance(s)}var y=false;var l={cancel:false,handled:false,elSource:n,elEvent:e};for(a=0;a<s.length;a++){var g=s[a].item;t=g.hotKey;l.hotKey=t;if(i){u8.log.debug(this.id,"Sending "+t.getDisplayString()+" to id '"+g.id+"'.")}t.raise("Hit",l);if(l.handled){y=true}if(l.cancel){break}}if(y){e.preventDefault();e.stopPropagation()}},_sortByDistance:function(t){if(t){t.sort(e)}},_onButtonHotKeyHit:function(e,t){var i=t.hotKey._button;var n=i.name;var s=i.action;if(s){var o={arguments:{buttonname:n}};u8.services.actions.execute(s,o)}}});u8.services.add({name:"hotKeys",scope:"Application",ctor:u8.Base.HotKeys});var e=u8._.cmp("distance")})();