/*
* JavaScript library ver. 0.1.1
* Copyright (c) 2007-2008 Sergey Voronkov(grey_asd@mail.ru)
* http://www.softcoder.ru
* Пожалуста не удаляйте и не изменяйте эти коментарии.
* 
*   Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
*/
function confirm(text,callback)
{
	var win = new Win({
		buttons:[
			{text:'Да',type:'submit'},
			{text:'Нет',onclick:function(){win.hide()}}
		]
	});
	win.content.innerHTML = "<p align='center'>"+text+"</p>";
	win.onclickok = function(){this.hide();callback();return false;};
	win.show();
}
function message(text)
{
	
}


var Win = jsfw.Class({
	__constructor:function(o)
	{
		this.o = $extend({
			modal:true,
			buttons:[
				{type:'submit',text:'Ok'},
				{text:'Отмена',onclick:this.cancel}
			]
		},o);
		this.dom = $.create('div',{className:'Win'});
		this.form = $.create('form',{action:'#',onsubmit:function()
			{
				if(_this.callEvent('onclickok'))_this.hide();
				return false;
			}},this.dom);
		this.content = $.create('div',this.form);
		if(this.o.tmpl) this.tmpl = jsFW.tmpl(this.o.tmpl);
		var c = $.create('center',this.form);
		var _this=this;
		for(var i=0,l=this.o.buttons.length;i<l;i++)
		{
			var b = this.o.buttons[i];
			var inp = $.create('input',{type:b.type || 'button',value:b.text},c);
			if(b.name) inp.name = b.name;
			if(b.onclick) inp.onclick = $d(this,b.onclick);
		}
	},
	cancel:function(){
		this.callEvent('onclickcancel');
		this.hide();
	},
	show:function(data)
	{
		if(this.tmpl) this.content.innerHTML = this.tmpl(data||{});
		document.body.appendChild(this.dom);
		if(this.o.modal) jsFW.displayBlock({opacity:'0.10',backgroundColor:'#C1D9F0'});
		var width = this.dom.clientWidth;
		var bwidth = document.body.clientWidth;
		this.dom.style.left = (bwidth-width)/2 +'px';
		
		var height = this.dom.clientHeight;
		var bheight = document.body.clientHeight;
		this.dom.style.top = (bheight-height)/2 +'px';
		
		this.isShow = true;
		this.form.elements[0].focus();
	},
	hide:function()
	{
		$.remove(this.dom);
		if(this.o.modal) jsFW.displayUnblock();
		this.isShow = false;
	}
},jsfw.Object);

