RTEditing = function(namen, id){
	var thisObj = this;
	this._form = $(id).get();
	this._fieldNames = namen;

}
RTEditing.prototype={
		init:function(){
			var thisObj = this;
			
			for(var i=0;i<this._fieldNames.length;i++){
				var editable = $('div').setAttribute('contenteditable', 'true').setAttribute('class', 'editable').get();
				var name = this._fieldNames[i];
				var val = this._form[this._fieldNames[i]].value;
				editable.innerHTML = val;
	
				$(this._form[this._fieldNames[i]]).remove();
				
				$(this._form).append($('input').setAttribute('type', 'hidden').setAttribute('name', this._fieldNames[i]).setAttribute('value', val).get());
				
				$(this._form).before(this.getToolbar(editable, this._form[name]));
				$(this._form).before(editable);
				
				(function(editable, target){
					$(editable).addEvent('keyup', function(){thisObj.refreshContent(this, target)});
				})(editable, this._form[name]);
				
				
				this.exec(editable, this._form[name], 'styleWithCSS', false);
			}
		},
		getToolbar:function(editable, target){
			
			var thisObj = this;
			
			var list = $('div').get();
			
			var bold = $('button').append('bold').get();
			$(bold).addEvent('click', function(){thisObj.exec(editable, target, 'bold')});
			$(list).append(bold);
			
			var italic = $('button').append('italic').get();
			$(italic).addEvent('click', function(){thisObj.exec(editable, target, 'italic');return false;});
			$(list).append(italic);
			
			var underline = $('button').append('underline').get();
			$(underline).addEvent('click', function(){thisObj.exec(editable, target, 'underline')});
			$(list).append(underline);
			
			var fontsize = $('select').get();
			$(fontsize).append($('option').setAttribute('value', '1').append('1').get());
			$(fontsize).append($('option').setAttribute('value', '2').append('2').get());
			$(fontsize).append($('option').setAttribute('value', '3').append('3').get());
			$(fontsize).addEvent('change', function(){thisObj.exec(editable, target, 'fontsize', this.value);});
			$(list).append(fontsize);
			
			var formatblock = $('select').get();
			$(formatblock).append($('option').setAttribute('value', '').append('select Style').get());
			$(formatblock).append($('option').setAttribute('value', 'h1').append('headline1').get());
			$(formatblock).append($('option').setAttribute('value', 'h2').append('headline2').get());
			$(formatblock).append($('option').setAttribute('value', 'h3').append('headline3').get());
			$(formatblock).append($('option').setAttribute('value', 'p').append('paragraph').get());
			$(formatblock).addEvent('change', function(){if(this.value=='')return false; thisObj.insertHtml(editable, target, '<'+this.value+'>', '</'+this.value+'>');});
			$(list).append(formatblock);
			
			var justify = $('select').get();
			$(justify).append($('option').setAttribute('value', 'left').append('left').get());
			$(justify).append($('option').setAttribute('value', 'right').append('right').get());
			$(justify).append($('option').setAttribute('value', 'center').append('center').get());
			$(justify).append($('option').setAttribute('value', 'full').append('full').get());
			$(justify).addEvent('change', function(){thisObj.exec(editable, target, 'justify'+this.value, null);});
			$(list).append(justify);
			
			var link = $('button').append('link').get();
			
			$(list).append(link);
			var linkInp = $('input').setStyle('display', 'none').get();
			
			var linkBut = $('button').setStyle('display', 'none').append('setLink').get();
			$(link).addEvent('click', function(){$(linkInp).setStyle('display', 'block');$(linkBut).setStyle('display', 'block');});
			$(linkBut).addEvent('click', function(){if(linkInp.value=='')return false; thisObj.insertHtml(editable, target, '<a href="'+linkInp.value+'">', '</a>')});
			$(list).append(linkInp);
			$(list).append(linkBut);
			
			var indent = $('button').append('indent').get();
			$(indent).addEvent('click', function(){thisObj.exec(editable, target, 'indent')});
			$(list).append(indent);
			
			var outdent = $('button').append('outdent').get();
			$(outdent).addEvent('click', function(){thisObj.exec(editable, target, 'outdent')});
			$(list).append(outdent);
			
			var removeformat = $('button').append('removeformat').get();
			$(removeformat).addEvent('click', function(){thisObj.exec(editable, target, 'removeformat');thisObj.exec(editable, target, 'unlink')});
			$(list).append(removeformat);
			
			return list;
		},
		exec:function(editable, target, commandName, commandArg){
			document.execCommand(commandName,false, commandArg);
			editable.focus();
			this.refreshContent(editable, target);			
		},
		insertHtml:function(editable, target, pre, post){
			
			var selObj = window.getSelection();
			var selRange = selObj.getRangeAt(0);
			var selected = selRange.toString() || 0;
			if(selected.length==0)
				return false;
			commandArg = pre+selected+post;
			selRange.deleteContents();
			this.exec(editable, target, 'inserthtml', commandArg);
		},
		refreshContent:function(src, target){
			target.value = src.innerHTML;
		}
}
