define(["backbone","Marionette","./KeyMap","underscore"],function(a,b,c,d){var e={},f=[],g="keyup:",h=new a.Wreqr.EventAggregator,i=function(a){this.initialize(a)};return d.extend(i.prototype,{endOf:":::",prefixManager:"__idEventManager",initialize:function(a){d.bindAll(this,"unbindAll","bind","trigger","_checkName","_unbindArray","onKeyDown","onKeyUp","unbindKeyboardEvents","getId","initCtr","removeKeyVisible","addKeyVisible"),this.blockReadingKeyEvents=!1,this.events=[],this.initCtr(a)},initCtr:function(a){this.thisCtrl=d.isObject(a)?a:{},this.thisCtrl.cid=this.thisCtrl.cid?this.thisCtrl.cid:d.uniqueId("c"),this.thisCtrl[this.prefixManager]=d.isString(this.thisCtrl[this.prefixManager])?this.thisCtrl[this.prefixManager]:this.endOf+this.thisCtrl.cid,e[this.getId()]=this.events},getId:function(){return this.thisCtrl[this.prefixManager]},events:[],unbindAll:function(){this.events=this._unbindArray(this.events),delete e[this.getId()]},_unbindArray:function(a){return d.each(a,function(a){this._checkName(a),h.unbind(a)},this),a=[]},trigger:function(a,b,c){this._checkName(a),d.each(e,function(e){d.each(e,function(e){if(d.isString(e)){var f=e.substring(0,e.indexOf(this.endOf));f&&f.length&&f===a&&h.trigger(e,b,c)}},this)},this)},bind:function(a,b){this._checkName(a),a+=this.getId(),e[this.getId()]||(e[this.getId()]=this.events),d.contains(this.events,a)?h.unbind(a):this.events.push(a),h.bind(a,b)},_checkName:function(a){if(void 0==a||null==a||""==a)throw alert("ERROR EVENT MANAGER: empty name event"),new Error("ERROR EVENT MANAGER: empty name event")},bindKeyboardEvents:function(){this.unbindKeyboardEvents(),$(window).keydown(this.onKeyDown),$(window).keyup(this.onKeyUp)},unbindKeyboardEvents:function(){$(window).unbind("keydown",this.onKeyDown),$(window).unbind("keyup",this.onKeyUp)},onKeyDown:function(a){if(!this.blockReadingKeyEvents){this.blockReadingKeyEvents=!0;var b=c.getValueByKeyCode(a.keyCode);if(b){var d={ctrl:a.ctrlKey,shift:a.shiftKey,cid:this.getId().replace(this.endOf,"")};this.trigger(b,d)}}},onKeyUp:function(a){if(this.blockReadingKeyEvents){var b=c.getValueByKeyCode(a.keyCode);if(b){var d=g+b,e={ctrl:a.ctrlKey,shift:a.shiftKey,cid:this.getId().replace(this.endOf,"")};this.trigger(d,e)}}this.blockReadingKeyEvents=!1},removeKeyVisible:function(a){f=d.difference(f,[a])},addKeyVisible:function(a){this.removeKeyVisible(a),f.push(a)}}),i});