var Editor = jsfw.Class(function(){
return{
	__constructor:function(o)
	{
		this.dom = $(o.el);
		this.isAutoPreview = false;//$('changeAutoPreview').checked;
		var el=false;
		this.isTipograf = (el=$('changeTipograf')) && el.checked;
		this.isColer = (el=$('changeColer')) && el.checked; 
		this.isNumberCode = (el=$('changeIsLineColer')) && el.checked;
		this.isHyphenWords = false;
		if(!this.dom) this.dom = $.create('div',{className:'Editor'});
		this.controlBar = new ControlBar({
			el:$.getTag('div','controlBar',this.dom)[0]
		})
		this.controlBar.addEvent('onclick',$d(this,this.eClickConrolBarButton));
		this.textarea = $.getTag('textarea','*',this.dom)[0];
		this.textarea.onkeyup = $d(this,this.eKeyUp);
		if(jsfw.browser.opera)
			this.textarea.onkeypress = $d(this,this.eKeyDown);
		else
			this.textarea.onkeydown = $d(this,this.eKeyDown);
			
		this.preview = $('previewContent');
		this.taInsertText = $('taInsertText');
		var _this=this;
		this.taInsertText.onmousedown = function()
		{
			_this.setCaretPos(0,this.value.length,this);
			this.focus();
			return false;
		};
		this.currentFileName = '';
		document.body.onkeyup = $d(this,this.eKeyUpBody);
		var selectTab;
		$.getTag('a','Tab',this.dom,function(el){
			$(el.hash.substr(1)).style.display = 'none';
			el.onclick = function(){
				if(selectTab)
				{
					$.removeClass(selectTab.parentNode.parentNode,'selected');
					var id = selectTab.hash.substr(1);
					$(id).style.display = 'none';
				}
				selectTab = this;
				$.addClass(selectTab.parentNode.parentNode,'selected');
				var id = selectTab.hash.substr(1);
				$(id).style.display = '';
				if(id=='preview') _this.previewShowResult();
				if(id=='insertText') _this.getResulText();
				return false;
			};
			return el;
		})[0].onclick();
	},
	ajaxLoaderShow:function()
	{
		this.ajaxLoader = $.create('div',{className:'AjaxLoader'},document.body);
		$.create('img',{src:'/images/blogeditor/empty.gif'},this.ajaxLoader);
	},
	ajaxLoaderHide:function()
	{
		$.remove(this.ajaxLoader);
	},
	changeAutoPreview:function(b){
		this.isAutoPreview = b.getDom().checked;
		return true;
	},
	changeColer:function(b){
		this.isColer = b.getDom().checked;
		return true;
	},
	changeIsLineColer:function(b){
		this.isNumberCode = b.getDom().checked;
		return true;
	},
	changeTipograf:function(b)
	{
		this.isTipograf = b.getDom().checked;
		return true;
	},
	eClickConrolBarButton:function(cb,cmd,b)
	{
		return this.execute(cmd,b);
	},
	clearText:function()
	{
		if(this.textarea.value)
		{
			confirm("Все не сохраненые данные будут утерены.<br/> Продолжать?",$d(this,function(){
				this.textarea.value = '';
				this.currentFileName ='';
				this.preview.contentWindow.document.getElementById('content').innerHTML = '';
				this.taInsertText.value = "";
				return true;
			}));
		}
		return false;
	},
	loadListFile:function(callback)
	{
		_this = this;
		this.ajaxLoaderShow();
		jsFW.displayBlock({opacity:'0.30',backgroundColor:'#C1D9F0'});
		try{
			jsFW.HttpRequest.submit({
				url:'listtopic',
				type:'json',
				onload:function(data,success)
				{
					if(success){
						if(!data.error)
						{
							callback.call(_this,data);
						}
						else if(data.error.code==1)
						{
							_this.login(callback);
						}
						else
						{
							alert(data.error.text);
						}
					}
					else
					{
						jsFW.displayUnblock();
						alert('Что то пошло не так :(');
					}
					_this.ajaxLoaderHide();
				}
			});
		}catch(e){
			this.ajaxLoaderHide();
			jsFW.displayUnblock();
		}
	},
	login:function(callback)
	{
		var _this = this;
		function send_login(url,callback)
		{
			var name = this.form['login'].value;
			var password = this.form['password'].value;
			if(name.length>3)
			{
				if(password.length>0)
				{
					_this.ajaxLoaderShow();
					try{
						jsFW.HttpRequest.submit({
							url:url,
							param:{
								name:name,
								password:password
							},
							type:'json',
							onload:function(data,success)
							{
								if(success){
									if(!data.error)
									{
										win.cancel();
										_this.ajaxLoaderHide();
										callback();
									}
									else if(data.error.code==2)
									{
										$('message_autorization').innerHTML = data.error.text;
									}
									else
									{
										alert(data.error.text);
									}
								}
								else
								{
									alert('Что то пошло не так :(');
								}
								_this.ajaxLoaderHide();
							}
						});
					}catch(e){
						_this.ajaxLoaderHide();
					}
				}
				else
				{
					$('message_autorization').innerHTML = 'Не введен пароль.';
				}
			}
			else
			{
				$('message_autorization').innerHTML = 'Имя должно быть более 3х символов';
			}
		}
		var win = new Win({
			tmpl:'autorization',
			buttons:[
						{text:'Войти',onclick:function(){
							send_login.call(this,'login.php',function(){
								_this.loadListFile(callback);
							});
							return false;
						}},
						{text:'Регистрация',onclick:function(){
							send_login.call(this,'register.php',function(){
								_this.loadListFile(callback);
							});
							return false;
						}},
						{text:'Отмена',onclick:function(){win.cancel()}}
					]
		});

		win.show();

	},
	openFile:function()
	{
		var _this = this;
		function open()
		{
			_this.loadListFile(_this.openListTopic);
		}
		if(this.textarea.value!='')
		{
			confirm("Все не сохраненные данные будут утерены.<br/> Продолжать?",open);
		}
		else open();
		return false;
	},
	openFileId:function(id)
	{
		jsFW.displayBlock({opacity:'0.30',backgroundColor:'#C1D9F0'});
		this.ajaxLoaderShow();
		function nodeToJson(node){
			var o = {};
			var fields = node.childNodes;
			for(var j=0,jl=fields.length;j<jl;j++)
			{
				var field = fields[j];
				if(field.nodeType!=1) return field.nodeValue;
				var n = field.getAttribute('name')
				o[n] = nodeToJson(field);
			}
			return o;
		}
		var _this = this;
		try{
			jsFW.HttpRequest.submit({
				url:'getarticle.php',
				type:'xml',
				param:{
					id:id
				},
				onload:function(xml,success)
				{
					if(success)
					{
						var obj = nodeToJson(xml.documentElement);
						if('object' == typeof obj.article)
						{
							_this.setText(obj.article.text)
							_this.currentFileName = obj.article.name;
						}
						jsFW.displayUnblock();
					}
					else
					{
						jsFW.displayUnblock();
						alert('Что то пошло не так :(');
					}
					_this.ajaxLoaderHide();
				}
			});
		}catch(e){
			this.ajaxLoaderHide();
			jsFW.displayUnblock();
		}
		return false;
	},
	setText:function(text)
	{
		this.textarea.value = text;
	},
	getText:function(text)
	{
		return this.textarea.value;
	},
	openListTopic:function(data)
	{
		var _this=this;
		var selectFile = false;
		var win = new Win({tmpl:'listArticle'});
		win.show(data);
		win.addEvent('onclickok',function(){
			if(selectFile.id)
			{
				_this.openFileId(selectFile.id);
				return true;
			}
			alert('Не выбрана статья');
			return false;
		});
		function eSelectFile()
		{
			if(selectFile) $.removeClass(selectFile,'selected');
			selectFile = this;
			$.addClass(selectFile,'selected');
		}
		$.getTag('li','file',win.content,function(el){
			el.onclick = eSelectFile;
		});
	},
	saveFile:function()
	{
		if(this.currentFileName)
		{
			this.saveAsFileName(this.currentFileName);
		}
		else
			this.saveAsFile();
		return false;
	},
	saveAsFile:function()
	{
		this.loadListFile(function(data)
		{
			var _this=this;
			var selectFile = false;
			var win = new Win({tmpl:'saveAs'});
			win.show(data);
			win.addEvent('onclickok',function(){
				var name = win.form['nameFile'].value;
				if(name.length>=3 && name.length <= 100)
				{
					if(data.articles.find('name',name))
					{
						confirm("Статья с таким именем уже существует.<br/>Перезаписать?",function(){
							_this.saveAsFileName(name);
							win.hide();
						});
						return false;
					}
					else _this.saveAsFileName(name);
					return true;
				}
				else
				{
					alert('Имя не менее 3 и не более 100 символов.');
					false;
				}
			});
			function eSelectFile()
			{
				if(selectFile) $.removeClass(selectFile,'selected');
				selectFile = this;
				$.addClass(selectFile,'selected');
				win.form['nameFile'].value = data.articles.find('id',selectFile.id).name;
			}
			$.getTag('li','file',win.content,function(el){
				el.onclick = eSelectFile;
			});
		});
		return false;
	},
	saveAsFileName:function(name)
	{
		this.currentFileName = name;
		this.ajaxLoaderShow();
		var _this = this;
		try{
			jsFW.HttpRequest.submit({
				url:'savearticle.php',
				type:'json',
				param:{
					name:name,
					text:this.getText()
				},
				onload:function(data,success)
				{
					if(success && data)
					{
						if('Object' == typeof data && data.error)
						{
							alert(data.error.text);
						}
						else
						{
							message("Статья сохранена");
						}
					}
					else
					{
						alert('Что то пошло не так :(');
					}
					_this.ajaxLoaderHide();
				}
			});
		}catch(e){
			this.ajaxLoaderHide();
		}
	},
	insertTag:function(tag,cursor,params)
	{
		var open = tag.open;
		var close = tag.close;
		if(params){
			for(var i=0,l=params.length;i<l;i++)
			{
				open = open.replace('{'+i+'}',params[i]);
				close = close.replace('{'+i+'}',params[i]);
			}
		}
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		this.textarea.focus();
		var cursor = cursor || this.getCaretPos();
		var text = this.textarea.value;
		var prevText = text.substr(0,cursor.start);
		var selectText = text.substr(cursor.start,cursor.length);
		var nextText = text.substr(cursor.start+cursor.length);
		var sp = '\n' + (" ".repeat(this.firsLineCountSpace(prevText)));
		var tagOpen = open.replace(/\{tab\}/gm,Editor.tab).replace(/\n/gm,sp);
		var tagClose = close.replace(/\{tab\}/gm,Editor.tab).replace(/\n/gm,sp);
		prevText += tagOpen;
		nextText = tagClose + nextText;
		this.textarea.value = prevText + selectText + nextText;
		this.setCaretPos(prevText.length,selectText.length);
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
	},
	insertText:function(text,selected)
	{
		if (!jsfw.browser.mozilla && !jsFW.browser.safari) // opera && ie
		{
			var s = document.selection;
			var sel=s.createRange();
			if(sel.parentElement()==this.textarea)
			{
				sel.text = text;
			}
		}
		else
		{
			var oldtext = this.textarea.value;
			var start = this.textarea.selectionStart;
			var end = this.textarea.selectionEnd;
			var newText = oldtext.substr(0,start)+text+oldtext.substr(end);
			this.textarea.value = newText;
			this.textarea.selectionStart=start+text.length;
			this.textarea.selectionEnd=end+text.length;
		}
	},
	getCaretPos:function()
	{
		var pos = 0,len=0;
		if('setSelectionRange' in this.textarea)
		{
			pos = this.textarea.selectionStart;
			len = this.textarea.selectionEnd-this.textarea.selectionStart;
		}
		else
		{
			var sel = document.selection.createRange();
			var clone = sel.duplicate();
			len = sel.text.length;
			sel.collapse(true);
			clone.moveToElementText(this.textarea);
			clone.setEndPoint('EndToEnd', sel);
			pos = clone.text.length;
		}
		return {start:pos,length:len};
	},
	setCaretPos:function(start,len,ta)
	{
		ta = ta || this.textarea;
		ta.focus();
		if('setSelectionRange' in ta)
		{
			ta.selectionStart = start;
			ta.selectionEnd = start+len;
		}
		else
		{
			var sel = document.selection.createRange();
			if(sel.parentElement()==ta)
			{
				sel.moveToElementText(ta);
				sel.collapse(true);
				sel.moveEnd("character",start+len);
				sel.moveStart("character",start);
				sel.select();
			}
		}
	},
	insertTagParam:function(tag)
	{
		var p = this.getCaretPos();
		this.winParam = new Win({tmpl:'editTag'});
		this.winParam.p =p;
		this.winParam.show(tag);
		var first = this.winParam.form.elements[0];
		if(first)
		{
			$.setCursor(first,0,first.value.length);
			first.focus();
		}
		var _this = this;
		this.winParam.addEvent('onclickok',function(){
			var open = tag.open;
			var close = tag.close;
			var params = [];
			for(var i=0,l=this.form.elements.length;i<l;i++)
			{
				params.push(this.form.elements[i].value);
			}
			try{
				_this.insertTag({open:open,close:close},p,params);
			}catch(er){}
			return true;
		}); 
	},
	closeWinParam:function(){
		if(this.winParam)
		{
			var p = this.winParam.p;
			if(p) this.setCaretPos(p.start,p.length);
			//this.winParam.hide();
			this.winParam = null;
		}
	},
	execute:function(cmd,b)
	{
		var ret = false;
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		var tag = Editor.insert_tag[cmd];
		if(tag)
		{
			if(tag.params)
			{
				this.insertTagParam(tag);
			}
			else
			{
				this.insertTag(tag);
			}
		}
		else if(cmd in this)
		{
			ret = this[cmd](b);
		}
		else if(cmd.indexOf('text-code-')>=0)
		{
			var lang = cmd.replace('text-code-','');
			var tag = Editor.insert_tag['text-code-color'];
			this.insertTag({open:(tag.open.replace('{class}',lang)),close:tag.close,params:tag.params});
		}
		if(this.isAutoPreview) this.previewShow();
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return ret;
	},
	insertTab:function(e)
	{
		var cursor = this.getCaretPos();
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		if(cursor.length==0)
		{
			this.insertText(Editor.tab);
		}
		else
		{
			var text = this.textarea.value;
			var first = text.lastIndexOf('\n',cursor.start)+1;
			var end = text.indexOf('\n',cursor.start+cursor.length);
			var prevText = text.substr(0,first);
			if(end<0)
			{
				var selectLine = text.substr(first);
				var nextText = '';
			}else
			{
				var selectLine = text.substring(first,end);
				var nextText = text.substr(end);
			}
			var t = selectLine.split('\n');
			selectLine = t.map('c->"'+Editor.tab+'"+c').join('\n');
			this.textarea.value = prevText + selectLine + nextText;
			var start = cursor.start+Editor.tab.length;
			
			var end = cursor.length+Editor.tab.length*(t.length-1);
			if(jsfw.browser.msie)
			{
				start--;
				end--;
			}
			this.setCaretPos(start,end)
		}
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return false;
	},
	deleteTab:function()
	{
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		var cursor = this.getCaretPos();
		var text = this.textarea.value;
		var start = cursor.start,len = Editor.tab.length;
		if(cursor.length==0)
		{
			if(cursor.length==0)
			{
				start-=len;
			}
			var txt = text.substr(start,len);
			if(txt == Editor.tab)
			{
				this.textarea.value = text.substr(0,start)+text.substr(start+len);
			}
			else
			{
				var i = txt.lastIndexOf('\n')+1;
				if(i>0)
				{
					start+= (i);
				}
			}
			this.setCaretPos(start,0);
		}
		else
		{
			var first = text.lastIndexOf('\n',cursor.start)+1;
			var end = text.indexOf('\n',cursor.start+cursor.length);
			var prevText = text.substr(0,first);
			if(end<0)
			{
				var selectLine = text.substr(first);
				var nextText = '';
			}else
			{
				var selectLine = text.substring(first,end);
				var nextText = text.substr(end);
			}
			var t = selectLine.split('\n');
			selectLine = t.map(function(c){
				var txt = c.substr(0,len);
				if(txt == Editor.tab)
				{
					return c.substr(len);
				}
				return c;
			}).join('\n');
			text = prevText + selectLine + nextText;
			if(text != this.textarea.value)
			{
				this.textarea.value = text;
				var start = cursor.start-Editor.tab.length;
				var end = cursor.length-Editor.tab.length*(t.length-1);
				if(jsfw.browser.msie)
				{
					start--;
					end--;
				}
				this.setCaretPos(start,end)
			}
		}
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return false;
	},
	firsLineCountSpace:function(text)
	{
		var i = text.lastIndexOf('\n')+1;
		var line = text.substr(i);
		var i=0;
		for(var l=line.length;i<l;i++)
		{
			if(line.charAt(i)!=' ') break;
		}
		return i;
	},
	enterTab:function()
	{
		var cursor = this.getCaretPos();
		var prevText = this.textarea.value.substr(0,cursor.start);
		var nextText = this.textarea.value.substr(cursor.start+cursor.length);
		var i = this.firsLineCountSpace(prevText);
		if(i>0)
		{
			var text = prevText + '\n'+(" ".repeat(i)) + nextText;
			i += cursor.start+1;
			if(jsfw.browser.opera || jsfw.browser.safari) i++;
			if(jsfw.browser.msie) i -= (prevText.split('\n').length-1);
			this.textarea.value = text;
			this.setCaretPos(i,0);
			return false;
		}
		return true;
	},
	eKeyUpBody:function(s,e)
	{
		if(e.keyCode == $e.keys.esc)this.closeWinParam();
	},
	eKeyUp:function()
	{
		if(this.isAutoPreview) this.previewShow();
	},
	getHotkey:function(e)
	{
		var hotkey = Editor.hotkey;
		if(e.ctrlKey)
		{
			hotkey = hotkey.ctrl;
			if(e.altKey)
			{
				hotkey = hotkey.alt;
			}else if(e.shiftKey)
			{
				hotkey = hotkey.shift;
			}
		}
		else if(e.altKey)
		{
			hotkey = hotkey.alt;
			if(e.shiftKey)
			{
				hotkey = hotkey.shift;
			}
		}else if(e.shiftKey)
		{
			hotkey = hotkey.shift;
		}
		return hotkey;
	},
	eKeyDown:function(s,e)
	{
		var code = $e.keysId[e.keyCode] || String.fromCharCode(e.keyCode);
		var hotkey = this.getHotkey(e);
		hotkey = hotkey[code];
		if(hotkey)
		{
			if('cmd' in hotkey && hotkey.cmd in this) 
			{
				if(!this[hotkey.cmd].apply(this,(hotkey.params||[]).concat(e))){
					if(jsfw.browser.msie) e.keyCode = 0;
					e.cancelBubble = true;
					e.returnValue = false;
					if(e.preventDefault) e.preventDefault();
					if(e.stopPropagation) e.stopPropagation();
					return false;
				}
			}
		}
	},
	lineUp:function(){
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		
		var text = this.textarea.value;
		var cursor = this.getCaretPos();
		var start = text.lastIndexOf('\n',cursor.start-1);
		if(start>=0)
		{
			var subText,prevText='',nextText='';
			var end = text.indexOf('\n',cursor.start);
			prevText = text.substr(0,start);
			if(end>=0) 
			{
				subText = text.substring(start,end);
				nextText = text.substr(end);
			}
			else 
			{
				subText = text.substr(start);
			}
			var i = prevText.lastIndexOf('\n',prevText.length-1);
			if(i>0)
			{
				this.textarea.value = prevText.substr(0,i)+subText+prevText.substr(i)+nextText;
				this.setCaretPos(i+1,0);
			}
			else
			{
				this.textarea.value = subText.substr(1)+'\n' + prevText +nextText;
				this.setCaretPos(0,0);
			}
		}
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return false;
	},
	lineDown:function(){
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		
		var text = this.getText();
		var cursor = this.getCaretPos();
		var end = text.indexOf('\n',cursor.start);
		if(end>=0)
		{
			var subText,prevText='',nextText='';
			var start = text.lastIndexOf('\n',cursor.start-1);
			nextText = text.substr(end);
			if(start>=0) 
			{
				prevText = text.substr(0,start);
				subText = text.substring(start,end);
			}
			else 
			{
				subText = text.substr(0,end);
			}
			var i = nextText.indexOf('\n',1);
			if(i>0)
			{
				this.textarea.value = prevText + nextText.substr(0,i) + subText + nextText.substr(i);
				this.setCaretPos(prevText.length+i+1,0);
			}
			else
			{
				this.textarea.value = prevText + nextText + subText;
				this.setCaretPos(prevText.length+nextText.length+1,0);
			}
		}
				this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return false;
	},
	clearLine:function()
	{
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		
		var text = this.getText();
		var cursor = this.getCaretPos();
		var srt = text.lastIndexOf('\n',cursor.start-1);
		var end = text.indexOf('\n',cursor.start);
		var prevText = text.substr(0,srt);
		if(end==-1)
		{
			var subText = text.substr(srt);
			var nextText = "";
		}
		else
		{
			var subText = text.substring(srt,end);
			var nextText = text.substr(end);
		}
		this.setText(prevText+nextText);
		this.setCaretPos(srt+1,0);
		
		this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return false;
	},
	copyLine:function()
	{
		var scrollTop = this.textarea.scrollTop;
		var scrollLeft = this.textarea.scrollLeft;
		
		var text = this.getText();
		var cursor = this.getCaretPos();
		if(cursor.length>0)
		{
			var prevText = text.substr(0,cursor.start);
			var subText = text.substr(cursor.start,cursor.length);
			var nextText = text.substr(cursor.start+cursor.length);
			this.setText(prevText+subText+subText+nextText);
			this.setCaretPos(cursor.start+cursor.length,cursor.length);
		}
		else
		{
			var srt = text.lastIndexOf('\n',cursor.start-1)+1;
			var end = text.indexOf('\n',cursor.start);
			var prevText = text.substr(0,srt);
			if(end==-1)
			{
				var subText = text.substr(srt);
				var nextText = "";
			}
			else
			{
				var subText = text.substring(srt,end);
				var nextText = text.substr(end);
			}
			this.setText(prevText+subText+"\n"+subText+nextText);
			var i = cursor.start+subText.length+1;
			if(jsfw.browser.opera || jsfw.browser.safari) i++;
			//alert(srt+" "+cursor.start)
			if(jsfw.browser.msie) i -= (prevText.split('\n').length);
			this.setCaretPos(i,0);
		}
				this.textarea.scrollTop = scrollTop;
		this.textarea.scrollLeft = scrollLeft;
		return false;
	},
	previewShow:function(text,f)
	{
		if(!this.previewContent) this.previewContent = this.preview.contentWindow.document.getElementById('content');
		if(this.previewContent)
		{
			var text = text || this.textarea.value;
			text = text.replace(/<p[^>]*>((\s|\S)*?)<\/p>/g,"$1<br/>").replace(/>(\s)+</gm,'/><').replace(/<code[^>]*>([\s\S]*?)<\/code>/igm,function(a,code)
			{
				return "<code>"+code.replace(/\t/gm,'    ').replace(/ /gm,'&nbsp;').replace(/</gm,'&lt;').replace(/>/gm,'&gt;')+"</code>";
			}).replace(/\n/gm,'<br/>');
			this.previewContent.innerHTML = text;
		}
	},
	getResulText:function()
	{
		var text = this.getText();
		this.taInsertText.value = "";
		this.ajaxLoaderShow();
		var _this = this;
		try{
			jsFW.HttpRequest.submit({
				url:'getResultText.php',
				param:{
					isTipograf:this.isTipograf,
					isColer:this.isColer,
					isNumberCode:this.isNumberCode,
					isHyphenWords:this.isHyphenWords,
					text:text
				},
				onload:function(text,success)
				{
					if(success)
					{
						_this.showResultText(text);
					}
					else
					{
						alert('Что то пошло не так :(');
					}
					_this.ajaxLoaderHide();
				}
			});
		}catch(e){
			this.ajaxLoaderHide();
		}
	},
	showResultText:function(text){
		text = text.replace(/<!--.*\n.*$/g,'');
		this.taInsertText.value = text;
	},
	previewShowResult:function()
	{
		var _this=this;
		this.ajaxLoaderShow();
		_this.preview.contentWindow.document.getElementById('content').innerHTML = '';
		try
		{
		jsFW.HttpRequest.submit({
				url:'getResultText.php',
				param:{
					isTipograf:this.isTipograf,
					isColer:this.isColer,
					isHyphenWords:this.isHyphenWords,
					isNumberCode:this.isNumberCode,
					text:this.textarea.value
				},
				onload:function(text,success)
				{
					if(success)
					{
						_this.preview.contentWindow.document.getElementById('content').innerHTML = text.replace(/>(\s)+</gm,'/> <').replace(/\n/gm,'<br/>').replace(/<hh user="([^'^"]*)"\/>/gm,'<a class="user_link" href="http://$1.habrahabr.ru/">$1</a>');
					}
					else
					{
						alert("что то пошло не так :(");
					}
					_this.ajaxLoaderHide();
				}
			});
		} catch(er){
			this.ajaxLoaderHide();
		}
	},
	setColorFont:function(colorPicker)
	{
		var tag = Editor.insert_tag['format-text-color'];
		if(tag)
		{
			this.insertTag(tag,null,['#'+colorPicker.getHexColor()]);
		}
	}
}},jsfw.Object);
Editor.tab = '    ';
Editor.insert_tag = {
	'format-text-bold':{open:'<strong>',close:'</strong>'},
	'format-text-italic':{open:'<em>',close:'</em>'},
	'format-text-underline':{open:'<u>',close:'</u>'},
	'format-text-strikethrough':{open:'<strike>',close:'</strike>'},
	'format-text-sup':{open:'<sup>',close:'</sup>'},
	'format-text-sub':{open:'<sub>',close:'</sub>'},
	'format-text-color':{open:'<font color="{0}">',close:'</font>'},
	'header-h1':{open:'<h1>',close:'</h1>'},
	'header-h2':{open:'<h2>',close:'</h2>'},
	'header-h3':{open:'<h3>',close:'</h3>'},
	'header-h4':{open:'<h4>',close:'</h4>'},
	'header-h5':{open:'<h5>',close:'</h5>'},
	'header-h6':{open:'<h6>',close:'</h6>'},
	'text-paragraf':{open:'<p>\n{tab}',close:'\n</p>'},
	'text-ul':{open:'<ul>\n{tab}<li>',close:'</li>\n</ul>'},
	'text-ol':{open:'<ol>\n{tab}<li>',close:'</li>\n</ol>'},
	'text-li':{open:'<li>',close:'</li>'},
	'text-blockquote':{open:'<blockquote>\n',close:'\n</blockquote>'},
	'text-br':{open:'<br/>\n',close:''},
	'text-pre':{open:'<pre>\n{tab}',close:'\n</pre>'},
	'text-code':{open:'<blockquote>\n<code>\n',close:'\n</code>\n</blockquote>'},
	'text-code-color':{open:'<blockquote>\n<code class="{class}">\n',close:'\n</code>\n</blockquote>'},
	'text-link':{open:'<a href="{0}" title="{1}">',close:'</a>',params:[{title:'Сылка',def:'http://'},{title:'Описание'}]},
	'text-image':{open:'<img src="{0}" alt="{1}"/>',close:'',params:[{title:'Источник',def:'http://'},{title:'Альтернативный текст'}]},
	'text-table':{open:'<table>\n{tab}<tr>\n{tab}{tab}<td>',close:'</td>\n{tab}</tr>\n</table>\n'},
	'text-table-tr':{open:'<tr>\n{tab}<td>',close:'</td>\n</tr>'},
	'text-table-td':{open:'<td>',close:'</td>'},
	'text-table-th':{open:'<th>',close:'</th>'},
	/* спец символы */ 
	'spec-sym-euro':{open:'&#8364;',close:''},
	'spec-sym-cent':{open:'&#162;',close:''},
	'spec-sym-copy':{open:'©',close:''},
	'spec-sym-r':{open:'®',close:''},
	'spec-sym-tm':{open:'™',close:''},
	'spec-sym-amp':{open:'&amp;',close:''},
	'spec-sym-p':{open:'¶',close:''},
	'spec-sym-short-tere':{open:'–',close:''},
	'spec-sym-tere':{open:'—',close:''},
	'spec-sym-quotes':{open:'«',close:'»'},
	'text-table-lt':{open:'&lt;',close:''},
	'text-table-gt':{open:'&gt;',close:''},
	
	'spec-sym-cent':{open:'&#162;',close:''},
	'spec-sym-pound':{open:'&#163;',close:''},
	'spec-sym-curren':{open:'&#164;',close:''},
					
	'spec-sym-sect':{open:'&#167;',close:''},
	'spec-sym-quote':{open:'&#34;',close:''},
	'spec-sym-lsquo':{open:'&#8216;',close:''},
	'spec-sym-rsquo':{open:'&#8217;',close:''},
	'spec-sym-ldquo':{open:'&#8220;',close:''},
	'spec-sym-rdquo':{open:'&#8221;',close:''},
	'spec-sym-lsaquo':{open:'&#8249;',close:''},
	'spec-sym-rsaquo':{open:'&#8250;',close:''},

	/*Хабра*/
	'habra-habracut':{open:'<habracut text="{0}"/>\n',close:'',params:[{title:'Читать дальше -> ',def:'Читать дальше'}]},
	'habra-user':{open:'<hh user="{0}"/>\n',close:'',params:[{title:'Имя хабраюзера'}]},
	'habra-video':{open:'<video>',close:'</video>'}
};
Editor.block_tag = ['div','ul','ol','p','h1','h2','h3','h4','h5','h6'];

