(function(){var a=this.Keyboard=new Class({Extends:Events,Implements:[Options],options:{defaultEventType:"keydown",active:false,manager:null,events:{},nonParsedEvents:["activate","deactivate","onactivate","ondeactivate","changed","onchanged"]},initialize:function(f){if(f&&f.manager){this._manager=f.manager;delete f.manager}this.setOptions(f);this._setup()},addEvent:function(h,g,f){return this.parent(a.parse(h,this.options.defaultEventType,this.options.nonParsedEvents),g,f)},removeEvent:function(g,f){return this.parent(a.parse(g,this.options.defaultEventType,this.options.nonParsedEvents),f)},toggleActive:function(){return this[this.isActive()?"deactivate":"activate"]()},activate:function(f){if(f){if(f.isActive()){return this}if(this._activeKB&&f!=this._activeKB){this.previous=this._activeKB;this.previous.fireEvent("deactivate")}this._activeKB=f.fireEvent("activate");a.manager.fireEvent("changed")}else{if(this._manager){this._manager.activate(this)}}return this},isActive:function(){return this._manager?(this._manager._activeKB==this):(a.manager==this)},deactivate:function(f){if(f){if(f===this._activeKB){this._activeKB=null;f.fireEvent("deactivate");a.manager.fireEvent("changed")}}else{if(this._manager){this._manager.deactivate(this)}}return this},relinquish:function(){if(this.isActive()&&this._manager&&this._manager.previous){this._manager.activate(this._manager.previous)}else{this.deactivate()}return this},manage:function(f){if(f._manager){f._manager.drop(f)}this._instances.push(f);f._manager=this;if(!this._activeKB){this.activate(f)}return this},drop:function(f){f.relinquish();this._instances.erase(f);if(this._activeKB==f){if(this.previous&&this._instances.contains(this.previous)){this.activate(this.previous)}else{this._activeKB=this._instances[0]}}return this},trace:function(){a.trace(this)},each:function(f){a.each(this,f)},_instances:[],_disable:function(f){if(this._activeKB==f){this._activeKB=null}},_setup:function(){this.addEvents(this.options.events);if(a.manager&&!this._manager){a.manager.manage(this)}if(this.options.active){this.activate()}else{this.relinquish()}},_handle:function(h,g){if(h.preventKeyboardPropagation){return}var f=!!this._manager;if(f&&this._activeKB){this._activeKB._handle(h,g);if(h.preventKeyboardPropagation){return}}this.fireEvent(g,h);if(!f&&this._activeKB){this._activeKB._handle(h,g)}}});var b={};var c=["shift","control","alt","meta"];var e=/^(?:shift|control|ctrl|alt|meta)$/;a.parse=function(h,g,k){if(k&&k.contains(h.toLowerCase())){return h}h=h.toLowerCase().replace(/^(keyup|keydown):/,function(m,l){g=l;return""});if(!b[h]){var f,j={};h.split("+").each(function(l){if(e.test(l)){j[l]=true}else{f=l}});j.control=j.control||j.ctrl;var i=[];c.each(function(l){if(j[l]){i.push(l)}});if(f){i.push(f)}b[h]=i.join("+")}return g+":keys("+b[h]+")"};a.each=function(f,g){var h=f||a.manager;while(h){g.run(h);h=h._activeKB}};a.stop=function(f){f.preventKeyboardPropagation=true};a.manager=new a({active:true});a.trace=function(f){f=f||a.manager;var g=window.console&&console.log;if(g){console.log("the following items have focus: ")}a.each(f,function(h){if(g){console.log(document.id(h.widget)||h.wiget||h)}})};var d=function(g){var f=[];c.each(function(h){if(g[h]){f.push(h)}});if(!e.test(g.key)){f.push(g.key)}a.manager._handle(g,g.type+":keys("+f.join("+")+")")};document.addEvents({keyup:d,keydown:d})})();