//(function() {
	var Flexi = function(selector, context) {
		//TODO: budowa hybrydy Ĺ‚Ä…czÄ…cej jQuery i Prototype niezaleĹĽnie od zaĹ‚adowanych bibliotek
		// lub budowa caĹ‚kowicie nowej biblioteki
		return jQuery(selector, context);
	}
//	Flexi.prototype.selectOptions = function() {
//			return this.options;
//		}

//});


function _IE() {
	return (document.all) ? 1 : 0;
}
var Style = 'body {' +
	'font-family: verdana, arial, sans-serif; ' +
	'font-size: 8pt; ' +
	'}' +
	'.editor_htm {' +
	'font-family: monaco, courier, monospace; ' +
	'font-size: 10pt; ' +
	'width: 96%; ' +
	'padding: 10px 2%; ' +
	'border: 1px solid #333333; ' +
	'background-color: #f0f5ff; ' +
	'margin: 10px 0; ' +
	'opacity: 0.8; ' +
	'} ' +
	'p {' +
	'margin: 0;' +
	'padding: 0;' +
	'}';

var UltraEdit = {
	bold: {
	name: 'bold',
	status: 'inactive',
	type: 'button',
	group: 0,
	doubleOff: 0,
	depend: 0,
	callback: 0,
	imageNoChange: 0
	},

	italic: {
		name: 'italic',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	underline: {
		name: 'underline',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	left: {
		name: 'left',
		status: 'active',
		type: 'button',
		group: 'align',
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	center: {
		name: 'center',
		status: 'inactive',
		type: 'button',
		group: 'align',
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	right: {
		name: 'right',
		status: 'inactive',
		type: 'button',
		group: 'align',
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	htm: {
		name: 'htm',
		status: 'inactive',
		type: 'button',
		group: 'code',
		doubleOff: 1,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	php: {
		name: 'php',
		status: 'inactive',
		type: 'button',
		group: 'code',
		doubleOff: 1,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	oli: {
		name: 'oli',
		status: 'inactive',
		type: 'button',
		group: 'lists',
		doubleOff: 1,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	uli: {
		name: 'uli',
		status: 'inactive',
		type: 'button',
		group: 'lists',
		doubleOff: 1,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	fontSize: {
		name: 'fontSize',
		status: 'active',
		type: 'select',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	font: {
		name: 'font',
		status: 'active',
		type: 'select',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	fontColor: {
		name: 'fontColor',
		status: 'active',
		type: 'specialControl',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	formattings: {
		name: 'formattings',
		status: 'active',
		type: 'select',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	link: {
		name: 'link',
		status: 'inactive',
		type: 'special',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: function() { UltraEdit.linkBox(); } ,
		imageNoChange: 0
	},

	columns: {
		name: 'columns',
		status: 'inactive',
		type: 'special',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: function(module, action) { UltraEdit.colsBox(module, action); } ,
		imageNoChange: 0
	},

	table: {
		name: 'table',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: function(module, action) { UltraEdit.tableBox(module, action); } ,
		imageNoChange: 0
	},

	settings: {
		name: 'settings',
		status: 'inactive',
		type: 'special',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: function(module, action) { UltraEdit.classBox(module, action); } ,
		imageNoChange: 0
	},
	image: {
		name: 'image',
		status: 'inactive',
		type: 'special',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: function(module, action) { UltraEdit.imageBox(module, action); } ,
		imageNoChange: 0
	},

	cut: {
		name: 'cut',
		status: 'inactive',
		type: 'special',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	copy: {
		name: 'copy',
		status: 'inactive',
		type: 'special',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	paste: {
		name: 'paste',
		status: 'inactive',
		type: 'special',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	undo: {
		name: 'undo',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	redo: {
		name: 'redo',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	sup: {
		name: 'sup',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	sub: {
		name: 'sub',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},
	strike: {
		name: 'strike',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	indent: {
		name: 'indent',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},
	outdent: {
		name: 'outdent',
		status: 'inactive',
		type: 'button',
		group: 0,
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 1
	},

	wysiwyg_view: {
		name: 'wysiwyg_view',
		status: 'active',
		type: 'button',
		group: 'view',
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	code_view: {
		name: 'code_view',
		status: 'inactive',
		type: 'button',
		group: 'view',
		doubleOff: 0,
		depend: 0,
		callback: 0,
		imageNoChange: 0
	},

	currentStyle: {},

	currentNode: 0,

	styleObject: function() {
		this.width = 'not_set';
		this.height = 'not_set';
		this.margin = 'not_set';
		this.margin_left = 'not_set';
		this.margin_right = 'not_set';
		this.margin_top = 'not_set';
		this.margin_bottom = 'not_set';
		this.padding = 'not_set';
		this.padding_left = 'not_set';
		this.padding_right = 'not_set';
		this.padding_top = 'not_set';
		this.padding_bottom = 'not_set';
		this.float = 'not_set';
		this.color = 'not_set';
		this.border = 'not_set';
		this.border_left = 'not_set';
		this.border_right = 'not_set';
		this.border_top = 'not_set';
		this.border_bottom = 'not_set';
		this.background_color = 'not_set';
		this.background_image = 'not_set';
		this.background = 'not_set';
	},

	styleObjectToString: function(obj) {
		var styleString = '';
		if(obj.border_left + obj.border_left + obj.border_left + obj.border_left == obj.border_left + obj.border_top + obj.border_right + obj.border_bottom) {
			obj.border = obj.border_left;
			obj.border_left = obj.border_top = obj.border_right = obj.border_bottom = 'not_set';
		}
		if(obj.margin_left + obj.margin_left + obj.margin_left + obj.margin_left == obj.margin_left + obj.margin_top + obj.margin_right + obj.margin_bottom) {
			obj.margin = obj.margin_left;
			obj.margin_left = obj.margin_top = obj.margin_right = obj.margin_bottom = 'not_set';
		}
		if(obj.padding_left + obj.padding_left + obj.padding_left + obj.padding_left == obj.padding_left + obj.padding_top + obj.padding_right + obj.padding_bottom) {
			obj.padding = obj.padding_left;
			obj.padding_left = obj.padding_top = obj.padding_right = obj.padding_bottom = 'not_set';
		}
		for(el in obj) {
			if(obj[el] != 'not_set' && obj[el].constructor == String) {
				styleString += el.replace('_', '-') + ': ' + obj[el] + '; ';
				Flexi(this.currentNode).css(el.replace('_', '-'), obj[el]);
			}
		}
		return styleString;
	},

	stringToStyleObject: function(string) {
		obj = new this.styleObject();
		var str_parts = string.split(';');
		for(var i = 0; i < str_parts.length; i++) {
			var css = str_parts[i].split(':');
			if(css[0] && css[1] != '') {
				obj[css[0].replace(' ', '').replace('-', '_')] = css[1].replace(' ', '');
			}
		}
		return obj;
	},

	imageBox: function(module, action) {
		if(!Flexi('#imageBox').length) {
			var div = this.nodeCreate('div',
					[ {name: 'class', value: 'linkBox'},
					  {name: 'id',   value: 'imageBox'} ], document);
			document.body.appendChild(div);

			offset = Flexi('#editorInterface_' + this.inst.editorId + '_image').offset();
			Flexi('#imageBox').css('top', (offset.top + 25) + 'px');
			Flexi('#imageBox').css('left', (offset.left) + 'px');
			load_action('#imageBox', module, action);

		} else {
			Flexi('#imageBox').toggle();
		}
	},

	colsBox: function(module, action) {
		if(!Flexi('#colsBox').length) {
			var div = this.nodeCreate('div',
					[ {name: 'class', value: 'colsBox'},
					  {name: 'id',   value: 'colsBox'} ], document);

			document.body.appendChild(div);
			var div1 = this.nodeCreate('img',
					[ {name: 'src', value: 'images/main_theme/editor_columns.gif'},
					  {name: 'id',   value: 'cols_2'} ], document);
			div.appendChild(div1);
			Flexi('#cols_2').click(function() {
				UltraEdit.inst.window.focus();
				UltraEdit.curPos(UltraEdit.inst);
				var node = UltraEdit.selection.anchorNode;
				//alert(UltraEdit.curRange.anchorNode.nodeName);
				var mem_p = null;
				while(node.parentNode.nodeName != 'BLOCKQUOTE' && node.parentNode.nodeName != 'BODY') {
					node = node.parentNode;
					if(node.nodeName == 'P' && node.parentNode && (node.parentNode.nodeName == 'BLOCKQUOTE' || node.parentNode.nodeName == 'BODY')) {
						mem_p = node;
					}
				}
				var block = UltraEdit.nodeCreate('blockquote',
						[ {name: 'alt',   value: 'div'},
						  {name: 'style', value: 'clear: both; float: left; width: 50%; min-height: 1em; background-color: transparent; margin: 0; outline: 1px dotted #999999;'},
						  {name: '_moz_dirty', value: ''} ]);
				if(mem_p && mem_p.innerHTML.replace('<br />','').replace('<br>','').length == 0) {
					mem_p.parentNode.insertBefore(block, mem_p);
				} else {
					node.parentNode.insertBefore(block, node.nextSibling);
				}
				var p = UltraEdit.nodeCreate('p', [{name: '_moz_dirty', value: ''}]);
				var text = UltraEdit.text('');
				p.appendChild(text);
				var br = UltraEdit.nodeCreate('br', [{name: '_moz_dirty', value: ''}]);
				p.appendChild(br);
				block.appendChild(p);
				block = UltraEdit.nodeCreate('blockquote',
						[ {name: 'alt',   value: 'div'},
						  {name: 'style', value: 'float: left; width: 50%; min-height: 1em; background-color: transparent; margin: 0; outline: 1px dotted #999999;'},
						  {name: '_moz_dirty', value: ''} ]);

				if(mem_p && mem_p.innerHTML.replace('<br />','').replace('<br>','').length == 0) {
					mem_p.parentNode.insertBefore(block, mem_p);
				} else {
					//alert(mem_p.innerHTML.length + mem_p.innerHTML);
					node.parentNode.insertBefore(block, node.nextSibling);
				}
				p = UltraEdit.nodeCreate('p', [{name: '_moz_dirty', value: ''}]);
				var text1 = UltraEdit.text('');
				p.appendChild(text1);
				br = UltraEdit.nodeCreate('br', [{name: '_moz_dirty', value: ''}]);
				p.appendChild(br);
				block.appendChild(p);
				UltraEdit.curRange.selectNode(text1);
				UltraEdit.curRange.setStart(text1, 0);
				UltraEdit.curRange.setEnd(text1, 0);
				Flexi('#colsBox').toggle();
			});
			div1 = this.nodeCreate('img',
					[ {name: 'src', value: 'images/main_theme/editor_columns.gif'},
					  {name: 'id',   value: 'cols_3'} ], document);
			div.appendChild(div1);
			div1 = this.nodeCreate('img',
					[ {name: 'src', value: 'images/main_theme/editor_columns.gif'},
					  {name: 'id',   value: 'cols_text_after'} ], document);
			div.appendChild(div1);
			Flexi('#cols_text_after').click(function() {
				UltraEdit.inst.window.focus();
				UltraEdit.curPos(UltraEdit.inst);
				var node = UltraEdit.selection.anchorNode;
				while(node.parentNode.nodeName != 'BLOCKQUOTE' && node.parentNode.nodeName != 'BODY') {
					node = node.parentNode;
				}
				if(node.parentNode.nodeName != 'BODY') {
					UltraEdit.insertParagraph(UltraEdit.inst, node.parentNode, 1);
				}
				Flexi('#colsBox').toggle();
			});
			div1 = this.nodeCreate('img',
					[ {name: 'src', value: 'images/main_theme/editor_columns.gif'},
					  {name: 'id',   value: 'cols_text_before'} ], document);
			div.appendChild(div1);
			Flexi('#cols_text_before').click(function() {
				alert('zzz');
				UltraEdit.inst.window.focus();
				UltraEdit.curPos(UltraEdit.inst);
				var node = UltraEdit.selection.anchorNode;
				while(node.parentNode.nodeName != 'BLOCKQUOTE' && node.parentNode.nodeName != 'BODY') {
					node = node.parentNode;
				}
				if(node.parentNode.nodeName != 'BODY') {
					UltraEdit.insertParagraph(UltraEdit.inst, node.parentNode.previousSibling, 1);
				}
				Flexi('#colsBox').toggle();
			});
			div1 = this.nodeCreate('img',
					[ {name: 'src', value: 'images/main_theme/editor_columns.gif'},
					  {name: 'id',   value: 'cols_block_after'} ], document);
			div.appendChild(div1);
			Flexi('#cols_block_after').click(function() {
				UltraEdit.inst.window.focus();
				UltraEdit.curPos(UltraEdit.inst);
				var node = UltraEdit.selection.anchorNode;
				while(node.parentNode.nodeName != 'BLOCKQUOTE' && node.parentNode.nodeName != 'BODY') {
					node = node.parentNode;
				}
				var block = UltraEdit.nodeCreate('blockquote',
						[ {name: 'style', value: 'background-color: transparent; float: left; margin: 0; min-height: 1em; outline: 1px dotted #999999; width: 50%;'},
						  {name: 'alt',   value: 'div'},
						  {name: '_moz_dirty', value: ''} ]);

				var p = UltraEdit.nodeCreate('p', [{name: '_moz_dirty', value: ''}]);
				var text = UltraEdit.text('');
				p.appendChild(text);
				var br = UltraEdit.nodeCreate('br', [{name: '_moz_dirty', value: ''}]);
				p.appendChild(br);
				block.appendChild(p);
				node.parentNode.parentNode.insertBefore(block,node.parentNode.nextSibling);
			});
			div1 = this.nodeCreate('img',
					[ {name: 'src', value: 'images/main_theme/editor_columns.gif'},
					  {name: 'id',   value: 'cols_block_before'} ], document);
			div.appendChild(div1);
			Flexi('#cols_block_before').click(function() {
				UltraEdit.inst.window.focus();
				UltraEdit.curPos(UltraEdit.inst);
				var node = UltraEdit.selection.anchorNode;
				while(node.parentNode.nodeName != 'BLOCKQUOTE' && node.parentNode.nodeName != 'BODY') {
					node = node.parentNode;
				}
				var block = UltraEdit.nodeCreate('blockquote',
						[ {name: 'style', value: 'background-color: transparent; clear: both; float: left; margin: 0; min-height: 1em; outline: 1px dotted #999999; width: 50%;'},
						  {name: 'alt',   value: 'div'},
						  {name: '_moz_dirty', value: ''} ]);

				var p = UltraEdit.nodeCreate('p', [{name: '_moz_dirty', value: ''}]);
				var text = UltraEdit.text('');
				p.appendChild(text);
				var br = UltraEdit.nodeCreate('br', [{name: '_moz_dirty', value: ''}]);
				p.appendChild(br);
				block.appendChild(p);
				node.parentNode.parentNode.insertBefore(block,node.parentNode);
			});
			offset = Flexi('#editorInterface_' + this.inst.editorId + '_columns').offset();
			Flexi('#colsBox').css('top', (offset.top + 25) + 'px');
			Flexi('#colsBox').css('left', (offset.left) + 'px');
			//load_action('#imageBox', module, action);

		} else {
			//a = document.parentNode.startOffset;
			Flexi('#colsBox').toggle();
		}
	},

	classBox: function(module, action) {
		//alert('visible: ' + Flexi('#classBox').length + Flexi('#classBox').css('display'));
		if(!Flexi('#classBox').length || Flexi('#classBox').css('display') == 'none') {
			var div = this.nodeCreate('div',
					[ {name: 'class', value: 'classBox'},
					  {name: 'id',   value: 'classBox'} ], document);
			document.body.appendChild(div);

			Flexi(div).load('/ultraedit/visualization.htm', function() {
				UltraEdit.colorPickerCreate(0, 'vis_color', Flexi('#vis_bg_color')[0]);
				Flexi('#vis_color').click(function () { Flexi('#color_picker_vis_color').toggle(); });
				Flexi('.color_picker_head').click(function () { return false; });
				Flexi('td[id*=color_picker_vis_color]').click(function () {
					color_parts = this.id.split('_');
					//alert(UltraEdit.currentStyle.background_color);
					//UltraEdit.action('fontColor', '#' + UltraEdit.colorValues[color_parts[3]] + UltraEdit.colorValues[color_parts[4]] + UltraEdit.colorValues[color_parts[5]]);
					UltraEdit.currentStyle.background_color = '#' + UltraEdit.colorValues[color_parts[4]] + UltraEdit.colorValues[color_parts[5]] + UltraEdit.colorValues[color_parts[6]];
					Flexi('#vis_color').add('#vis_inside_center').css('background-color', UltraEdit.currentStyle.background_color);
				});
				UltraEdit.colorPickerCreate(0, 'border_color', Flexi('#vis_border_color')[0]);
				Flexi('#border_color').click(function () { Flexi('#color_picker_border_color').toggle(); });
				//Flexi('.color_picker_head').click(function () { return false; });
				Flexi('td[id*=color_picker_border_color]').click(function () {
					color_parts = this.id.split('_');
					//UltraEdit.action('fontColor', '#' + UltraEdit.colorValues[color_parts[3]] + UltraEdit.colorValues[color_parts[4]] + UltraEdit.colorValues[color_parts[5]]);
					//UltraEdit.currentStyle.background_color = '#' + UltraEdit.colorValues[color_parts[4]] + UltraEdit.colorValues[color_parts[5]] + UltraEdit.colorValues[color_parts[6]];
					Flexi('#border_color').css('background-color', '#' + UltraEdit.colorValues[color_parts[4]] + UltraEdit.colorValues[color_parts[5]] + UltraEdit.colorValues[color_parts[6]]);
				});
				Flexi('#color_picker_input_fontColor').focus(function() {
					UltraEdit.colorInput = 1;
				});
				Flexi('#color_picker_input_fontColor').blur(function() {
					UltraEdit.colorInput = 0;
				});
				Flexi().keypress(function(e) {
					var keycode = (_IE()) ? window.event.keyCode : e.which;
					var val = Flexi('#color_picker_input_fontColor').val();
					if(val.match(/#[0-9abcdef]{1,6}/)) {
						Flexi('#color_picker_choice_fontColor').css('background-color', val + '000000'.slice(0, 7 - val.length));
					}
					if(UltraEdit.colorInput && keycode == 13) {
						if(val.match(/#[0-9abcdef]{6}/)) {
							UltraEdit.action('fontColor', val);
						}
						Flexi('#color_picker_fontColor').toggle();
						return false;
					}
					return true;
				});
				UltraEdit.inst.window.focus();
				UltraEdit.curPos(UltraEdit.inst);

				//alert(UltraEdit.currentNode.innerHTML);
				if(UltraEdit.currentNode) {
					var node = UltraEdit.currentNode;
				} else {
					var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
				}
				//alert(node.nodeName);

				if(node.hasAttribute('id')) {
					var ue_id = node.getAttribute('id').split('_');
					if(ue_id[0] == 'UEgen') {
						if(UltraEdit.styleObjects[ue_id[1]]) {
							UltraEdit.currentStyle = UltraEdit.styleObjects[ue_id[1]];
						}
					}
				} else {
					UltraEdit.currentStyle = (node.hasAttribute('style')) ? UltraEdit.stringToStyleObject(node.getAttribute('style')) : new UltraEdit.styleObject();
					var offset = Flexi('#editorInterface_' + UltraEdit.inst.editorId + '_settings').offset();
					Flexi('#classBox').css('top', (offset.top + 25) + 'px');
					Flexi('#classBox').css('left', (offset.left - 275) + 'px');
					UltraEdit.globalId += 1;
					UltraEdit.styleObjects[UltraEdit.globalId] = UltraEdit.currentStyle;
				}

				var properties = Flexi('table.all_styles td input');

				Flexi('table.all_styles td input').each(function() {
					var prop_name = Flexi(this).attr('name').replace('cssval_','');
					//alert(prop_name + UltraEdit.currentStyle[prop_name]);
					if(typeof UltraEdit.currentStyle[prop_name] != 'undefined' && UltraEdit.currentStyle[prop_name] != 'not_set') {
						Flexi(this).val(UltraEdit.currentStyle[prop_name]);
					}
				});
				if(UltraEdit.currentStyle.width != 'not_set') {
					var w = parseInt(UltraEdit.currentStyle.width);
					Flexi('input[name=vis_width]').val(w);
					Flexi('select[name=width_units]').val(UltraEdit.currentStyle.width.replace(w, ''));
				}
				if(UltraEdit.currentStyle.height  != 'not_set') {
					var h = parseInt(UltraEdit.currentStyle.height);
					Flexi('input[name=vis_height]').val(h);
					Flexi('select[name=height_units]').val(UltraEdit.currentStyle.height.replace(h, ''));
				}
				if(UltraEdit.currentStyle.background_color != 'not_set') {
					Flexi('#vis_color').add('#vis_inside_center').css('background-color', UltraEdit.currentStyle.background_color);
				}
				if(UltraEdit.currentStyle.border != 'not_set') {
					var b = UltraEdit.currentStyle.border.split(' ');
					var width = parseInt(b[0]);
					var normalized = (width > 6) ? '6px ' + b[1] + ' ' + b[2] : UltraEdit.currentStyle.border;
					Flexi('input[name=vis_border_width]').val(b[0]);
					Flexi('select[name=vis_border_style]').val(b[1]);
					var color = b[2];
					color += (b[3]) ? b[3] : '';
					color += (b[4]) ? b[4] : '';
					Flexi('#border_color').css('background-color', color);
					Flexi('#vis_l_border').css('border-right', normalized).css('width', 6 - width + 'px');
					Flexi('#vis_t_border').css('border-bottom', normalized).css('height', 6 - width + 'px');
					Flexi('#vis_r_border').css('border-left', normalized).css('width', 6 - width + 'px');
					Flexi('#vis_b_border').css('border-top', normalized).css('height', 6 - width + 'px');
				}
				if(UltraEdit.currentStyle.margin != 'not_set') {
					var m = parseInt(UltraEdit.currentStyle.margin);
					Flexi('input[name=vis_margin]').val(m);
					Flexi('select[name=margin_units]').val(UltraEdit.currentStyle.margin.replace(m, ''));
					Flexi('#vis_l_margin').add('#vis_t_margin').add('#vis_r_margin').add('#vis_b_margin').html(UltraEdit.currentStyle.margin);
				}
				if(UltraEdit.currentStyle.padding != 'not_set') {
					var m = parseInt(UltraEdit.currentStyle.padding);
					Flexi('input[name=vis_padding]').val(m);
					Flexi('select[name=padding_units]').val(UltraEdit.currentStyle.padding.replace(m, ''));
					Flexi('#vis_l_padding').add('#vis_t_padding').add('#vis_r_padding').add('#vis_b_padding').html(UltraEdit.currentStyle.padding);
				}
			});
		} else {
			Flexi('#classBox')[0].parentNode.removeChild(Flexi('#classBox')[0]);
		}
	},

	tableBox: function(module, action) {
		if(!Flexi('#tableBox').length) {
			var div = this.nodeCreate('div',
					[ {name: 'class', value: 'tableBox'},
					  {name: 'id',   value: 'tableBox'} ], document);
			document.body.appendChild(div);

			offset = Flexi('#editorInterface_' + this.inst.editorId + '_table').offset();
			Flexi('#tableBox').css('top', (offset.top + 25) + 'px');
			Flexi('#tableBox').css('left', (offset.left) + 'px');
			load_action('#tableBox', module, action);

		} else {
			Flexi('#tableBox').toggle();
		}
	},

	linkBox: function() {
		//className = (_IE()) ? 'className' : 'class';
		if(!Flexi('#linkBox').length || Flexi('#linkBox').css('display') == 'none') {
			var div = this.nodeCreate('div',
					[ {name: 'class', value: 'linkBox'},
					  {name: 'id',   value: 'linkBox'} ], document);
			document.body.appendChild(div);
			offset = Flexi('#editorInterface_' + this.inst.editorId + '_link').offset();
			Flexi('#linkBox').css('top', (offset.top + 25) + 'px');
			Flexi('#linkBox').css('left', (offset.left) + 'px');

			Flexi(div).load('/ultraedit/link.htm', function() {
				Flexi('#link_form').submit(function() {
					params = new Array();
					if(Flexi('input[name=link_url]').val() != '') {
						params.push({name: 'href', value: Flexi('input[name=link_url]').val()});
						if(Flexi('input[name=link_title]').val() != '') {
							params.push({name: 'title', value: Flexi('input[name=link_title]').val()});
						}
						if(Flexi('input[name=link_class]').val() != '') {
							params.push({name: 'class', value: Flexi('input[name=link_class]').val()});
						}
						var a = UltraEdit.nodeCreate('a', params);
						if(Flexi('input[name=link_anchor]').val() != '') {
							var anchorText = UltraEdit.text(Flexi('input[name=link_anchor]').val());
						} else {
							anchorText = UltraEdit.text(Flexi('input[name=link_url]').val());
						}
						a.appendChild(anchorText);
						UltraEdit.curRange.insertNode(a);
						Flexi('#linkBox').remove();
					};
					return false;
				});
			});
		} else {
			Flexi('#linkBox').remove();
		}
	},

	globalId: 0,

	styleObjects: [],

	initialized: 0,

	iFrames: new Object(),

	inst: {},

	initialStyle: 'font-family: Verdana; font-size: 11pt; color: #000000; text-align: left;',

	editorWidth: '100%',

	cssFiles: [],

	em2pt: 10,

	exportCss: 'editor_styles.css',

	newLine: 0,

	fonts: ['Verdana', 'Arial', 'Trebuchet MS', 'Helvetica', 'Georgia', 'Courier'],

	sizes: ['6pt', '7pt', '8pt', '9pt', '10pt', '11pt', '12pt', '13pt', '14pt', '15pt', '16pt', '18pt', '20pt', '24pt'],

	formattingsValues: ['normal', 'Heading 1', 'Heading 2', 'Heading 3', 'Heading 4', 'paragraph', 'address', 'blockquote', 'code'],

	colorValues: ['00', '33', '66', '99', 'cc', 'ff'],

	htmlAwaitStart: '',

	htmlAwaitEnd: '',

	functionKey: 0,

	styleString: '',

	colorInput: 0,

	htmlView: 0,

	tagStart: 0,

	hintActive: -1,

	singleTags: ['BR', 'IMG', 'HR', 'INPUT'],

	openedTag: '',

	selectedNodeParam: '',

	tagsTable: [
		{
			name: 'a',
			attrs: ['href'],
			attrsValues: [''],
			isSingle: 0 },
		{
			name: 'abbr',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'acronym',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'address',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'area',
			attrs: ['shape', 'coords'],
			attrsValues: ['', ''],
			isSingle: 0 },
		{
			name: 'b',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'big',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'blockquote',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'br',
			attrs: [],
			attrsValues: [],
			isSingle: 1 },
		{
			name: 'caption',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'cite',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'code',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'col',
			attrs: [],
			attrsValues: [],
			isSingle: 1 },
		{
			name: 'colgroup',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'dd',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'del',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'dfn',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'div',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'dl',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'dt',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'em',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'fieldset',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'form',
			attrs: ['action', 'method'],
			attrsValues: ['', 'post'],
			isSingle: 0 },
		{
			name: 'h1',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'h2',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'h3',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'h4',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'h5',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'h6',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'hr',
			attrs: [],
			attrsValues: [],
			isSingle: 1 },
		{
			name: 'i',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'iframe',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'img',
			attrs: ['alt', 'src'],
			attrsValues: ['', ''],
			isSingle: 1 },
		{
			name: 'input',
			attrs: ['type', 'name'],
			attrsValues: ['', ''],
			isSingle: 1 },
		{
			name: 'ins',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'kbd',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'label',
			attrs: ['for'],
			attrsValues: [''],
			isSingle: 0 },
		{
			name: 'legend',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'li',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'link',
			attrs: [],
			attrsValues: [],
			isSingle: 1 },
		{
			name: 'map',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'meta',
			attrs: [],
			attrsValues: [],
			isSingle: 1 },
		{
			name: 'noscript',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'object',
			attrs: ['type', 'data'],
			attrsValues: ['', ''],
			isSingle: 0 },
		{
			name: 'ol',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'optgroup',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'option',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'p',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'param',
			attrs: ['name', 'value'],
			attrsValues: ['', ''],
			isSingle: 1 },
		{
			name: 'pre',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'q',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'samp',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'script',
			attrs: ['type'],
			attrsValues: ['text/javascript'],
			isSingle: 0 },
		{
			name: 'select',
			attrs: ['name'],
			attrsValues: [''],
			isSingle: 0 },
		{
			name: 'small',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'span',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'strong',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'style',
			attrs: ['type'],
			attrsValues: ['text/css'],
			isSingle: 0 },
		{
			name: 'sub',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'sup',
			attrs: [],
			attrsValues: [],
			isSingle: 1 },
		{
			name: 'table',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'tbody',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'tbody',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'td',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'textarea',
			attrs: ['name'],
			attrsValues: [''],
			isSingle: 0 },
		{
			name: 'tfoot',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'th',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'thead',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'title',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'tr',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'tt',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'ul',
			attrs: [],
			attrsValues: [],
			isSingle: 0 },
		{
			name: 'var',
			attrs: [],
			attrsValues: [],
			isSingle: 0 }
	],

	iFrameCreate: function(selector) {
		var iframe = document.createElement('iframe');
		iframe.setAttribute('id', 'ultraEdit_' + Flexi(selector).attr('id'));
		if(_IE()) {
			iframe.setAttribute('className', 'ultraedit_iframe');
			iframe.setAttribute('frameBorder', '0');
			iframe.setAttribute('allowTransparency', 'true');
			iframe.setAttribute('src', '/ultraedit/editor_content_ie.htm');

		} else {
			iframe.setAttribute('class', 'ultraedit_iframe');
			iframe.setAttribute('src', '/ultraedit/editor_content_fx.htm');
		}
		return iframe;
	},

	instance: function(selector, form_selector, params) {
		UltraEdit.hostPath = window.location.protocol + '//' + window.location.host + '/';
		this.ta = Flexi(selector);
		this.editorId = '';
		this.initialized = 0;
		this.sel = selector;
		this.htmlView = 0;
		this.htmlAwaitStart = '';
		this.htmlAwaitEnd = '';
		this.tagStart = 0;
		this.newLine = 0;
		this.hintActive = -1;
		this.cssGenFile = (params.cssGenFile) ? params.cssGenFile : 'ultraEditGen.css';

		this.submit = function() {

			if(this.htmlView) {
				var text = UltraEdit.DOMTexttoText(this.content.body);
				this.content.body.innerHTML = text;
				//UltraEdit.generateClasses(UltraEdit.content.body);
			} else {
				UltraEdit.generateClasses(this.content.body);
				UltraEdit.domFilter(this.content.body);
				var text = this.content.body.innerHTML;
			}
			Flexi('#' + this.ta[0].id).val(text);
		};

		Flexi(selector).hide();
		var iBar = UltraEdit.nodeCreate('div', [{name: 'class', value: 'bottom_bar'}, {name: 'id', value: 'bottom_bar_' + this.ta[0].id}], document);
		Flexi(selector)[0].parentNode.insertBefore(UltraEdit.iFrameCreate(selector), Flexi(selector)[0]);
		Flexi(selector)[0].parentNode.insertBefore(iBar, Flexi(selector)[0]);

		this.iElements = new Object();

		setTimeout(function(inst) {
			inst.iFrame = Flexi('#ultraEdit_' + inst.ta[0].id)[0];
			inst.content = inst.iFrame.contentWindow.document;
			inst.window = inst.iFrame.contentWindow;

			inst.editorId = inst.ta[0].id;
			UltraEdit.iFrames['ultraEdit_' + inst.ta[0].id] = inst;

			var initialContent = Flexi('#' + inst.ta[0].id).val();
			if(initialContent.length) {
				inst.content.body.innerHTML = initialContent;
				UltraEdit.log('1282', 'INSTANCE, BEFORE CLASSES TO STYLE: ' + inst.ta[0].id);
				UltraEdit.classesToStyle(inst);

			}


			inst.content.designMode = "on";
			UltraEdit.includeStyleSheet('../ultraedit/ultraedit.css', inst.content);
			UltraEdit.includeStyleSheet('../ultraedit/' + inst.cssGenFile, inst.content);

//			Flexi('img').mouseup(function(e) {
//				alert('img');
//			});

			Flexi(inst.content).mouseup(function(e) {
				if(params.mouseup) {
					params.mouseup();
				}
				var iframe = (_IE()) ? this.parentWindow.frameElement : this.defaultView.frameElement;
				var instance = UltraEdit.iFrames[iframe.id];
				return UltraEdit.mouseUp(e, inst);
			});

			Flexi(inst.content).click(function(e) {
				if(params.click) {
					params.click();
				}
			});

			Flexi(inst.content).keyup(function(e) {
				var iframe = (_IE()) ? this.parentWindow.frameElement : this.defaultView.frameElement;
				var instance = UltraEdit.iFrames[iframe.id];
				return UltraEdit.keyUp(e, inst);
			});

			Flexi(inst.content).keypress(function(e) {
				var iframe = (_IE()) ? this.parentWindow.frameElement : this.defaultView.frameElement;
				var instance = UltraEdit.iFrames[iframe.id];
				return UltraEdit.keyPress(e, inst);
			});

			Flexi(inst.content).keydown(function(e) {

				var iframe = (_IE()) ? this.parentWindow.frameElement : this.defaultView.frameElement;
				var instance = UltraEdit.iFrames[iframe.id];
				return UltraEdit.keyDown(e, inst);
			});

			//UltraEdit.setDesignMode(params);
			UltraEdit.setEnvironment(params);

			Flexi(form_selector).submit(function() {
				if(inst.htmlView) {
					var text = UltraEdit.DOMTexttoText(inst.content.body);
					inst.content.body.innerHTML = text;
					//UltraEdit.generateClasses(UltraEdit.content.body);
				} else {
					var text = inst.content.body.innerHTML;
					//UltraEdit.generateClasses(UltraEdit.content.body);
				}
				Flexi('#' + inst.ta[0].id).val(text);
			});
			inst.initialized = 1;
		}, 500, this);

	},

	init: function(selector, form_selector, params) {
		///alert('ppp');
		if(selector.constructor == Array) {
			var ta = new Array();
			for(var i = 0; i <= selector.length; i++) {
				ta.push(Flexi(selector[i])[0]);
				Flexi('#ultraEdit_' + ta[i].id).focus(function() {
					UltraEdit.iFrame = this;
					UltraEdit.content = UltraEdit.iFrame.contentWindow.document;
					UltraEdit.window = UltraEdit.iFrame.contentWindow;
				});
			}
		} else {
			var ta = Flexi(selector);
		}

		/* Insert iframe */
		if(selector.constructor == Array) {
			for(var i = 0; i <= selector.length - 1; i++) {
				var iframe = this.iFrameCreate(selector[i]);
				//alert('xxx' + selector[i]);
				var iBar = this.nodeCreate('div', [{name: 'class', value: 'bottom_bar'}]);
				Flexi(selector[i]).hide();
				Flexi(selector)[0].parentNode.insertBefore(iBar, Flexi(selector[i])[0]);
				Flexi(selector[i])[0].parentNode.insertBefore(iframe, Flexi(selector[i])[0]);
			}
		} else {
			alert('BLEBLE');
			var iBar = this.nodeCreate('div', [{name: 'class', value: 'bottom_bar'}]);
			var iframe = this.iFrameCreate(selector);
			Flexi(selector).hide();
			///alert('ppp');
			Flexi(selector)[0].parentNode.insertBefore(iBar, Flexi(selector)[0]);
			Flexi(selector)[0].parentNode.insertBefore(iframe, Flexi(selector)[0]);
		}
		this.iElements = new Object();
		//this.styleObjects = new Array();
		setTimeout(function() {
			//alert('zzz');
			UltraEdit.iFrame = Flexi('#ultraEdit_' + ta[0].id)[0];
			UltraEdit.content = UltraEdit.iFrame.contentWindow.document;
			UltraEdit.window = UltraEdit.iFrame.contentWindow;

			UltraEdit.setDesignMode(params);
			UltraEdit.setEnvironment(params);

//			Flexi(form_selector).submit(function() {
//				if(UltraEdit.htmlView) {
//					var text = UltraEdit.DOMTexttoText(UltraEdit.content.body);
//					UltraEdit.content.body.innerHTML = text;
//					UltraEdit.generateClasses(UltraEdit.content.body);
//				} else {
//					var text = UltraEdit.content.body.innerHTML;
//					UltraEdit.generateClasses(UltraEdit.content.body);
//				}
//				Flexi('#' + ta[0].id).val(text);
//			});
			this.initialized = 1;
		}, 500);
	},

	includeStyleSheet: function(path, objectContext) {

		var bodyStyle = objectContext.createElement('link');
		bodyStyle.setAttribute('rel', 'stylesheet');
		bodyStyle.setAttribute('type', 'text/css');
		bodyStyle.setAttribute('media', 'screen');
		bodyStyle.setAttribute('href', path);
		var styleTextRepresentation = objectContext.createTextNode(Style);
		bodyStyle.appendChild(styleTextRepresentation);
		Flexi('head', objectContext)[0].appendChild(bodyStyle);
	},

	mouseUp: function(e, inst) {
		var e = (_IE()) ? inst.event : e;
		this.inst = inst;
		if(inst.content.body.innerHTML.length <= 5) {
			UltraEdit.insertParagraph(inst);
			UltraEdit.empty(inst);
		}
		//alert('zzzz');
		if(e.which == 1 && inst.htmlView) {
			var childs = inst.content.body.childNodes[0].childNodes;
			for(var c = 0; c <= childs.length - 1; c++) {
				if(childs[c].nodeName == 'P') {
					childs[c].removeAttribute('style');
				}
			}
			UltraEdit.curPos(inst);
			var anchorNode = UltraEdit.selection.anchorNode;
			var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
			//alert(anchorNode.nodeName + node.nodeName);
			var p = UltraEdit.searchParents(node, 'P');
			if(p.hasAttribute('style')) {
				p.removeAttribute('style');
				p.setAttribute('style', 'background-color: #dde5ff');
			} else {
				p.setAttribute('style', 'background-color: #dde5ff');
			}
		}
		UltraEdit.curPos(inst);
		var anchorNode = UltraEdit.selection.anchorNode;
		var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
		//alert(anchorNode.nodeName + node.nodeName + this);
		var path = '';
		var tmp = node;
		var level = 0;
		var bar = Flexi('#bottom_bar_' + inst.editorId)[0];
		while(bar.childNodes.length) {
			bar.removeChild(bar.firstChild);
		}
		while(tmp.nodeName != 'BODY') {
			var pathDiv = UltraEdit.nodeCreate('span',
					[ {name: 'id', value: 'EUpath_' + level},
					  {name: 'class', value: 'eu_path'} ], document);
			var nextChar = (level) ? ' > ' : '';
			pathDiv.appendChild(UltraEdit.text(tmp.nodeName + nextChar));
			Flexi('#bottom_bar_' + inst.editorId)[0].insertBefore(pathDiv, Flexi('#bottom_bar_' + inst.editorId)[0].firstChild);
			Flexi('#EUpath_' + level).click(function() {
				var id = Flexi(this).attr('id').split('_');
				Flexi(this).css('background-color', '#ff9900');
				//alert(id);
				UltraEdit.inst.window.focus();
				UltraEdit.curPos(inst);
				var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
				for(var i = 0; i <= id[1] - 1; i++) {
					node = node.parentNode;
				}
				UltraEdit.currentNode = node;
				//alert(UltraEdit.currentNode);
			});
			level += 1;
			//path = tmp.nodeName + '>' + path;
			tmp = tmp.parentNode;
		}
		//Flexi('.bottom_bar').html(path);
		UltraEdit.interfaceUpdate(inst, e) ;
	},

	keyUp: function(e, inst) {
		var e = (_IE()) ? inst.iFrame.contentWindow.event : e;
		this.inst = inst;
		if(_IE()) {
			//UltraEdit.interfaceUpdate(e) ;
		} else {
			navigation_key = 0;
			var navigation_keys = [1, 8, 13, 33, 34, 35, 36, 37, 38, 39, 40, 46];

			for(k in navigation_keys) {
				if(e.which == navigation_keys[k]) {
					navigation_key = 1;
					UltraEdit.interfaceUpdate(inst, e);
					break;
				}
			}
		}
		//alert(e.ctrlKey + ' : ' + e.which );

		UltraEdit.curPos(inst);
		var anchorNode = UltraEdit.selection.anchorNode;
		if(e.ctrlKey && e.which == 86) {
			var parent = anchorNode.parentNode;
			this.filterCode(parent);
		}
		var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
		if(node && node.nodeType == 1 && (node.getAttribute('alt') == 'tagEnd' || node.getAttribute('alt') == 'tagClose') && anchorNode && anchorNode.nodeValue && UltraEdit.curRange.startOffset == anchorNode.nodeValue.length) {
			if(node.nextSibling && node.nextSibling.nodeName == '#text') {
				UltraEdit.curRange.setStart(node.nextSibling, 0);
				UltraEdit.curRange.setEnd(node.nextSibling, 0);
			} else {
				var text = UltraEdit.nodeCreate('span',
						[ {name: 'class',      value: 'text'},
						  {name: 'alt',        value: 'text'} ]);
				var textval = UltraEdit.text('');
				text.appendChild(textval);
				node.parentNode.insertBefore(text, node.nextSibling);
				UltraEdit.curRange.setStart(textval, 0);
				UltraEdit.curRange.setEnd(textval, 0);
			}
		} else if(node.nodeType == 1 && node.getAttribute('alt') == 'tagEnd' && UltraEdit.curRange.startOffset == 0) {
			if(node.previousSibling && node.nodeType == 1 && node.getAttribute('alt') == 'attr') {
				UltraEdit.curRange.setStart(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
				UltraEdit.curRange.setEnd(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
			} else {
				var text = UltraEdit.nodeCreate('span',
						[ {name: 'class',      value: 'attr'},
						  {name: 'alt',        value: 'attr'} ]);
				var textval = UltraEdit.text('');
				text.appendChild(textval);
				node.parentNode.insertBefore(text, node);
				UltraEdit.curRange.setStart(textval, 0);
				UltraEdit.curRange.setEnd(textval, 0);
			}
		} else if(node.nodeType == 1 && (node.getAttribute('alt') == 'tag' || node.getAttribute('alt') == 'tagClose') && UltraEdit.curRange.startOffset == 0) {
			if(node.previousSibling && node.previousSibling.nodeName == '#text') {
				UltraEdit.curRange.setStart(node.previousSibling, 0);
				UltraEdit.curRange.setEnd(node.previousSibling, 0);
			} else {
				var text = UltraEdit.nodeCreate('span',
						[ {name: 'class',      value: 'text'},
						  {name: 'alt',        value: 'text'} ]);
				var textval = UltraEdit.text('');
				text.appendChild(textval);
				node.parentNode.insertBefore(text, node);
				UltraEdit.curRange.setStart(textval, 0);
				UltraEdit.curRange.setEnd(textval, 0);
			}
		} else if(node.nodeType == 1 && node.getAttribute('alt') == 'tagText' && anchorNode && UltraEdit.curRange.startOffset == anchorNode.nodeValue.length) {
			if(node.nextSibling && node.nextSibling.nodeType == 1 && node.nextSibling.getAttribute('alt') == 'attrText') {
				UltraEdit.curRange.setStart(node.nextSibling.firstChild, 0);
				UltraEdit.curRange.setEnd(node.nextSibling.firstChild, 0);
			} else if(node.previousSibling && node.previousSibling.nodeType == 1 && node.previousSibling.getAttribute('alt') == 'attrText'){
					if(node.nextSibling  && node.nextSibling.nodeType == 1 && node.nextSibling.getAttribute('alt') == 'attr') {
						UltraEdit.curRange.setStart(node.nextSibling.firstChild, 0);
						UltraEdit.curRange.setEnd(node.nextSibling.firstChild, 0);
					} else {
						var attr = UltraEdit.nodeCreate('span',
								[ {name: 'class',      value: 'attr'},
								  {name: 'alt',        value: 'attr'} ]);
						var textval = UltraEdit.text('');
						attr.appendChild(textval);
						node.parentNode.insertBefore(attr, node.nextSibling);
						UltraEdit.curRange.setStart(textval, 0);
						UltraEdit.curRange.setEnd(textval, 0);
					}
			}
		} else if(node.nodeType == 1 && node.getAttribute('alt') == 'tagText' && UltraEdit.curRange.startOffset == 0) {
			if(node.previousSibling && node.previousSibling.nodeType == 1 && node.previousSibling.getAttribute('alt') == 'attrText') {
				//alert(node.previousSibling.firstChild);
				UltraEdit.curRange.setStart(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
				UltraEdit.curRange.setEnd(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
			} else if(node.previousSibling && node.previousSibling.nodeType == 1 && node.previousSibling.getAttribute('alt') == 'attr') {
				UltraEdit.curRange.setStart(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
				UltraEdit.curRange.setEnd(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
//				var text = UltraEdit.nodeCreate('span',
//						[ {name: 'class',      value: 'text'},
//						  {name: 'alt',        value: 'text'} ]);
//				var textval = UltraEdit.text('');
//				text.appendChild(textval);
//				node.parentNode.insertBefore(text, node);
//				UltraEdit.curRange.setStart(textval, 0);
//				UltraEdit.curRange.setEnd(textval, 0);
			}
		}
		if(inst.newLine) {
			setTimeout(function(inst){
				UltraEdit.curPos(inst);
				var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
				var p = UltraEdit.searchParents(node, 'P');

				p.previousSibling.removeAttribute('style');

				if(p.hasAttribute('style')) {
					p.removeAttribute('style');
					p.setAttribute('style', 'background-color: #dde5ff');
				} else {
					p.setAttribute('style', 'background-color: #dde5ff');
				}
				inst.newLine = 0;
			}, 100, inst);
		}
		if(inst.tagStart == 1) {
			inst.tagStart = 2;
			setTimeout(function(inst) {
				UltraEdit.curPos(inst);
				var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
				var tagNode = UltraEdit.nodeCreate('span',
						[ {name: 'class',      value: 'tag'},
						  {name: 'alt',        value: 'tag'},
						  {name: '_moz_dirty', value: ''} ]);

				var tagBegining = UltraEdit.text('<');
				tagNode.appendChild(tagBegining);
				//if(node.getAttribute('class') == 'text') {
					//if(node.nextSibling) {
						//node.parentNode.insertBefore(tagNode, node.nextSibling);
					//} else {

					//}
					UltraEdit.curRange.insertNode(tagNode);
					UltraEdit.curRange.selectNode(tagBegining);
					UltraEdit.curRange.setStart(tagBegining, 1);
					UltraEdit.curRange.setEnd(tagBegining, 1);
				//} else{

				//}
			}, 100, inst);
		}
		if(inst.tagStart == 2 && navigation_keys.indexOf(e.which) < 0) {
			setTimeout(function(inst) {
				var char = (e.shiftKey) ? String.fromCharCode(keykode) : String.fromCharCode(keykode).toLowerCase();
				UltraEdit.curPos(inst);
				var node =  UltraEdit.selection.anchorNode;
				var partialTag = node.nodeValue.slice(1);
				var matchedTags = new Array();
				var ids = new Array();
				for(var o in UltraEdit.tagsTable) {

					if(typeof UltraEdit.tagsTable[o] == 'object') {
						if(partialTag.length && partialTag == UltraEdit.tagsTable[o].name.substring(0, partialTag.length)) {
							matchedTags.push(UltraEdit.tagsTable[o].name);
							ids.push(o);
						}
					}
				}
				//var coords = UltraEdit.nodePos(node);
				//alert(coords.left + ' ' + coords.top);
				Flexi('#ue_hint', inst.content).remove();
				if(partialTag.length) {
					UltraEdit.createHint(node, matchedTags, ids);
				}
			}, 250, inst);
		}

		if(e.which == 35) {
//			var tmp = anchorNode;
//			while(tmp.nodeName != 'P' && tmp.nodeName != 'BODY') {
//				tmp = tmp.parentNode;
//			}
//			//alert(tmp.innerHTML.indexOf('\n'));
//			while(tmp.childNodes.length && UltraEdit.notEmpty(tmp, 1)) {
//				tmp = tmp.lastChild;
//				while(tmp.nodeName == 'BR') {
//					tmp = tmp.previousSibling;
//				}
//			}
//			UltraEdit.curRange.selectNode(tmp);
//			UltraEdit.curRange.setStart(tmp, tmp.nodeValue.length);
//			UltraEdit.curRange.setEnd(tmp, tmp.nodeValue.length);
//			return false;

			//alert(anchorNode.nodeName + ' : ' + anchorNode.nodeValue);
			//alert(anchorNode.lastChild.previousSibling.previousSibling.nodeName + ' : ' + anchorNode.lastChild.previousSibling.previousSibling.nodeValue);
			//alert(anchorNode.lastChild.previousSibling.nodeName + ' : ' + anchorNode.lastChild.previousSibling.nodeValue);
			//alert(anchorNode.nodeName + ' : ' + anchorNode.nodeValue);
		}
		if(0 && inst.content.body.lastChild.isSameNode(anchorNode)) {
			var n = anchorNode;
			//alert(anchorNode.nodeName);
			n = inst.content.body.firstChild;
			while(n.nextSibling && n.nextSibling.nodeName == 'P') {
				n = n.nextSibling;
			}
			while(n.childNodes.length) {
				var foundedInRow = 0;
				for(var k = n.childNodes.length - 1; k >= 0; k--) {
					if((n.childNodes[k].nodeName != '#text' && n.childNodes[k].childNodes.length) || (n.childNodes[k].nodeName == '#text' && n.childNodes[k].nodeValue.length)) {
						n = n.childNodes[k];
						foundedInRow = 1;
						break;
					}
				}
				if(!foundedInRow) {
					break;
					//n = n.firstChild;
				}
			}
			var last = n;
			last = (last.nodeName == '#text') ? last : last.lastChild;
				UltraEdit.curRange.selectNode(last);
				UltraEdit.curRange.setStart(last, last.nodeValue.length);
				UltraEdit.curRange.setEnd(last, last.nodeValue.length);
				//return false;

			//n = (n.previousSibling) ? n.previousSibling : n;

		}
		if(e.which != 32 && !navigation_key) {
			this.removeEmpty(inst.content.body);
		}

	},

	removeEmpty: function(element) {
		for(var i = 0; i < element.childNodes.length; i++) {
			if(element.childNodes[i].nodeName == '#text') {
//				if(!element.childNodes[i].nodeValue.length) {
//					element.childNodes[i].parentNode.removeChild(element.childNodes[i]);
//				}
			} else {
				var toClear = ['STRONG', 'EM', 'SPAN', 'B', 'I', 'U', 'SUP', 'SUB'];
				if(!element.childNodes[i].childNodes.length && toClear.indexOf(element.childNodes[i].nodeName) >= 0) {
					element.childNodes[i].parentNode.removeChild(element.childNodes[i]);
				} else {
					//this.removeEmpty(element.childNodes[i]);
				}
			}
		}
	},

	keyDown: function(e, inst) {
		var e = (_IE()) ? UltraEdit.iFrame.contentWindow.event : e;
		e.which = (_IE()) ? e.keyCode : e.which;
		this.inst = inst;

		UltraEdit.curPos(inst);
		var anchorNode = UltraEdit.selection.anchorNode;
		var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;

		var navigation_keys = [1, 8, 13, 33, 34, 35, 36, 37, 38, 39, 40, 46];
		//Flexi('#textarea_ext').val(inst.htmlView + ' : ' + inst.hintActive + ' : ' + e.which);
		if(inst.htmlView) {

			/* behaviour in tag node */
			if(node.nodeType == 1 && node.getAttribute('alt') == 'tag') {
				if(e.which == 13 || e.which == 32) {
					//Flexi('#textarea_ext').val('zenon');
					var stop = UltraEdit.htmlAttr(node, e.which);
					if(this.inst.hintActive < 0) {
						return stop;
					}
				} else if(e.which == 190) {
					//UltraEdit.htmlCloseTag(node);
				}
			}
			/* behaviour in attribute node */
			else if(node.nodeType == 1 && node.getAttribute('alt') == 'attr') {
				if(e.which == 107) {
					return UltraEdit.htmlEmptyAttrText(node);
				}
				if(e.which == 8) {
					UltraEdit.joinPrev(node);
				}
			}
			/* behaviour in attribute value node */
			else if(node.nodeName == 1 && node.getAttribute('alt') == 'attrText') {
				/* " PRESSED */
				if(e.which == 222) {
					UltraEdit.closeAttrText(node);
				}
			}
			/* < PRESSED */
			if(inst.htmlView && e.shiftKey && e.which == 188 && !inst.tagStart && node.nodeType == 1 && node.getAttribute('alt') == 'text') {
				inst.tagStart = 1;
				return false;
			}
			/* > PRESSED */
			if(inst.htmlView && e.shiftKey && e.which == 190 && inst.tagStart) {
				UltraEdit.htmlCloseTag();
				return false;
			}
			/* TAB PRESSED */
			if(e.which == 9) {
				UltraEdit.insertTab(node);
				return false;
			}

			/* lines highlighting */
			if(e.which == 38 || e.which == 40 || e.which == 13) {
				inst.newLine = (e.which == 13) ? 1 : 0;
				var p = UltraEdit.searchParents(node, 'P');

				if(p.nodeName != 'BODY') {
					if(e.which == 38 && p.previousSibling && p.previousSibling.nodeName == 'P') {
						if(p.hasAttribute('style')) {
							p.removeAttribute('style');
						}
						if(p.previousSibling.hasAttribute('style')) {
							p.previousSibling.removeAttribute('style');
							p.previousSibling.setAttribute('style', 'background-color: #dde5ff');
						} else {
							p.previousSibling.setAttribute('style', 'background-color: #dde5ff');
						}
					} else if(e.which == 40 && p.nextSibling && p.nextSibling.nodeName == 'P'){
						if(p.hasAttribute('style')) {
							p.removeAttribute('style');
						}
						//alert(p.nextSibling + p.nextSibling.nodeName);
						if(p.nextSibling.hasAttribute('style')) {
							p.nextSibling.removeAttribute('style');
							p.nextSibling.setAttribute('style', 'background-color: #dde5ff');
						} else {
							p.nextSibling.setAttribute('style', 'background-color: #dde5ff');
						}
					}
				} else {
					if(p.nextSibling.hasAttribute('style')) {
						p.nextSibling.removeAttribute('style');
					}
					p.firstChild.setAttribute('style', 'background-color: #dde5ff');
				}
			}

			/* hint window active */
			if(inst.hintActive >= 0) {
				if(e.which == 38 && Flexi('#ue_hint', inst.content)[0].childNodes[inst.hintActive - 1]) {
					/* UP ARROW pressed */
					Flexi(Flexi('#ue_hint', inst.content)[0].childNodes[inst.hintActive - 1], inst.content).css('background-color', '#002277').css('color', '#ffffff');
					Flexi(Flexi('#ue_hint', inst.content)[0].childNodes[inst.hintActive], inst.content).css('background-color', 'transparent').css('color', '#444444');
					inst.hintActive -= 1;
					return false;
				} else if(e.which == 40  && Flexi('#ue_hint', inst.content)[0].childNodes[inst.hintActive + 1]) {
					/* DOWN ARROW pressed */
					Flexi(Flexi('#ue_hint', inst.content)[0].childNodes[inst.hintActive + 1], inst.content).css('background-color', '#002277').css('color', '#ffffff');
					Flexi(Flexi('#ue_hint', inst.content)[0].childNodes[inst.hintActive], inst.content).css('background-color', 'transparent').css('color', '#444444');
					inst.hintActive += 1;
					return false;
				} else if(e.which == 13) {
					/* ENTER pressed */
					UltraEdit.tagInsert();
					return false;

				} else if(e.which == 27) {
					/* ESCAPE pressed */
					Flexi('#ue_hint', inst.content).remove();
					inst.hintActive = -1;
					inst.tagStart = 0;
					return false;
				} else {
					return true;
				}
			}

		}

		Flexi('#comments_inside7').html(' ' + inst.htmlView + e.shiftKey + e.which + inst.tagStart);

		if(_IE()) {
			//UltraEdit.interfaceUpdate(e) ;
		} else {
			UltraEdit.curPos(inst);
			node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
			Flexi('.comments_news_body').html(e.which + node.nodeName);

		}
	},

	keyPress: function(e, inst) {
		var e = (_IE()) ? inst.iFrame.contentWindow.event : e;
		this.inst = inst;

		/* ENTER pressed */
		if(e.which == 13 && !inst.htmlView) {
			//alert('xxxx');
			/* get current node */
			UltraEdit.curPos(inst);
			node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
			/* insert paragraph on the end */
			if(UltraEdit.isLast(node) && UltraEdit.curRange.startOffset == UltraEdit.selection.anchorNode.length) {
				/* get parents chain */
				var nodes = new Array();
				var tmp_node = node;
				while(tmp_node.nodeName != 'P') {
					nodes.push(tmp_node);
					tmp_node = tmp_node.parentNode;
				}
				/* insert empty paragraph */
				UltraEdit.insertParagraph(inst, tmp_node);
				/* insert parents chain in new paragraph */
				for(var n = nodes.length - 1; n >= 0; n--) {
					var style = nodes[n].getAttribute('style');
					UltraEdit.insertTag(nodes[n].nodeName, (style) ? style : '', '');
				}
				UltraEdit.interfaceUpdate(inst, e);
				return false;
			}
		} else if(e.which == 13 && inst.htmlView) {
			return false;
		}
//		if(inst.htmlView) {
//			alert(e.which);
//			if(e.which == 188) {
//				return false;
//			}
//		}
		UltraEdit.interfaceUpdate(inst, e) ;
	},
  // obecnie niewykorzystywana - do skasowania w przyszĹ‚oĹ›ci
	setDesignMode: function(params) {

		this.content.designMode = "on";

		if(!_IE()) {
			this.bodyStyle = this.content.createElement('style');
			this.bodyStyle = this.content.createElement('link');
			this.bodyStyle.setAttribute('rel', 'stylesheet');
			this.bodyStyle.setAttribute('type', 'text/css');
			this.bodyStyle.setAttribute('media', 'screen');
			this.bodyStyle.setAttribute('href', '../ultraedit/ultraedit.css');
			this.styleTextRepresentation = this.content.createTextNode(Style);
			this.bodyStyle.appendChild(this.styleTextRepresentation);
			Flexi('head', this.content)[0].appendChild(this.bodyStyle);
		}

		jQuery(this.iFrame.contentWindow.document).mouseup(function(e) {
			var e = (_IE()) ? UltraEdit.iFrame.contentWindow.event : e;

			if(UltraEdit.content.body.innerHTML.length <= 5) {
				UltraEdit.insertParagraph();
				UltraEdit.empty();
			}
			if(e.which == 1 && UltraEdit.htmlView) {
				var childs = UltraEdit.content.body.childNodes[0].childNodes;
				for(var c = 0; c <= childs.length - 1; c++) {
					if(childs[c].nodeName == 'P') {
						childs[c].removeAttribute('style');
					}
				}
				UltraEdit.curPos();
				var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
				var p = UltraEdit.searchParents(node, 'P');
				if(p.hasAttribute('style')) {
					p.removeAttribute('style');
					p.setAttribute('style', 'background-color: #dde5ff');
				} else {
					p.setAttribute('style', 'background-color: #dde5ff');
				}
			}
			UltraEdit.interfaceUpdate(e) ;
		});

		// KEY UP
		Flexi(this.iFrame.contentWindow.document).keyup(function(e) {
				var e = (_IE()) ? UltraEdit.iFrame.contentWindow.event : e;
				if(_IE()) {
					//UltraEdit.interfaceUpdate(e) ;
				} else {
					navigation_key = 0;
					var navigation_keys = [1, 8, 13, 33, 34, 35, 36, 37, 38, 39, 40, 46];

					for(k in navigation_keys) {
						if(e.which == navigation_keys[k]) {
							UltraEdit.interfaceUpdate(e);
							break;
						}
					}
				}
				UltraEdit.curPos();
				var anchorNode = UltraEdit.selection.anchorNode;
				var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
				if(node && node.nodeType == 1 && (node.getAttribute('alt') == 'tagEnd' || node.getAttribute('alt') == 'tagClose') && anchorNode && anchorNode.nodeValue && UltraEdit.curRange.startOffset == anchorNode.nodeValue.length) {
					if(node.nextSibling && node.nextSibling.nodeName == '#text') {
						UltraEdit.curRange.setStart(node.nextSibling, 0);
						UltraEdit.curRange.setEnd(node.nextSibling, 0);
					} else {
						var text = UltraEdit.nodeCreate('span',
								[ {name: 'class',      value: 'text'},
								  {name: 'alt',        value: 'text'} ]);
						var textval = UltraEdit.text('');
						text.appendChild(textval);
						node.parentNode.insertBefore(text, node.nextSibling);
						UltraEdit.curRange.setStart(textval, 0);
						UltraEdit.curRange.setEnd(textval, 0);
					}
				} else if(node.nodeType == 1 && node.getAttribute('alt') == 'tagEnd' && UltraEdit.curRange.startOffset == 0) {
					if(node.previousSibling && node.nodeType == 1 && node.getAttribute('alt') == 'attr') {
						UltraEdit.curRange.setStart(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
						UltraEdit.curRange.setEnd(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
					} else {
						var text = UltraEdit.nodeCreate('span',
								[ {name: 'class',      value: 'attr'},
								  {name: 'alt',        value: 'attr'} ]);
						var textval = UltraEdit.text('');
						text.appendChild(textval);
						node.parentNode.insertBefore(text, node);
						UltraEdit.curRange.setStart(textval, 0);
						UltraEdit.curRange.setEnd(textval, 0);
					}
				} else if(node.nodeType == 1 && (node.getAttribute('alt') == 'tag' || node.getAttribute('alt') == 'tagClose') && UltraEdit.curRange.startOffset == 0) {
					if(node.previousSibling && node.previousSibling.nodeName == '#text') {
						UltraEdit.curRange.setStart(node.previousSibling, 0);
						UltraEdit.curRange.setEnd(node.previousSibling, 0);
					} else {
						var text = UltraEdit.nodeCreate('span',
								[ {name: 'class',      value: 'text'},
								  {name: 'alt',        value: 'text'} ]);
						var textval = UltraEdit.text('');
						text.appendChild(textval);
						node.parentNode.insertBefore(text, node);
						UltraEdit.curRange.setStart(textval, 0);
						UltraEdit.curRange.setEnd(textval, 0);
					}
				} else if(node.nodeType == 1 && node.getAttribute('alt') == 'tagText' && anchorNode && UltraEdit.curRange.startOffset == anchorNode.nodeValue.length) {
					if(node.nextSibling && node.nextSibling.nodeType == 1 && node.nextSibling.getAttribute('alt') == 'attrText') {
						UltraEdit.curRange.setStart(node.nextSibling.firstChild, 0);
						UltraEdit.curRange.setEnd(node.nextSibling.firstChild, 0);
					} else if(node.previousSibling && node.previousSibling.nodeType == 1 && node.previousSibling.getAttribute('alt') == 'attrText'){
							if(node.nextSibling  && node.nextSibling.nodeType == 1 && node.nextSibling.getAttribute('alt') == 'attr') {
								UltraEdit.curRange.setStart(node.nextSibling.firstChild, 0);
								UltraEdit.curRange.setEnd(node.nextSibling.firstChild, 0);
							} else {
								var attr = UltraEdit.nodeCreate('span',
										[ {name: 'class',      value: 'attr'},
										  {name: 'alt',        value: 'attr'} ]);
								var textval = UltraEdit.text('');
								attr.appendChild(textval);
								node.parentNode.insertBefore(attr, node.nextSibling);
								UltraEdit.curRange.setStart(textval, 0);
								UltraEdit.curRange.setEnd(textval, 0);
							}
					}
				} else if(node.nodeType == 1 && node.getAttribute('alt') == 'tagText' && UltraEdit.curRange.startOffset == 0) {
					if(node.previousSibling && node.previousSibling.nodeType == 1 && node.previousSibling.getAttribute('alt') == 'attrText') {
						//alert(node.previousSibling.firstChild);
						UltraEdit.curRange.setStart(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
						UltraEdit.curRange.setEnd(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
					} else if(node.previousSibling && node.previousSibling.nodeType == 1 && node.previousSibling.getAttribute('alt') == 'attr') {
						UltraEdit.curRange.setStart(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
						UltraEdit.curRange.setEnd(node.previousSibling.firstChild, node.previousSibling.firstChild.length);
//						var text = UltraEdit.nodeCreate('span',
//								[ {name: 'class',      value: 'text'},
//								  {name: 'alt',        value: 'text'} ]);
//						var textval = UltraEdit.text('');
//						text.appendChild(textval);
//						node.parentNode.insertBefore(text, node);
//						UltraEdit.curRange.setStart(textval, 0);
//						UltraEdit.curRange.setEnd(textval, 0);
					}
				}
				if(UltraEdit.newLine) {
					setTimeout(function(){
						UltraEdit.curPos();
						var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
						var p = UltraEdit.searchParents(node, 'P');

						p.previousSibling.removeAttribute('style');

						if(p.hasAttribute('style')) {
							p.removeAttribute('style');
							p.setAttribute('style', 'background-color: #dde5ff');
						} else {
							p.setAttribute('style', 'background-color: #dde5ff');
						}
						UltraEdit.newLine = 0;
					}, 100);
				}
				if(UltraEdit.tagStart == 1) {
					UltraEdit.tagStart = 2;
					setTimeout(function() {
						UltraEdit.curPos();
						var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
						var tagNode = UltraEdit.nodeCreate('span',
								[ {name: 'class',      value: 'tag'},
								  {name: 'alt',        value: 'tag'},
								  {name: '_moz_dirty', value: ''} ]);

						var tagBegining = UltraEdit.text('<');
						tagNode.appendChild(tagBegining);
						//if(node.getAttribute('class') == 'text') {
							//if(node.nextSibling) {
								//node.parentNode.insertBefore(tagNode, node.nextSibling);
							//} else {

							//}
							UltraEdit.curRange.insertNode(tagNode);
							UltraEdit.curRange.selectNode(tagBegining);
							UltraEdit.curRange.setStart(tagBegining, 1);
							UltraEdit.curRange.setEnd(tagBegining, 1);
						//} else{

						//}
					}, 100);
				}
				if(UltraEdit.tagStart == 2 && navigation_keys.indexOf(e.which) < 0) {
					setTimeout(function() {
						var char = (e.shiftKey) ? String.fromCharCode(keykode) : String.fromCharCode(keykode).toLowerCase();
						UltraEdit.curPos();
						var node =  UltraEdit.selection.anchorNode;
						var partialTag = node.nodeValue.slice(1);
						var matchedTags = new Array();
						var ids = new Array();
						for(var o in UltraEdit.tagsTable) {

							if(typeof UltraEdit.tagsTable[o] == 'object') {
								if(partialTag.length && partialTag == UltraEdit.tagsTable[o].name.substring(0, partialTag.length)) {
									matchedTags.push(UltraEdit.tagsTable[o].name);
									ids.push(o);
								}
							}
						}
						//var coords = UltraEdit.nodePos(node);
						//alert(coords.left + ' ' + coords.top);
						Flexi('#ue_hint', UltraEdit.content).remove();
						if(partialTag.length) {
							UltraEdit.createHint(node, matchedTags, ids);
						}
					}, 250);
				}
			});

		// KEY DOWN
		Flexi(this.iFrame.contentWindow.document).keydown(function(e) {

			var e = (_IE()) ? UltraEdit.iFrame.contentWindow.event : e;
			e.which = (_IE()) ? e.keyCode : e.which;

			UltraEdit.curPos();
			var anchorNode = UltraEdit.selection.anchorNode;
			var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;

			var navigation_keys = [1, 8, 13, 33, 34, 35, 36, 37, 38, 39, 40, 46];

			if(UltraEdit.htmlView) {

				/* behaviour in tag node */
				if(node.nodeType == 1 && node.getAttribute('alt') == 'tag') {
					if(e.which == 13 || e.which == 32) {
						var stop = UltraEdit.htmlAttr(node, e.which);
						if(UltraEdit.hintActive < 0) {
							return stop;
						}
					} else if(e.which == 190) {
						//UltraEdit.htmlCloseTag(node);
					}
				}
				/* behaviour in attribute node */
				else if(node.nodeType == 1 && node.getAttribute('alt') == 'attr') {
					if(e.which == 107) {
						return UltraEdit.htmlEmptyAttrText(node);
					}
					if(e.which == 8) {
						UltraEdit.joinPrev(node);
					}
				}
				/* behaviour in attribute value node */
				else if(node.nodeName == 1 && node.getAttribute('alt') == 'attrText') {
					/* " PRESSED */
					if(e.which == 222) {
						UltraEdit.closeAttrText(node);
					}
				}
				/* < PRESSED */
				if(UltraEdit.htmlView && e.shiftKey && e.which == 188 && !UltraEdit.tagStart && node.nodeType == 1 && node.getAttribute('alt') == 'text') {
					UltraEdit.tagStart = 1;
					return false;
				}
				/* > PRESSED */
				if(UltraEdit.htmlView && e.shiftKey && e.which == 190 && UltraEdit.tagStart) {
					UltraEdit.htmlCloseTag();
					return false;
				}
				/* TAB PRESSED */
				if(e.which == 9) {
					UltraEdit.insertTab(node);
					return false;
				}

				/* lines highlighting */
				if(e.which == 38 || e.which == 40 || e.which == 13) {
					UltraEdit.newLine = (e.which == 13) ? 1 : 0;
					var p = UltraEdit.searchParents(node, 'P');

					if(p.nodeName != 'BODY') {
						if(e.which == 38 && p.previousSibling && p.previousSibling.nodeName == 'P') {
							if(p.hasAttribute('style')) {
								p.removeAttribute('style');
							}
							if(p.previousSibling.hasAttribute('style')) {
								p.previousSibling.removeAttribute('style');
								p.previousSibling.setAttribute('style', 'background-color: #dde5ff');
							} else {
								p.previousSibling.setAttribute('style', 'background-color: #dde5ff');
							}
						} else if(e.which == 40 && p.nextSibling && p.nextSibling.nodeName == 'P'){
							if(p.hasAttribute('style')) {
								p.removeAttribute('style');
							}
							//alert(p.nextSibling + p.nextSibling.nodeName);
							if(p.nextSibling.hasAttribute('style')) {
								p.nextSibling.removeAttribute('style');
								p.nextSibling.setAttribute('style', 'background-color: #dde5ff');
							} else {
								p.nextSibling.setAttribute('style', 'background-color: #dde5ff');
							}
						}
					} else {
						if(p.nextSibling.hasAttribute('style')) {
							p.nextSibling.removeAttribute('style');
						}
						p.firstChild.setAttribute('style', 'background-color: #dde5ff');
					}
				}

				/* hint window active */
				if(UltraEdit.hintActive >= 0) {
					if(e.which == 38 && UltraEdit.hintActive - 1 >= 0 && Flexi('#ue_hint', UltraEdit.content)[0].childNodes[UltraEdit.hintActive - 1]) {
						/* UP ARROW pressed */
						Flexi(Flexi('#ue_hint', UltraEdit.content)[0].childNodes[UltraEdit.hintActive - 1], this.content).css('background-color', '#002277').css('color', '#ffffff');
						Flexi(Flexi('#ue_hint', UltraEdit.content)[0].childNodes[UltraEdit.hintActive], this.content).css('background-color', 'transparent').css('color', '#444444');
						UltraEdit.hintActive -= 1;
						return false;
					} else if(e.which == 40  && Flexi('#ue_hint', UltraEdit.content)[0].childNodes[UltraEdit.hintActive + 1]) {
						/* DOWN ARROW pressed */
						Flexi(Flexi('#ue_hint', UltraEdit.content)[0].childNodes[UltraEdit.hintActive + 1], this.content).css('background-color', '#002277').css('color', '#ffffff');
						Flexi(Flexi('#ue_hint', UltraEdit.content)[0].childNodes[UltraEdit.hintActive], this.content).css('background-color', 'transparent').css('color', '#444444');
						UltraEdit.hintActive += 1;
						return false;
					} else if(e.which == 13) {
						/* ENTER pressed */
						UltraEdit.tagInsert();
						return false;

					} else if(e.which == 27) {
						/* ESCAPE pressed */
						Flexi('#ue_hint', UltraEdit.content).remove();
						UltraEdit.hintActive = -1;
						UltraEdit.tagStart = 0;
						return false;
					} else {
						return true;
					}
				}

			}

			Flexi('#comments_inside7').html(' ' + UltraEdit.htmlView + e.shiftKey + e.which + UltraEdit.tagStart);

			if(_IE()) {
				//UltraEdit.interfaceUpdate(e) ;
			} else {
				UltraEdit.curPos();
				node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
				Flexi('.comments_news_body').html(e.which + node.nodeName);

			}
		});
		Flexi(this.iFrame.contentWindow.document).keypress(function(e) {
			var e = (_IE()) ? UltraEdit.iFrame.contentWindow.event : e;

			/* ENTER pressed */
			if(e.which == 13) {
				/* get current node */
				UltraEdit.curPos();
				node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
				/* insert paragraph on the end */
				if(UltraEdit.isLast(node)) {
					/* get parents chain */
					var nodes = new Array();
					var tmp_node = node;
					while(tmp_node.nodeName != 'P') {
						nodes.push(tmp_node);
						tmp_node = tmp_node.parentNode;
					}
					/* insert empty paragraph */
					UltraEdit.insertParagraph(tmp_node);
					/* insert parents chain in new paragraph */
					for(var n = nodes.length - 1; n >= 0; n--) {
						var style = nodes[n].getAttribute('style');
						UltraEdit.insertTag(nodes[n].nodeName, (style) ? style : '', '');
					}
					UltraEdit.interfaceUpdate(e);
					return false;
				}
			}
			UltraEdit.interfaceUpdate(e) ;
		});

		if(params) {
			switch(params.interface) {
				case 'extended':
					this.initInterface('extended');
					break;
				case 'custom':
					break;
				default:
					this.initInterface('clasic');
			}
		} else {
			this.initInterface('clasic');
		}

	},

	setEnvironment: function(params) {
		if(params) {
			switch(params.environment) {
				case 'custom':
					break;
				case 'file':

					break;
				case 'element':
					this.getEnvironmentFromElement(params.elementSelector);
					break;
				default:
					this.setDefaultEnvironment();
			}
		} else {
			this.setDefaultEnvironment();
		}
	},

	setDefaultEnvironment: function(inst) {
		//alert(inst.content);
		inst.content.body.setAttribute('style', this.initialStyle + ' width: ' + this.editorWidth);
	},

	setCustomEnvironment: function(inst, params) {
		if(inst.initialized) {
			UltraEdit.setUserEnv(inst, params);
		} else {
			setTimeout(UltraEdit.setCustomEnvironment, 100, inst, params);
		}
	},

	setUserEnv: function(inst, params) {
		//this.inst = inst;
		this.initialStyle = (params.initialStyle) ? params.initialStyle : this.initialStyle;
		this.editorWidth = (params.editorWidth) ? params.editorWidth : this.editorWidth;
		this.exportCss = (params.exportCss) ? params.exportCss : this.exportCss;
		if(params.cssFiles) {
			var head = inst.content.documentElement.childNodes[0];
			for(var i = 0; i < params.cssFiles.length; i ++) {
				var cssNode = document.createElement('link');
				cssNode.type = 'text/css';
				cssNode.rel = 'stylesheet';
				cssNode.href = params.cssFiles[i];
				cssNode.media = 'screen';
				head.appendChild(cssNode);
			}
		}
		setTimeout(function(inst) {
			Flexi('body', inst.content).css('background','transparent none no-repeat 0 0');
			Flexi('html', inst.content).css('background','#ffffff none no-repeat 0 0');
		}, 200, inst);
		this.setDefaultEnvironment(inst);
	},

	getEnvironmentFromElement: function(selector) {
		this.content.body.setAttribute('style',
				'font-family: ' + this.css(Flexi(selector)[0], 'font-family') + '; ' +
				'font-size: ' + this.css(Flexi(selector)[0], 'font-size') + '; ' +
				'color: ' + this.css(Flexi(selector)[0], 'color') + '; ' +
				'text-align: ' + this.css(Flexi(selector)[0], 'text-align') + '; ' +
				'width: ' + this.css(Flexi(selector)[0], 'width') + '; ' +
				'padding: ' + this.css(Flexi(selector)[0], 'padding')
		);
	},

	css: function(element, css_param, contextWindow) {
		contextWindow =  contextWindow | window;
		var css = contextWindow.getComputedStyle(element, null).getPropertyValue(css_param);
		return css;
	},

	insertTab: function(node) {
		var indent = this.nodeCreate('span',
				[ {name: 'style',      value: 'padding-left: 1.3em'},
				  {name: '_moz_dirty', value: ''} ]);
		node.parentNode.insertBefore(this.text(''), node);
		node.parentNode.insertBefore(indent, node);
	},

	htmlCloseTag: function() {
		UltraEdit.curPos(this.inst);
		var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;

		var htm = (UltraEdit.openedTag) ? UltraEdit.openedTag : node.firstChild.nodeValue.substring(1);

		tag = UltraEdit.nodeCreate('span',
				[ {name: 'class',      value: 'tag'},
				  {name: 'alt',        value: 'tagClose'} ]);
		tag.appendChild(UltraEdit.text('</' + htm + '>'));
		node.parentNode.insertBefore(tag, node.nextSibling);

		tag = UltraEdit.nodeCreate('span',
				[ {name: 'class',      value: 'text'},
				  {name: 'alt',        value: 'text'} ]);
		var text = UltraEdit.text('');
		tag.appendChild(text);
		node.parentNode.insertBefore(tag, node.nextSibling);

		var tag = UltraEdit.nodeCreate('span',
				[ {name: 'class',      value: 'tag'},
				  {name: 'alt',        value: 'tagEnd'},
				  {name: '_moz_dirty', value: ''} ]);
		tag.appendChild(UltraEdit.text('>'));
		node.parentNode.insertBefore(tag, node.nextSibling);

		UltraEdit.curRange.setStart(text, 0);
		UltraEdit.curRange.setEnd(text, 0);

		Flexi('#ue_hint', UltraEdit.content).remove();
		UltraEdit.tagStart = 0;
		UltraEdit.hintActive = -1;
		UltraEdit.openedTag = '';
	},

	tagInsert: function() {
		UltraEdit.curPos(this.inst);
		var node = (UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode;
		//alert(node + ' ' + node.firstChild + node.firstChild.nodeValue + node.innerHTML + node.previousSibling.innerHTML);
		var tag = UltraEdit.nodeCreate('span',
				[ {name: 'class',      value: 'tag'},
				  {name: 'alt',        value: 'tag'},
				  {name: '_moz_dirty', value: ''} ]);
		node.parentNode.insertBefore(tag, node);

		Flexi(Flexi('#ue_hint', this.inst.content)[0].childNodes[this.inst.hintActive], this.inst.content).attr('id').slice(10);
		var tagId = Flexi(Flexi('#ue_hint', this.inst.content)[0].childNodes[this.inst.hintActive], this.inst.content).attr('id').slice(10);

		tag.appendChild(UltraEdit.text('<' + UltraEdit.tagsTable[tagId].name));
		var firstEmpty = null;
		for(var t = 0; t<= UltraEdit.tagsTable[tagId].attrs.length - 1; t++) {
			var name = UltraEdit.nodeCreate('span',
					[ {name: 'class',      value: 'attr'},
					  {name: 'alt',        value: 'attr'} ]);
			var textAttr = UltraEdit.text(' ' + UltraEdit.tagsTable[tagId].attrs[t]);
			name.appendChild(textAttr);

			var tagTextStart = UltraEdit.nodeCreate('span',
					[ {name: 'class', value: 'text'},
					  {name: 'alt',   value: 'tagText'} ]);
			tagTextStart.appendChild(UltraEdit.text('="'));

			var value = UltraEdit.nodeCreate('span',
					[ {name: 'class',      value: 'attrText'},
					  {name: 'alt',        value: 'attrText'} ]);

			var textAttrValue = UltraEdit.text(UltraEdit.tagsTable[tagId].attrsValues[t]);
			if(!textAttrValue.nodeValue && !firstEmpty) {
				firstEmpty = textAttrValue;
			}
			value.appendChild(textAttrValue);

			var tagTextEnd = UltraEdit.nodeCreate('span',
					[ {name: 'class', value: 'text'},
					  {name: 'alt',   value: 'tagText'} ]);
			tagTextEnd.appendChild(UltraEdit.text('"'));

			node.parentNode.insertBefore(name, node);
			node.parentNode.insertBefore(tagTextStart, node);
			node.parentNode.insertBefore(value, node);
			node.parentNode.insertBefore(tagTextEnd, node);
		}
		tag = UltraEdit.nodeCreate('span',
				[ {name: 'class',      value: 'tag'},
				  {name: 'alt',        value: 'tagEnd'} ]);
		if(UltraEdit.tagsTable[tagId].isSingle) {
			tag.appendChild(UltraEdit.text(' />'));
			node.parentNode.insertBefore(tag, node);
			if(firstEmpty) {
				UltraEdit.curRange.setStart(firstEmpty, 0);
				UltraEdit.curRange.setEnd(firstEmpty, 0);
			}
		} else {
			tag.appendChild(UltraEdit.text('>'));
			node.parentNode.insertBefore(tag, node);
			tag = UltraEdit.nodeCreate('span',
					[ {name: 'class',      value: 'text'},
					  {name: 'alt',        value: 'text'} ]);
			var text = UltraEdit.text('');
			tag.appendChild(text);
			node.parentNode.insertBefore(tag, node);
			tag = UltraEdit.nodeCreate('span',
					[ {name: 'class',      value: 'tag'},
					  {name: 'alt',        value: 'tagClose'} ]);
			tag.appendChild(UltraEdit.text('</' + UltraEdit.tagsTable[tagId].name + '>'));
			node.parentNode.insertBefore(tag, node);

			UltraEdit.curRange.setStart(text, 0);
			UltraEdit.curRange.setEnd(text, 0);
		}
		//alert(node.innerHTML + 'XXX' + node.previousSibling.innerHTML + 'YYY' + node.previousSibling.previousSibling.previousSibling.previousSibling.innerHTML);
		Flexi(node.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling, this.inst.content).remove();
		Flexi('#ue_hint', this.inst.content).remove();

		this.inst.hintActive = -1;
		this.inst.tagStart = 0;
	},

	joinPrev: function(node) {
		if(node.previousSibling && node.previousSibling.nodeType == 1 && node.previousSibling.getAttribute('alt') == 'tag' && this.curRange.startOffset == 0 || this.curRange.startOffset == 1) {
			var attrText = node.firstChild.nodeValue;
			var pos = (node.previousSibling || node.previousSibling.firstChild) ? node.previousSibling.firstChild.nodeValue.length + this.curRange.startOffset : this.curRange.startOffset;

			var child = node.previousSibling.removeChild(node.previousSibling.firstChild);
			node.removeChild(node.firstChild);
			node.previousSibling.appendChild(this.text(child.nodeValue + attrText));
			this.curRange.setStart(node.previousSibling.firstChild, pos);
			this.curRange.setEnd(node.previousSibling.firstChild, pos);
		}
	},

	htmlAttr: function(node, code) {
		var space = (code == 13) ? '' : '';
		//var space = (this.hintActive >= 0) ? '' : space;
		var pos = (code == 13) ? 0 : 0;

		if(node.firstChild && this.curRange.startOffset == node.firstChild.nodeValue.length) {
			var afterbreak = '';
			var cur_pos = node.firstChild.nodeValue.length;
		} else if(node.firstChild) {
			var cur_pos = this.curRange.startOffset;
			var afterbreak = node.firstChild.nodeValue.slice(this.curRange.startOffset);
		}

		this.openedTag = node.firstChild.nodeValue.substring(1, cur_pos);

		var attr = this.nodeCreate('span',
				[ {name: 'class',      value: 'attr'},
				  {name: 'alt',        value: 'attr'} ]);
		var attrText = this.text(space + afterbreak);
		attr.appendChild(attrText);
		node.parentNode.insertBefore(attr, node.nextSibling);

		if(node.firstChild && this.curRange.startOffset != node.firstChild.nodeValue.length) {
			var child = node.removeChild(node.firstChild);
			node.appendChild(this.text(child.nodeValue.substring(0, cur_pos)));
		}

		this.curRange.setStart(attrText, pos);
		this.curRange.setEnd(attrText, pos);

		return (code == 13) ? true : false;
	},

	htmlEmptyAttrText: function(node) {
		var tagTextEnd = this.nodeCreate('span',
				[ {name: 'class',      value: 'text'},
				  {name: 'alt',        value: 'tagText'} ]);
		tagTextEnd.appendChild(this.text('"'));
		node.parentNode.insertBefore(tagTextEnd, node.nextSibling);

		var attrVal = this.nodeCreate('span',
				[ {name: 'class',      value: 'attrText'},
				  {name: 'alt',        value: 'attrText'} ]);
		var attrValText = this.text('');
		attrVal.appendChild(attrValText);
		node.parentNode.insertBefore(attrVal, node.nextSibling);

		var tagText = this.nodeCreate('span',
				[ {name: 'class',      value: 'text'},
				  {name: 'alt',        value: 'tagText'} ]);
		tagText.appendChild(this.text('="'));
		node.parentNode.insertBefore(tagText, node.nextSibling);

		this.curRange.setStart(attrValText, 0);
		this.curRange.setEnd(attrValText, 0);

		return false;
	},

	nodePos: function(node) {
		var depth = 0;
		var coords = Flexi(node.parentNode).offset();
		return coords;
	},

	createHint: function(node, elements, ids) {
		if(elements.length) {
			this.inst.hintActive = 0;
			var coords = Flexi(node.parentNode).offset();
			var bl = this.inst.content.createElement('blockquote');
			bl.setAttribute('style', 'background-color: #ffffaa; color: #444444; font-family: "Courier New"; padding: 1px; border: 1px solid #666666; position: absolute; z-index: 100; top: ' + coords.top + 'px; left: ' + coords.left + 'px;');
			bl.setAttribute('id', 'ue_hint');
			for(var i = 0; i <= elements.length - 1; i++) {
				var p = this.inst.content.createElement('p');
				if(i == 0) {
					p.setAttribute('style', 'padding: 0 30px 0 5px; background-color: #002277; color: #ffffff;');
				} else {
					p.setAttribute('style', 'padding: 0 30px 0 5px;');
				}
				p.setAttribute('id', 'ue_tag_id_' + ids[i]);
				p.appendChild(this.inst.content.createTextNode('<> ' + elements[i]));
				bl.appendChild(p);
			}
			this.inst.content.body.appendChild(bl);
		}
	},

	empty: function(inst) {
		nodes = inst.content.body.childNodes;
		inst.content.body.innerHTML = '';
//		for(var c = 0; c <= nodes.length - 2; c++) {
//			//if(nodes[c+1]) {
//				nodes[0].parentNode.removeChild(nodes[c]);
//			//}
//		}
	},

	isLast: function(node) {
		while(node.nodeName != 'BODY') {
			//alert(node.nodeName + )
			if(node.nextSibling && node.nextSibling.nodeValue && node.nextSibling.nodeValue.length) {
				return false;
			}
			node = node.parentNode;
		}
		return true;
	},



	searchParents: function(node, name) {
		while(name != node.nodeName && node.nodeName != 'BODY') {
			node = node.parentNode;
		}
		return node;
	},

	searchChildren: function(node, name) {


	},

	curPos: function(inst) {
		if(_IE()) {
			this.selection = inst.content.selection;
			if (this.selection.type=="Control") {
				// control selection
				this.curRange = this.selection.createRange();
				if (this.curRange.length==1) {
					var elem = this.curRange.item(0);
				}
				else {
					// multiple control selection
					//return null
				}
			} else {
				this.curRange = this.selection.createRange();
				var elem = this.curRange.parentElement();
			}
			//return getAncestor(elem, filter);
		} else {
			inst.window.focus();
			this.selection = inst.window.getSelection();
			this.curRange = this.selection.getRangeAt(0);
			range = this.curRange.cloneRange();
			range.setStart(inst.content.body, 0);
			this.cursorPosition = range.toString().length;
		}
	},

	interfaceStatus: function() {
		this.UE = UltraEdit;
		this.bold = 0;
		this.italic = 0;
		this.underline = 0;
		this.fontSize = '';
		this.font = '';
		this.fontColor = '';
		this.left = 0;
		this.center = 0;
		this.right = 0;
//		this.justify = 0;
		this.uli = 0;
		this.oli = 0;
//		this.link = 0;
		this.sup = 0;
		this.sub = 0;
		this.strike = 0;
		this.styleString = '';
		this.curFind = document.createTextNode('');
		this.curFindTag = document.createTextNode('');
		this.instance = document.createTextNode('');

		this.curNodeForTag = {};

		this.initFormattingMemory = function() {
			this.curNodeForTag.font = UltraEdit.inst.content.body;
			this.curNodeForTag.fontSize = UltraEdit.inst.content.body;
			this.curNodeForTag.fontColor = UltraEdit.inst.content.body;
		},

		this.testBold = function(param) {
			//alert('bold');
			if(param.constructor == String) {
				this.bold = (/font-weight: (bold|bolder)/.test(param)) ? 1 : 0;
			} else if(typeof param == 'object' && param.nodeType == 1) {
				this.bold = (param.nodeName == 'STRONG' || param.nodeName == 'B') ? 1 : 0;
				this.curFind = (this.bold) ? param : this.curFind;
				this.curFindTag = (this.bold) ? document.createTextNode('bold') : this.curFindTag;
			}
			return this.bold;
		};
		this.testItalic = function(param) {
			if(param.constructor == String) {
				this.italic = (/font-style: italic/.test(param)) ? 1 : 0;
			} else if(typeof param == 'object' && param.nodeType == 1) {
				this.italic = (param.nodeName == 'EM' || param.nodeName == 'I') ? 1 : 0;
				this.curFind = (this.italic) ? param : this.curFind;
				this.curFindTag = (this.italic) ? document.createTextNode('italic') : this.curFindTag;
			}
			return this.italic;
		};
		this.testUnderline = function(param) {
			//alert(param);
			if(param.constructor == String) {
				this.underline = (/text-decoration: underline/.test(param)) ? 1 : 0;
			} else if(typeof param == 'object' && param.nodeType == 1) {
				if(param.nodeName == 'U') {
					this.underline = 1;//(param.nodeName == 'U') ? 1 : 0;
				} else {
					if(param.hasAttribute('style')) {
						var style = param.getAttribute('style');
						this.underline = (/text-decoration: underline/.test(style)) ? 1 : 0;
					}
				}
				this.curFind = (this.underline) ? param : this.curFind;
				this.curFindTag = (this.underline) ? document.createTextNode('underline') : this.curFindTag;
			}
			return this.underline;
		};
		this.testSup = function(param) {
			if(typeof param == 'object' && param.nodeType == 1) {
				this.sup = (param.nodeName == 'SUP') ? 1 : 0;
				this.curFind = (this.sup) ? param : this.curFind;
				this.curFindTag = (this.sup) ? document.createTextNode('sup') : this.curFindTag;
			}
			return this.sup;
		};
		this.testSub = function(param) {
			if(typeof param == 'object' && param.nodeType == 1) {
				this.sub = (param.nodeName == 'SUB') ? 1 : 0;
				this.curFind = (this.sub) ? param : this.curFind;
				this.curFindTag = (this.sub) ? document.createTextNode('sub') : this.curFindTag;
			}
			return this.sub;
		};
		this.testStrike = function(param) {
			if(param.constructor == String) {
				this.strike = (/text-decoration: line-through/.test(param)) ? 1 : 0;
			} else if(typeof param == 'object' && param.nodeType == 1) {
				if(param.nodeName == 'DEL') {
					this.strike = 1;//(param.nodeName == 'DEL') ? 1 : 0;
				} else {
					if(param.hasAttribute('style')) {
						this.strike = (/text-decoration: line-through/.test(param.getAttribute('style'))) ? 1 : 0;
					}
				}
				this.curFind = (this.strike) ? param : this.curFind;
				this.curFindTag = (this.strike) ? document.createTextNode('strike') : this.curFindTag;
			}
			return this.strike;
		};
		this.testUli = function(param) {
			if(typeof param == 'object' && param.nodeType == 1) {
				this.uli = (param.nodeName == 'UL') ? 1 : 0;
				this.curFind = (this.uli) ? param : this.curFind;
				this.curFindTag = (this.uli) ? document.createTextNode('uli') : this.curFindTag;
			}
			return this.uli;
		};
		this.testOli = function(param) {
			if(typeof param == 'object' && param.nodeType == 1) {
				this.oli = (param.nodeName == 'OL') ? 1 : 0;
				this.curFind = (this.oli) ? param : this.curFind;
				this.curFindTag = (this.oli) ? document.createTextNode('oli') : this.curFindTag;
			}
			return this.uli;
		};
		this.testLeft = function(param) {
			if(param.constructor == String) {
				this.left = (/text-align: left/.test(param)) ? 1 : 0;
			} else if(typeof param == 'object' && param.nodeType == 1) {
				this.left = (UltraEdit.inst.window.getComputedStyle(param, '').getPropertyValue('text-align') == 'left') ? 1 : 0;
				this.center = (this.left) ? 1 : this.center;
				this.right = (this.left) ? 1 : this.right;
				if(param.hasAttribute('style')) {
					this.curFind = (/text-align: left/.test(param.getAttribute('style'))) ? param : this.curFind;
					this.curFindTag = (this.left) ? document.createTextNode('left') : this.curFindTag;
				} else {
					this.curFind = UltraEdit.inst.content.body;
				}
			}
			return this.left;
		};
		this.testCenter = function(param) {
			if(param.constructor == String) {
				this.center = (/text-align: center/.test(param)) ? 1 : 0;
			} else if(typeof param == 'object' && param.nodeType == 1) {
				this.center = (UltraEdit.inst.window.getComputedStyle(param, '').getPropertyValue('text-align') == 'center') ? 1 : 0;
				this.left = (this.center) ? 1 : this.left;
				this.right = (this.center) ? 1 : this.right;
				if(param.hasAttribute('style')) {
					this.curFind = (/text-align: center/.test(param.getAttribute('style'))) ? param : this.curFind;
					this.curFindTag = (this.center) ? document.createTextNode('center') : this.curFindTag;
				} else {
					this.curFind = UltraEdit.inst.content.body;
				}
			}
			return this.center;
		};
		this.testRight = function(param) {
			if(param.constructor == String) {
				this.right = (/text-align: right/.test(param)) ? 1 : 0;
			} else if(typeof param == 'object' && param.nodeType == 1) {
				this.right = (UltraEdit.inst.window.getComputedStyle(param, '').getPropertyValue('text-align') == 'right') ? 1 : 0;
				this.left = (this.right) ? 1 : this.left;
				this.center = (this.right) ? 1 : this.center;
				if(param.hasAttribute('style')) {
					this.curFind = (/text-align: right/.test(param.getAttribute('style'))) ? param : this.curFind;
					this.curFindTag = (this.right) ? document.createTextNode('right') : this.curFindTag;
				} else {
					this.curFind = UltraEdit.inst.content.body;
				}
			}
			return this.right;
		};
		this.testFontSize = function(param, formatNotStrict) {
			if(param.constructor == String) {
				var match = param.match(/font-size: ([1-9][0-9]{0,1}pt)/);
				//Flexi('.comments_news_body').append('!!!MATCH!!!' + match);
				this.fontSize = (match) ? match[1] : this.fontSize;
			} else if(typeof param == 'object' && param.nodeType == 1) {
				//Flexi('.comments_news_body').append('!!!MATCH!!!' + match);
				if(param.hasAttribute('style')) {
					var style = param.getAttribute('style');
					var match = style.match(/font-size: ([1-9][0-9]{0,1}pt)/);
					var size = (match) ? match[1] : '';
					var founded = /font-size: ([1-9][0-9]pt)/.test(param.getAttribute('style'));
					this.curFind = (founded) ? param : this.curFind;
					this.curFindTag = (founded) ? document.createTextNode('fontSize') : this.curFindTag;
					if(match) {
						this.curNodeForTag['fontSize'] = param;
					}
				} else if(param.nodeName == 'BODY'){
					var size = Math.round(3 * parseInt(UltraEdit.inst.window.getComputedStyle(param, '').getPropertyValue('font-size')) / 4) + 'pt';
					this.curFind = UltraEdit.inst.content.body;
				} else {
					var size = '';
				}
				//alert('FONT'+UltraEdit.interfaceObj.fontSize+':'+size+':'+this.fontSize);
				if(UltraEdit.interfaceObj.fontSize == '') {
					jQuery('#editorInterface_' + UltraEdit.inst.editorId + '_fontSize').val(size);
					this.fontSize = UltraEdit.interfaceObj.fontSize = size;
				} else {
					if(size != this.fontSize) {
						jQuery('#editorInterface_' + UltraEdit.inst.editorId + '_fontSize').val('10pt');
						this.fontSize = '';
					}
				}
				this.fontSize =  size;
			}
			//alert(this.fontSize);
			return this.fontSize;
		};
		this.testFont = function(param, formatNotStrict) {
			if(param.constructor == String) {
				var match = param.match(/font-family: ([a-zA-Z _"]+);/);
				this.font = (match) ? match[1] : this.font;
			} else if(typeof param == 'object' && param.nodeType == 1) {
				if(param.hasAttribute('style')) {
					var style = param.getAttribute('style');
					var match = style.match(/font-family: ([a-zA-Z _"]+);/);
					var family = (match) ? match[1] : '';
					var founded = /font-family: ([a-zA-Z _"]+);/.test(param.getAttribute('style'));
					this.curFind = (founded) ? param : this.curFind;
					this.curFindTag = (founded) ? document.createTextNode('font') : this.curFindTag;
				} else if(param.nodeName == 'BODY') {
					var family = UltraEdit.inst.window.getComputedStyle(param, '').getPropertyValue('font-family');
					this.curFind = UltraEdit.inst.content.body;
				} else {
					var family = '';
				}

				//alert('FONTFAMILY'+UltraEdit.interfaceObj.font+':'+family+':'+this.font);
				if(UltraEdit.interfaceObj.font == '') {
					jQuery('#editorInterface_' + UltraEdit.inst.editorId + '_font').val(family);
					this.font = UltraEdit.interfaceObj.font = family;
				} else {
					if(family != this.font) {
						jQuery('#editorInterface_' + UltraEdit.inst.editorId + '_font').val('Verdana');
						this.font = '';
					}
				}
				this.font = family;
			}
			return this.font;
		};
		this.testFontColor = function(param, formatNotStrict) {
			var fromBody = 0;
			if(param.constructor == String) {
				var match = param.match(/^([ ]?color:) (rgb\([0-9]{1,3},[ ]?[0-9]{1,3},[ ]?[0-9]{1,3}\)|#[0-9abcdef]{6})/);
				this.fontColor = (match) ? match[1] : this.fontColor;
				//alert(match);
				//alert('aaa' + param);
				//alert('bbb' + match[1] + match[2]);
				var color = (match) ? match[2] : '';
				jQuery('.color_choice').css('background-color', (match) ? match[2] : '#000000');
			} else if(typeof param == 'object' && param.nodeType == 1) {

				//alert(param + "color: rgb(0,255,153)".match(/color: (rgb\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)|#[0-9abcdef]{6})/));
				if(param.hasAttribute('style') && param.nodeName != 'BODY') {
					var style = param.getAttribute('style');
					var styles = style.split(';');
					for(var s = 0; s < styles.length; s++) {
						var match = styles[s].match(/^([ ]?color:) (rgb\([0-9]{1,3},[ ]?[0-9]{1,3},[ ]?[0-9]{1,3}\)|#[0-9abcdef]{6})/);
						if(match) {
							break;
						}
					}
					var color = (match) ? match[2] : '';
					//var founded = /color: (rgb\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)|#[0-9abcdef]{6})/.test(param.getAttribute('style'));
					//alert(match);
					this.curFind = (match) ? param : this.curFind;
					this.curFindTag = (match) ? document.createTextNode('fontColor') : this.curFindTag;
					if(match) {
						this.curNodeForTag['fontColor'] = param;
					}
					//alert(this.fontColor + this.curFindTag + founded + match + param.getAttribute('style'));
				} else if(param.nodeName == 'BODY') {
					fromBody = 1;
					var color = UltraEdit.inst.window.getComputedStyle(param, '').getPropertyValue('color');
					this.curFind = UltraEdit.inst.content.body;
//					if(typeof this.curNodeForTag['fontColor'] === 'undefined') {
//						this.curNodeForTag['fontColor'] = this.instance.content.body;
//					}
					UltraEdit.interfaceObj.fontColor == '';
				} else {
					var color = '';
				}
				//alert('notStrict'+formatNotStrict);
//				if(typeof formatNotStrict) {
//					alert('notStrict2'+formatNotStrict);
//				}
//				alert('TEST1'+ (UltraEdit.interfaceObj.fontColor == '' || typeof UltraEdit.interfaceObj.fontColor === 'undefined'));
//				alert('TEST2'+ (typeof formatNotStrict === 'undefined' || formatNotStrict == 1));
				//alert('FONTCOLOR'+UltraEdit.interfaceObj.fontColor+':'+color+':'+this.fontColor);
				//alert('FC:'+UltraEdit.interfaceObj.fontColor+' FNS:'+formatNotStrict);
				if(UltraEdit.interfaceObj.fontColor == '' || typeof UltraEdit.interfaceObj.fontColor === 'undefined' || formatNotStrict == 1) {
					jQuery('.color_choice').css('background-color', color);
					if(param.nodeName != 'BODY') {
						this.fontColor = UltraEdit.interfaceObj.fontColor = color;
					} else {
						this.fontColor = color;
					}
				} else {
					if(color != this.fontColor) {
						jQuery('.color_choice').css('background-color', '#000000');
						this.fontColor = '';
					}
				}
			}
			return this.fontColor;
		};

		this.testIsParagraph = function(param) {
			if(param.nodeName == 'P') {
				return 1;
			}
			return 0;
		};

		this.testIsBlock = function(param) {
			if(param.nodeName == 'BLOCKQUOTE' || param.nodeName == 'DIV') {
				return 1;
			}
			return 0;
		};

		this.testIsTableCell = function(param) {
			if(param.nodeName == 'TD') {
				return 1;
			}
			return 0;
		};

		this.testIsFont = function(param) {
			if(param.nodeName == 'SPAN') {
				if(param.hasAttribute('style')) {
					var attr = param.getAttribute('style');
					var match = attr.match(/font-family: ([a-zA-Z _"]+);/);
					if(match) {
						return 1;
					}
				}
			}
			return 0;
		};

		this.testIsFontColor = function(param) {
			if(param.nodeName == 'SPAN') {
				if(param.hasAttribute('style')) {
					var attr = param.getAttribute('style');
					var match = attr.match(/^([ ]?color:) (rgb\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)|#[0-9abcdef]{6})/);
					if(match) {
						return 1;
					}
				}
			}
			return 0;
		};

		this.testIsFontSize = function(param) {
			if(param.nodeName == 'SPAN') {
				if(param.hasAttribute('style')) {
					var attr = param.getAttribute('style');
					var match = attr.match(/font-size: ([1-9][0-9]{0,1}pt)/);
					if(match) {
						return 1;
					}
				}
			}
			return 0;
		};

		this.testTag = function(name, args) {
			if(args) {
				name = 'test' + name.slice(0,1).toUpperCase() + name.slice(1);
				return this[name](args);
			}
			return 0;
		};
		this.testGroupInactivity = function(tag) {
			for(var el in UltraEdit.inst.iElements) {
				//alert(el + tag + UltraEdit.iElements[el].group + UltraEdit.iElements[tag].group + this[el]);
				if(UltraEdit.inst.iElements[tag].group == 0) {
					break;
				} else {
					if(UltraEdit.inst.iElements[el].group == UltraEdit.inst.iElements[tag].group) {
						if(this[el]) {
							return 0;
						}
					}
				}
			}
			return 1;
		};

		this.reset = function(nodeType) {
			if(nodeType) {
				(this[nodeType].constructor == String) ? this[nodeType] = '' : this[nodeType] = 0;
			} else {
				for(var tag in this) {
					if(tag != 'styleString' && (this[tag].constructor == String || this[tag].constructor == Number)) {
						(this[tag].constructor == String) ? this[tag] = '' : this[tag] = 0;
					}
				}
			}
		};

		this.testDOM = function(inst, node, nodeType) {
			this.instance = inst;
			this.reset(nodeType);
			this.initFormattingMemory();
			while(node.nodeName != 'HTML') {
				if(node.hasAttribute('style')) {
					this.styleString = node.getAttribute('style');
				}
				if(!nodeType) {
					for(var tag in this) {
						if(tag != 'styleString' && typeof this[tag] != 'undefined' && (this[tag].constructor == String || this[tag].constructor == Number)) {
							if(!this[tag] && this.testGroupInactivity(tag)) {
								this.UE.setInactive(inst, tag, tag);
								if(this.testTag(tag, this.styleString) || this.testTag(tag, node)) {
									this.UE.setActive(inst, tag, this[tag]);
								}
							}
						}
					}
				} else {
					if(!this[nodeType] && this.testGroupInactivity(nodeType)) {
						this.testTag(nodeType, node);
					}
				}
				node = node.parentNode;
			}
		};
	},

	insertTag: function(tag, styleName, styleValue, beforeNode) {
		this.curPos(this.inst);
		//alert('tag ' + tag);
		var newNode = this.inst.content.createElement(tag);
		if(!_IE()) {
			newNode.setAttribute('_moz_dirty', '');
		}
		//alert(styleName);
		if(styleName != '') {
			if(styleValue == '') {
				newNode.setAttribute('style', styleName);
			} else {
				newNode.setAttribute('style', styleName + ': ' + styleValue);
			}
		}
		var textNode = this.inst.content.createTextNode('');
		newNode.appendChild(textNode);
		if(beforeNode) {
			beforeNode.parentNode.insertBefore(newNode, beforeNode);
		} else {
			this.curRange.insertNode(newNode);
		}
		this.curRange.selectNode(textNode);
		this.curRange.setStart(textNode, 0);
		this.curRange.setEnd(textNode, 0);
	},

	insertParagraph: function(inst, node, noStyle) {
		this.curPos(inst);
		var newNode = inst.content.createElement('p');
		newNode.setAttribute('_moz_dirty', '');
		var textNode = inst.content.createTextNode('');
		newNode.appendChild(textNode);
		var brNode = inst.content.createElement('br');
		brNode.setAttribute('_moz_dirty', '');
		brNode.setAttribute('type', '_moz');
		newNode.appendChild(brNode);
		if(node) {
			if(node.hasAttribute('style') && !noStyle) {
				newNode.setAttribute('style', node.getAttribute('style'));
			}
			newNode = node.parentNode.insertBefore(newNode, node.nextSibling);
		} else {
			this.curRange.insertNode(newNode);
		}
		this.curRange.selectNode(textNode);
		this.curRange.setStart(textNode, 0);
		this.curRange.setEnd(textNode, 0);

	},

	interfaceUpdate: function(inst, e) {
		keykode = (_IE()) ? e.keyCode : e.which;
		//Flexi('.comments_news_body').html(keykode);
		navigation_key = 0;
		navigation_keys = [1, 8, 13, 33, 34, 35, 36, 37, 38, 39, 40, 46];
		for(k in navigation_keys) {
			if(keykode == navigation_keys[k]) {
				navigation_key = 1;
				break;
			}
		}
		//alert(inst.htmlAwaitStart && inst.htmlAwaitStart.constructor == String && inst.htmlAwaitStart != '' && !navigation_key || keykode == 13);
		//Flexi('#content_bar').html(inst.htmlAwaitStart);
		if(inst.htmlAwaitStart && inst.htmlAwaitStart.constructor == String && inst.htmlAwaitStart != '' && !navigation_key || keykode == 13) {
			//alert(inst.htmlAwaitStart);
			var char = (e.shiftKey) ? String.fromCharCode(keykode) : String.fromCharCode(keykode).toLowerCase();
			//Flexi('.comments_news_body').html(this.htmlAwaitStart + char);
			if(_IE()) {
				this.curRange.pasteHTML(inst.htmlAwaitStart + char + inst.htmlAwaitEnd);
				this.curRange.move('character', inst.htmlAwaitEnd.length);
				this.curRange.select();
			} else {
				var elements = inst.htmlAwaitStart.split('!');
				inst.htmlAwaitStart = inst.htmlAwaitEnd = '';
				for(var el in elements) {
					if(elements[el] != '' && typeof elements[el] != 'function') {
						var elParams = elements[el].split(',');
						this.insertTag(elParams[0], elParams[1], elParams[2]);
					}
				}
			}
			inst.htmlAwaitStart = inst.htmlAwaitEnd = '';
			//alert(this.htmlAwaitStart);
		}
		if(navigation_key) {
			//alert(this.selection.anchorNode);
			this.curPos(inst);
			//Flexi('.comments_news_body').html(this.content.body.innerHTML.length);
			if(!_IE() && inst.content.body.innerHTML.length <= 5) {
				this.insertParagraph(inst);
				nodes = inst.content.body.childNodes;
				for(var c = 0; c <= nodes.length - 2; c++) {
					if(nodes[c+1]) {
						nodes[0].parentNode.removeChild(nodes[c+1]);
					}
				}
			}
			var rangeLen = (_IE()) ? this.curRange.text.length : this.curRange.toString().length;
			if(!this.curRange.toString().length) {
				start = end = node = (_IE()) ? this.curRange.parentElement() : ((this.selection.anchorNode.nodeName == '#text') ? this.selection.anchorNode.parentNode : this.selection.anchorNode);
				var parent = node.parentNode;
				temp = start;
				var interface = new this.interfaceStatus();
				interface.testDOM(inst, node);
			} else {
				start = this.curRange.startContainer;
				end = this.curRange.endContainer;

				this.interfaceUpdateBySelection(start, end, new this.interfaceStatus());
				//node = this.curRange.commonAncestorContainer;
				//node = (node.nodeName == '#text') ? node.parentNode : node;

			}
		}
	},

	interfaceObj: {},

	interfaceUpdateBySelection: function(start, end, interfaceOBj) {
//		(this.testSelection(start, start, end, 'bold', 0, 0)) ? this.setActive(inst, 'bold', 'bold'); : this.setInactive(inst, 'bold', 'bold');
		//alert(this.testSelection(start, start, end, 'bold', 0, 0));

		this.interfaceObj = new this.interfaceStatus();
//		if(this.testSelection(start, start, end, 'bold', 0, 0)) {
//			interfaceOBj['bold'] = 1;
//			this.setActive(this.inst, 'bold', 'bold');
//		} else {
//			interfaceOBj['bold'] = 0;
//			this.setInactive(this.inst, 'bold', 'bold');
//		}
//		if(this.testSelection(start, start, end, 'italic', 0, 0)) {
//			interfaceOBj['italic'] = 1;
//			this.setActive(this.inst, 'italic', 'italic');
//		} else {
//			interfaceOBj['italic'] = 0;
//			this.setInactive(this.inst, 'italic', 'italic');
//		}
//		if(this.testSelection(start, start, end, 'underline', 0, 0)) {
//			interfaceOBj['underline'] = 1;
//			this.setActive(this.inst, 'underline', 'underline');
//		} else {
//			interfaceOBj['underline'] = 0;
//			this.setInactive(this.inst, 'underline', 'underline');
//		}
		if(this.testSelection(start, start, end, 'fontColor', 0, 0)) {
			interfaceOBj['fontColor'] = 1;
			this.setActive(this.inst, 'fontColor', this.interfaceObj.fontColor);
		} else {
			interfaceOBj['fontColor'] = 0;
			this.setInactive(this.inst, 'fontColor', 'fontColor');
		}
//		if(this.testSelection(start, start, end, 'fontSize', 0, 0)) {
//			interfaceOBj['fontSize'] = 1;
//			this.setActive(this.inst, 'fontSize', this.interfaceObj.fontSize);
//		} else {
//			interfaceOBj['fontSize'] = 0;
//			this.setInactive(this.inst, 'fontSize', 'fontSize');
//		}
//		if(this.testSelection(start, start, end, 'font', 0, 0)) {
//			interfaceOBj['font'] = 1;
//			this.setActive(this.inst, 'font', this.interfaceObj.font);
//		} else {
//			interfaceOBj['font'] = 0;
//			this.setInactive(this.inst, 'font', 'font');
//		}
//		(this.testSelection(start, start, end, 'italic', 0, 0)) ? this.setActive(inst, 'italic', 'italic'); : this.setInactive(inst, 'italic', 'italic');
//		(this.testSelection(start, start, end, 'underline', 0, 0)) ? this.setActive(inst, 'underline', 'underline'); : this.setInactive(inst, 'underline', 'underline');
	},

	getAllNodesBeetween: function(startNode, endNode, foundedNodes, minChild) {

		if(!foundedNodes) {
			foundedNodes = new Array();
		}
		if(startNode.nodeName == 'BODY' || startNode == endNode) {
			return foundedNodes;
		} else {
			if(startNode.childNodes) {
				if(this.inNodes(startNode)) {
					if(startNode.parentNode.lastChild == startNode) {
						this.getAllNodesBeetween(startNode.nextSibling, endNode, foundedNodes, minChild);
					} else {
						this.getAllNodesBeetween(startNode.parentNode, endNode, foundedNodes, minChild)
					}
				} else {
					if(this.inNodes(minChild)) {
						this.getAllNodesBeetween(startNode.parentNode, endNode, foundedNodes, minChild);
					} else {
						this.getAllNodesBeetween(startNode.childNodes[minChild], endNode, foundedNodes, minChild);
					}
				}
			} else {
				if(startNode.parentNode.lastChild == startNode) {
					this.getAllNodesBeetween(startNode.parentNode, endNode, foundedNodes, minChild);
				} else {
					this.getAllNodesBeetween(startNode.nextSibling, endNode, foundedNodes, minChild);
				}
			}
		}
	},

	setCustomInterface: function(inst, selector, params) {
		if(inst.initialized) {
			UltraEdit.setInterface(inst, selector, params);
		} else {
			setTimeout(UltraEdit.setCustomInterface, 100, inst, selector, params);
		}
	},

	setInterface: function(inst, selector, params) {
		if(selector) {
			var iContainer = Flexi(selector)[0];
		} else {
			var iContainer = document.createElement('div');
			iContainer.setAttribute('style', 'width: 100%; height: 56px;');
			inst.iFrame.parentNode.insertBefore(iContainer, inst.iFrame);
		}
		className = (_IE()) ? 'className' : 'class';
		for(var el in params)  {
			switch(el) {
				case 'basic':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'bold', editorButtonBlock);
					this.iButtonCreate(inst, 'italic', editorButtonBlock);
					this.iButtonCreate(inst, 'underline', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'align':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'left', editorButtonBlock);
					this.iButtonCreate(inst, 'center', editorButtonBlock);
					this.iButtonCreate(inst, 'right', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'lists':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'oli', editorButtonBlock);
					this.iButtonCreate(inst, 'uli', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'lists_ext':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'oli', editorButtonBlock);
					this.iButtonCreate(inst, 'uli', editorButtonBlock);
					this.iButtonCreate(inst, 'outdent', editorButtonBlock);
					this.iButtonCreate(inst, 'indent', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'undo':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'undo', editorButtonBlock);
					this.iButtonCreate(inst, 'redo', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'clipboard':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'cut', editorButtonBlock);
					this.iButtonCreate(inst, 'copy', editorButtonBlock);
					this.iButtonCreate(inst, 'paste', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'code':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'htm', editorButtonBlock);
					this.iButtonCreate(inst, 'php', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'textExt':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'sup', editorButtonBlock);
					this.iButtonCreate(inst, 'sub', editorButtonBlock);
					this.iButtonCreate(inst, 'strike', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'view':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'wysiwyg_view', editorButtonBlock);
					this.iButtonCreate(inst, 'code_view', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'formattings':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iSelectCreate(inst, 'formattings', 'formattingsValues', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'font':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iSelectCreate(inst, 'fontSize', 'sizes', editorButtonBlock);
					this.iSelectCreate(inst, 'font', 'fonts', editorButtonBlock);
					this.colorPickerCreate(inst, 'fontColor', editorButtonBlock);

					iContainer.appendChild(editorButtonBlock);
					//TODO: SprawdziÄ‡ gdzie jest dobindowane ukrywanie tabeli kolorĂłw
					//prawdopodobnie nie bÄ™dzie dziaĹ‚aÄ‡ po wyrwaniu z kontekstu strony (czyĹĽby poniĹĽej?)
					Flexi('#' + inst.editorId + '_fontColor').click(function () { Flexi('#color_picker_' + inst.editorId + '_fontColor').toggle(); });
					Flexi('.color_picker_head').click(function () { return false; });
					Flexi('td[id*=color_picker_' + inst.editorId + '_fontColor]').click(function () {
						color_parts = this.id.split('_');
						UltraEdit.action('fontColor', '#' + UltraEdit.colorValues[color_parts[color_parts.length - 3]] + UltraEdit.colorValues[color_parts[color_parts.length - 2]] + UltraEdit.colorValues[color_parts[color_parts.length - 1]]);
					});
					Flexi('#color_picker_input_' + inst.editorId + '_fontColor').focus(function() {
						UltraEdit.colorInput = 1;
					});
					Flexi('#color_picker_input_' + inst.editorId + '_fontColor').blur(function() {
						UltraEdit.colorInput = 0;
					});
					Flexi().keypress(function(e) {
						var keycode = (_IE()) ? window.event.keyCode : e.which;
						var val = Flexi('#color_picker_input_' + inst.editorId + '_fontColor').val();
						if(val.match(/#[0-9abcdef]{1,6}/)) {
							Flexi('#color_picker_choice_' + inst.editorId + '_fontColor').css('background-color', val + '000000'.slice(0, 7 - val.length));
						}
						if(UltraEdit.colorInput && keycode == 13) {
							if(val.match(/#[0-9abcdef]{6}/)) {
								UltraEdit.action('fontColor', val);
							}
							Flexi('#color_picker_' + inst.editorId + '_fontColor').toggle();
							return false;
						}
						return true;
					});
//					alert(UltraEdit.inst.sel);
//					alert('select#editorInterface_' + UltraEdit.inst.editorId + '_fontSize');
//					alert(Flexi('select#editorInterface_' + UltraEdit.inst.editorId + '_fontSize').length);
					Flexi('select#editorInterface_' + inst.editorId + '_fontSize').change(function () { UltraEdit.action('fontSize', this.selectedIndex) });
					Flexi('select#editorInterface_' + inst.editorId + '_font').change(function () { UltraEdit.action('font', this.selectedIndex) });
					break;
				case 'link':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'link', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'image':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'image', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'link_img':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'image', editorButtonBlock);
					this.iButtonCreate(inst, 'link', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					//Flexi('select#editorInterface_link').click(function () { UltraEdit.link.callback(); });
					break;
				case 'block':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'columns', editorButtonBlock);
					this.iButtonCreate(inst, 'table', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'settings':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');
					this.iButtonCreate(inst, 'settings', editorButtonBlock);
					iContainer.appendChild(editorButtonBlock);
					break;
				case 'specials':
					var editorButtonBlock = document.createElement('div');
					editorButtonBlock.setAttribute(className, 'editor_button_block');

					for(var sb = 0; sb < params[el].length; sb++ ) {
						//alert(params[el]);
						SPbutton = {
								name: params[el][sb].name,
								iconActive: params[el][sb].iconActive,
								iconInactive: params[el][sb].iconInactive,
								click: params[el][sb].click
						};
						//alert(SPbutton.name);
						this.iSPButtonCreate(inst, SPbutton, editorButtonBlock);
					}
					iContainer.appendChild(editorButtonBlock);
					Flexi('#editorInterface_' + inst.editorId + '_' + SPbutton.name).click(function() {
						SPbutton.click();
					});
					break;

			}
			editorButtonBlock = null;
		}

		for(el in inst.iElements) {
			//alert(inst.iElements[el].name);
			if(inst.iElements[el].type == 'button') {
				Flexi('#editorInterface_' + inst.editorId + '_' + inst.iElements[el].name).click(function () { UltraEdit.action(Flexi(this).attr('alt')) });
			}
			if(inst.iElements[el].type == 'special') {
				if(typeof inst.iElements[el].callback == 'function') {
					Flexi('#editorInterface_' + inst.editorId + '_' + inst.iElements[el].name).click( function() {
						el = Flexi(this).attr('alt');
						if(inst.iElements[el].name == 'image') {
							inst.iElements[el].callback('editorPlugin', 'imageInsert');
						} else {
							inst.iElements[el].callback();
						}
					});
				}
			}
		}
	},

	iButtonCreate: function(inst, name, block) {
		button = document.createElement('img');
		button.setAttribute('src', this.hostPath + 'images/main_theme/editor_' + name + '.gif');
		button.setAttribute('alt', name);
		button.setAttribute('id', 'editorInterface_' + inst.editorId + '_' + name);
		block.appendChild(button);

		inst.iElements[name] = this[name];
	},

	iSPButtonCreate: function(inst, sp_button, block) {
		button = document.createElement('img');
		button.setAttribute('src', this.hostPath + sp_button.iconInactive);
		button.setAttribute('alt', sp_button.name);
		button.setAttribute('id', 'editorInterface_'  + inst.editorId + '_' + sp_button.name);
		block.appendChild(button);

		inst.iElements[sp_button.name] = sp_button;
	},

	iSelectCreate: function(inst, name, optionArray, block) {
		select = document.createElement('select');
		select.setAttribute('id', 'editorInterface_' + inst.editorId + '_' + name);
		//select.setAttribute('class', 'editorInterface_select');
		inst.iElements[name] = this[name];
		for(opt in this[optionArray]) {
			if(typeof this[optionArray][opt] != 'function') {
				option = document.createElement('option');
				option.setAttribute('value', this[optionArray][opt]);
				text = document.createTextNode(this[optionArray][opt]);
				option.appendChild(text);
				select.appendChild(option);
			}
		}
		block.appendChild(select);
		setTimeout(function() {
			Flexi('select').css('display', 'block').css('float', 'left');
		}	, 200);
	},

	colorPickerCreate: function(inst, name, block) {
		var div = document.createElement('div');
		div.setAttribute('class', 'color_choice');
		div.setAttribute('id', inst.editorId + '_' + name);
		var hiddenDiv = document.createElement('div');
		hiddenDiv.setAttribute('class', 'hidden');
		hiddenDiv.setAttribute('id', 'color_picker_' + inst.editorId + '_' + name);
		var head = document.createElement('div');
		head.setAttribute('class', 'color_picker_head');
		var choice = document.createElement('div');
		choice.setAttribute('class', 'color_picker_choice');
		choice.setAttribute('id', 'color_picker_choice_' + inst.editorId + '_' +  name);
		var input = document.createElement('input');
		input.setAttribute('class', 'color_picker_input');
		input.setAttribute('id', 'color_picker_input_' + inst.editorId + '_' +  name);
		var clear = document.createElement('div');
		clear.setAttribute('class', 'clear');
		var table = document.createElement('table');
		table.setAttribute('class', 'color_picker');
		table.setAttribute('id', 'color_picker_table_' + inst.editorId + '_' +  name);
		var cellStyle = 'width: 10px; height: 12px; border: 1px solid #000000;';
		table.setAttribute('style', 'border-collapse: collapse; border-spacing: 0');
		for(i = 0; i < 6; i++) {
			row = document.createElement('tr');
			for(j = 0; j < 6; j++) {
				cell = document.createElement('td');
				cell.setAttribute('style', cellStyle + ' background-color: #00' + this.colorValues[j] + this.colorValues[i]);
				cell.setAttribute('id', 'color_picker_' + inst.editorId + '_' + name + '_0_' + j + '_' + i);
				row.appendChild(cell);
			}
			for(j = 0; j < 6; j++) {
				cell = document.createElement('td');
				cell.setAttribute('style', cellStyle + ' background-color: #33' + this.colorValues[j] + this.colorValues[i]);
				cell.setAttribute('id', 'color_picker_' + inst.editorId + '_' + name + '_1_' + j + '_' + i);
				row.appendChild(cell);
			}
			for(j = 0; j < 6; j++) {
				cell = document.createElement('td');
				cell.setAttribute('style', cellStyle + ' background-color: #66' + this.colorValues[j] + this.colorValues[i]);
				cell.setAttribute('id', 'color_picker_' + inst.editorId + '_' + name + '_2_' + j + '_' + i);
				row.appendChild(cell);
			}
			table.appendChild(row);
		}
		for(i = 0; i < 6; i++) {
			row = document.createElement('tr');
			for(j = 0; j < 6; j++) {
				cell = document.createElement('td');
				cell.setAttribute('style', cellStyle + ' background-color: #99' + this.colorValues[j] + this.colorValues[i]);
				cell.setAttribute('id', 'color_picker_' + inst.editorId + '_' + name + '_3_' + j + '_' + i);
				row.appendChild(cell);
			}
			for(j = 0; j < 6; j++) {
				cell = document.createElement('td');
				cell.setAttribute('style', cellStyle + ' background-color: #cc' + this.colorValues[j] + this.colorValues[i]);
				cell.setAttribute('id', 'color_picker_' + inst.editorId + '_' + name + '_4_' + j + '_' + i);
				row.appendChild(cell);
			}
			for(j = 0; j < 6; j++) {
				cell = document.createElement('td');
				cell.setAttribute('style', cellStyle + ' background-color: #ff' + this.colorValues[j] + this.colorValues[i]);
				cell.setAttribute('id', 'color_picker_' + inst.editorId + '_' + name + '_5_' + j + '_' + i);
				row.appendChild(cell);
			}
			table.appendChild(row);
		}
		head.appendChild(choice);
		head.appendChild(input);
		head.appendChild(clear);
		hiddenDiv.appendChild(head);
		hiddenDiv.appendChild(table);
		div.appendChild(hiddenDiv);
		block.appendChild(div);

		if(inst) {
			inst.iElements[name] = this[name];
		}
	},

	initGroups: function(selectorparams) {
		;
	},

	selectModify: function(select_selector, value) {
		Flexi(select_selector + ' option[value=' + value + ']')[0].selected = true;
	},

	DOMfragmentInsertTag: function(tag, attrs, startNode, endNode) {
		var startPath = this.nodePath(startNode, this.inst.content.body);
		var endPath = this.nodePath(endNode, this.inst.content.body);

		var level = startPath.length;
		var tmp = startNode;
		var nodeTextEnd = tmp.nodeValue.slice(this.curRange.startOffset);
		tmp.nodeValue = tmp.nodeValue.substring(0, this.curRange.startOffset);

		while(level > 0) {
			if(this.pathCompareLevel(startPath, endPath, level) < level) {
				var newNode = this.nodeCreate(tag, attrs);
				tmp.parentNode.insertBefore(newNode, tmp.nextSibling);
				if(level == startPath.length) {
					newNode.appendChild(this.text(nodeTextEnd));
				}
				tmp = tmp.nextSibling;
				while(tmp.nextSibling) {
					newNode.appendChild(tmp.nextSibling);
					tmp = tmp.nextSibling;
				}
			} else {
				var pos = startPath[level];
				while(pos != endPath[level]) {
					tmp = tmp.nextSibling;
					var newNode = this.nodeCreate(tag, attrs);
					tmp.parentNode.insertBefore(newNode, tmp.nextSibling);
					tmp = tmp.nextSibling;
					newNode.appendChild(tmp.nextSibling);
					pos += 1;
				}

			}
			level -= 1;
			tmp = tmp.parentNode;
		}

	},

	pathCompareLevel: function(startPath, endPath, curr) {
		for(var i = 0; i < curr; i++) {
			if(startPath[i] != endPath[i]) {
				return i + 1;
			}
		}
		return 100000;
	},

	closeTag: function(node, nodeType) {
		/* save children chain of closing tag */
		var interface = new this.interfaceStatus();
		var textNode = node;
		if(node.nodeName == '#text') {
			node = node.parentNode;
		}
		var nodes = new Array();
		while(node.nodeName != 'BODY') {
			interface.testTag(nodeType, node);
			nodes.push(node);
			if(interface.curFind.nodeType == 1) {
				break;
			}
			node = node.parentNode;
		}
		//alert(textNode);
		/* determine insertion point */
//		var selectNode = (nodes[nodes.length - 1].nextSibling) ? nodes[nodes.length - 1].nextSibling : nodes[nodes.length - 1];
//		if(nodes[nodes.length - 1].nextSibling) {
//			var selectNode = nodes[nodes.length - 1].nextSibling;
//		} else {
//			var selectNode = nodes[nodes.length - 1];
//		}
//		this.curRange.selectNode(selectNode);
//		this.curRange.setStart(selectNode, 0);
//		this.curRange.setEnd(selectNode, 0);

		//var textNode = (anchorNode.nodeName == '#text') ? anchorNode : anchorNode.firstChild;
		//alert(node.isSameNode(node.cloneNode(true)));
		var simpleClose = 0;
		if(this.curRange.collapsed) {
			//alert('len: ' + textNode.nodeValue.length);
			if(textNode.nodeName != '#text') {
				simpleClose = 1;
				//nodes[nodes.length - 1].parentNode.insertBefore(newNode, nodes[nodes.length - 1].nextSibling);
				//alert(nodes.length + node.nodeName + nodes[nodes.length - 1].nodeName);
//				if(nodes[nodes.length - 1].nextSibling) {
//					this.curRange.selectNode(nodes[nodes.length - 1].nextSibling);
//					this.curRange.setStart(nodes[nodes.length - 1].nextSibling, 0);
//					this.curRange.setEnd(nodes[nodes.length - 1].nextSibling, 0);
//					var clone = nodes[nodes.length - 1].nextSibling;
//				} else {
//					//var clone = nodes[nodes.length - 1];
//				}
				if(nodes.length - 2 >= 0) {
					//alert(textNode.nodeName + textNode.parentNode.nodeName + nodes[nodes.length - 2]);
					nodes[nodes.length - 1].parentNode.insertBefore(nodes[nodes.length - 2], nodes[nodes.length - 1].nextSibling);
					this.curRange.selectNode(nodes[nodes.length - 2]);
					this.curRange.setStart(nodes[nodes.length - 2], 0);
					this.curRange.setEnd(nodes[nodes.length - 2], 0);
				} else {
					var temp = nodes[nodes.length - 1].parentNode;
					nodes[nodes.length - 1].parentNode.removeChild(nodes[nodes.length - 1]);
					this.curRange.selectNode(temp);
					this.curRange.setStart(temp, 0);
					this.curRange.setEnd(temp, 0);
				}
				if(nodes.length - 3 >= 0) {
					for(var n = nodes.length - 3; n >= 0; n--) {
						//alert(nodes[n].nodeName);
						if(nodes[n].nodeName != '#text') {
							var style = (nodes[n].hasAttribute('style')) ? nodes[n].getAttribute('style') : '';
							if(style) {
								this.insertTag(nodes[n].nodeName, style, '');
							} else {
								this.insertTag(nodes[n].nodeName, '', '');
							}
						}
					}
				}
			} else {
					var offset = this.curRange.startOffset;
					var path = this.nodePath(textNode, nodes[nodes.length - 1]);
					//alert(path);
					var clone = nodes[nodes.length - 1].cloneNode(true);
					textNode.nodeValue = textNode.nodeValue.substring(0, offset);
					clone = nodes[nodes.length - 1].parentNode.insertBefore(clone, nodes[nodes.length - 1].nextSibling);
					var node = this.nodeFromPath(path, clone);
					var tmpNode = node;
					node.nodeValue = node.nodeValue.slice(offset);
					for(var p = 0; p < path.length; p++) {
						while(tmpNode.previousSibling) {
							tmpNode.parentNode.removeChild(tmpNode.previousSibling);
						}
						tmpNode = tmpNode.parentNode;
					}


					while(!nodes[nodes.length - 1].isSameNode(textNode)) {
						while(textNode.nextSibling) {
							textNode.parentNode.removeChild(textNode.nextSibling);
						}
						textNode = textNode.parentNode;
					}
					//var newText = this.inst.content.createTextNode('');
					//alert(this.inst.sel);
					//nodes[nodes.length - 1].parentNode.insertBefore(newText, nodes[nodes.length - 1].nextSibling);
//					this.curRange.selectNode(newText);
//					this.curRange.setStart(newText, 0);
//					this.curRange.setEnd(newText, 0);
			}
		}

		/* insert children chain */
		if(!simpleClose) {
			if(nodes.length - 2 >= 0) {
				for(var n = nodes.length - 2; n >= 0; n--) {
					//alert(nodes[n].nodeName);
					if(nodes[n].nodeName != '#text') {
						var style = (nodes[n].hasAttribute('style')) ? nodes[n].getAttribute('style') : '';
						if(style) {
							(n == nodes.length - 2) ? this.insertTag(nodes[n].nodeName, style, '', clone) : this.insertTag(nodes[n].nodeName, style, '');
						} else {
							(n == nodes.length - 2) ? this.insertTag(nodes[n].nodeName, '', '', clone) : this.insertTag(nodes[n].nodeName, '', '');
						}
					}
				}
			} else {
				var newText = this.inst.content.createTextNode('');
				nodes[nodes.length - 1].parentNode.insertBefore(newText, nodes[nodes.length - 1].nextSibling);
				this.curRange.selectNode(newText);
				this.curRange.setStart(newText, 0);
				this.curRange.setEnd(newText, 0);
			}
		}
	},

	reopenTag: function(node, nodeType, newNode) {
		/* save children chain of closing tag */
		var interface = new this.interfaceStatus();
		var textNode = node;
		var nodes = new Array();
		while(node.nodeName != 'BODY') {
			interface.testTag(nodeType, node);
			nodes.push(node);
			if(interface.curFindTag.nodeValue == nodeType) {
				break;
			}
			node = node.parentNode;
		}
		//if(!textNode || !textNode.nodeValue) {
//		nodes[nodes.length - 1].parentNode.insertBefore(newNode, nodes[nodes.length - 1].nextSibling);
//		this.curRange.selectNode(newNode);
//		this.curRange.setStart(newNode, 0);
//		this.curRange.setEnd(newNode, 0);

		//alert(anchorNode.nodeName);
		//var textNode = (anchorNode.nodeName == '#text') ? anchorNode : anchorNode.firstChild;
		//alert(node.isSameNode(node.cloneNode(true)));
		//}
		if(this.curRange.collapsed) {
			if(!textNode || !textNode.nodeValue || this.curRange.startOffset == textNode.nodeValue.length) {
				nodes[nodes.length - 1].parentNode.insertBefore(newNode, nodes[nodes.length - 1].nextSibling);
				this.curRange.selectNode(newNode);
				this.curRange.setStart(newNode, 0);
				this.curRange.setEnd(newNode, 0);
			} else {
					var offset = this.curRange.startOffset;
					var path = this.nodePath(textNode, nodes[nodes.length - 1]);
					//alert(path);
					var clone = nodes[nodes.length - 1].cloneNode(true);
					textNode.nodeValue = textNode.nodeValue.substring(0, offset);
					clone = nodes[nodes.length - 1].parentNode.insertBefore(clone, nodes[nodes.length - 1].nextSibling);
					var node = this.nodeFromPath(path, clone);
					node.nodeValue = node.nodeValue.slice(offset);
					for(var p = 0; p < path.length; p++) {
						for(var i = 0; i < path[p]; i++) {
							node.parentNode.removeChild(node.parentNode.childNodes[i]);
						}
						node = node.parentNode;
					}
//					for(p = 0; p < path.length; p++) {
//						var len = textNode.childNodes.length;
//						for(i = path[p] + 1; i < len; i++) {
//							alert(p + ' ' + i + ' ' + path[p] + textNode.parentNode.childNodes[i].innerHTML);
//							textNode.parentNode.removeChild(textNode.parentNode.childNodes[i]);
//						}
//						textNode = textNode.parentNode;
//					}
					while(!nodes[nodes.length - 1].isSameNode(textNode)) {
						while(textNode.nextSibling) {
							textNode.parentNode.removeChild(textNode.nextSibling);
						}
						textNode = textNode.parentNode;
					}
					nodes[nodes.length - 1].parentNode.insertBefore(newNode, nodes[nodes.length - 1].nextSibling);
					this.curRange.selectNode(newNode);
					this.curRange.setStart(newNode, 0);
					this.curRange.setEnd(newNode, 0);
			}
		}

		/* determine insertion point */
//		var selectNode = (nodes[nodes.length - 1].nextSibling) ? nodes[nodes.length - 1].nextSibling : nodes[nodes.length - 1];
//		if(nodes[nodes.length - 1].nextSibling) {
//			var selectNode = nodes[nodes.length - 1].nextSibling;
//		} else {
//			var selectNode = nodes[nodes.length - 1];
//		}
//		this.curRange.selectNode(selectNode);
//		this.curRange.setStart(selectNode, 0);
//		this.curRange.setEnd(selectNode, 0);

		/* insert new tag */
		//alert('Reopen: ' + node.nodeName + nodeType + newNode.nodeName + nodes.length);
		//this.insertTag(newNode.nodeName, newNode.getAttribute('style'), '');

		/* insert children chain */
		for(var n = nodes.length - 2; n >= 0; n--) {
			//alert(nodes[n]);
			if(nodes[n].nodeName != '#text') {
				var style = (nodes[n].hasAttribute('style')) ? nodes[n].getAttribute('style') : '';
				if(style) {
					this.insertTag(nodes[n].nodeName, style, '');
				} else {
					this.insertTag(nodes[n].nodeName, '', '');
				}
			}
		}
	},

	nodePath: function(node, root) {
		var p = new Array();
		var count = 0;
		while(!node.isSameNode(root)) {
			//alert(node.innerHTML);
			while(node.previousSibling) {
				node = node.previousSibling;
				count += 1;
			}
			p.push(count);
			count = 0;
			node = node.parentNode;
		}

		return p;
	},

	nodeFromPath: function(path, referenceNode) {
		for(var p = path.length - 1; p >= 0; p--) {
			referenceNode = referenceNode.childNodes[path[p]];
		}
		return referenceNode;
	},

	//treeTag: function()

	formatNodeCreate: function(formatNodeName) {
		switch(formatNodeName) {
			case 'bold':
				return this.nodeCreate('STRONG', []);
			case 'italic':
				return this.nodeCreate('EM', []);
			case 'underline':
				return this.nodeCreate('SPAN', [{name: 'style', value: 'text-decoration: underline'}]);
			case 'sup':
				return this.nodeCreate('SUP', []);
			case 'sub':
				return this.nodeCreate('SUB', []);
			case 'fontColor':
				return this.nodeCreate('SPAN', [{name: 'style', value: 'color: ' + UltraEdit.selectedNodeParam}]);
			case 'fontSize':
				return this.nodeCreate('SPAN', [{name: 'style', value: 'font-size: ' + UltraEdit.selectedNodeParam}]);
			case 'font':
				return this.nodeCreate('SPAN', [{name: 'style', value: 'font-family: ' + UltraEdit.selectedNodeParam}]);
		}
	},

	formatTree: function(current, start, end, formatNodeName, childsTested, startLevel, curLevel, TopAllowedNodeName, startFormatted, childFormatted, formattings) {
		var pom = (current.nodeName == '#text') ? current.nodeValue : current.innerHTML;
		if(typeof formattings === 'undefined') {
			formatNodes = new Array();
		} else {
			formatNodes = formattings;
		}

		///alert('CURRENT: ' + current.nodeName + ' ::: ' + pom);
		//alert(current.parentNode.previousSibling && current.parentNode.nodeName != TopAllowedNodeName);
		if(current.isSameNode(end)) {
			if(end.isSameNode(start)) {
				///alert(current.nodeName);
				var formatNode = this.formatNodeCreate(formatNodeName);
				this.curRange.surroundContents(formatNode);
				this.curRange.selectNode(formatNode.firstChild);
				//this.curRange.selectNodeContents(formatNode);
				this.curRange.setStart(formatNode.firstChild, 0);
				///alert(formatNode.firstChild.nodeValue.length);
				this.curRange.setEnd(formatNode.firstChild, formatNode.firstChild.nodeValue.length);
				return 1;
			}
			tmp = current;
			var notFirst = 0;
			while(!this.isTopAllowedNodeForFormatNode(tmp, TopAllowedNodeName)/* && tmp.parentNode.nodeName != TopAllowedNodeName*/) {
				if(tmp.previousSibling) {
					///alert('ENDNODEWHILE ' + tmp.nodeName + ' ' + tmp.previousSibling.nodeName + tmp.previousSibling.nodeValue);
					var formatNode = this.formatNodeCreate(formatNodeName);
					tmp.parentNode.insertBefore(formatNode, tmp);
					formatNodes.push(formatNode);

					var curPath = this.nodePath(formatNode, this.inst.content.body);
					//var referenceNode = this.nodeFromPath(startPath, this.inst.content.body);
					var startPath = this.nodePath(start, this.inst.content.body);
					//var location = (startPath.length - curPath.length > 0) ? startPath.length - curPath.length : 0;

					var startLocation = (this.isParent(formatNode.parentNode, start)) ? startPath[startPath.length - curPath.length] : 0;
					for(var i = curPath[0] - 1; i >= startLocation; i--) {
					//for(var i = startLocation; i < curPath[0]; i++) {
						var pom1 = (tmp.parentNode.childNodes[i].nodeName == '#text') ? tmp.parentNode.childNodes[i].nodeValue : tmp.parentNode.childNodes[i].innerHTML;
						///alert('BEFOREENDNODE' + tmp.parentNode.childNodes[i] + ' : ' + pom1 + ' ::: ' + this.notEmpty(tmp.parentNode.childNodes[i], 1));
						if(!this.isParent(tmp.parentNode.childNodes[i], start) && !start.isSameNode(tmp.parentNode.childNodes[i]) && !formatNode.isSameNode(tmp.parentNode.childNodes[i])) {
							//this.unformatSubtree(tmp.parentNode.childNodes[i], formatNodeName);
							formatNode.insertBefore(tmp.parentNode.childNodes[i], formatNode.firstChild);
						} else {
							if(start.isSameNode(tmp.parentNode.childNodes[i])) {
								formatNode.insertBefore(this.text(start.nodeValue.slice(this.curRange.startOffset)), formatNode.firstChild);
								start.nodeValue = start.nodeValue.substring(0, this.curRange.startOffset);
							}
						}
					}

//					while(!this.isParent(tmp.previousSibling, start) && tmp.previousSibling) {
//
//					}
					if(this.notEmpty(tmp.previousSibling, 1)) {
						///alert('ISEMPTY' + tmp.previousSibling.nodeName + ' val: ' + tmp.previousSibling.nodeValue + ' html: ' + tmp.previousSibling.innerHTML);
						notFirst = 1;
					}
				}
				tmp = tmp.parentNode;
			}
			if(typeof formatNode === 'undefined') {
				///alert('ONLYEND');
				var insertionPoint = (this.isTopAllowedNodeForFormatNode(tmp, TopAllowedNodeName)) ? end : tmp;
				var formatNode = this.formatNodeCreate(formatNodeName);
				insertionPoint.parentNode.insertBefore(formatNode, insertionPoint);
				formatNodes.push(formatNode);
			}
			var child = formatNode.appendChild(this.text(end.nodeValue.substring(0, this.curRange.endOffset)));
			if(notFirst) {
				///alert('NOT EMPTY');
				end.nodeValue = end.nodeValue.slice(this.curRange.endOffset);
			} else {
				tmp = end;
				while(!this.isParent(tmp, formatNode)) {
					if(tmp.nodeName != '#text') {
						var newNode = this.nodeCreate(tmp.nodeName, []);
						var newNode = tmp.cloneNode(false);
//						while(newNode.childNodes.length) {
//							newNode.removeNode(newNode.firstChild);
//						}
						//newNode.innerHTML = '';
					//} else {
						//var newNode = this.text(end.nodeValue.substring(0, this.curRange.endOffset));
					//}
						var replaced = child.parentNode.replaceChild(newNode, child);
						///alert('REPLACED' + replaced.nodeName + replaced.nodeValue);
						newNode.appendChild(replaced);
						//tmp = newNode;
						child = newNode;
					}
					tmp = tmp.parentNode;
				}
				end.nodeValue = end.nodeValue.slice(this.curRange.endOffset);
			}
			//alert(current.parentNode.previousSibling.nodeName);
//			if(current.parentNode.previousSibling && current.parentNode.nodeName != TopAllowedNodeName) {
//				alert(formatNode);
//				current.parentNode.parentNode.insertBefore(formatNode, current.parentNode.parentNode.firstChild);
//				var tmp = formatNode.nextSibling;
//				while(!tmp.nextSibling.isSameNode(end.parentNode)) {
//					formatNode.appendChild(tmp.nextSibling);
//					tmp = tmp.nextSibling;
//				}
//				alert(this.curRange.offsetStart);
//			}

			for(var k = 0; k < formatNodes.length; k++) {
				this.unformatSubtree(formatNodes[k], formatNodeName, current);
			}
		} else {
			if(current.nextSibling) {
				///alert('ISNEXTEXISTS' + current.nextSibling + ' : ' + this.notEmptyNext(current.nextSibling, 1));
			}
			//alert(current);
//			this.showNode('.window_left_pannel',current);
//			this.showNode('.window_left_pannel',current.firstChild);
//			this.showNode('.window_left_pannel',current.nextSibling);
//			alert(this.notEmptyNext(current.nextSibling, 1));
			if(current.firstChild && !childsTested) {
				this.formatTree(current.firstChild, start, end, formatNodeName, 0, startLevel, curLevel + 1, TopAllowedNodeName, startFormatted, childFormatted, formatNodes);
			} else if(current.nextSibling && this.notEmptyNext(current.nextSibling, 1)){
				current = current.nextSibling;
				while(!this.notEmpty(current, 1)) {
					current = current.nextSibling;
				}
				///alert('NEXT');
				this.formatTree(current, start, end, formatNodeName, 0, startLevel, curLevel, TopAllowedNodeName, startFormatted, 0, formatNodes);
			} else {
				if(!current.parentNode) {
					//alert(current);
				}
//				while(!current.parentNode) {
//					var x = 12345678.12345678 / 7654321.7654321;
//				}
				var is_par = this.isParent(current.parentNode, start);
//				alert(current.nodeValue + is_par);
//				alert(this.notEmpty(current));


				///alert('ISPARENT:' + is_par);
				///alert('CURPARENT:' + current.parentNode.nodeName);
				///alert('CHILDFORMATED:' + childFormatted);
				///alert('NOTEMPTY:' + this.notEmpty(current, 1));

				if((is_par || this.isTopAllowedNodeForFormatNode(current.parentNode, TopAllowedNodeName)) && !childFormatted && this.notEmpty(current, 1) && !this.isParent(current.parentNode, end)) {
					//alert('aaa'+ current.nodeName + current.nodeValue);
					var startPath = this.nodePath(start, this.inst.content.body);
					//alert(startPath);
					var parentPath = this.nodePath(current.parentNode, this.inst.content.body);
					while(startPath.length > parentPath.length + 1) {
						startPath.shift();
					}
					//current.parentNode.insertBefore(formatNode, current);
					var formatNode = this.formatNodeCreate(formatNodeName);

					var referenceNode = (is_par) ? this.nodeFromPath(startPath, this.inst.content.body) : current;
					//alert(this.inst.content.body.nodeName);
					//alert(current);
					///(current.nodeName == '#text') ? alert('TXT'+current.nodeValue) : alert('CHILD'+current.firstChild.nodeValue);
					///alert('BEFOREINSERT: '+current.parentNode);
					//(referenceNode.nextSibling && referenceNode.nextSibling.nodeName == '#text' || referenceNode.nextSibling.nodeName == 'BR') ? alert('TXT'+referenceNode.nextSibling.nodeValue) : alert('CHILD'+referenceNode.nextSibling.firstChild.nodeValue);
					if(this.isTopAllowedNodeForFormatNode(current.parentNode, TopAllowedNodeName) && !is_par) {
						current.parentNode.insertBefore(formatNode, current.parentNode.firstChild);
					} else {
						///alert(current.parentNode.innerHTML);
						current.parentNode.insertBefore(formatNode, referenceNode.nextSibling);
					}
					formatNodes.push(formatNode);

						var border = (is_par) ? startPath[0] + 2 : 1;
						var border = (border < 1) ? 1 : border;
						//alert('BORDER' + border);
						//this.showChildren('.window_left_pannel',referenceNode.parentNode);

						var oldref = referenceNode.parentNode;
					for(var i = referenceNode.parentNode.childNodes.length - 1; i >= border; i--) {

						//alert(referenceNode.parentNode.childNodes[i]);
						//var aaa = (referenceNode.parentNode.childNodes[i].innerHTML) ? referenceNode.parentNode.childNodes[i].innerHTML : referenceNode.parentNode.childNodes[i].nodeValue;
						//alert('FORMATNODEADD:' + 'POZ: ' + i + ' : ' + referenceNode.parentNode.childNodes[i] + ' ' + this.notEmpty(referenceNode.parentNode.childNodes[i], 1) + ' ' + aaa);
						//this.showChildren('.window_left_pannel',referenceNode.parentNode);
						//var ref = (referenceNode.parentNode.isSameNode(oldref)) ? oldref : oldref;
						formatNode.insertBefore(oldref.childNodes[i], formatNode.firstChild);

					}
					//this.unformatSubtree(formatNode, formatNodeName, current);
					if(!startFormatted) {
						formatNode.insertBefore(this.text(start.nodeValue.slice(this.curRange.startOffset)), formatNode.firstChild);
						start.nodeValue = start.nodeValue.substring(0, this.curRange.startOffset);
						startFormatted = 1;
					}
					childFormatted = 1;
				}
				//setTimeout(this.formatTree, 100, current.parentNode, start, end, formatNodeName, 1, startLevel, curLevel - 1, TopAllowedNodeName, startFormatted, childFormatted);
				//setTimeout(function() {
				if(typeof formatNode != 'undefined') {
					UltraEdit.formatTree(formatNode.parentNode, start, end, formatNodeName, 1, startLevel, curLevel - 1, TopAllowedNodeName, startFormatted, childFormatted, formatNodes);
				} else {
					UltraEdit.formatTree(current.parentNode, start, end, formatNodeName, 1, startLevel, curLevel - 1, TopAllowedNodeName, startFormatted, childFormatted, formatNodes);
				}
				//}, 500);
				//this.formatTree(current.parentNode, start, end, formatNodeName, 1, startLevel, curLevel - 1, TopAllowedNodeName, startFormatted, childFormatted);
			}
		}

	},

	testSelection: function(current, start, end, formatNodeName, startFormatted, found) {
		var selectionStatus = 0;
//		if(current.nodeName == 'HTML') {
//			return 0;
//		}
		if(current.isSameNode(start)) {
			current = current.parentNode;
		}
		//alert('CURRENT: ' + current.nodeName + ' ' + current.nodeValue + ' ' + current.innerHTML);
		if(found) {
			//alert('FOUND');
			if(this.isParent(current, end)) {
				return 1;
			} else {
				//alert('NOT PARENT END');
				if(this.isTopAllowedNodeForFormatNode(current, formatNodeName)) {
					found = 0;
					//alert(this.notEmpty(current.nextSibling, 1) + 'TOP ALLOWED' + current.nextSibling.nextSibling.firstChild.nodeName + ':' + current.nextSibling.nextSibling.firstChild.innerHTML + current.nodeName + ':' + current.innerHTML + ' | ' + current.nextSibling.nodeName + ':' + current.nextSibling.innerHTML + ' | ' + current.nextSibling.nextSibling.nodeName + ':'+current.nextSibling.nextSibling.nodeValue + ':'+current.nextSibling.nextSibling.innerHTML);
					while(current.nextSibling && !this.notEmpty(current.nextSibling, 1)) {
						//alert('EMPTY'+current.nextSibling.nodeValue);
						current = current.nextSibling;
					}

					//alert('CURNEXT'+current.nodeName+' '+current.nodeValue+' '+current.innerHTML);
					selectionStatus = this.testSelection(current.nextSibling, start, end, formatNodeName, startFormatted, found);
				} else {
					//alert('NOT TOP');
					if(current.nextSibling) {
						//alert('NEXT');
						found = 0;
						selectionStatus = this.testSelection(current.nextSibling, start, end, formatNodeName, startFormatted, found);
					} else {
						//alert('PARENT');
						selectionStatus = this.testSelection(current.parentNode, start, end, formatNodeName, startFormatted, found);
					}
				}
			}
		} else {
			//alert('NOT FOUND');
			if(this.isParent(current, start)) {
				//alert('PARENT START');
				if(this.isTopAllowedNodeForFormatNode(current, formatNodeName)) {
					//alert('TOP ALLOWED');
					if(startFormatted) {
						//alert('START FORMATTED');
						found = 0;
						selectionStatus = this.testSelection(current.nextSibling, start, end, formatNodeName, startFormatted, found);
					} else {
						//alert('START NOT FORMATTED');
						return 0;
					}
				} else {
					//alert('NOT TOP');
					//status = new this.interfaceStatus();
					var func = 'test' + formatNodeName.substring(0,1).toUpperCase() + formatNodeName.slice(1);

					if(this.interfaceObj[func](current)) {
						//alert('FORMAT');
						startFormatted = 1;
						if(this.isParent(current, end)) {
							return 1;
						} else {
							if(current.nextSibling && this.notEmpty(current.nextSibling, 1)) {
								//alert('NEXT');
								found = 0;
								selectionStatus = this.testSelection(current.nextSibling, start, end, formatNodeName, startFormatted, found);
							} else {
								//alert('PARENT');
								found = 1;
								selectionStatus = this.testSelection(current.parentNode, start, end, formatNodeName, startFormatted, found);
							}
						}
					} else {
						//alert('NOT FORMAT');
						found = 0;
						selectionStatus = this.testSelection(current.parentNode, start, end, formatNodeName, startFormatted, found);
					}
				}
			} else {
				//alert('NOT PARENT START');
				//status = new this.interfaceStatus();
				var func = 'test' + formatNodeName.substring(0,1).toUpperCase() + formatNodeName.slice(1);
				if(this.interfaceObj[func](current)) {
					//alert('FORMAT');
					if(this.isParent(current, end)) {
						return 1;
					} else {
						//alert('NOT PARENT END');
						found = 1;
						if(current.nextSibling) {
							//alert('NEXT');
							found = 0;
							selectionStatus = this.testSelection(current.nextSibling, start, end, formatNodeName, startFormatted, found);
						} else {
							//alert('PARENT');
							selectionStatus = this.testSelection(current.parentNode, start, end, formatNodeName, startFormatted, found);
						}
					}
				} else {
					//alert('NOT FORMAT');
					if(current.firstChild) {
						//alert('CHILD');
						current = current.firstChild;
						while(!this.notEmpty(current, 1)) {
							//alert('EMPTYCHILD'+current.nextSibling.nodeValue);
							current = current.nextSibling;
						}
						selectionStatus = this.testSelection(current, start, end, formatNodeName, startFormatted, found);
					} else {
						return 0;
					}
				}
			}
		}
		return selectionStatus;
	},

	unformatSubtree: function(node, formatName, curNode) {
		//alert('unformatSubtree - '+ node.nodeName + ': ' + node.innerHTML);
		if(node.firstChild) {
			var tmp = node.firstChild;
		} else {
			return 1;
		}
		var noFirst = 0;
		while(tmp != null || !noFirst) {
			//alert('TMP'+typeof tmp);
			//alert('TMP'+tmp.nodeName);
			noFirst = 1;
			var status = new this.interfaceStatus();
			var func = 'test' + formatName.substring(0,1).toUpperCase() + formatName.slice(1);
			//alert('func'+status[func](tmp));
			if(formatName == 'font' || formatName == 'fontSize' || formatName == 'fontColor') {
				var testStatus = status[func](tmp, 1);
				//alert('FONTCOLOR TEST: ' + testStatus + 'aaa' + tmp.nodeName + ' : ' + tmp.nodeValue + ' : ' + tmp.innerHTML);
			} else {
				var testStatus = status[func](tmp);
			}
			if(testStatus) {
				//alert('REMOVE');
				//alert(tmp.childNodes.length+'FORMAT'+tmp.nodeName);
				var next = tmp.nextSibling;
//				var next = (tmp.nextSibling) ? tmp.nextSibling : tmp;
				for(var j = 0; j < tmp.childNodes.length; j++) {
					if(this.notEmpty(tmp.childNodes[j], 1)) {
						//alert('FORMATCHILDS: '+ tmp.childNodes[j].nodeValue);
						var a = node.insertBefore(tmp.childNodes[j], tmp);
						j -= 1;
					}
					//alert('FORMATCHILDS'+tmp.childNodes[j].nodeValue + tmp.innerHTML + node.innerHTML);
				}
				if(tmp.isSameNode(curNode)) {
					//alert('NIE USUWAĆ NOD BAZOWY');
				}

				var z = node.removeChild(tmp);
				tmp = next;
			} else {
				tmp = tmp.nextSibling;
				//tmp = (tmp.nextSibling) ? tmp.nextSibling : tmp;
			}
		}
		//alert('AFTERREMOVE'+node.innerHTML);
		for(var j = 0; j < node.childNodes.length; j++) {
			this.unformatSubtree(node.childNodes[j], formatName);
		}

	},

	isTopAllowedNodeForFormatNode: function(node, formatNodeName) {
		frontierNames = new Array();
		status = new this.interfaceStatus();
		var testVal = 0;
		switch(formatNodeName) {
			case 'bold':
			case 'italic':
			case 'underline':
			case 'sup':
			case 'sub':
				/*testVal += status['testIsFont'](node);
				testVal += status['testIsFontSize'](node);
				testVal += status['testIsFontColor'](node);*/
				testVal += status['testIsParagraph'](node);
				testVal += status['testIsBlock'](node);
				break;
			case 'font':
			case 'fontSize':
			case 'fontColor':
				testVal += status['testIsParagraph'](node);
				testVal += status['testIsBlock'](node);
				break;

		}
		return testVal;

	},

	showChildren: function(selector, node) {
		var string = '<strong>NODE:</strong> ' + node.nodeName + '<br><strong>NODE HTML:</strong> ' + node.innerHTML + '<br /><br />';
		for(var i = 0; i < node.childNodes.length; i++) {
			string += '<strong>POZ:</strong> ' + i + ' <strong>NAME:</strong> ' + node.childNodes[i].nodeName + '<br /><strong>VALUE:</strong> ' + node.childNodes[i].nodeValue + '<br /><strong>HTML:</strong> ' + node.childNodes[i].innerHTML + '<br />';
		}
		Flexi(selector).html(string);
		alert('CHILDS');
	},
	showNode: function(selector, node) {
		var string = '';
		if(node) {
			string += '<strong>NAME:</strong> ' + node.nodeName + '<br /><strong>VALUE:</strong> ' + node.nodeValue + '<br /><strong>HTML:</strong> ' + node.innerHTML + '<br />';
		} else {
			string += 'EMPTY';
		}

		Flexi(selector).html(string);
		alert('NODE');
	},

	notEmptyNode: 0,

	notEmpty: function(node, reset) {
		if(reset) {
			this.notEmptyNode = 0;
		}
		if(node.nodeName == '#text') {
			var nVal = node.nodeValue;
			while(nVal.length && nVal.charAt(0) == '\n') {
				var nVal = nVal.replace('\n', '');
			}
			this.notEmptyNode = (nVal.length) ? 1 : this.notEmptyNode;
		} else {
			for(var i = 0; i < node.childNodes.length; i++) {
				this.notEmpty(node.childNodes[i]);
			}
		}
		return this.notEmptyNode;
	},

	notEmptyNext: function(node, reset) {
		//if(reset) {
			this.notEmptyNode = 0;
		//}
		var tmp = node;
		while(tmp) {
			//alert('TESTNOTEMPTYNEXT' + node.nodeName);
			this.notEmpty(tmp);
			tmp = tmp.nextSibling;
		}
		return this.notEmptyNode;
	},

	isParent: function(testNode, potentialChild) {
		var res = testNode.compareDocumentPosition(potentialChild);
		//alert(testNode.nodeName);
		//alert('xxx' + res);
		if(res == 20) {
			return true;
		}
		return false;
	},

	action: function(el, param) {
		var inst = this.inst;
		//alert(inst.sel);
		if(!inst.iElements[el].imageNoChange) {
			if(inst.iElements[el].group) {
				for(iEl in inst.iElements) {
					if(typeof(inst.iElements[iEl]) == 'object' && inst.iElements[el].group == inst.iElements[iEl].group && iEl != el) {
						this.setInactive(inst, iEl, iEl);
					}
				}
				if(inst.iElements[el].doubleOff && inst.iElements[el].status == 'active') {
					this.setInactive(inst, el, el);
				} else {
					this.setActive(inst, el, el);
				}
			} else {
				if(inst.iElements[el].status == 'inactive') {
					this.setActive(inst, el, el);
				} else {
					this.setInactive(inst, el, el);
				}
			}
		}
		if(_IE()) {
			//this.iFrame.contentWindow.focus();
		}
		var anchorStart = this.selection.anchorNode;
		if(this.curRange.toString().length == 0) {
			var start = end = node = (_IE()) ? this.curRange.parentElement() : ((this.selection.anchorNode.nodeName == '#text') ? this.selection.anchorNode.parentNode : this.selection.anchorNode);
		} else {
			var start = this.curRange.startContainer;
			var startOffset = this.curRange.startOffset;
			var end = this.curRange.endContainer;
			var endOffset = this.curRange.endOffset;
			var node = this.curRange.commonAncestorContainer;
			node = (node.nodeName == '#text') ? node.parentNode : node;
		}
		switch(el) {
			case 'bold':
				if(!_IE()) {
					this.curPos(inst);
					var interface = new this.interfaceStatus();
					if(this.curRange.toString().length) {
						//alert('fff');
						this.testSelection(start, start, end, 'bold', 0, 0);
						//alert('fff2');
					} else {
						interface.testDOM(inst, node, 'bold');
					}
					if(!interface.bold) {
						//alert('xcxc');
						if(this.curRange.toString().length) {
								var tmp = start;
								var startLevel = 0;
								while(tmp.nodeName != 'BODY') {
									startLevel += 1;
									tmp = tmp.parentNode;
								}
								this.formatTree(start, start, end, 'bold', 0, startLevel, startLevel, 'bold', 0, 0);

							//var content = this.curRange.extractContents();
							//UltraEdit.curPos(inst);
//							var strong = this.nodeCreate('strong', []);
//							this.curRange.surroundContents(strong);
//							this.curRange.setStart(strong, 0);
//							this.curRange.setEnd(strong, 0);
						} else {
							inst.htmlAwaitStart += 'strong,,!';
							inst.htmlAwaitEnd = '</strong>' + inst.htmlAwaitEnd;
						}
					} else {
						if(this.curRange.toString().length) {

								var content = this.curRange.extractContents();
								UltraEdit.curPos(inst);
								node = this.selection.anchorNode;
								this.closeTag(node, 'bold');
								//this.reopenTag(node, 'bold', newColor);
								//this.reopenTag(node, nodeType, newNode);
								//alert(this.curRange.toString().length);
//							alert(content.firstChild);
//							setTimeout(function() {
//								UltraEdit.curPos(inst);
//								node = (_IE()) ? UltraEdit.curRange.parentElement() : ((UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode);
//								alert(UltraEdit.selection.anchorNode.parentNode);
							//	for(var i = content.childNodes.length - 1; i >= 0; i--) {
//									node.parentNode.insertBefore(content.childNodes[i], node);
						//			alert(content.childNodes[i].nodeName + '  ' + content.childNodes[i].nodeValue);
							//	}
								//alert(content.firstChild);
								//UltraEdit.curRange.insertNode(content.firstChild);
						//	}, 10);
						} else {
							this.closeTag(anchorStart,'bold');
						}
					}
				} else {
					inst.content.execCommand('bold', false, false);
				}
	      break;
	    case 'italic':
	    	if(!_IE()) {
	    		this.curPos(inst);
					var interface = new this.interfaceStatus();
					interface.testDOM(inst, node, 'italic');
					//alert(interface.italic);
					if(!interface.italic) {
						if(this.curRange.toString().length ) {
							var tmp = start;
							var startLevel = 0;
							while(tmp.nodeName != 'BODY') {
								startLevel += 1;
								tmp = tmp.parentNode;
							}
							this.formatTree(start, start, end, 'italic', 0, startLevel, startLevel, 'italic', 0, 0);
						} else {
							inst.htmlAwaitStart += 'em,,!';
							inst.htmlAwaitEnd = '</em>' + inst.htmlAwaitEnd;
						}
					} else {
						if(this.curRange.toString().length) {
							var content = this.curRange.extractContents();
							setTimeout(function() {
								UltraEdit.curPos(inst);
//								node = (_IE()) ? UltraEdit.curRange.parentElement() : ((UltraEdit.selection.anchorNode.nodeName == '#text') ? UltraEdit.selection.anchorNode.parentNode : UltraEdit.selection.anchorNode);
//								alert(UltraEdit.selection.anchorNode.parentNode);
//								for(var i = content.childNodes.length - 1; i >= 0; i--) {
//									node.parentNode.insertBefore(content.childNodes[i], node);
//								}
								UltraEdit.curRange.insertNode(content);
							}, 1);
						} else {
							this.closeTag(anchorStart,'italic');
						}
					}
	    	} else {
	    		inst.content.execCommand('italic', false, false);
	    	}
	      break;
	    case 'underline':
	    	if(!_IE()) {
					this.curPos(inst);
					var interface = new this.interfaceStatus();
					interface.testDOM(inst, node, 'underline');
					//alert(interface.underline);
					if(!interface.underline) {
						if(this.curRange.toString().length /*&& start.isSameNode(end)*/) {
							var tmp = start;
							var startLevel = 0;
							while(tmp.nodeName != 'BODY') {
								startLevel += 1;
								tmp = tmp.parentNode;
							}
							this.formatTree(start, start, end, 'underline', 0, startLevel, startLevel, 'underline', 0, 0);
						} else {
							inst.htmlAwaitStart += 'span,text-decoration,underline!';
							inst.htmlAwaitEnd = '</span>' + inst.htmlAwaitEnd;
						}
					} else {
						this.closeTag(anchorStart,'underline');
					}
				} else {
					inst.content.execCommand('underline', false, false);
				}
	      break;
	    case 'left':
	    	var p = this.searchParents(anchorStart, 'P');
	    	Flexi(p).css('text-align', 'left');
	    	//inst.content.execCommand('justifyLeft', false, false);
	      break;
	    case 'center':
	    	var p = this.searchParents(anchorStart, 'P');
	    	Flexi(p).css('text-align', 'center');
	    	//inst.content.execCommand('justifyCenter', false, false);
	      break;
	    case 'right':
	    	var p = this.searchParents(anchorStart, 'P');
	    	Flexi(p).css('text-align', 'right');
	    	//inst.content.execCommand('justifyRight', false, false);
	      break;
	    case 'uli':
	    	inst.content.execCommand('insertUnorderedList', false, false);
	    	setTimeout(function(inst, node) {
	    		UltraEdit.curPos(inst);
	    		var node = UltraEdit.selection.anchorNode;
	    		alert(node.nodeName);
	    		if(node.nodeName == 'BODY' || (node.nodeName == '#text' && node.parentNode.nodeName == 'BODY')) {
	    			UltraEdit.insertParagraph(inst);
	    		}
	    	}, 25, inst, node);
	      break;
	    case 'oli':
	    	inst.content.execCommand('insertOrderedList', false, false);
	      break;
	    case 'undo':
	    	inst.content.execCommand('undo', false, false);
	      break;
	    case 'redo':
	    	inst.content.execCommand('redo', false, false);
	      break;
	    case 'sup':
	    	if(!_IE()) {
					this.curPos(inst);
					var interface = new this.interfaceStatus();
					interface.testDOM(inst, node, 'sup');
					if(!interface.sup) {
						if(this.curRange.toString().length /*&& start.isSameNode(end)*/) {
							var tmp = start;
							var startLevel = 0;
							while(tmp.nodeName != 'BODY') {
								startLevel += 1;
								tmp = tmp.parentNode;
							}
							this.formatTree(start, start, end, 'sup', 0, startLevel, startLevel, 'sup', 0, 0);
						} else {
							inst.htmlAwaitStart += 'sup,,!';
							inst.htmlAwaitEnd = '</strong>' + inst.htmlAwaitEnd;
						}
					} else {
						this.closeTag(anchorStart,'sup');
					}
				} else {
					inst.content.execCommand('superscript', false, false);
				}
	      break;
	    case 'sub':
	    	if(!_IE()) {
					this.curPos(inst);
					var interface = new this.interfaceStatus();
					interface.testDOM(inst, node, 'sub');
					if(!interface.sub) {
						if(this.curRange.toString().length /*&& start.isSameNode(end)*/) {
							var tmp = start;
							var startLevel = 0;
							while(tmp.nodeName != 'BODY') {
								startLevel += 1;
								tmp = tmp.parentNode;
							}
							this.formatTree(start, start, end, 'sub', 0, startLevel, startLevel, 'sub', 0, 0);
						} else {
							inst.htmlAwaitStart += 'sub,,!';
							inst.htmlAwaitEnd = '</strong>' + inst.htmlAwaitEnd;
						}
					} else {
						this.closeTag(anchorStart,'sub');
					}
				} else {
					inst.content.execCommand('subscript', false, false);
				}
	      break;
	    case 'strike':
	    	if(!_IE()) {
					this.curPos(inst);
					var interface = new this.interfaceStatus();
					interface.testDOM(inst, node, 'strike');
					if(!interface.strike) {
						//inst.htmlAwaitStart += 'del,,!';
						inst.htmlAwaitStart += 'span,text-decoration,line-through!';
						inst.htmlAwaitEnd = '</span>' + inst.htmlAwaitEnd;
					} else {
						this.closeTag(anchorStart,'strike');
					}
				} else {
					inst.content.execCommand('strikeThrough', false, false);
				}
	      break;
	    case 'fontColor':
	    	//this.content.execCommand('foreColor', false, param);
	    	inst.iFrame.contentWindow.focus();
	    	this.curPos(inst);
	    	if(_IE()) {
	    		inst.htmlAwaitStart += '<span style="color: ' + this.sizes[param] + '">';
	    		inst.htmlAwaitEnd = '</span>' + inst.htmlAwaitEnd;
	    	} else {
	    		//this.curPos(inst);
					var interface = new this.interfaceStatus();
					interface.testDOM(inst, node, 'fontColor');
					//alert(interface.curFind.nodeName);
					var newColor = inst.content.createElement('span');
					newColor.setAttribute('style', 'color: ' + param);
					jQuery('.color_choice').css('background-color', param);
					if(interface.curNodeForTag['fontColor'] && interface.curNodeForTag['fontColor'].nodeName != 'BODY') {
					//if(interface.curFind.nodeName != 'BODY') {
						//alert('ZZZ'+interface.curFind.nodeName);
						this.reopenTag(anchorStart, 'fontColor', newColor);
					} else {
						if(this.curRange.toString().length) {
							var tmp = start;
							var startLevel = 0;
							while(tmp.nodeName != 'BODY') {
								startLevel += 1;
								tmp = tmp.parentNode;
							}
							this.selectedNodeParam = param;
							this.formatTree(start, start, end, 'fontColor', 0, startLevel, startLevel, 'fontColor', 0, 0);
						} else {
							inst.htmlAwaitStart += 'span,color,' + param + '!';
							inst.htmlAwaitEnd = '</span>' + inst.htmlAwaitEnd;
						}
					}
	    	}
	      break;
	    case 'fontSize':
	    	inst.iFrame.contentWindow.focus();
	    	this.curPos(inst);
	    	if(_IE()) {
	    		inst.htmlAwaitStart += '<span style="font-size: ' + this.sizes[param] + '">';
	    		inst.htmlAwaitEnd = '</span>' + inst.htmlAwaitEnd;
	    	} else {
	    		//this.curPos(inst);
					var interface = new this.interfaceStatus();
					interface.testDOM(inst, node, 'fontSize');
					//alert(interface.curFind.nodeName);
					var newSize = inst.content.createElement('span');
					newSize.setAttribute('style', 'font-size: ' + this.sizes[param]);
					if(interface.curNodeForTag['fontSize'] && interface.curNodeForTag['fontSize'].nodeName != 'BODY') {
						this.reopenTag(anchorStart, 'fontSize', newSize);
					} else {
						if(this.curRange.toString().length) {
							var tmp = start;
							var startLevel = 0;
							while(tmp.nodeName != 'BODY') {
								startLevel += 1;
								tmp = tmp.parentNode;
							}
							this.selectedNodeParam = this.sizes[param];
							this.formatTree(start, start, end, 'fontSize', 0, startLevel, startLevel, 'fontSize', 0, 0);
						} else {
							inst.htmlAwaitStart += 'span,font-size,' + this.sizes[param] + '!';
							inst.htmlAwaitEnd = '</span>' + inst.htmlAwaitEnd;
						}
					}
	    	}
	      break;
	    case 'formattings':
	    	break;
	    case 'font':
	    	inst.iFrame.contentWindow.focus();
	    	this.curPos(inst);
	    	if(_IE()) {
	    		inst.htmlAwaitStart += '<span style="font-family: ' + this.fonts[param] + '">';
	    		inst.htmlAwaitEnd = '</span>' + inst.htmlAwaitEnd;
	    	} else {
	    		//this.curPos(inst);
					var interface = new this.interfaceStatus();
					interface.testDOM(inst, node, 'font');
					//alert(interface.curFind.nodeName);
					var newFont = inst.content.createElement('span');
					newFont.setAttribute('style', 'font-family: ' + this.fonts[param]);
					if(interface.curFind.nodeName != 'BODY') {
						this.reopenTag(anchorStart, 'font', newFont);
					} else {
						inst.htmlAwaitStart += 'span,font-family,' + this.fonts[param] + '!';
						inst.htmlAwaitEnd = '</span>' + inst.htmlAwaitEnd;
					}
	    	}
	      break;
	    case 'link':
	    	if(_IE()) {
	    		var newLink = this.content.createElement("a");
	    		var someText = document.createTextNode(param[1]);
	    		newLink.href = param[0];
	    		newLink.innerHTML = param[1];
	    		this.content.body.appendChild(newLink);
	    		var range = this.content.body.createTextRange();
	    		range.collapse(true);
	    		range.moveEnd('character', this.content.body.innerHTML.length);
	    		range.moveStart('character', this.content.body.innerHTML.length);
	    		range.select();

	    	} else {
	    		this.content.execCommand('insertHTML', false, '<a href="' + param[0] + '>' + param[1] + '</a>');
	    	}
	      break;
	    case 'indent':
	    	inst.content.execCommand('indent', false, '');
	    	break;
	    case 'outdent':
	    	inst.content.execCommand('outdent', false, '');
	    	break;
	    case 'columns':
	    	//alert('zenon tu byĹ‚');
	      break;
	    case 'table':
	    	inst.content.execCommand('outdent', false, '');
	    	break;
	    case 'htm':
	    	if(_IE()) {
	    		div = this.content.createElement("div");
	    		div.className = 'editor_htm'
	    		div.hideFocus = true;

	    		var range = this.content.body.createTextRange();
	    		var selection_range = this.content.selection.createRange().duplicate()
	    		range.moveToElementText(this.content.body); // Selects all the text
	    		after_range = range.duplicate();
	    		range.setEndPoint("EndToStart", selection_range); // Moves the end where we need it
	    		curPos = range.htmlText.length;

	    		after_range.setEndPoint("StartToEnd", selection_range); // Moves the start where we need it

	    		this.content.body.innerHTML = range.htmlText + '<div class="editor_htm"></div>' + after_range.htmlText;

	    	} else {
		    	this.iFrame.contentWindow.focus();
		    	sel = this.iFrame.contentWindow.getSelection();

		    	//alert(jQuery(sel.anchorNode).attr('class'));
		    	if(!jQuery(sel.anchorNode).attr('class') && !jQuery(sel.anchorNode.parentNode).attr('class')) {
		    		this.content.execCommand('insertHTML', false, '<div class="editor_htm"><br _moz_dirty="" type="_moz"/></div>');
		    	} else {
		    		//Flexi('.editor_htm', UltraEdit.content).removeAttr('_moz_dirty');
		    		//this.content.execCommand('insertHTML', false, '<br _moz_editor_bogus_node="TRUE" _moz_dirty=""/>');
		    		br = this.content.createElement('br');
		    		br.setAttribute('_moz_dirty', '');
		    		//br.setAttribute('_moz_editor_bogus_node', 'TRUE');
		    		br.setAttribute('type', '_moz')
		    		text = this.content.createTextNode('');
		    		//this.content.body.appendChild(br);
		    		this.content.body.appendChild(text);
		    		sel = this.iFrame.contentWindow.getSelection();
		    		range = sel.getRangeAt(0);
		    		range.setStart(Flexi('body', this.content)[0].lastChild,0);
		    		range.setEnd(Flexi('body', this.content)[0].lastChild,0);
		    	}
		    	//Flexi('.editor_htm', UltraEdit.content).replace('<div class="editor_htm"></div>');
		    	this.iFrame.contentWindow.focus();
	    	}
			break;
	    case 'php':
	    	//this.content.execCommand('insertHTML', false, '<div class="editor_php"></div>');
	    	if(this.htmlView) {
	    		this.DOMTexttoText(this.content.body);
	    		this.htmlView = 0;
	    	} else {
	    		this.DOMtoDOMText(this.content.body);
	    		this.htmlView = 1;
	    	}
	    	break;
	    case 'wysiwyg_view':
	    	if(inst.htmlView) {
	    		this.DOMTexttoText(inst.content.body);
	    		//alert(this.editorWidth);
	    		Flexi('body', inst.content).css('width', this.editorWidth);
	    		inst.htmlView = 0;
	    	}
	    	break;
	    case 'code_view':
	    	if(!inst.htmlView) {
	    		this.DOMtoDOMText(inst.content.body);
	    		Flexi('body', inst.content).css('width', '4000px');
	    		inst.htmlView = 1;
	    	}

		}
		inst.iFrame.contentWindow.focus();
	},

	setActive: function(inst, which, param) {
		if(inst.iElements[which].type == 'button') {
			if(inst.iElements[which].status = 'inactive') {
				Flexi('#editorInterface_' + inst.editorId + '_' + which).attr('src', Flexi('#editorInterface_' + inst.editorId + '_' + which).attr('src').replace(which, which + '_active'));
			}
			inst.iElements[which].status = 'active';
		} else if(inst.iElements[which].type == 'select') {
			Flexi('#editorInterface_' + inst.editorId + '_' + which + ' option[value=' + param + ']')[0].selected = true;
			//this.selectModify('#editorInterface_' + which, param);
		}
	},

	setInactive: function(inst, which, mark) {
//		for(el in inst.iElements) {
//			alert(inst.iElements[el]);
//		}
		//alert(inst + mark + inst.sel + inst.iElements);
		if(inst.iElements[mark].type == 'button') {
			if(inst.iElements[mark].status = 'active') {
				jQuery('img[src*=' + which + ']').attr('src', jQuery('img[src*=' + which + ']').attr('src').replace('_active', ''));
			}
			inst.iElements[mark].status = 'inactive';
		}
		if(inst.iElements[mark].group) {
			UltraEdit.setGroupInactive(inst, which, mark);
		}
	},

	setGroupInactive: function(inst, which, mark) {
		for(var el in inst.iElements) {
			if(inst.iElements[el].group == inst.iElements[mark].group) {
				jQuery('img[src*=' + el + ']').attr('src', jQuery('img[src*=' + el + ']').attr('src').replace('_active', ''));
			}
		}
	},

	initInterface: function(params) {
		if(typeof params == 'string') {
			if(params == 'extended') {
				this.initExtendedInterface();
			} else {
				this.initClasicInterface();
			}
		} else {
			;
		}
	},

	initClasicInterface: function() {
		this.iElements = {
			'bold': this.bold,
			'italic': this.italic,
			'underline': this.underline,
			'left': this.left,
			'center': this.center,
			'right': this.right,
			'uli': this.uli,
			'oli': this.oli
		}
	},

	trackKeys: function(e) {
		if(e.which == 13) {
			this.newLine = 1;
		}
	},

	nodeCreate: function(name, attrs, context) {
		var doc = (context) ? context : this.inst.content;
		node = doc.createElement(name);
		for(i = 0; i <= attrs.length - 1; i++) {
			node.setAttribute(attrs[i].name, attrs[i].value);
		}
		return node;
	},

	text: function(content, context) {
		var doc = (context) ? context : this.inst.content;
		return doc.createTextNode(content);
	},

	appendList: function(node, nodeList) {
		for(i = 0; i <= nodeList.length - 1; i++) {
			node.appendChild(nodeList[i]);
		}
		return node;
	},

	hiddenAttr: function(attribute) {
		if(attribute.name == '_moz_dirty') {
			return true;
		} else if(attribute.name == 'type' && attribute.value == '_moz') {
			return true;
		}
		return false;
	},

	transformNode: function(node, depth) {
		var attributies = '';
		var p = (!_IE()) ? this.nodeCreate('p', [{name: '_moz_dirty', value: ''}]) : this.nodeCreate('p', []);

		if(depth > 1) {
			for(var d = 2; d <= depth; d++) {
				var indent = this.nodeCreate('span',
						[ {name: 'style',      value: 'padding-left: 1.3em'},
						  {name: '_moz_dirty', value: ''} ]);

				p.appendChild(this.text(''));
				p.appendChild(indent);
			}
		}
		if(node.nodeType == 1) {
			var attr = node.attributes;

			var tag = this.nodeCreate('span',
					[ {name: 'class', value: 'tag'},
					  {name: 'alt',   value: 'tag'} ]);
			var tagText = this.text('<' + node.nodeName.toLowerCase());
			tag.appendChild(tagText);
			p.appendChild(tag);

			for(var i = 0; i <= attr.length - 1; i++) {
				if(!this.hiddenAttr(attr[i])) {
					var name = this.nodeCreate('span',
							[ {name: 'class', value: 'attr'},
							  {name: 'alt',   value: 'attr'} ]);
					name.appendChild(this.text(' ' + attr[i].name));

					var tagTextStart = this.nodeCreate('span',
							[ {name: 'class', value: 'text'},
							  {name: 'alt',   value: 'tagText'} ]);
					tagTextStart.appendChild(this.text('="'));

					var value = this.nodeCreate('span',
							[ {name: 'class', value: 'attrText'},
							  {name: 'alt',   value: 'attrText'} ]);
					value.appendChild(this.text(attr[i].value));

					var tagTextEnd = this.nodeCreate('span',
							[ {name: 'class', value: 'text'},
							  {name: 'alt',   value: 'tagText'} ]);
					tagTextEnd.appendChild(this.text('"'));

					this.appendList(p, [name, tagTextStart, value, tagTextEnd]);
				}
			}

			tag = this.nodeCreate('span',
					[ {name: 'class', value: 'tag'},
					  {name: 'alt',   value: 'tagEnd'} ]);
			(this.singleTags.indexOf(node.nodeName) >= 0) ? tag.appendChild(this.text(' />')) : tag.appendChild(this.text('>'));
			p.appendChild(tag);

		} else if(node.nodeName == '#text'){
			if(node.nodeValue.length > 0) {
				var textNode = this.nodeCreate('span',
						[ {name: 'class', value: 'text'},
						  {name: 'alt',   value: 'text'} ]);
				textNode.appendChild(this.text(node.nodeValue));
				p.appendChild(textNode);
			}
		}
		return p;
	},

	closeNode: function(node, depth) {
		var p = (!_IE()) ? this.nodeCreate('p', [{name: '_moz_dirty', value: ''}]) : this.nodeCreate('p', []);

		if(depth > 1) {
			for(var d = 2; d <= depth; d++) {
				var indent = this.nodeCreate('span',
						[ {name: 'style',      value: 'padding-left: 1.3em'},
						  {name: '_moz_dirty', value: ''} ]);

				p.appendChild(this.text(''));
				p.appendChild(indent);
			}
		}
		var tag = this.nodeCreate('span',
				[ {name: 'class', value: 'tag'},
				  {name: 'alt',   value: 'tagClose'} ]);
		tag.appendChild(this.text('</' + node.nodeName.toLowerCase() + '>'));
		p.appendChild(tag);

		return p;
	},

	DOMTexttoText: function(masterNode, domText) {
		if(!domText) {
			var domText = '';
		}
		for(var i = 0; i<= masterNode.childNodes.length - 1; i++) {
			if(masterNode.childNodes[i].nodeName == '#text') {
				domText += masterNode.childNodes[i].nodeValue;
			} else {
				domText = this.DOMTexttoText(masterNode.childNodes[i], domText);
			}
		}
		if(masterNode.nodeName == 'BODY') {
			masterNode.innerHTML = domText;
		}

		return domText;
	},

	DOMtoDOMText: function(masterNode, domText, depth) {
		if(!domText) {
			var domText = this.nodeCreate('div', [ {name: 'style', value: 'font-family: "Courier New"'} ]);
		}
		depth = (!depth) ? 1 : depth + 1;

		if(masterNode.childNodes) {
			for(var i = 0; i<= masterNode.childNodes.length - 1; i++) {

				if(masterNode.childNodes[i].nodeType == 1) {
					var tagNode = this.transformNode(masterNode.childNodes[i], depth);
					domText.appendChild(tagNode);
					this.DOMtoDOMText(masterNode.childNodes[i], domText, depth);

					if(this.singleTags.indexOf(masterNode.childNodes[i].nodeName) < 0) {
						var closeNode = this.closeNode(masterNode.childNodes[i], depth);
						domText.appendChild(closeNode);
					}
				} else if(masterNode.childNodes[i].nodeName == '#text' && depth != 1 && masterNode.childNodes[i].nodeValue.length > 0){
					var tagNode = this.transformNode(masterNode.childNodes[i], depth);
					domText.appendChild(tagNode);
				}
			}
			if(depth == 1) {
				this.empty(this.inst);
				this.inst.content.body.appendChild(domText);
			}
		}
	},

	domFilter: function(masterNode) {
		//alert('DOM FILTER');
		if(masterNode && masterNode.childNodes  && masterNode.childNodes.length) {
			for(var i = 0; i < masterNode.childNodes.length; i++) {
//				if(!this.notEmpty(masterNode.childNodes[i], 1) && masterNode.childNodes[i].nodeName != 'BR' && masterNode.childNodes[i].nodeName != 'IMG' && masterNode.childNodes[i].nodeName != 'P') {
//					alert(masterNode.childNodes[i].nodeName + ' : ' + masterNode.childNodes[i].innerHTML + ' : ' + masterNode.childNodes[i].nodeValue);
//					masterNode.removeChild(masterNode.childNodes[i]);
//					if(i > 0) {
//						i -= 1;
//					}
//				}
				if(masterNode.childNodes[i].nodeName == 'BLOCKQUOTE' && masterNode.childNodes[i].hasAttribute('alt')) {
					//alert('bleble');
					var alt = masterNode.childNodes[i].getAttribute('alt');
					if(alt == 'div') {
						var div = this.nodeCreate('div', []);
						masterNode.insertBefore(div, masterNode.childNodes[i]);
						for(var j = 0; j < masterNode.childNodes[i+1].attributes.length; j++) {
							div.setAttribute(masterNode.childNodes[i+1].attributes[j].name, masterNode.childNodes[i+1].attributes[j].value);
						}
						for(var k = 0; k < masterNode.childNodes[i+1].childNodes.length; k++) {
							div.appendChild(masterNode.childNodes[i+1].childNodes[k]);
						}
						masterNode.removeChild(masterNode.childNodes[i+1]);
					}
				}
				this.domFilter(masterNode.childNodes[i]);
			}
		}
	},

	filterCode: function(masterNode) {
		var position = -1;
		for(var i = 0; i < masterNode.childNodes.length; i++) {
			if(masterNode.childNodes[i].nodeName == 'BR' && masterNode.childNodes[i].nextSibling) {
				tmp = masterNode.childNodes[i];
				while(tmp.nextSibling && tmp.nextSibling.nodeName == '#text') {
					if(tmp.nextSibling.nodeValue.length) {
						break;
					}
					tmp = tmp.nextSibling;
				}
				if(tmp.nextSibling && tmp.nextSibling.nodeName == 'BR') {
					var save = tmp.nextSibling;
					masterNode.removeChild(masterNode.childNodes[i]);
					masterNode.removeChild(save);
					position = i;
					break;
				}

			}
//			var found = 0;
//			tmp = masterNode.childNodes[i];
//			while(tmp.nodeName != 'P' && tmp.nodeName != 'HTML') {
//				tmp = tmp.parentNode;
//			}
//			if(tmp.nodeName == 'HTML' && masterNode.childNodes[i].nodeName != 'BODY') {
//				var p = this.nodeCreate('P',[]);
//				masterNode.childNodes[i].parentNode.insertBefore(p, masterNode.childNodes[i]);
//				p.appendChild(masterNode.childNodes[i]);
//			}

		}
		if(position > -1) {
			var p = this.nodeCreate('P',[]);
			masterNode.parentNode.insertBefore(p, masterNode.nextSibling);
			for(i = position;  i < masterNode.childNodes.length; i++) {
				//alert(masterNode.childNodes[i].nodeName + ':' + masterNode.childNodes[i].nodeValue);
				p.appendChild(masterNode.childNodes[i]);
				i -= 1;
			}
			//p.removeChild(save);
			this.filterCode(p);
		}

	},

	filter: function(text) {
		while(text.match(/blockquote alt="div"/)) {
			text = text.replace('blockquote alt="div"', 'div').replace('/blockquote', '/div');
		}
		alert(text);
		return text;
	},

	nodeToPass: {},

	nodesToChange: [],

	k: 0,

	classNodes: function(masterNode, attr) {
		for(var i = 0; i < masterNode.childNodes.length; i++) {
			//alert(masterNode.childNodes[i].nodeName + masterNode.childNodes[i].innerHTML);
			//if(masterNode.childNodes[i].nodeType ==  1) {
				//alert(/*masterNode.childNodes[i].hasAttribute(attr)*/ jQuery(masterNode.childNodes[i]).attr('class'));
			//}
			if(masterNode.childNodes[i].nodeType ==  1 && masterNode.childNodes[i].hasAttribute(attr) && masterNode.childNodes[i].getAttribute(attr).length) {
				//alert('attr:' + attr + ' ' + masterNode.childNodes[i].nodeName + ' ' + masterNode.childNodes[i].hasAttribute(attr) + ' ' + masterNode.childNodes[i].getAttribute(attr));
				this.nodesToChange.push(masterNode.childNodes[i]);
			}
			this.classNodes(masterNode.childNodes[i], attr);
		}
	},

	generateClasses: function(masterNode) {
		this.nodesToChange = [];
		var file = this.inst.cssGenFile;

		jQuery.ajaxSetup({async: false});
		this.classNodes(masterNode, 'style');

		for(var i = 0; i < this.nodesToChange.length; i++) {
			this.nodeToPass = this.nodesToChange[i];
			jQuery.post('../ultraedit/generateClasses.php', 'file=' + file + '&style=' + escape(this.nodesToChange[i].getAttribute('style')), function(data) {
				UltraEdit.nodeToPass.removeAttribute('style');
				UltraEdit.nodeToPass.setAttribute('class', 'ultraEdit_gen_' + data);
			});
		}
		jQuery.ajaxSetup({async: true});
	},

	classesToStyle: function(inst) {
		jQuery.ajaxSetup({async: false});
		this.nodesToChange = new Array();
		this.classNodes(inst.content.body, 'class');
		UltraEdit.log('5248', 'classesToStyle');
		jQuery.get('../ultraedit/' + inst.cssGenFile, function(data) {
			UltraEdit.log('5249', 'classesToStyle GET');
			for(var i = 0; i < UltraEdit.nodesToChange.length; i++) {
				UltraEdit.log('5250', 'ITERATOR: ' + i + '<BR /> NODE HTML: ' + UltraEdit.nodesToChange[i].innerHTML + '<br />VALUE: ' + UltraEdit.nodesToChange[i].nodeValue);
				var className = UltraEdit.nodesToChange[i].getAttribute('class');
				UltraEdit.log('5251', 'CLASSNAME: ' + className);
				var match = data.match(className);
				UltraEdit.log('5253', 'MATCH: ' + match);
				//alert('Match' + match);
				//alert(match != 'null');
				//alert(match != null);
				if(match != 'null') {
					var start = data.indexOf(className);
					var end = start + data.slice(start).indexOf('}');
					var style = data.substring(start, end);
					style = style.replace(className + ' {','').replace('\n','').replace('\t','');
					UltraEdit.log('5230', 'CLEAR STYLE: ' + style);
					UltraEdit.nodesToChange[i].removeAttribute('class');
					UltraEdit.nodesToChange[i].setAttribute('style', style);
					UltraEdit.log('5263', 'NODE STYLE ATTR: ' + UltraEdit.nodesToChange[i].getAttribute('style'));
					//alert(style);
				}
			}
		});
		jQuery.ajaxSetup({async: true});
	},

	log: function(line, text) {
		jQuery.ajaxSetup({async: false});
		jQuery.post('../ultraedit/log.php', 'line=' + line + '&text=' + text);
		//jQuery.ajaxSetup({async: true});
	}

};

function load_module(which) {

	window_content = jQuery.get('http://ultraportal.home/' + which + '/index/',
		function(data) {
			jQuery('.window_pannel').html(data);
			//eval(which + '()');
			//alert(data);
		});

}
function load_action(selector, module, action) {

	window_content = jQuery.get('http://ultraportal.home/' + module + '/' + action + '/',
		function(data) {
			jQuery(selector).html(data);
			//eval(module + '()');

		});
		//alert('http://ultraportal.home/' + module + '/' + action + '/');
		//jQuery(selector).load('http://ultraportal.home/' + module + '/' + action + '/');
		//jQuery(selector).html('vvvv');
	return false;
}
function load_component(selector, parameters) {
	jQuery.post('http://ultraportal.home/loadRemote/index', parameters,
		function(data) {
			if(parameters.append) {
				jQuery(selector).html(jQuery(selector).html() + data);
			}
			else {
				jQuery(selector).html(data);
			}
			//eval(module + '()');
			//alert(data);
		});
}
function userProfile(){
	jQuery('.info td').click( function() {
		jQuery('.info tr td').css('background-color', 'transparent').css('color', '#000000');
		jQuery('.info tr#' + this.parentNode.id + ' td').css('background-color', '#002266').css('color', '#ffffff');

		id_parts = this.parentNode.id.split('_');
		load_component('.info_body_right2_bottom', {
			module_name: 'userProfile',
			component: 'messageView',
			id: id_parts[1]
		});
	});
}

function image_popup(which, max_width, max_height, image_height) {

	var cH = document.documentElement.clientHeight;
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0) {
	    if (window.pageYOffset)
	        ScrollTop = window.pageYOffset;
	    else
	        ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	//alert(ScrollTop);
	var div = document.createElement('div');
	div.setAttribute('id', 'popup_master_div');
	div.className = 'image_popup_div';

	var img = document.createElement('img');
	if (which.src) {
		img.setAttribute('src', which.src);
	} else {
		img.setAttribute('src', which);
	}
	img.setAttribute('id', 'img_popup');
	//img.setAttribute('onclick', 'removeImagePopup()');
	div.appendChild(img);
	document.getElementsByTagName('BODY')[0].appendChild(div);

	jQuery('#img_popup').click(function() {
		removeImagePopup();
	});

	var imgH = jQuery('#img_popup').css('height');
	var imgW = jQuery('#img_popup').css('width');
	var divH = jQuery('#popup_master_div').css('height');
	var divW = jQuery('#popup_master_div').css('width');

	if(imgH > divH) {
		jQuery('#img_popup').css('height', divH);
	}
	if(imgW > divW) {
		jQuery('#img_popup').css('width', divW);
	}

	if (image_height != null) {
		var centerY = image_height/2;
	} else {
		var centerY = Math.round(cH/2);
	}
	imgH = jQuery('#img_popup').css('height');
	imgW = jQuery('#img_popup').css('width');
	jQuery('#img_popup').css('height', '1px');
	jQuery('#img_popup').css('width',  '1px');
	jQuery('#popup_master_div').css('width', '1px');
	jQuery('#popup_master_div').css('height', '1px');
	jQuery('#popup_master_div').css('left', jQuery('#popup_master_div').css('left'));
	jQuery('#popup_master_div').css('top', centerY + 'px');

	jQuery('#popup_master_div').css('margin', ScrollTop + 'px 0 0 0');

	$('popup_master_div').morph({width: imgW, height: imgH});
	$('img_popup').morph({width: imgW, height: imgH});
	new Effect.Opacity('popup_master_div', { from: 0.01, to: 0.99});
	new Effect.Move('popup_master_div', { x: '-' + Math.round(parseInt(imgW)/2), y: '-' + Math.round(parseInt(imgH)/2), mode: 'relative' });

}
function removeImagePopup() {
	jQuery('#popup_master_div').css('background-color', 'transparent');
	new Effect.Puff('img_popup', {duration: 0.7});
	setTimeout(function() {
		jQuery('#popup_master_div').remove();
	}, 720);
}

function offerPopup(width, height) {

	//var imgW = jQuery('.oferta_master')[0].offsetWidth + 'px';
	var imgW = '802px';
	var imgH = (jQuery('#jobOffer_body')[0].offsetHeight + jQuery('.offer_window_top')[0].offsetHeight + jQuery('.offer_window_footer')[0].offsetHeight) + 'px';

	jQuery('#jobOffer').css('width', imgW);
	jQuery('#jobOffer').css('height', imgH);
	jQuery('#jobOffer').css('overflow', 'hidden');
	jQuery('#jobOffer').css('width', '1px');
	jQuery('#jobOffer').css('height', '1px');
	jQuery('#jobOffer').css('left', Math.round(document.documentElement.clientWidth/2));

	setTimeout(function() {
		$('jobOffer').morph({width: imgW, height: imgH});
		new Effect.Opacity('jobOffer', { from: 0.01, to: 0.99});
		new Effect.Move('jobOffer', { x: '-' + Math.round(parseInt(imgW)/2), y: 0, mode: 'relative' });
	}, 700);

}

function change_module_header(which, moduleId) {
//	switch(which) {
//	case 'uslugi'
//	}
	jQuery('#ultra_modules_' + moduleId + '_head').attr('id', 'ultra_modules_' + moduleId + '_head_' + which);

}

activeWindows = new Array();
function WindowInfo(root,handler,parameters)
{
	this.id = root.id;
	this.parameters = parameters;
	this.handlerId = handler.id;
	this.positionX = null;
	this.positionY = null;
	this.minimalizedX = null;
	this.minimalizedY = null;
	this.status = "closed";
	this.width = null;
	this.height = null;
	this.layer = null;
	this.maxLayer = 1;

	this.show = function(type,x,y,callback) {
		defaultWindow.maxLayer += 1;
		this.layer = defaultWindow.maxLayer;
		this.status = "open";
		//alert(this.id);
		//jQuery('#url_pannel_body').html(this);
		$(this.id).style.display = 'block';
		$(this.id).style.zIndex = this.layer;
		this.width = parseInt(window.getComputedStyle($(this.id),'').width);
		this.height = parseInt(window.getComputedStyle($(this.id),'').height);
		//activeWindows[this.id] = this;
		//type == 'xy_center' ? Drag.init($(this.id)) : Drag.init($(this.id+'_top'),x,y,$(this.id));
		new Draggable(this.id, { handle: this.id + '_top' });
		if(parameters)
		{
			if(parameters.type == 'remote')
			{
				this.parameters.requestParams.postBody = 't1='+$('textarea1').value+'&t2='+$('textarea2').value;
				new Ajax.Updater(this.parameters.area,this.parameters.file,this.parameters.requestParams);
				//new Ajax.Updater(parameters.area,parameters.file,{postBody: parameters.requestParams.postBody,
				//evalscripts: true, onComplete: parameters.requestParams.onComplete});
				return false;
			}
		}
		//eval(callback);
	}
	this.close = function()
	{
		$(this.id).style.display = 'none';
		this.status = "closed";
	}
	this.update = function()
	{
		new Ajax.Updater(parameters.area,parameters.file,parameters.requestParams);
	}
	this.ontop = function()
	{
		if(this.layer < defaultWindow.maxLayer)
		{
			defaultWindow.maxLayer += 1;
			this.layer = defaultWindow.maxLayer;
		}
		$(this.id).style.zIndex = this.layer;
		//activeWindows[this.id].show('',200,10);
		new Draggable(this.id, { handle: this.id + '_top' });
	}
	this.minimalize = function(x,y)
	{
		if(this.status != "minimalized")
		{
			jQID = '#' + this.id;
			this.minimalizedX = parseInt(jQuery('#' + this.id).css('left'));
			this.minimalizedY = parseInt(jQuery('#' + this.id).css('top'));
			transitionX = this.minimalizedX - this.positionX - parseInt(window.getComputedStyle($(this.id),'').marginLeft);
			transitionY = this.minimalizedY - this.positionY - parseInt(window.getComputedStyle($(this.id),'').marginTop);

			//$(this.id).style.zIndex = 2;
			new Effect.Move($(this.id),{ x: 10, y: -132, mode: 'absolute'});
			//Effect.BlindUp(this.id+'_body');
			//Effect.BlindUp(this.id+'_bottom');
			$(this.id).morph({width: '300px', height: '32px'});
			//jQuery('#' + this.id + ' > div').css('display', 'none');
			jQuery(jQID + ' .window_title').css('width', '165px').css('overflow','hidden');
			this.status = "minimalized";
		}
	}
	this.maximalize = function()
	{
		if(this.status != "open")
		{
			jQID = '#' + this.id;
			transitionX = this.positionX - this.minimalizedX + parseInt(window.getComputedStyle($(this.id),'').marginLeft);
			transitionY = this.positionY - this.minimalizedY + parseInt(window.getComputedStyle($(this.id),'').marginTop);
			//$(this.id).style.zIndex = 2;
			//jQuery('#' + this.id + ' > div').css('display', 'block');
			new Effect.Move($(this.id),{ x: this.minimalizedX, y: this.minimalizedY, mode: 'absolute'});
			//Effect.BlindDown(this.id+'_body');
			//Effect.BlindDown(this.id+'_bottom');
			jQuery(jQID + ' .window_title').css('width', 'auto').css('overflow','none');
			$(this.id).morph({width: this.width + 'px', height: this.height + 'px'});
			this.status = "open";
		}
	}
	this.minimalizeAll = function()
	{
		i = 0;
		for(w in activeWindows)
		{
			if(activeWindows[w].status == "open")
			{
				activeWindows[w].minimalize(2,480+i*35);
			}
			i+=1;
		}
	}
	this.maximalizeAll = function()
	{
		for(w in activeWindows)
		{
			if(activeWindows[w].status == "minimalized")
			{
				activeWindows[w].maximalize();
			}
		}
	}
}
function addTread(url) {
	//jQuery('.content').load(url);
	//return false;
}
function post_copy(id) {
	jQuery('#answer').val(jQuery('#content'+id).html());
	return false;
}
function captcha_choice(which) {
	if(jQuery('input[name=captcha]').val() == "") {
		jQuery('input[name=captcha]').val(jQuery(which).html());
	}
	else {
		jQuery('input[name=captcha]').val(jQuery('input[name=captcha]').val() + ', ' + jQuery(which).html());
	}
}
function login(url) {
	window.location = url;
}
function login_pannel() {
	if(jQuery('.login_pannel1').css('display') == 'none') {
		jQuery('.login_pannel1').css('display','block');
		jQuery('.login_pannel2').css('display','block');
	}
	else {
		jQuery('.login_pannel1').css('display','none');
		jQuery('.login_pannel2').css('display','none');
	}
	return false;
}
function blog_pannel_change(button1,button2,pannel1,pannel2) {

	if(jQuery(button1).attr('class') == 'blog_new_link_active') {
		jQuery(button1).attr('class','blog_new_link_passive');
		jQuery(button2).attr('class','blog_top_link_active');
		//iblog+=1;
	}
	else {
		jQuery(button1).attr('class','blog_new_link_active');
		jQuery(button2).attr('class','blog_top_link_passive');
	}
	jQuery(pannel1).toggleClass('none');
	jQuery(pannel2).toggleClass('none');
	//jQuery('#aaa').html('aaa'+iblog);
}
function download_pannel_change(button1,button2,pannel1,pannel2) {

	if(jQuery(button1).attr('class') == 'download_new_link_active') {
		jQuery(button1).attr('class','download_new_link_passive');
		jQuery(button2).attr('class','download_top_link_active');
		//iblog+=1;
	}
	else {
		jQuery(button1).attr('class','download_new_link_active');
		jQuery(button2).attr('class','download_top_link_passive');
	}
	jQuery(pannel1).toggleClass('none');
	jQuery(pannel2).toggleClass('none');
	//jQuery('#aaa').html('aaa'+iblog);
}
function color_picker_show(which) {
	/*if(jQuery(which).css('visibility') == 'hidden') {
		jQuery(which).css('visibility','visible');
	}
	else if(jQuery(which).css('visibility') == 'visible') {
		jQuery(which).css('visibility','hidden');
	}
	else if(jQuery(which).css('visibility') == 'hide') {
		jQuery(which).css('visibility','show');
	}
	else if(jQuery(which).css('visibility') == 'show') {
		jQuery(which).css('visibility','hide');
	}*/
	if(jQuery(which).css('display') == 'none') {
		jQuery(which).css('display','block');
	}
	else if(jQuery(which).css('display') == 'block') {
		jQuery(which).css('display','none');
	}
}
function color_choice(name,r,g,b) {
	color_array = new Array('00','33','66','99','cc','ff');
	jQuery('#'+name).css('backgroundColor','#'+color_array[r]+color_array[g]+color_array[b]);
	jQuery('#color_picker_'+name).css('visibility','hidden');
	jQuery('input[name='+name+']').val('#'+color_array[r]+color_array[g]+color_array[b]);
}
function load_form_elements(url,form_url) {
	url_parts = url.split('/');
	param = url_parts[url_parts.length - 1];
	jQuery('form').attr('action',form_url);

	if(param == 'top') {
		jQuery('#submit_button').html('<input class="form_button_left" type="image" src="/images/main_theme/refresh_button.gif" onclick="return refresh_top()" />');
	}
	if(param == 'header') {
		jQuery('#submit_button').html('<input class="form_button_left" type="image" src="/images/main_theme/refresh_button.gif" onclick="return refresh_header()" />');
	}
	if(param == 'menu') {
		jQuery('#submit_button').html('<input class="form_button_left" type="image" src="/images/main_theme/refresh_button.gif" onclick="return refresh_menu()" />');
	}
	if(param == 'site') {
		jQuery('#submit_button').html('<input class="form_button_left" type="image" src="/images/main_theme/refresh_button.gif" onclick="return refresh_site()" />');
	}
	if(param == 'body') {
		jQuery('#submit_button').html('<input class="form_button_left" type="image" src="/images/main_theme/refresh_button.gif" onclick="return refresh_body()" />');
	}
	if(param == 'text') {
		jQuery('#submit_button').html('<input class="form_button_left" type="image" src="/images/main_theme/refresh_button.gif" onclick="return refresh_text()" />');
	}
	jQuery('.form_body_right').load(url);
}
function refresh_top() {
	jQuery('form').submit();
	date = new Date();
	time2 = date.getMilliseconds();
	setTimeout("jQuery('.top_site1').css('backgroundImage','url(\"/data/userblogs/1/top.jpg?id='+time2+'\")')",2000);
	return false;
}
function refresh_header() {
	jQuery('form').submit();
	date = new Date();
	time2 = date.getMilliseconds();
	setTimeout("jQuery('.ultra_header_blog1').css('backgroundImage','url(\"/data/userblogs/1/header.jpg?id='+time2+'\")')",2000);
	return false;
}
function refresh_menu() {
	jQuery('form').submit();
	date = new Date();
	time2 = date.getMilliseconds();
	setTimeout("jQuery('.aktual').css('backgroundImage','url(\"/data/userblogs/1/aktual.png?id='+time2+'\")')",1000);
	setTimeout("jQuery('.tutorial').css('backgroundImage','url(\"/data/userblogs/1/tutorial.png?id='+time2+'\")')",1000);
	setTimeout("jQuery('.blogosfera').css('backgroundImage','url(\"/data/userblogs/1/blogosfera.png?id='+time2+'\")')",1000);
	setTimeout("jQuery('.forum').css('backgroundImage','url(\"/data/userblogs/1/forum.png?id='+time2+'\")')",1000);
	setTimeout("jQuery('.download').css('backgroundImage','url(\"/data/userblogs/1/download.png?id='+time2+'\")')",1000);
	setTimeout("jQuery('.logowanie').css('backgroundImage','url(\"/data/userblogs/1/logowanie.png?id='+time2+'\")')",1000);
	return false;
}
function refresh_site() {
	jQuery('form').submit();
	date = new Date();
	time2 = date.getMilliseconds();
	jQuery('body').css('background-color',jQuery('input[name=body_bg_color]').val());
	setTimeout("jQuery('body').css('backgroundImage','url(\"/data/userblogs/1/body.jpg?id='+time2+'\")')",2000);
	return false;
}
function refresh_body() {

	jQuery('form').submit();
	date = new Date();
	time2 = date.getMilliseconds();
	jQuery('.master_blog1').css('background-color',jQuery('input[name=layer1_bg_color]').val());
	setTimeout("jQuery('.master_blog1').css('backgroundImage','url(\"/data/userblogs/1/site_body_layer1.jpg?id='+time2+'\")')",2000);
	jQuery('.ultra_body_2blog1').css('background-color',jQuery('input[name=layer2_bg_color]').val());
	if(jQuery('input[name=layer2_bg]').val() != '') {
		//jQuery('#submit_button').html(jQuery('input[name=layer2_bg]').val());
		setTimeout("jQuery('.ultra_body_2blog1').css('backgroundImage','url(\"/data/userblogs/1/site_body_layer2.jpg?id='+time2+'\")')",2000);
	}
	return false;
}
function refresh(url,time) {
	jQuery('body').css('background-color',jQuery('input[name=body_bg_color]').val());
	setTimeout("jQuery('body').css('backgroundImage','url(\"/data/userblogs/1/body.jpg?id='+time2+'\")')",2000);
	//jQuery('.ultra_header_blog1').load(url);
	//jQuery('form').ajaxSubmit();
	jQuery('form').submit();
	date = new Date();
	time2 = date.getMilliseconds();
	setTimeout("jQuery('.aktual').css('backgroundImage','url(\"/data/userblogs/1/aktual.png?id='+time2+'\")')",500);
	setTimeout("jQuery('.tutorial').css('backgroundImage','url(\"/data/userblogs/1/tutorial.png?id='+time2+'\")')",500);
	setTimeout("jQuery('.blogosfera').css('backgroundImage','url(\"/data/userblogs/1/blogosfera.png?id='+time2+'\")')",500);
	setTimeout("jQuery('.forum').css('backgroundImage','url(\"/data/userblogs/1/forum.png?id='+time2+'\")')",500);
	setTimeout("jQuery('.download').css('backgroundImage','url(\"/data/userblogs/1/download.png?id='+time2+'\")')",500);
	setTimeout("jQuery('.logowanie').css('backgroundImage','url(\"/data/userblogs/1/logowanie.png?id='+time2+'\")')",500);
	setTimeout("jQuery('.ultra_header_blog1').css('backgroundImage','url(\"/data/userblogs/1/header.jpg?id='+time2+'\")')",2000);
	setTimeout("jQuery('.top_site1').css('backgroundImage','url(\"/data/userblogs/1/top.jpg?id='+time2+'\")')",2000);

	return false;
}
function rating_over(which) {
	//which.src = which.src.replace('.gif','_hover.gif');
	jQuery('#rating_img2').attr('src', '/images/main_theme/stars_rate' + which + '.gif');
	//alert(which);
}
function rating_out(which) {
	jQuery('#rating_img2').attr('src', '/images/main_theme/stars_rate0.gif');
}
function rate(which,url){
	jQuery('.download_ratings_block').html("<span class='thanks'>DziÄ™kujemy za oddanie oceny!</span>");
	jQuery('.download_rated').load(url);
	return false;
}
function editor(which)
{
	if(which.innerHTML == "")
	{
		which.innerHTML += "<span class='cursor'>|</span>"
	}
	document.onkeypress = function(e) {

		if(window.event) // IE
		keynum = e.keyCode;
		else if(e.which) // Netscape/Firefox/Opera
			keynum = e.which;
		keychar = String.fromCharCode(keynum);
		//document.write(keychar);
		which.innerHTML = which.innerHTML.replace('<','&lt;');
	}
}
String.prototype.add = function(text) {
	return this+text+"<span class='cursor'>|</span>";
}
String.prototype.replaceCursor = function()
{
	return this.replace("<span class='cursor'>|</span>");
}

function keypressed(e)
{
	/*if(window.event) // IE
		keynum = e.keyCode;
	else if(e.which) // Netscape/Firefox/Opera
		keynum = e.which;
	keychar = String.fromCharCode(keynum);
	$('editor').innerHTML() += "aaaa";
	*/
}
function homepage_load()
{
	//$$('.html_11_head')[0].innerHTML = "<img style='margin-left: 0px' alt='strefa webmastera' src='../web/images/main_theme/webmaster_zone.jpg' />";
	//$$('.html_12_head')[0].innerHTML = "<img style='margin-left: 0px' alt='strefa webmastera' src='../web/images/main_theme/webmaster_zone2.gif' />";
	//$$('.html_21_head')[0].innerHTML = "<img style='margin-left: 0px' alt='strefa webmastera' src='../web/images/main_theme/webdesign.jpg' />";
	//$$('.html_22_head')[0].innerHTML = "<img style='margin-left: 0px' alt='strefa webmastera' src='../web/images/main_theme/webdesign2.gif' />";
	/*if($$('.html_11_head')[0])
	{
		$$('.html_11_head')[0].innerHTML = "";
		$$('.html_12_head')[0].innerHTML = "";
		$$('.html_21_head')[0].innerHTML = "";
		$$('.html_22_head')[0].innerHTML = "";
	}*/
	a = 2;
}