var tString=0,tAnyOfString=1,tNotOfString=2,tBackRefString=3,tReference=4,tVKey=5,tAny=6,tSwitch=7;if(window.console)var log=function(){},error=function(){};else{log=function(){};error=function(){}}
var getElement=function(a){var b=null;if(typeof a!=="object"){b=document.getElementById(a);return typeof b!=="object"?null:b}else return a},addEventHandler=function(a,b,c){if(a.addEventListener)a.addEventListener(b,c,false);else a.attachEvent&&c.call?a.attachEvent("on"+b,function(){return c.call(a,window.event)}):error("Can't add event listner")},removeEventHandler=function(a,b,c){if(a.removeEventListener)a.removeEventListener(b,c,false);else a.detachEvent&&a.detachEvent("on"+b,c)};
JKME={enabledTags:["TEXTAREA","INPUT"],config:{showMenu:true,menuClassName:"jkMenu",logoUrl:"http://ayaronlineeditor.co.cc/keymagic.png"},attach:function(a){return new JKME.engine(a)},detach:function(a){a=getElement(a);try{engine=a.$;removeEventHandler(a,"keypress",engine.onkeypress);removeEventHandler(a,"keydown",engine.onkeydown);removeEventHandler(a,"mousemove",engine.onmousemove);removeEventHandler(a,"mouseout",engine.onmouseout);document.body.removeChild(engine.menu.el)}catch(b){error(b);return false}return true},switchLayout:function(a,
b){el=getElement(a);el!==null&&el.$&&el.$.switchLayout(b)}};if(!JKME.keyboard)JKME.keyboard={lastkb:window.KMKL,events:{keyboardchanged:[]},keyboards:{Default:{path:"",loaded:true,keyboard:{eat:false}}},addEventListener:function(a,b){this.events[a].push(b)},callEventListeners:function(a,b){for(var c in this.events[a])this.events[a][c](b)},addKeyboard:function(a,b,c){newKeyboard={path:a,loaded:false};this.keyboards[b]=newKeyboard;if(c!==undefined){this.keyboards[b].loaded=true;this.keyboards[b].keyboard=c}this.callEventListeners("keyboardchanged")},load:function(a,
b){var c=document.getElementsByTagName("body")[0];newjs=document.createElement("script");newjs.onreadystatechange=function(){if(newjs.readyState==="loaded"||newjs.readyState==="complete"){newjs.onreadystatechange=null;b("success")}};newjs.onerror=function(){if(newjs.readyState==="loaded"||newjs.readyState==="complete"){newjs.onreadystatechange=null;b("fail");c.removeChild(newjs)}};newjs.onload=function(){b("success")};newjs.onerror=function(){b("fail");c.removeChild(newjs)};newjs.src=a;newjs.type=
"text/javascript";c.appendChild(newjs)},loadKeyboard:function(a,b){var c=this.keyboards[a];loading=true;c.loaded==false?this.load(c.path,function(e){if(e==="fail"){c.error=true;error("Failed to load keyboard. Invalid keyboard path?");b("fail")}else if(this.lastkb==window.KMKL){c.error=true;error("Failed to load keyboard. Invalid keyboard file?");b("fail")}else{this.lastkb=window.KMKL;c.keyboard=window.KMKL;c.loaded=true;b("success")}JKME.keyboard.callEventListeners("keyboardchanged")}):b("success")},
createOption:function(a,b){if(b===undefined)b=true;var c=document.createElement("option");c.selected=b;c.defaultSelected=b;c.innerHTML=a;c.value=a;return c},getKeyboardsAsOptions:function(a){var b=document.createElement("select"),c=false,e;for(e in this.keyboards){var d=this.createOption(e,e==a);if(this.keyboards[e].error)d.className+="error ";if(c)d.className+="alt ";b.appendChild(d);c=!c}return b}};JKME.menu=function(a,b){this.el=document.createElement("div");this.el.className=JKME.config.menuClassName;this.el.id=b;this.el.style.display="none";this.el.$=a;this.timerId=null;imgDiv=document.createElement("div");log(imgDiv.style);imgDiv.style.backgroundImage="url("+JKME.config.logoUrl+")";imgDiv.style.width="20px";imgDiv.style.height="20px";imgDiv.style.margin="0 10px 0 5px";imgDiv.style.cssFloat="left";imgDiv.style.styleFloat="left";this.el.appendChild(imgDiv);selectDiv=document.createElement("div");
selectDiv.style.cssFloat="left";selectDiv.style.styleFloat="left";this.selectDiv=selectDiv;this.el.appendChild(selectDiv);msgDiv=document.createElement("div");msgDiv.style.position="absolute";msgDiv.style.top="0";msgDiv.style.right="0";this.msgDiv=msgDiv;this.el.appendChild(msgDiv);var c=this.el,e=this;c.onclick=function(){e.showMenu(c.parentNode)};c.onmouseout=function(d){log("menu > mouseout",d);e.hideMenu(1E3)};c.onmousemove=function(){log("menu > mousemove");e.showMenu(c.parentNode)}};
JKME.menu.prototype={onKeyboardSelectionChanged:function(){},options:null,updateMenu:function(){this.options=options=JKME.keyboard.getKeyboardsAsOptions(this.el.$.keyboardName);this.selectDiv.innerHTML="";this.selectDiv.appendChild(this.options);this.options.onchange=function(){menu=this.parentNode.parentNode.$.menu;menu.onKeyboardSelectionChanged(this.value)}},showMenu:function(a){log("menu > showMenu",this.timerId);if(JKME.config.showMenu!==false){if(this.timerId){clearTimeout(this.timerId);this.timerId=
null}for(var b=0;b<JKME.enabledTags.length;b++){tag=JKME.enabledTags[b];if(a.tagName==tag){offsetLeft=a.offsetLeft;offsetTop=a.offsetTop;if(a.offsetParent)for(offsetParentElement=a.offsetParent;offsetParentElement.offsetParent!=null;){offsetLeft+=offsetParentElement.offsetLeft;offsetTop+=offsetParentElement.offsetTop;offsetParentElement=offsetParentElement.offsetParent}this.el.style.left=offsetLeft+"px";if(this.el.currentStyle){styles=document.body.currentStyle;height=parseInt(styles.height)||0;marginT=
parseInt(styles.paddingTop)||0;marginB=parseInt(styles.paddingBottom)||0}else{styles=document.defaultView.getComputedStyle(this.el,"");height=parseInt(styles.getPropertyValue("height"));marginT=parseInt(styles.getPropertyValue("padding-top"));marginB=parseInt(styles.getPropertyValue("padding-bottom"))}this.el.style.top=offsetTop-height-marginT-marginB-(this.el.$.env.ie?20:10)+"px";this.el.style.display="block"}}}},hideMenu:function(a){log("menu > hideMenu");if(this.timerId){clearTimeout(this.timerId);
this.timerId=null}if(a===undefined)a=100;var b=this.el;this.timerId=setTimeout(function(){b.style.display="none"},a)},showMessage:function(a){this.msgDiv.innerHTML=a}};JKME.engine=function(a){this.element=getElement(a);if(this.element===null)error("Can't find the element.");else{this.element.$&&JKME.detach(this.element);this.element.$=this;this.menuId="jkMenu"+Math.floor(Math.random()*1E5);var b=this.menu=new JKME.menu(this,this.menuId);document.body.appendChild(b.el);b.onKeyboardSelectionChanged=function(f){this.el.$.switchLayout(f)};b.updateMenu();JKME.keyboard.addEventListener("keyboardchanged",function(){b.updateMenu()});var c=this;a=function(){b.showMenu(this);
b.hideMenu(1E3)};var e=function(){b.hideMenu(500)},d=function(f){return c.processKeyEvent(f)},g=function(f){if(f.keyCode==8&&(c.env.cs||c.env.ie))return c.processKeyEvent(f);else if(f.keyCode==32&&f.shiftKey){log(c.keyboardName,c.lastKeyboardName);if(!(c.keyboardName=="Default"&&c.lastKeyboardName!=null))c.lastKeyboardName="Default";ob=c.lastKeyboardName;c.lastKeyboardName=c.keyboardName;c.keyboardName=ob;c.keyboard=JKME.keyboard.keyboards[c.keyboardName].keyboard;c.reset();c.menu.updateMenu();b.showMenu(f.srcElement||
f.target);b.hideMenu(1E3);f.preventDefault&&f.preventDefault();return false}};this.onmousemove=a;this.onmouseout=e;this.onkeypress=d;this.onkeydown=g;addEventHandler(this.element,"mousemove",a);addEventHandler(this.element,"mouseout",e);addEventHandler(this.element,"keypress",d);addEventHandler(this.element,"keydown",g)}};
JKME.engine.prototype={keyboardName:"Default",keyboard:JKME.keyboard.keyboards.Default.keyboard,lastKeyboardName:null,contextHistory:[],backRef:[],switches:{},hotkey:{},context:"",matchedVK:false,shouldMatchAgain:false,env:{cs:/chrome|safari/i.test(navigator.userAgent),ie:/MSIE/i.test(navigator.userAgent),opera:/opera/i.test(navigator.userAgent)},onSwitchedLayout:function(){},switchLayout:function(a){var b=this;b.menu.showMessage("Loading");JKME.keyboard.keyboards[a]&&JKME.keyboard.loadKeyboard(a,
function(c){log(c);if(c==="success"){b.menu.showMessage("");b.keyboardName=a;b.keyboard=JKME.keyboard.keyboards[a].keyboard;b.reset();b.onSwitchedLayout(b,a);b.menu.updateMenu()}else{b.menu.showMessage("Failed to load");setTimeout(function(){b.menu.showMessage("")},2E3)}})},reset:function(){this.context="";this.switches={};this.contextHistory=[]},commit:function(a,b){log("commit",this.context,b);scrollTop=a.scrollHeight-a.scrollTop;scrollLeft=a.scrollWidth-a.scrollLeft;a.value=this.context+b;a.scrollTop=
a.scrollHeight-scrollTop;a.scrollLeft=a.scrollWidth-scrollLeft;if(b===undefined||b.length==0){if(this.env.ie){textInputRange=a.createTextRange();textInputRange.collapse(false);textInputRange.select()}}else if(b!=""&&this.env.ie){textInputRange=a.createTextRange();textInputRange.collapse(false);fix=b.split("\n").length-1;textInputRange.moveStart("character",-b.length+fix);textInputRange.moveEnd("character",-b.length+fix);textInputRange.select()}else if(b!=""||this.env.opera){a.selectionStart=this.context.length;
a.selectionEnd=this.context.length}},getInputSelection:function(a){var b=0,c=0,e,d,g;if(typeof a.selectionStart=="number"&&typeof a.selectionEnd=="number"){b=a.selectionStart;c=a.selectionEnd}else if(document.selection!=undefined)if((d=document.selection.createRange())&&d.parentElement()==a){g=a.value.length;e=a.value.replace(/\r\n/g,"\n");c=a.createTextRange();c.moveToBookmark(d.getBookmark());a=a.createTextRange();a.collapse(false);if(c.compareEndPoints("StartToEnd",a)>-1)b=c=g;else{b=-c.moveStart("character",
-g);b+=e.slice(0,b).split("\n").length-1;if(c.compareEndPoints("EndToEnd",a)>-1)c=g;else{c=-c.moveEnd("character",-g);c+=e.slice(0,c).split("\n").length-1}}}return{start:b,end:c}}};JKME.engine.prototype.processKeyEvent=function(a){log("processKeyEvent",a,this);if(this.env.opera)if(a.keyCode>=33&&a.keyCode<=40)return;if(this.on!==false){var b=this.env.ie?a.srcElement:a.target,c=this.getInputSelection(b),e=b.value.substr(0,c.start);c=b.value.substr(c.end,b.value.length);if(this.context!=e){this.reset();this.context=e}e=this.context;var d=this.getKeyCode(a),g={shift:d.shift,alt:a.altKey,ctrl:a.ctrlKey,cmd:a.metaKey==undefined?false:a.metaKey},f=this.processInput(d.keycode,String.fromCharCode(this.env.ie|
this.env.opera?a.keyCode:a.charCode),g);if(!f&&d.keycode)this.switches={};d=0;if(f){for(;f;)if(this.shouldMatchAgain){g={shift:false,alt:false,ctrl:false,cmd:false};f=this.processInput(0,"",g);d++;if(d==20){error("Max loop exceeded.");return}if(f){if(a.keyCode==8&&this.contextHistory.length)if(this.context==this.contextHistory[this.contextHistory.length])this.contextHistory.pop();else this.contextHistory=[]}else{this.updateHistory(e);break}}else{if(a.keyCode==8&&this.contextHistory.length)if(this.context==
this.contextHistory[this.contextHistory.length])mthis.contextHistory.pop();else this.contextHistory=[];else this.updateHistory(e);break}this.commit(b,c);a.preventDefault&&a.preventDefault();return false}else if(!(g.alt||g.ctrl||g.cmd)){if(a.keyCode==8){if(this.keyboard.autobksp==true&&this.contextHistory.length!=0){this.context=this.contextHistory[this.contextHistory.length-1];this.contextHistory.pop();this.commit(b,c);a.preventDefault&&a.preventDefault();return false}else if(this.context.length){this.context=
this.context.substr(0,this.context.length-1);this.contextHistory=[]}return true}if(!(a.charCode<=32||a.keyCode<=32))if(this.keyboard.eat){log("eaten");a.preventDefault&&a.preventDefault();return false}else{log("not eaten");this.context+=String.fromCharCode(this.env.ie|this.env.opera?a.keyCode:a.charCode);this.updateHistory(e)}}}};JKME.engine.prototype.updateHistory=function(a){if(this.contextHistory.length==0)this.contextHistory.push(a);else a!=this.contextHistory[this.contextHistory.length-1]&&this.contextHistory.push(a)};
JKME.engine.prototype.getKeyCode=function(a){var b=false,c=this.env.ie|this.env.opera?a.keyCode:a.charCode;log("getKeyCode",0,b);if(c>=65&&c<=90){b=true;a=c}else if(c>=97&&c<=122)a=c-32;else if(c>=48&&c<=57)a=c;else switch(c){case 96:a=192;break;case 126:a=192;b=true;break;case 33:a=49;b=true;break;case 64:a=50;b=true;break;case 35:a=51;b=true;break;case 36:a=52;b=true;break;case 37:a=53;b=true;break;case 94:a=54;b=true;break;case 38:a=55;b=true;break;case 42:a=56;b=true;break;case 40:a=57;b=true;
break;case 41:a=48;b=true;break;case 45:a=189;break;case 95:a=189;b=true;break;case 61:a=187;break;case 43:a=187;b=true;break;case 91:a=219;break;case 123:a=219;b=true;break;case 93:a=221;break;case 125:a=221;b=true;break;case 92:a=220;break;case 124:a=220;b=true;break;case 59:a=186;break;case 58:a=186;b=true;break;case 39:a=222;break;case 34:a=222;b=true;break;case 44:a=188;break;case 60:a=188;b=true;break;case 46:a=190;break;case 62:a=190;b=true;break;case 47:a=191;break;case 63:a=191;b=true;break;
default:a=a.keyCode}log("getKeyCode",a,b);return{keycode:a,shift:b}};JKME.engine.prototype.processInput=function(a,b,c){log("processInput",a,b,c);for(var e in this.keyboard.rules){var d=this.keyboard.rules[e];if(this.matchRule(d,a,b,c)){log("matchRule=true",d);if(this.matchedVK===false)this.context+=b;if(a)this.switches={};a=this.processOutput(d);log("processOutput="+a,this.context);return a}}return false};
JKME.engine.prototype.processOutput=function(a){var b=a.length,c="",e;for(itemIdx in a.rhs){var d=a.rhs[itemIdx];switch(d.type){case tString:c+=d.value;break;case tBackRefString:if(this.backRef.length<=d.index)continue;e=a.lhs[d.index].value.indexOf(this.backRef[d.index]);if(e!=-1)c+=d.value.charAt(e);break;case tReference:if(this.backRef.length<=d.index)continue;c+=this.backRef[d.index];break;case tVKey:c+=String.fromCharCode(d.keyCode);break;case tSwitch:log("switch"+d.switchId);this.switches[d.switchId]=
this.switches[d.switchId]!==undefined?!this.switches[d.switchId]:true}}log(this,this.context,c);this.context=this.context.substr(0,this.context.length-b);this.context+=c;this.shouldMatchAgain=c.length==0||c.length==1&&c.charCodeAt(0)>32&&c.charCodeAt(0)<127?false:true;return true};
JKME.engine.prototype.matchKeyStates=function(a,b,c){var e=0;b={shift:b.shift,ctrl:b.ctrl,alt:b.alt,cmd:b.cmd};for(var d in c){var g=c[d];if(g.type==tVKey){switch(g.keyCode){case 16:if(b.shift==true)b.shift=false;else return-1;break;case 17:if(b.ctrl==true)b.ctrl=false;else return-1;break;case 18:if(b.alt==true)b.alt=false;else return-1;break;default:if(g.keyCode!=a)return-1}e++}}if(b.ctrl==true||b.alt==true||b.cmd==true)return-1;return e};
JKME.engine.prototype.matchRule=function(a,b,c,e){var d=this.context;this.matchedVK=false;b=this.matchKeyStates(b,e,a.lhs);if(b==-1)return false;else if(b==0){if(c!="")d+=c}else this.matchedVK=true;c=a.length;b=d.length;if(c>b)return false;d=d.substr(b-c);c=0;this.backRef=[];for(var g in a.lhs){b=a.lhs[g];e=d.charAt(c);switch(b.type){case tString:for(var f=0;f<b.value.length;f++){var h=b.value.charAt(f);if(c==d.length)return false;if(h!=e)return false;e=d.charAt(++c)}this.backRef.push(b.value);break;
case tAnyOfString:if(b.value.indexOf(e)==-1)return false;else{this.backRef.push(e);c++}break;case tNotOfString:if(b.value.indexOf(e)!=-1)return false;else{this.backRef.push(e);c++}break;case tAny:h=d.charAt(c++);if(h>="!"&&h<="}"||h>="\u00ff"&&h<"\uffff")c++;else return false;this.backRef.push(h);break;case tSwitch:if(this.switches[b.switchId]!==undefined){if(!this.switches[b.switchId])return false}else return false}}return true};

/*
 * jKeyMagic
 * http://code.google.com/p/keymagic
 *
 * Copyright 2011, Thant Thet Khin Zaw
 * Licensed under GPL Version 2 license.
 *
 */

