(function(){function a(v,w){if(CKEDITOR.env.ie)v.removeAttribute(w);else delete v[w];};var b=/^(?:td|th)$/;function c(v){var w=v.createBookmarks(),x=v.getRanges(),y=[],z={};function A(I){if(y.length>0)return;if(I.type==CKEDITOR.NODE_ELEMENT&&b.test(I.getName())&&!I.getCustomData('selected_cell')){CKEDITOR.dom.element.setMarker(z,I,'selected_cell',true);y.push(I);}};for(var B=0;B<x.length;B++){var C=x[B];if(C.collapsed){var D=C.getCommonAncestor(),E=D.getAscendant('td',true)||D.getAscendant('th',true);if(E)y.push(E);}else{var F=new CKEDITOR.dom.walker(C),G;F.guard=A;while(G=F.next()){var H=G.getParent();if(H&&b.test(H.getName())&&!H.getCustomData('selected_cell')){CKEDITOR.dom.element.setMarker(z,H,'selected_cell',true);y.push(H);}}}}CKEDITOR.dom.element.clearAllMarkers(z);v.selectBookmarks(w);return y;};function d(v){var w=0,x=v.length-1,y={},z,A,B;while(z=v[w++])CKEDITOR.dom.element.setMarker(y,z,'delete_cell',true);w=0;while(z=v[w++]){if((A=z.getPrevious())&&!A.getCustomData('delete_cell')||(A=z.getNext())&&!A.getCustomData('delete_cell')){CKEDITOR.dom.element.clearAllMarkers(y);return A;}}CKEDITOR.dom.element.clearAllMarkers(y);B=v[0].getParent();if(B=B.getPrevious())return B.getLast();B=v[x].getParent();if(B=B.getNext())return B.getChild(0);return null;};function e(v){var w=v.cells;for(var x=0;x<w.length;x++){w[x].innerHTML='';if(!CKEDITOR.env.ie)new CKEDITOR.dom.element(w[x]).appendBogus();}};function f(v,w){var x=v.getStartElement().getAscendant('tr');if(!x)return;var y=x.clone(true);y.insertBefore(x);e(w?y.$:x.$);};function g(v){if(v instanceof CKEDITOR.dom.selection){var w=c(v),x=w.length,y=[],z,A,B;for(var C=0;C<x;C++){var D=w[C].getParent(),E=D.$.rowIndex;!C&&(A=E-1);y[E]=D;C==x-1&&(B=E+1);}var F=D.getAscendant('table'),G=F.$.rows,H=G.length;z=new CKEDITOR.dom.element(B<H&&F.$.rows[B]||A>0&&F.$.rows[A]||F.$.parentNode);for(C=y.length;C>=0;C--){if(y[C])g(y[C]);}return z;}else if(v instanceof CKEDITOR.dom.element){F=v.getAscendant('table');if(F.$.rows.length==1)F.remove();else v.remove();}return 0;};function h(v,w){var x=v.getStartElement(),y=x.getAscendant('td',true)||x.getAscendant('th',true);if(!y)return;var z=y.getAscendant('table'),A=y.$.cellIndex;for(var B=0;B<z.$.rows.length;B++){var C=z.$.rows[B];if(C.cells.length<A+1)continue;y=new CKEDITOR.dom.element(C.cells[A].cloneNode(false));if(!CKEDITOR.env.ie)y.appendBogus();var D=new CKEDITOR.dom.element(C.cells[A]);if(w)y.insertBefore(D);else y.insertAfter(D);}};function i(v){var w=[],x=v[0]&&v[0].getAscendant('table'),y,z,A,B;
for(y=0,z=v.length;y<z;y++)w.push(v[y].$.cellIndex);w.sort();for(y=1,z=w.length;y<z;y++){if(w[y]-w[y-1]>1){A=w[y-1]+1;break;}}if(!A)A=w[0]>0?w[0]-1:w[w.length-1]+1;var C=x.$.rows;for(y=0,z=C.length;y<z;y++){B=C[y].cells[A];if(B)break;}return B?new CKEDITOR.dom.element(B):x.getPrevious();};function j(v){if(v instanceof CKEDITOR.dom.selection){var w=c(v),x=i(w);for(var y=w.length-1;y>=0;y--){if(w[y])j(w[y]);}return x;}else if(v instanceof CKEDITOR.dom.element){var z=v.getAscendant('table');if(!z)return null;var A=v.$.cellIndex;for(y=z.$.rows.length-1;y>=0;y--){var B=new CKEDITOR.dom.element(z.$.rows[y]);if(!A&&B.$.cells.length==1){g(B);continue;}if(B.$.cells[A])B.$.removeChild(B.$.cells[A]);}}return null;};function k(v,w){var x=v.getStartElement(),y=x.getAscendant('td',true)||x.getAscendant('th',true);if(!y)return;var z=y.clone();if(!CKEDITOR.env.ie)z.appendBogus();if(w)z.insertBefore(y);else z.insertAfter(y);};function l(v){if(v instanceof CKEDITOR.dom.selection){var w=c(v),x=w[0]&&w[0].getAscendant('table'),y=d(w);for(var z=w.length-1;z>=0;z--)l(w[z]);if(y)n(y,true);else if(x)x.remove();}else if(v instanceof CKEDITOR.dom.element){var A=v.getParent();if(A.getChildCount()==1)A.remove();else v.remove();}};function m(v){var w=v.getBogus();w&&w.remove();v.trim();};function n(v,w){var x=new CKEDITOR.dom.range(v.getDocument());if(!x['moveToElementEdit'+(w?'End':'Start')](v)){x.selectNodeContents(v);x.collapse(w?false:true);}x.select(true);};function o(v){var w=v.$.rows,x=-1,y=[];for(var z=0;z<w.length;z++){x++;!y[x]&&(y[x]=[]);var A=-1;for(var B=0;B<w[z].cells.length;B++){var C=w[z].cells[B];A++;while(y[x][A])A++;var D=isNaN(C.colSpan)?1:C.colSpan,E=isNaN(C.rowSpan)?1:C.rowSpan;for(var F=0;F<E;F++){if(!y[x+F])y[x+F]=[];for(var G=0;G<D;G++)y[x+F][A+G]=w[z].cells[B];}A+=D-1;}}return y;};function p(v,w,x){var y=v[w];if(typeof x=='undefined')return y;for(var z=0;y&&z<y.length;z++){if(x.is&&y[z]==x.$)return z;else if(z==x)return new CKEDITOR.dom.element(y[z]);}return x.is?-1:null;};function q(v,w,x){var y=[];for(var z=0;z<v.length;z++){var A=v[z];if(typeof x=='undefined')y.push(A[w]);else if(x.is&&A[w]==x.$)return z;else if(z==x)return new CKEDITOR.dom.element(A[w]);}return typeof x=='undefined'?y:x.is?-1:null;};function r(v,w,x){var y=c(v),z;if((w?y.length!=1:y.length<2)||(z=v.getCommonAncestor())&&z.type==CKEDITOR.NODE_ELEMENT&&z.is('table'))return false;var A,B=y[0],C=B.getAscendant('table'),D=o(C),E=D.length,F=D[0].length,G=B.getParent().$.rowIndex,H=p(D,G,B);if(w){var I;
try{I=D[w=='up'?G-1:w=='down'?G+1:G][w=='left'?H-1:w=='right'?H+1:H];}catch(aa){return false;}if(!I||B.$==I)return false;y[w=='up'||w=='left'?'unshift':'push'](new CKEDITOR.dom.element(I));}var J=B.getDocument(),K=G,L=0,M=0,N=!x&&new CKEDITOR.dom.documentFragment(J),O=0;for(var P=0;P<y.length;P++){A=y[P];var Q=A.getParent(),R=A.getFirst(),S=A.$.colSpan,T=A.$.rowSpan,U=Q.$.rowIndex,V=p(D,U,A);O+=S*T;M=Math.max(M,V-H+S);L=Math.max(L,U-G+T);if(!x){if(m(A),A.getChildren().count()){if(U!=K&&R&&!(R.isBlockBoundary&&R.isBlockBoundary({br:1}))){var W=N.getLast(CKEDITOR.dom.walker.whitespaces(true));if(W&&!(W.is&&W.is('br')))N.append(new CKEDITOR.dom.element('br'));}A.moveChildren(N);}P?A.remove():A.setHtml('');}K=U;}if(!x){N.moveChildren(B);if(!CKEDITOR.env.ie)B.appendBogus();if(M>=F)B.removeAttribute('rowSpan');else B.$.rowSpan=L;if(L>=E)B.removeAttribute('colSpan');else B.$.colSpan=M;var X=new CKEDITOR.dom.nodeList(C.$.rows),Y=X.count();for(P=Y-1;P>=0;P--){var Z=X.getItem(P);if(!Z.$.cells.length){Z.remove();Y++;continue;}}return B;}else return L*M==O;};function s(v,w){var x=c(v);if(x.length>1)return false;else if(w)return true;var y=x[0],z=y.getParent(),A=z.getAscendant('table'),B=o(A),C=z.$.rowIndex,D=p(B,C,y),E=y.$.rowSpan,F,G,H,I;if(E>1){G=Math.ceil(E/2);H=Math.floor(E/2);I=C+G;var J=new CKEDITOR.dom.element(A.$.rows[I]),K=p(B,I),L;F=y.clone();for(var M=0;M<K.length;M++){L=K[M];if(L.parentNode==J.$&&M>D){F.insertBefore(new CKEDITOR.dom.element(L));break;}else L=null;}if(!L)J.append(F,true);}else{H=G=1;J=z.clone();J.insertAfter(z);J.append(F=y.clone());var N=p(B,C);for(var O=0;O<N.length;O++)N[O].rowSpan++;}if(!CKEDITOR.env.ie)F.appendBogus();y.$.rowSpan=G;F.$.rowSpan=H;if(G==1)y.removeAttribute('rowSpan');if(H==1)F.removeAttribute('rowSpan');return F;};function t(v,w){var x=c(v);if(x.length>1)return false;else if(w)return true;var y=x[0],z=y.getParent(),A=z.getAscendant('table'),B=o(A),C=z.$.rowIndex,D=p(B,C,y),E=y.$.colSpan,F,G,H;if(E>1){G=Math.ceil(E/2);H=Math.floor(E/2);}else{H=G=1;var I=q(B,D);for(var J=0;J<I.length;J++)I[J].colSpan++;}F=y.clone();F.insertAfter(y);if(!CKEDITOR.env.ie)F.appendBogus();y.$.colSpan=G;F.$.colSpan=H;if(G==1)y.removeAttribute('colSpan');if(H==1)F.removeAttribute('colSpan');return F;};var u={thead:1,tbody:1,tfoot:1,td:1,tr:1,th:1};CKEDITOR.plugins.tabletools={init:function(v){var w=v.lang.table;v.addCommand('cellProperties',new CKEDITOR.dialogCommand('cellProperties'));CKEDITOR.dialog.add('cellProperties',this.path+'dialogs/tableCell.js');
v.addCommand('tableDelete',{exec:function(x){var y=x.getSelection(),z=y&&y.getStartElement(),A=z&&z.getAscendant('table',true);if(!A)return;y.selectElement(A);var B=y.getRanges()[0];B.collapse();y.selectRanges([B]);var C=A.getParent();if(C.getChildCount()==1&&C.getName()!='body')C.remove();else A.remove();}});v.addCommand('rowDelete',{exec:function(x){var y=x.getSelection();n(g(y));}});v.addCommand('rowInsertBefore',{exec:function(x){var y=x.getSelection();f(y,true);}});v.addCommand('rowInsertAfter',{exec:function(x){var y=x.getSelection();f(y);}});v.addCommand('columnDelete',{exec:function(x){var y=x.getSelection(),z=j(y);z&&n(z,true);}});v.addCommand('columnInsertBefore',{exec:function(x){var y=x.getSelection();h(y,true);}});v.addCommand('columnInsertAfter',{exec:function(x){var y=x.getSelection();h(y);}});v.addCommand('cellDelete',{exec:function(x){var y=x.getSelection();l(y);}});v.addCommand('cellMerge',{exec:function(x){n(r(x.getSelection()),true);}});v.addCommand('cellMergeRight',{exec:function(x){n(r(x.getSelection(),'right'),true);}});v.addCommand('cellMergeDown',{exec:function(x){n(r(x.getSelection(),'down'),true);}});v.addCommand('cellVerticalSplit',{exec:function(x){n(s(x.getSelection()));}});v.addCommand('cellHorizontalSplit',{exec:function(x){n(t(x.getSelection()));}});v.addCommand('cellInsertBefore',{exec:function(x){var y=x.getSelection();k(y,true);}});v.addCommand('cellInsertAfter',{exec:function(x){var y=x.getSelection();k(y);}});if(v.addMenuItems)v.addMenuItems({tablecell:{label:w.cell.menu,group:'tablecell',order:1,getItems:function(){var x=v.getSelection(),y=c(x);return{tablecell_insertBefore:CKEDITOR.TRISTATE_OFF,tablecell_insertAfter:CKEDITOR.TRISTATE_OFF,tablecell_delete:CKEDITOR.TRISTATE_OFF,tablecell_merge:r(x,null,true)?CKEDITOR.TRISTATE_OFF:CKEDITOR.TRISTATE_DISABLED,tablecell_merge_right:r(x,'right',true)?CKEDITOR.TRISTATE_OFF:CKEDITOR.TRISTATE_DISABLED,tablecell_merge_down:r(x,'down',true)?CKEDITOR.TRISTATE_OFF:CKEDITOR.TRISTATE_DISABLED,tablecell_split_vertical:s(x,true)?CKEDITOR.TRISTATE_OFF:CKEDITOR.TRISTATE_DISABLED,tablecell_split_horizontal:t(x,true)?CKEDITOR.TRISTATE_OFF:CKEDITOR.TRISTATE_DISABLED,tablecell_properties:y.length>0?CKEDITOR.TRISTATE_OFF:CKEDITOR.TRISTATE_DISABLED};}},tablecell_insertBefore:{label:w.cell.insertBefore,group:'tablecell',command:'cellInsertBefore',order:5},tablecell_insertAfter:{label:w.cell.insertAfter,group:'tablecell',command:'cellInsertAfter',order:10},tablecell_delete:{label:w.cell.deleteCell,group:'tablecell',command:'cellDelete',order:15},tablecell_merge:{label:w.cell.merge,group:'tablecell',command:'cellMerge',order:16},tablecell_merge_right:{label:w.cell.mergeRight,group:'tablecell',command:'cellMergeRight',order:17},tablecell_merge_down:{label:w.cell.mergeDown,group:'tablecell',command:'cellMergeDown',order:18},tablecell_split_horizontal:{label:w.cell.splitHorizontal,group:'tablecell',command:'cellHorizontalSplit',order:19},tablecell_split_vertical:{label:w.cell.splitVertical,group:'tablecell',command:'cellVerticalSplit',order:20},tablecell_properties:{label:w.cell.title,group:'tablecellproperties',command:'cellProperties',order:21},tablerow:{label:w.row.menu,group:'tablerow',order:1,getItems:function(){return{tablerow_insertBefore:CKEDITOR.TRISTATE_OFF,tablerow_insertAfter:CKEDITOR.TRISTATE_OFF,tablerow_delete:CKEDITOR.TRISTATE_OFF};
}},tablerow_insertBefore:{label:w.row.insertBefore,group:'tablerow',command:'rowInsertBefore',order:5},tablerow_insertAfter:{label:w.row.insertAfter,group:'tablerow',command:'rowInsertAfter',order:10},tablerow_delete:{label:w.row.deleteRow,group:'tablerow',command:'rowDelete',order:15},tablecolumn:{label:w.column.menu,group:'tablecolumn',order:1,getItems:function(){return{tablecolumn_insertBefore:CKEDITOR.TRISTATE_OFF,tablecolumn_insertAfter:CKEDITOR.TRISTATE_OFF,tablecolumn_delete:CKEDITOR.TRISTATE_OFF};}},tablecolumn_insertBefore:{label:w.column.insertBefore,group:'tablecolumn',command:'columnInsertBefore',order:5},tablecolumn_insertAfter:{label:w.column.insertAfter,group:'tablecolumn',command:'columnInsertAfter',order:10},tablecolumn_delete:{label:w.column.deleteColumn,group:'tablecolumn',command:'columnDelete',order:15}});if(v.contextMenu)v.contextMenu.addListener(function(x,y){if(!x)return null;while(x){if(x.getName() in u)return{tablecell:CKEDITOR.TRISTATE_OFF,tablerow:CKEDITOR.TRISTATE_OFF,tablecolumn:CKEDITOR.TRISTATE_OFF};x=x.getParent();}return null;});},getSelectedCells:c};CKEDITOR.plugins.add('tabletools',CKEDITOR.plugins.tabletools);})();