Editor.hotkey = {
	'tab':{
		cmd:'insertTab'
	},
	enter:{
		cmd:'enterTab'
	},
	ctrl:{
		B:{
			cmd:'execute',
			params:['format-text-bold']
		},
		I:{
			cmd:'execute',
			params:['format-text-italic']
		},
		U:{
			cmd:'execute',
			params:['format-text-underline']
		},
		S:{
			cmd:'saveFile'
		},
		O:{
			cmd:'openFile'
		},
		D:{
			cmd:'copyLine'
		},
		L:{
			cmd:'clearLine'
		},
		alt:{
			
		},
		shift:{
			up:{
				cmd:'lineUp'
			},
			down:{
				cmd:'lineDown'
			}
		}
	},
	alt: {
		'1':{
			cmd:'execute',
			params:['header-h1']
		},
		'2':{
			cmd:'execute',
			params:['header-h2']
		},
		'3':{
			cmd:'execute',
			params:['header-h3']
		},
		'4':{
			cmd:'execute',
			params:['header-h4']
		},
		'5':{
			cmd:'execute',
			params:['header-h5']
		},
		'6':{
			cmd:'execute',
			params:['header-h6']
		},
		P:{
			cmd:'execute',
			params:['text-paragraf']
		},
		U:{
			cmd:'execute',
			params:['text-ul']
		},
		O:{
			cmd:'execute',
			params:['text-ol']
		},
		L:{
			cmd:'execute',
			params:['text-li']
		},
		N:{
			cmd:'execute',
			params:['text-pre']
		},
		A:{
			cmd:'execute',
			params:['text-link']
		},
		T:{
			cmd:'execute',
			params:['text-table']
		},
		R:{
			cmd:'execute',
			params:['text-table-tr']
		},
		D:{
			cmd:'execute',
			params:['text-table-td']
		},
		H:{
			cmd:'execute',
			params:['text-table-th']
		},
		Q:{
			cmd:'execute',
			params:['text-blockquote']
		},
		C:{
			cmd:'execute',
			params:['text-code']
		},
		shift: {
			H:{
				cmd:'execute',
				params:['habra-habracut']
			},
			U:{
				cmd:'execute',
				params:['habra-user']
			},
			V:{
				cmd:'execute',
				params:['habra-video']
			}
		}
	},
	shift: {
		tab:{
			cmd:'deleteTab'
		},
		enter:{
			cmd:'execute',
			params:['text-br']
		}
	}
};

