// -------------------------------------------------------------------------------
// --- DEFINES
// -------------------------------------------------------------------------------
	var rTypeNone = 0;
	var rTypeText = 1;
	var rTypeRich = 2;
// -------------------------------------------------------------------------------
// --- описание структуры rPath
// -------------------------------------------------------------------------------
	var orPath=null;
	function rPath(base_url,css_file){ // Пути до файлов RE
		this.base = base_url;				// до корня 
		this.image= "img/";					// до иконок 
		this.css	= css_file;				// файл с css
		// копия себя самого
		orPath = this;
	}
// -------------------------------------------------------------------------------
// --- описание класса rImages
// -------------------------------------------------------------------------------
	var orImages = null;
	var wrImages = null;
	function rImages(upload_url,swidth,sheight){ // Поле с картинками для вставки
		if(orImages == null){
			this.upload	= upload_url;	 // до upload файлов
			this.width	= swidth;	 // ширина картинки
			this.height	= sheight;	 // высота картинки
			orImages=this;
		}
	}
// -------------------------------------------------------------------------------
// --- описание класса rLinks
// -------------------------------------------------------------------------------
	var orLinks = null;
	var wrLinks = null;
	function rLinks(upload_url){ // 
		if(orLinks == null){
			this.upload	= upload_url;	 // до upload файлов
			orLinks=this;
		}
	}
// -------------------------------------------------------------------------------
// --- описание класса rText
// -------------------------------------------------------------------------------
	var orTextActive = null;
	function rText(id){ // Поле редактирования RE
		if(textObj = document.all[id]){
			var originalContent = textObj.value;	// сохраняем оригинальную заполнение
			this.width	= textObj.style.width;		// сохраняем оригинальную ширину
			this.height	= textObj.style.height;		// сохраняем оригинальную высоту
			this.id			= id;											// сохраняем оригинальный идентификатор объекта
			this.type		= rTypeNone;							// текущий тип поля редактирования
			// метод : переключение типа поля редактирования RE			
			this.tswitch = rText_tswitch;
			// метод : синхронизация поля редактирования и скрытого поля хранения RE
			this.synchro = rText_synchro;
			// создаем скрытый объект для сохранения данных
			textObj.outerHTML = '<input type=hidden id="'+this.id+'" name="'+textObj.name+'"><input type=hidden id="re'+this.id+'">'; // сгенеренный HTML взамен старого
			this.content = document.all[this.id];
			this.editor  = document.all['re'+this.id];
			this.content.value = originalContent;	
			this.rObject				= this;
			this.editor.rObject = this;		
			this.tswitch(rTypeRich);
		}
	}
// -------------------------------------------------------------------------------
// --- методы класса rText
// -------------------------------------------------------------------------------
	function rText_tswitch(type){ // переключение типа поля редактирования RE
		this.synchro();
		var is_not_editable = !(document.designMode);
		var oid = 're'+this.id;
		if(type==rTypeText || is_not_editable){
			// создаем новый объект на месте старого
			this.editor.outerHTML = '<textarea	id="'+oid+'" style="width:' +this.width+ '; height:' +this.height+ ';"></textarea>';
			this.editor  = document.all[oid]; // запоминаем его
			this.editor.value  = this.content.value; // обновляем значение из сохраненного	
			this.type = rTypeText;
			// устанавливаем события
			this.editor.onkeypress  = function() { orTextActive.synchro(); }
			this.editor.onkeyup     = function() { orTextActive.synchro(); }
			this.editor.onmouseup   = function() { orTextActive.synchro(); }
			this.editor.ondrop			= function() { setTimeout(orTextActive.synchro,1000);	}     
			this.editor.oncut				= function() { setTimeout(orTextActive.synchro,1000); }
			this.editor.onpaste			= function() { setTimeout(orTextActive.synchro,1000); }
		}else if(type==rTypeRich){
			// создаем новый объект на месте старого
			this.editor.outerHTML = '<iframe	id="'+oid+'" style="width:' +this.width+ '; height:' +this.height+ ';"></iframe>';
			this.editor = document.all[oid]; // запоминаем его
			this.edocument = document.frames[oid];
			var html = '<html>';
			html += '<head>';
			html += '<link rel=stylesheet type=text/css href="'+orPath.base+orPath.css+'">';
			html += '<link rel=stylesheet type=text/css href="'+orPath.base+'richedit.css">';
			html += '</head>';
			html += '<body bgcolor=#ffffff topmargin=0 leftmargin=0 marginheight=0 marginwidth=0 class="richedit">';
			html += '</body>';
			html += '</html>';
			this.edocument.document.designMode = "on";
			this.edocument.document.open();
			this.edocument.document.write(html);
			this.edocument.document.close();
			this.edocument.document.body.innerHTML =  this.content.value; // обновляем значение из сохраненного	
			this.edocument.rObject = this;
			this.type = rTypeRich;		
			// устанавливаем события
			this.edocument.document.onkeypress     = function() { orTextActive.synchro(); }
			this.edocument.document.onkeyup        = function() { orTextActive.synchro(); }
			this.edocument.document.onmouseup      = function() { orTextActive.synchro(); }
			this.edocument.document.body.ondrop    = function() { setTimeout(orTextActive.synchro,1000);	}     
			this.edocument.document.body.oncut     = function() { setTimeout(orTextActive.synchro,1000); }
			this.edocument.document.body.onpaste   = function() { setTimeout(orTextActive.synchro,1000); }
		}
		this.rObject				= this;
		this.editor.rObject = this;		
		// устанавливаем события
		this.editor.onfocus			= function() { orTextActive = this.rObject;	orPanel.update(); }
		this.editor.focus();
	}
// --- 
	function rText_synchro(){// синхронизация поля редактирования и скрытого поля хранения RE	
		if(orTextActive!=null){
			if(orTextActive.type==rTypeText){
				orTextActive.content.value = orTextActive.editor.value;	
			}else if(orTextActive.type==rTypeRich){
				// очистка не нужных тегов и прочей грязи
				if(orPanel.autoformat){
					var removeTags = orTextActive.edocument.document.all;
					for(var i=removeTags.length-1;i>=0;i--){
						removeTags[i].removeAttribute("size");
						removeTags[i].removeAttribute("face");
						removeTags[i].removeAttribute("style");
						if(removeTags[i].tagName!="IMG"){
							removeTags[i].removeAttribute("width");
							removeTags[i].removeAttribute("height");
							removeTags[i].removeAttribute("border");
							removeTags[i].removeAttribute("cellPadding");
							removeTags[i].removeAttribute("cellSpacing");
							
						}
					}
				}
				orTextActive.content.value = orTextActive.edocument.document.body.innerHTML;	
			}
		}
		return true;
	}
// -------------------------------------------------------------------------------
// --- рабочии функции
// -------------------------------------------------------------------------------
	function rGetParent(elmChild,tagName){ // найти родительский объект с заданным tagName
		var elmParent = elmChild;
		while(elmParent.tagName.toUpperCase()!=tagName && elmParent.tagName.toUpperCase()!="BODY"){
			elmParent = elmParent.parentNode;
		}
		return (elmParent.tagName.toUpperCase()==tagName)?elmParent:null;
	}

