/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

(function(){var a=CKEDITOR.tools.cssLength,b=function(g){var h=this.id;if(!g.info)g.info={};g.info[h]=this.getValue();},c=function(g,h,i){var j=g.split(';');for(i=0;i<j.length;i++){if(j[i].split(':')[0].replace(' ','')==h)return j[i].split(':')[1].split(' ')[i].replace('px','');}},d=function(g,h){var i=g.split(';');for(i=0;i<i.length;i++){if(i[i].split(':')[0].replace(' ','')==h)return i[i].split(':')[1].replace(' ','').replace('px','');}};function e(g){var h=0,i=0;for(var j=0,k,l=g.$.rows.length;j<l;j++){k=g.$.rows[j],h=0;for(var m=0,n,o=k.cells.length;m<o;m++){n=k.cells[m];h+=n.colSpan;}h>i&&(i=h);}return i;};function f(g,h){var i=function(k){return new CKEDITOR.dom.element(k,g.document);},j=g.plugins.dialogadvtab;return{title:g.lang.table.title,minWidth:310,minHeight:CKEDITOR.env.ie?310:280,onLoad:function(){var k=this,l=k.getContentElement('advanced','advStyles');if(l)l.on('change',function(m){var n=this.getStyle('width',''),o=k.getContentElement('info','txtWidth');o&&o.setValue(n,true);var p=this.getStyle('height',''),q=k.getContentElement('info','txtHeight');q&&q.setValue(p,true);});},onShow:function(){var s=this;var k=g.getSelection(),l=k.getRanges(),m=null,n=s.getContentElement('info','txtRows'),o=s.getContentElement('info','txtCols'),p=s.getContentElement('info','txtWidth'),q=s.getContentElement('info','txtHeight');if(h=='tableProperties'){if(m=k.getSelectedElement())m=m.getAscendant('table',true);else if(l.length>0){if(CKEDITOR.env.webkit)l[0].shrink(CKEDITOR.NODE_ELEMENT);var r=l[0].getCommonAncestor(true);m=r.getAscendant('table',true);}s._.selectedElement=m;}if(m){s.setupContent(m);n&&n.disable();o&&o.disable();}else{n&&n.enable();o&&o.enable();}p&&p.onChange();q&&q.onChange();},onOk:function(){var k=g.getSelection(),l=this._.selectedElement&&k.createBookmarks(),m=this._.selectedElement||i('table'),n=this,o={};this.commitContent(o,m);if(o.info){var p=o.info;if(!this._.selectedElement){var q=m.append(i('tbody')),r=parseInt(p.txtRows,10)||0,s=parseInt(p.txtCols,10)||0;for(var t=0;t<r;t++){var u=q.append(i('tr'));for(var v=0;v<s;v++){var w=u.append(i('td')),x='border:'+p.txtBorder+'px solid #000000; padding:'+p.txtCellPad+'px;';w.$.style.cssText=x;if(!CKEDITOR.env.ie)w.append(i('br'));}}}else{var r=parseInt(p.txtRows,10)||0,s=parseInt(p.txtCols,10)||0;if(r>0&&s>0){var y=r*s,x='border:'+p.txtBorder+'px solid #000000; padding:'+p.txtCellPad+'px;';for(var t=0;t<y;t++)this._.selectedElement.$.getElementsByTagName('td')[t].style.cssText=x;}}var z=p.selHeaders;
if(!m.$.tHead&&(z=='row'||z=='both')){var A=new CKEDITOR.dom.element(m.$.createTHead());q=m.getElementsByTag('tbody').getItem(0);var B=q.getElementsByTag('tr').getItem(0);for(t=0;t<B.getChildCount();t++){var C=B.getChild(t);if(C.type==CKEDITOR.NODE_ELEMENT&&!C.data('cke-bookmark')){C.renameNode('th');C.setAttribute('scope','col');}}A.append(B.remove());}if(m.$.tHead!==null&&!(z=='row'||z=='both')){A=new CKEDITOR.dom.element(m.$.tHead);q=m.getElementsByTag('tbody').getItem(0);var D=q.getFirst();while(A.getChildCount()>0){B=A.getFirst();for(t=0;t<B.getChildCount();t++){var E=B.getChild(t);if(E.type==CKEDITOR.NODE_ELEMENT){E.renameNode('td');E.removeAttribute('scope');}}B.insertBefore(D);}A.remove();}if(!this.hasColumnHeaders&&(z=='col'||z=='both'))for(u=0;u<m.$.rows.length;u++){E=new CKEDITOR.dom.element(m.$.rows[u].cells[0]);E.renameNode('th');E.setAttribute('scope','row');}if(this.hasColumnHeaders&&!(z=='col'||z=='both'))for(t=0;t<m.$.rows.length;t++){u=new CKEDITOR.dom.element(m.$.rows[t]);if(u.getParent().getName()=='tbody'){E=new CKEDITOR.dom.element(u.$.cells[0]);E.renameNode('td');E.removeAttribute('scope');}}p.txtHeight?m.setStyle('height',p.txtHeight):m.removeStyle('height');p.txtWidth?m.setStyle('width',p.txtWidth):m.removeStyle('width');if(!m.getAttribute('style'))m.removeAttribute('style');}if(!this._.selectedElement){g.insertElement(m);setTimeout(function(){var F=new CKEDITOR.dom.element(m.$.rows[0].cells[0]),G=new CKEDITOR.dom.range(g.document);G.moveToPosition(F,CKEDITOR.POSITION_AFTER_START);G.select(1);},0);}else try{k.selectBookmarks(l);}catch(F){}},contents:[{id:'info',label:g.lang.table.title,elements:[{type:'hbox',widths:[null,null],styles:['vertical-align:top'],children:[{type:'vbox',padding:0,children:[{type:'text',id:'txtRows','default':3,label:g.lang.table.rows,required:true,controlStyle:'width:5em',validate:function(){var k=true,l=this.getValue();k=k&&CKEDITOR.dialog.validate.integer()(l)&&l>0;if(!k){alert(g.lang.table.invalidRows);this.select();}return k;},setup:function(k){this.setValue(k.$.rows.length);},commit:b},{type:'text',id:'txtCols','default':2,label:g.lang.table.columns,required:true,controlStyle:'width:5em',validate:function(){var k=true,l=this.getValue();k=k&&CKEDITOR.dialog.validate.integer()(l)&&l>0;if(!k){alert(g.lang.table.invalidCols);this.select();}return k;},setup:function(k){this.setValue(e(k));},commit:b},{type:'html',html:'&nbsp;'},{type:'select',id:'selHeaders','default':'',label:g.lang.table.headers,items:[[g.lang.table.headersNone,''],[g.lang.table.headersRow,'row'],[g.lang.table.headersColumn,'col'],[g.lang.table.headersBoth,'both']],setup:function(k){var l=this.getDialog();
l.hasColumnHeaders=true;for(var m=0;m<k.$.rows.length;m++){var n=k.$.rows[m].cells[0];if(n&&n.nodeName.toLowerCase()!='th'){l.hasColumnHeaders=false;break;}}if(k.$.tHead!==null)this.setValue(l.hasColumnHeaders?'both':'row');else this.setValue(l.hasColumnHeaders?'col':'');},commit:b},{type:'text',id:'txtBorder','default':1,label:g.lang.table.border,controlStyle:'width:3em',validate:CKEDITOR.dialog.validate.number(g.lang.table.invalidBorder),setup:function(k){var l=k.$.style.cssText.toLowerCase();if(navigator.userAgent.indexOf('Firefox')>=0)this.setValue(c(l,'border',1));else this.setValue(c(l,'border-top',2));},commit:function(k,l){var o=this;if(o.getValue()){var m=o.getDialog().getContentElement('advanced','advStyles');m&&m.updateStyle('border',o.getValue()+'px solid #000000');}var n=o.id;if(!k.info)k.info={};k.info[n]=o.getValue();}},{id:'cmbAlign',type:'select','default':'',label:g.lang.common.align,items:[[g.lang.common.notSet,''],[g.lang.common.alignLeft,'left'],[g.lang.common.alignCenter,'center'],[g.lang.common.alignRight,'right']],onChange:function(){var k=this.getDialog().getContentElement('advanced','advStyles');if(this.getValue()=='center'){k&&k.updateStyle('margin-left','auto');k&&k.updateStyle('margin-right','auto');}else if(this.getValue()=='right'){k&&k.updateStyle('margin-left','auto');k&&k.updateStyle('margin-right','0px');}else{k&&k.updateStyle('margin-left','0px');k&&k.updateStyle('margin-right','0px');}},setup:function(k){var l=k.getStyle('margin-left'),m=k.getStyle('margin-right'),n='';if(l=='auto'&&m=='auto')n='center';else if(l=='auto'&&m!='auto')n='right';else if(l!='auto'&&m=='auto')n='left';this.setValue(n||'');},commit:b}]},{type:'vbox',padding:0,children:[{type:'hbox',widths:['5em'],children:[{type:'text',id:'txtWidth',controlStyle:'width:5em',label:g.lang.common.width,title:g.lang.common.cssLengthTooltip,'default':500,getValue:a,validate:CKEDITOR.dialog.validate.cssLength(g.lang.common.invalidCssLength.replace('%1',g.lang.common.width)),onChange:function(){var k=this.getDialog().getContentElement('advanced','advStyles');k&&k.updateStyle('width',this.getValue());},setup:function(k){var l=k.getStyle('width');l&&this.setValue(l);},commit:b}]},{type:'hbox',widths:['5em'],children:[{type:'text',id:'txtHeight',controlStyle:'width:5em',label:g.lang.common.height,title:g.lang.common.cssLengthTooltip,'default':'',getValue:a,validate:CKEDITOR.dialog.validate.cssLength(g.lang.common.invalidCssLength.replace('%1',g.lang.common.height)),onChange:function(){var k=this.getDialog().getContentElement('advanced','advStyles');
k&&k.updateStyle('height',this.getValue());},setup:function(k){var l=k.getStyle('width');l&&this.setValue(l);},commit:b}]},{type:'html',html:'&nbsp;'},{type:'text',id:'txtCellSpace',controlStyle:'width:3em',label:g.lang.table.cellSpace,'default':1,validate:CKEDITOR.dialog.validate.number(g.lang.table.invalidCellSpacing),setup:function(k){var l=k.$.style.cssText.toLowerCase();this.setValue(d(l,'border-spacing')||'');},commit:function(k,l){var n=this;if(n.getValue()){l.setAttribute('cellSpacing',n.getValue());var m=n.getDialog().getContentElement('advanced','advStyles');m&&m.updateStyle('border-collapse','separate');m&&m.updateStyle('border-spacing',n.getValue()+'px');}else l.removeAttribute('cellspacing');}},{type:'text',id:'txtCellPad',controlStyle:'width:3em',label:g.lang.table.cellPad,'default':1,validate:CKEDITOR.dialog.validate.number(g.lang.table.invalidCellPadding),setup:function(k){var l=k.$.getElementsByTagName('td')[0].style.cssText.toLowerCase();if(navigator.userAgent.indexOf('Firefox')>=0)this.setValue(d(l,'padding')||'');else this.setValue(d(l,'padding-right')||'');},commit:b}]}]},{type:'html',align:'right',html:''},{type:'vbox',padding:0,children:[{type:'text',id:'txtCaption',label:g.lang.table.caption,setup:function(k){var o=this;o.enable();var l=k.getElementsByTag('caption');if(l.count()>0){var m=l.getItem(0),n=m.getFirst(CKEDITOR.dom.walker.nodeType(CKEDITOR.NODE_ELEMENT));if(n&&!n.equals(m.getBogus())){o.disable();o.setValue(m.getText());return;}m=CKEDITOR.tools.trim(m.getText());o.setValue(m);}},commit:function(k,l){if(!this.isEnabled())return;var m=this.getValue(),n=l.getElementsByTag('caption');if(m){if(n.count()>0){n=n.getItem(0);n.setHtml('');}else{n=new CKEDITOR.dom.element('caption',g.document);if(l.getChildCount())n.insertBefore(l.getFirst());else n.appendTo(l);}n.append(new CKEDITOR.dom.text(m,g.document));}else if(n.count()>0)for(var o=n.count()-1;o>=0;o--)n.getItem(o).remove();}},{type:'text',id:'txtSummary',label:g.lang.table.summary,setup:function(k){this.setValue(k.getAttribute('summary')||'');},commit:function(k,l){if(this.getValue())l.setAttribute('summary',this.getValue());else l.removeAttribute('summary');}}]}]},j&&j.createAdvancedTab(g)]};};CKEDITOR.dialog.add('table',function(g){return f(g,'table');});CKEDITOR.dialog.add('tableProperties',function(g){return f(g,'tableProperties');});})();
