(function(a){a.plugins.Emoticons=function(e){var k=a.Event,i,g,c,b,m,j,l,d;i=a.each;g=a.extend;c=a.isIE;b=a.isGecko;l=a.DOM;this.settings=m=g({emoticons:{happy:[":)"],neutral:[":|"],sad:[":("],grin:[":D"],surprised:[":O"],wink:[";)"],devil:["}:)"],tongue:[":P"],lol:[':@'],mad:[':['],shocked:['8|'],cool:['B]']},row_length:4,trans_img:a.baseURL+"plugins/emoticons/img/trans.gif",skip_css:0,auto_convert:1},e.settings.emoticons);if(!m.skip_css){l.loadCSS(a.baseURL+"/plugins/emoticons/css/editor.css")}j="";i(m.emoticons,function(h){i(h,function(n){if(j.length!=0){j+="|"}j+=n.replace(/([^a-zA-Z0-9])/g,"\\$1")})});j=new RegExp(j,"g");g(e.commands,{mceEmoticons:function(B,A,y){var q,C=this,h=e.settings.id,o=l.getPos(y.target),z,w;if(e.hideMenu){return e.hideMenu()}function x(n){e.hideMenu=null;k.remove(document,"click",x);k.remove(e.getDoc(),"click",x);l.get(h+"_memoticons").style.display="none";return 1}q=l.get(h+"_memoticons");if(!q){q=l.get(h+"_t");q=l.add(document.body,"div",{id:h+"_memoticons","class":"punymce_emoticons punymce"});q=l.add(q,"table",{"class":"punymce"});q=l.add(q,"tbody");z=m.row_length;i(m.emoticons,function(p,n){if(z==m.row_length){r=l.add(q,"tr");z=0}z++;k.add(l.add(l.add(r,"td"),"a",{href:"#","class":"emoticon "+n}),"mousedown",function(t){x.call(C);e.selection.setNode(e.dom.create("img",{title:p[0]||n,src:m.trans_img,"class":"emoticon "+n}));return k.cancel(t)})})}k.add(document,"click",x,C);k.add(e.getDoc(),"click",x,C);e.hideMenu=x;s=l.get(h+"_memoticons").style;s.left=o.x+"px";s.top=(o.y+y.target.clientHeight+2)+"px";s.display="block"}});function f(h){var n;i(m.emoticons,function(p,o){i(p,function(q){if(q==h){n=o;return false}});return !n});return n}e.onPreProcess.add(function(p,q){var n=q.node.getElementsByTagName("img"),h=[];i(n,function(o){h.push(o)});i(h,function(t){var o=e.dom.getAttr(t,"class");if(o&&o.indexOf("emoticon")!=-1){t.parentNode.replaceChild(e.getDoc().createTextNode(t.getAttribute("title")),t)}})});e.onSetContent.add(function(n,p){var h=[];d=p.content.replace(/(<\/?[^>]+>|:\/\/)/g,function(o){return o.replace(j,function(q){var t=f(q);if(t){h.push(q);return"¤"+h.length+"¤"}return q})});d=d.replace(j,function(o){return''});d=d.replace(/¤([^¤]+)¤/g,function(q,o){return h[parseInt(o)-1]});p.content=d});e.onInit.add(function(){var h=e.dom;if(!m.skip_css){h.loadCSS(a.baseURL+"/plugins/emoticons/css/content.css")}k.add(e.getDoc(),"controlselect",function(n){if(h.getAttr(n.target,"class").indexOf("emoticon")!=-1){return k.cancel(n)}});if(b){k.add(e.getDoc(),"mousedown",function(n){if(h.getAttr(n.target,"class").indexOf("emoticon")!=-1){e.getDoc().execCommand("enableObjectResizing",false,false);return k.cancel(n)}else{e.getDoc().execCommand("enableObjectResizing",false,true)}});k.add(e.getDoc(),"keydown",function(u){var y=e.selection,v=y.getSel(),w=e.getDoc(),o=y.getRng(),x,p,q,t;x=o.startContainer;p=o.startOffset;if(x.nodeType==1){return}if(x){if(u.keyCode==39&&p==x.nodeValue.length){q=x.nextSibling;if(q&&q.nodeName=="IMG"){q=q.nextSibling;o=w.createRange();o.setStart(q,0);o.setEnd(q,0);v.removeAllRanges();v.addRange(o);return k.cancel(u)}}if(u.keyCode==37&&p==0){q=x.previousSibling;if(q&&q.nodeName=="IMG"){q=q.previousSibling;t=q.nodeValue.length;o=w.createRange();o.setStart(q,t);o.setEnd(q,t);v.removeAllRanges();v.addRange(o);return k.cancel(u)}}}})}k.add(e.getDoc(),"keypress",function(t){var p,x,o,w,v=e.getDoc(),u=e.selection,y=u.getSel(),n=u.getRng();function q(C){var B,A,z;if(!c){B=n.startContainer;A=n.startOffset;z=n.endOffset;if(A>0&&B.nodeType==3){return B.nodeValue.substring(Math.max(0,A+C),z)}}else{n=n.duplicate();n.moveStart("character",C);return n.text}}if(a.isOldWebKit||!m.auto_convert){return true}if(/(ttp|ftp):/i.test(q(-4))){return}o=q(-1);p=o+String.fromCharCode(t.charCode||t.keyCode);j.lastIndex=0;if(!o||!j.test(p)||w=="/"){return}if(x=f(p)){if(!c){n.setStart(n.startContainer,n.startOffset-1);y.removeAllRanges();y.addRange(n)}else{n=e.selection.getRng();n.moveStart("character",-1);n.select()}e.selection.setNode(h.create("img",{id:"emoticon",title:p,src:m.trans_img,"class":"emoticon "+x}));p=h.get("emoticon");h.setAttr(p,"id","");e.selection.select(p);e.selection.collapse(0);return k.cancel(t)}})});g(e.tools,{emoticons:{cmd:"mceEmoticons",title:a.I18n.emoticons}})};a.extend(a.I18n,{emoticons:"Insert emoticon"})})(punymce);