/**
* tableInsert
* @description: An example button plugin for nicEdit
* @requires: nicCore, nicPane, nicAdvancedButton
* @author: Inpego
* @version: 0.0.1
*/

/* START CONFIG */

var nicTableOptions = {
		buttons : {
				'table' : {name : __('Добавить таблицу'), type : 'nicEditorTableButton'}
		}/* NICEDIT_REMOVE_START */,iconFiles : {'table' : '/javascripts/nicEdit/table/icons/table.gif'}/* NICEDIT_REMOVE_END */
};
/* END CONFIG */
var nicEditorTableButton = nicEditorAdvancedButton.extend({
	addPane : function() {
		this.tbl=this.ne.selectedInstance.selElm().parentTagBefore('TABLE',this.ne.selectedInstance.elm);
		if(!this.tbl)
		{
			form_opts={
			'' : {type : 'title', txt : 'Добавить/изменить таблицу',label_width:80},
			'rows' : {type : 'text', txt : 'Строки', style : {width: '40px'}},
			'cols' : {type : 'text', txt : 'Столбцы', style : {width: '40px'}},
			'cellspacing' : {type : 'text', txt : 'Отступ', style : {width: '40px'}},
			'cellpadding' : {type : 'text', txt : 'Набивка', style : {width: '40px'}},
			'border' : {type : 'text', txt : 'Граница', style : {width: '40px'}},
			'height' : {type : 'text', txt : 'Высота', style : {width: '40px'}},
			'width' : {type : 'text', txt : 'Ширина', style : {width: '40px'}},
			'class' : {type : 'text', txt : 'CSS-класс', style : {width: '80px'}},
			'align' : {type : 'select', txt : 'Выравнивание', options : {none : 'По умолчанию','left' : 'по левому краю', 'right' : 'по правому краю','center':'по центру'}}};
		}
		else
		{
			form_opts={
			'' : {type : 'title', txt : 'Добавить/изменить таблицу',label_width:80},
			'border' : {type : 'text', txt : 'Граница', style : {width: '40px'}},
			'align' : {type : 'select', txt : 'Выравнивание', options : {none : 'По умолчанию','left' : 'по левому краю', 'right' : 'по правому краю','center':'по центру'}}};
		}
		this.addForm(form_opts,this.tbl);
	},

	submit : function(e) {
		this.removePane();

		if(!this.tbl)
		{
			var cols=2;
			var rows=2;
			var border=0;
			var cellpadding=-1;
			var cellspacing=-1;
			var html='';

			cols=this.inputs['cols'].value;
			rows=this.inputs['rows'].value;
			border=(this.inputs['border'].value != "") ? this.inputs['border'].value  : 0;
			cellpadding=(this.inputs['cellpadding'].value != "") ? this.inputs['cellpadding'].value : "";
			cellspacing=(this.inputs['cellspacing'].value != "") ? this.inputs['cellspacing'].value : "";
			align=this.getSelectValue('align');
			width=this.inputs['width'].value;
			height=this.inputs['height'].value;

			html+='<table';
			html+=this.makeAttrib('border',border);
			html+=this.makeAttrib('cellpadding',cellpadding);
			html+=this.makeAttrib('cellspacing',cellspacing);
			html+=this.makeAttrib('align');
			html+=this.makeAttrib('class');
			html+=this.makeAttrib('height');
			html+=this.makeAttrib('width');
			html+='>';

			for(y=0;y<rows;y++)
			{
				html += "<tr>";

				for(x=0;x<cols;x++)
				{
					if(!bkLib.isMSIE)
					{
						html += '<td><br mce_bogus="1"/></td>';
					}
					else
					{
						html += '<td></td>';
					}
				}
				html += "</tr>";
			}
			if(bkLib.isMSIE)
			{
				var range=document.selection.createRange();
				range.pasteHTML(html);
				range.collapse(false);
				range.select();
			}
			else
			{
				this.ne.nicCommand('insertHTML',html);
			}
		}
		else
		{
			this.tbl.border=this.extr('border');
			this.tbl.align=this.getSelectValue('align');
		}
	},

	makeAttrib:function (attrib,value)
	{
		value=this.extr(attrib,value);
		return ' ' + attrib + '="' + value + '"';
	},

	extr:function (attrib,value)
	{
		var valueElm=this.inputs[attrib];

		if(typeof(value)=="undefined" || value==null)
		{
			value="";

			if(valueElm)
			{
				value=valueElm.value;
			}
		}

		if(value=="")
		{
			return "";
		}

		// XML encode it
		value=value.replace(/&/g, '&amp;');
		value=value.replace(/\"/g, '&quot;');
		value=value.replace(/</g, '&lt;');
		value=value.replace(/>/g, '&gt;');

		return value;
	},

	getSelectValue:function (field_name)
	{
		var elm = this.inputs[field_name];

		if (elm == null || elm.options == null || elm.selectedIndex === -1)
		{
			return "";
		}

		return elm.options[elm.selectedIndex].value;
	}

});
nicEditors.registerPlugin(nicPlugin,nicTableOptions);