var ControlBar = jsfw.Class({
	__constructor:function(o)
	{
		this.dom = $(o.el);
		if(!this.dom) this.dom = $.create('div',{className:'controlBar'});
		
		this.domButtons = $.getTag('ul','Buttons',this.dom)[0];

		var _this = this;
		if(this.domButtons){
			this.tabs = $.getTag('a','Button',this.domButtons,function(e)
			{
				var id = e.hash.substr(1);
				var b = new Button({el:e,id:id});
				var panel = $(id);
				if(!panel) panel = $.create('div',{className:'Panel'},_this.dom);
				else
				{
					var buttons = $.getTag('div','GroupButton-right',panel,function(buttons){
						var bs = [];
						var e;
						for(var i=buttons.childNodes.length;i--;)
						{
							e = buttons.childNodes[i];
							if($.ifClass(e,'Button'))
							{
								var b = new Button({el:e,id:e.id});
							}
							else if($.ifClass(e,'ButtonList'))
							{
								var b = new ButtonList({el:e,id:e.id});
							}else if($.ifClass(e,'ColorPicker'))
							{
								var b = new ColorPicker({el:e,id:e.id});
							}
							
							if(b)
							{
								b.addEvent('onclick',$d(_this,_this.clickButton));
								bs.push(b);
								b=null;
							}
						}
						if(bs.length>0) $.addClass(bs[0].getDom(),'last');
						return bs;
					});
				}
				panel.style.display = 'none';
				var tab = {
					button:b,
					panel:panel
				};
				b.addEvent('onclick',$d(_this,_this.selectTab,[tab]));
				return tab;
			});
		}
		else{
			this.domButtons = $.create('ul',{className:'Buttons'});
		}
		if(this.tabs && this.tabs.length>1) this.selectTab(null,null,this.tabs[1]);
	},
	clickButton:function(button,e)
	{
		return this.callEvent('onclick',[button.id,button]);
	},
	selectTab:function(button,e,tab)
	{
		if(this.sTab!=tab)
		{
			if(this.sTab)
			{
				this.sTab.button.unselect();
				this.sTab.panel.style.display = 'none';
			}
			tab.button.select();
			tab.panel.style.display = '';
			this.sTab=tab;
			this.callEvent('onselecttab',[tab]);
		}
		return false;
	}
},jsfw.Object);

