(function(){function a(f,g){var h=g.block||g.blockLimit;if(!h||h.getName()=='body')return CKEDITOR.TRISTATE_OFF;return b(h,f.config.useComputedState)==this.value?CKEDITOR.TRISTATE_ON:CKEDITOR.TRISTATE_OFF;};function b(f,g){g=g===undefined||g;var h;if(g)h=f.getComputedStyle('text-align');else{while(!f.hasAttribute||!(f.hasAttribute('align')||f.getStyle('text-align'))){var i=f.getParent();if(!i)break;f=i;}h=f.getStyle('text-align')||f.getAttribute('align')||'';}h&&(h=h.replace(/-moz-|-webkit-|start|auto/i,''));!h&&g&&(h=f.getComputedStyle('direction')=='rtl'?'right':'left');return h;};function c(f){if(f.editor.readOnly)return;var g=f.editor.getCommand(this.name);g.state=a.call(this,f.editor,f.data.path);g.fire('state');};function d(f,g,h){var j=this;j.name=g;j.value=h;var i=f.config.justifyClasses;if(i){switch(h){case 'left':j.cssClassName=i[0];break;case 'center':j.cssClassName=i[1];break;case 'right':j.cssClassName=i[2];break;case 'justify':j.cssClassName=i[3];break;}j.cssClassRegex=new RegExp('(?:^|\\s+)(?:'+i.join('|')+')(?=$|\\s)');}};function e(f){var g=f.editor,h=new CKEDITOR.dom.range(g.document);h.setStartBefore(f.data.node);h.setEndAfter(f.data.node);var i=new CKEDITOR.dom.walker(h),j;while(j=i.next()){if(j.type==CKEDITOR.NODE_ELEMENT){if(!j.equals(f.data.node)&&j.getDirection()){h.setStartAfter(j);i=new CKEDITOR.dom.walker(h);continue;}var k=g.config.justifyClasses;if(k)if(j.hasClass(k[0])){j.removeClass(k[0]);j.addClass(k[2]);}else if(j.hasClass(k[2])){j.removeClass(k[2]);j.addClass(k[0]);}var l='text-align',m=j.getStyle(l);if(m=='left')j.setStyle(l,'right');else if(m=='right')j.setStyle(l,'left');}}};d.prototype={exec:function(f){var r=this;var g=f.getSelection(),h=f.config.enterMode;if(!g)return;var i=g.createBookmarks(),j=g.getRanges(true),k=r.cssClassName,l,m,n=f.config.useComputedState;n=n===undefined||n;for(var o=j.length-1;o>=0;o--){l=j[o].createIterator();l.enlargeBr=h!=CKEDITOR.ENTER_BR;while(m=l.getNextParagraph(h==CKEDITOR.ENTER_P?'p':'div')){m.removeAttribute('align');m.removeStyle('text-align');var p=k&&(m.$.className=CKEDITOR.tools.ltrim(m.$.className.replace(r.cssClassRegex,''))),q=r.state==CKEDITOR.TRISTATE_OFF&&(!n||b(m,true)!=r.value);if(k){if(q)m.addClass(k);else if(!p)m.removeAttribute('class');}else if(q){m.setStyle('text-align',r.value);m.setAttribute('align',r.value);}}}f.focus();f.forceNextSelectionCheck();g.selectBookmarks(i);}};CKEDITOR.plugins.add('justify',{init:function(f){var g=new d(f,'justifyleft','left'),h=new d(f,'justifycenter','center'),i=new d(f,'justifyright','right'),j=new d(f,'justifyblock','justify');
f.addCommand('justifyleft',g);f.addCommand('justifycenter',h);f.addCommand('justifyright',i);f.addCommand('justifyblock',j);f.ui.addButton('JustifyLeft',{label:f.lang.justify.left,command:'justifyleft'});f.ui.addButton('JustifyCenter',{label:f.lang.justify.center,command:'justifycenter'});f.ui.addButton('JustifyRight',{label:f.lang.justify.right,command:'justifyright'});f.ui.addButton('JustifyBlock',{label:f.lang.justify.block,command:'justifyblock'});f.on('selectionChange',CKEDITOR.tools.bind(c,g));f.on('selectionChange',CKEDITOR.tools.bind(c,i));f.on('selectionChange',CKEDITOR.tools.bind(c,h));f.on('selectionChange',CKEDITOR.tools.bind(c,j));f.on('dirChanged',e);},requires:['domiterator']});})();