var Button = jsfw.Class(function(){
	var count_id = 0;
return {
	__constructor:function(o)
	{
		this.dom = $(o.el);
		this.id = o.id || count_id++;
		if(!this.dom) this.dom = $.create('a',{className:'controlBar'});
		this.dom.onclick = $d(this,function(s,e){return this.callEvent('onclick',[e]);});
	},
	select:function()
	{
		$.addClass(this.dom.parentNode.parentNode,'selected');
	},
	unselect:function()
	{
		$.removeClass(this.dom.parentNode.parentNode,'selected');
	},
	getDom:function()
	{
		return this.dom;
	}
}},jsfw.Object);

var ButtonList = jsfw.Class(function(){
	var count_id = 0;
return {
	__constructor:function(o)
	{
		this.dom = $(o.el);
		if(!this.dom) this.dom = $.create('a',{className:'controlBar'});
		this.list = this.dom.getElementsByTagName('ul')[0];
		//this.list.style.display = 'none';
		this.toggleList = $.getTag('a','toggleList',this.dom)[0];
		this.toggleList.onclick = $d(this,this.eToogleList);
		var dSelectItem = $d(this,this.eSelectItem);
		$.getTag('a','Button',this.dom)[0].onclick = $d(this,function(){this.hideList();this.callEvent('onclick');return false;});
		this.id = $.getTag('a','Button',this.list,function(e){
			e.onclick = dSelectItem;
			return e;
		})[0].id;
	},
	eSelectItem:function(a)
	{
		this.id = a.id;
		this.hideList();
		this.callEvent('onclick');
		return false;
	},
	eToogleList:function()
	{
		if(this.isShow) this.hideList(); else this.showList();
		return false;
	},
	showList:function()
	{
		if(!this.isShow)
		{
			$.addClass(this.dom,'show');
			jsFW.displayBlock({opacity:'0.1',onclick:$d(this,this.hideList)});
			jsfw.fx.opacity(this.list,0,1,{time:200});
		}
		this.isShow = true;
	},
	hideList:function()
	{
		if(this.isShow)
		{
			$.opacity(this.list,0);
			var _this = this;
			jsfw.fx.opacity(this.list,1,0,{
				time:100,
				oncalbackEnd:function()
				{
					$.removeClass(_this.dom,'show');
					jsfw.displayUnblock();
				}
			});
		}
		this.isShow = false;
	},
	select:function()
	{
		$.addClass(this.dom,'selected');
	},
	unselect:function()
	{
		$.removeClass(this.dom,'selected');
	},
	getDom:function()
	{
		return this.dom;
	}
}},jsfw.Object);

var ColorPicker = jsfw.Class(function(){
	var count_id = 0;
	var hexbase=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
	var jl=[],i=0;
	for(var x=0;x<16;x++) for(var y=0;y<16;y++) jl[i++]=hexbase[x]+hexbase[y];
	var H=63,W=63,h=2,w=3;
	var total=1657;
	var aR=new Array(total);
	var aG=new Array(total);
	var aB=new Array(total);
	for (var i=0;i<256;i++){
		aR[i+510]=aR[i+765]=aG[i+1020]=aG[i+5*255]=aB[i]=aB[i+255]=0;
		aR[510-i]=aR[i+1020]=aG[i]=aG[1020-i]=aB[i+510]=aB[1530-i]=i;
		aR[i]=aR[1530-i]=aG[i+255]=aG[i+510]=aB[i+765]=aB[i+1020]=255;
		if(i<255){
			aR[i/2+1530]=127;
			aG[i/2+1530]=127;
			aB[i/2+1530]=127;
		}
	}
	function getColor(i,j)
	{
		var R=aR[i]-j;
		if(R<0)R=0;
		if(R>255||isNaN(R))R=255;
		var G=aG[i]-j;
		if(G<0)G=0;
		if(G>255||isNaN(G))G=255;
		var B=aB[i]-j;
		if(B<0)B=0;
		if(B>255||isNaN(B))B=255;
		return [R,G,B];
	}
	function getColorPos(s,e)
	{
		var p = $.offset(s);
		var x = (e.clientX-p.left)*W/s.clientWidth;
		var y = (e.clientY-p.top)*H/s.clientHeight;
		var j=Math.round(y*(510/(H+1))-255);
		var i=Math.round(x*(total/W));
		return getColor(i,j);
	}
return {
	__constructor:function(o)
	{
		this.dom = $(o.el);
		this.id = this.dom.id;
		this.showPicker = $.getTag('a','showPicker',this.dom)[0];
		this.displayColor = $.getTag('i','*',this.dom)[0];
		this.setColor([0,0,0]);
		this.showPicker.onclick = $d(this,this.eShowPicker);
		this.domColorPicker = $.create('div',{className:'listColor'},this.dom);
		$.getTag('a','Button',this.dom)[0].onclick = $d(this,function(){this.callEvent('onclick',[this.color]);return false;});
		this.createThemColor();
		this.createUserColor();
		
		
		this.domPalette = $.create('div');
		this.palette = $.create('div',{className:'Palette',onmousemove:$d(this,this.eMouseMovePalette),onclick:$d(this,this.eSelectColorPalette)},this.domPalette);
		var d = $.create('div',{className:'contPeviewPalette'},this.domPalette);
		this.peviewColorPalette = $.create('div',{className:'peviewPalette'},d);
		this.selectColorPalette = $.create('div',{className:'selectPalette'},d);
		this.winPalett = new Win({
									modal:false,
									buttons:[
										{text:'Добавить',onclick:$d(this,this.eSelectPalett)},
										{text:'Закрыть',onclick:$d(this,this.eExitPalett)}
									]
								});
		this.winPalett.content.appendChild(this.domPalette);
	},
	createThemColor:function()
	{
		$.create('<h6>Цвета темы</h6>',this.domColorPicker);
		//var color = ['FFFFFF','000000','EEECE1','1F497D','4F81BD','C0504D','9BBB59','8064A2','4BACC6','F79646'];
		var color = [[255,255,255],[0,0,0],[238,236,225],[31,73,125],[79,129,189],[192,80,77],[155,187,89],[128,100,162],[75,172,198],[247,150,70]];
		var colorGr = [
			[[242,242,242],[216,216,216],[191,191,191],[165,165,165],[127,127,127]],
			[[127,127,127],[89,89,89],[63,63,63],[38,38,38],[12,12,12]],
			[[221,217,195],[196,189,151],[147,137,83],[73,68,41],[29,27,16]],
			[[198,217,240],[141,179,226],[84,141,212],[23,54,93],[15,36,62]],
			[[219,229,241],[184,204,228],[149,179,215],[54,96,146],[36,64,97]],
			[[242,220,219],[229,185,183],[217,150,148],[149,55,52],[99,36,35]],
			[[235,241,221],[215,227,188],[195,214,155],[118,146,60],[79,97,40]],
			[[229,224,236],[204,193,217],[178,162,199],[95,73,122],[63,49,81]],
			[[219,238,243],[183,221,232],[146,205,220],[49,133,155],[32,88,103]],
			[[253,234,218],[251,213,181],[250,192,143],[227,108,9],[151,72,6]]
		];
		for(var i=0,l=color.length;i<l;i++)
		{
			var c = color[i];
			$.create('a',{href:'#',className:'color',style:{backgroundColor:'rgb('+c.join(',')+')'},onclick:$d(this,this.eSelectColor,[c])},this.domColorPicker);
		}
		for(var i=0,l=color.length;i<l;i++)
		{
			var gr = $.create('span',{className:'groupColor'},this.domColorPicker);
			for(var j=0;j<5;j++)
			{
				var c = colorGr[i][j];
				$.create('a',{href:'#',className:'color',style:{backgroundColor:'rgb('+c.join(',')+')'},onclick:$d(this,this.eSelectColor,[c])},gr);
			}
		}
		$.create('<span style="display:block;clear:left"/>',this.domColorPicker);
	},
	createUserColor:function()
	{
		$.create('<h6>Другии цвета</h6>',this.domColorPicker);
		var d = $.create('<span style="display:block"/>',this.domColorPicker);
		this.userColor = [];
		for(var i=0,l=10;i<l;i++)
		{
			var c = jsfw.cookie.get('color'+i);
			var set = c?true:false;
			c = c?c.split(','):[255,255,255];
			this.userColor[i] = {
				index:i,
				set:set,
				obj:$.create('a',{href:'#',className:'color',style:{backgroundColor:'rgb('+c.join(',')+')'},onclick:$d(this,this.eSelectUserColor,[i])},d),
				color:c
			};
		}
		$.create('a',{className:'Button',href:'#',innerHTML:'Выбрать...',onclick:$d(this,function(){this.winPalett.show();return false;})},this.domColorPicker);
	},
	eSelectColor:function(s,e,c)
	{
		this.setColor(c);
		this.eHidePicker();
		this.callEvent('onclick',[c]);
		return false;
	},
	eSelectUserColor:function(s,e,i)
	{
		var uc = this.userColor[i];
		if(this.winPalett.isShow)
		{
			this.unSelectPalett();
			this.selectPalett(uc);
		}
		else
		{
			this.eSelectColor(s,e,uc.color);
		}
	},
	unSelectPalett:function()
	{
		if(this.selectedPalett) $.removeClass(this.selectedPalett.obj,'selected');
		this.selectedPalett = null;
	},
	selectPalett:function(p)
	{
		this.selectedPalett = p;
		$.addClass(this.selectedPalett.obj,'selected');
	},
	eSelectPalett:function()
	{
		var p = this.selectedPalett;
		if(!p)
		{
			for(var i=0,l=this.userColor.length;i<l;i++)
			{
				p = this.userColor[i];
				if(!p.set) break;
			}
		}
		p.color = this.selectPalettColor;
		var c = this.selectPalettColor.join(',');
		p.obj.style.backgroundColor = 'rgb('+c+')';
		p.set = true;
		jsfw.cookie.set('color'+p.index,c);
	},
	eExitPalett:function()
	{
		this.winPalett.hide();
		this.unSelectPalett();
	},
	setColor:function(c)
	{
		this.color = c;
		this.displayColor.style.backgroundColor = 'rgb('+c.join(',')+')';
	},
	eShowPicker:function()
	{
		if(!this.isShow)
		{
			$.addClass(this.dom,'show');
			jsFW.displayBlock({opacity:'0.1',onclick:$d(this,this.eHidePicker)});
			jsfw.fx.opacity(this.domColorPicker,0,1,{time:200});
		}
		this.isShow = true;
		return false;
	},
	eHidePicker:function()
	{
		$.removeClass(this.dom,'show');
		this.unSelectPalett();
		jsfw.displayUnblock();
		this.isShow = false;
		this.winPalett.hide()
	},
	getDom:function()
	{
		return this.dom;
	},
	getHexColor:function()
	{
		return jl[this.color[0]]+jl[this.color[1]]+jl[this.color[2]];
	},
	getColor:function()
	{
		return this.color;
	},
	eMouseMovePalette:function(s,e)
	{
		var color = getColorPos(s,e);
		this.peviewColorPalette.style.backgroundColor = 'rgb('+color.join(',')+')';
	},
	eSelectColorPalette:function(s,e){
		var color = getColorPos(s,e);
		this.selectPalettColor = color;
		this.selectColorPalette.style.backgroundColor = 'rgb('+color.join(',')+')';
	}
}},jsfw.Object)



jsfw.ready(function(){
	if(jsfw.browser.msie && jsfw.browser.version<7 
		|| jsfw.browser.mozilla && jsfw.browser.version<3 
		|| jsfw.browser.opera && jsfw.browser.version<9)
	{
		$.remove($('editor'));
		$('browserError').style.display = '';
	}
	else
	{
		$.remove($('browserError'));
		window.habraeditor = new Editor({
			el:'editor'
		});
		$('editor').style.display = '';
	}
});
