var permit_tag=['span','p','b','div','a','font','img','br'];
var not_empty_attr=['style','class','aling'];
var remove_css=['text-indent','font-family','font-size','display','letter-spacing','line-height','margin','overflow','float'];

function jse_clear_html(obj){
	if(obj.find('meta').get(0)==null) return;
	obj.find("meta, link, style").remove();
	jse_clear_html_rek(obj);
}
var num_rek=0;
function jse_clear_html_rek(obj){
	if(obj.get(0)==null){
		return false;
	} 
	num_rek++;
	obj.children().each(function(){
		if(jse_find_in_array(this.tagName.toLowerCase(),permit_tag)==null)
			$(this).remove();
		var tclass=new String($(this).attr("class"));
		if(tclass.search(/^Mso/)!=-1)$(this).attr("class",'');
		$(this).removeAttr('lang');  
		
		var len=remove_css.length;
		for(i=0;i<len;i++)
			$(this).css(remove_css[i],'');
		
		var len=not_empty_attr.length;
		for(i=0;i<len;i++)
			if($(this).attr(not_empty_attr[i])==null)$(this).removeAttr(not_empty_attr[i]);
		
		jse_clear_html_rek($(this));
	}); 
	num_rek--;
	if(num_rek==0){
		
		var html=obj.html();
		html=html.replace(/\n/g,' ');
		html=html.replace(/<style>.*<\/style>/g,'');
		html=html.replace(/<!--\[i[a-z0-9\s]*\]>.*<!\[endif\]-->/g,'');
/*		while(html.search(/<!--\[i[a-z0-9\s]*\]>[.\s]*<!\[endif\]-->/i)!=-1)
			html=html.replace(/<!--\[i[a-z0-9\s]*\]>[.\s]*<!\[endif\]-->/i,''); */
//		alert(html);
		obj.html(html);
	} 
}
function jse_find_in_array(wsearch,warray){
	var len=warray.length;
	for(i=0;i<len;i++)
		if(warray[i]==wsearch) return i;
	return null;
} 


//
var jse_now_load_where=null;
var jse_loadimg_folder=null;
var jse_pathtoloadimg='';

var jse_array_dom=new Array();
var jse_array_index=-1;

//
jQuery.fn.jseditor=function(options){

	// настройки по умолчанию
	var options = jQuery.extend({
		sbold:true,
		sitalic:true,
		sunderline:true,
		slineThrough:true,
		ssup:true,
		ssub:true,
		sfontSize:true,
		slink:true,
		simg:false,
		saudio:false,
		svideo:false,
		sleft:true,
		scenter:true,
		sright:true,
		sjustify:true,
		sclear:true,
		scut:false,
		simg:false,
		svideo:false,
		scolor:true,
		sbgcolor:true,
		scopyto:false,
		scopytoid:false, // номер редактора куда скопировать текст
		sbquote:false,
		sbquote_func:'',  // функция в которую передать цитату
		
		pathtoimg:'', 
		pathtoloadimg:'',
		jseheight:300, // высота редактора
		init_text:'',

		loadimg_folder:'user_foto_tmp/',
		loadimg_x:150,
		loadimg_y:50,
		loadimg_width:500,
		loadimg_height:333,
		loadimg_hide:'',
		loadimg_imgsize:5242880,
		loadimg_imgsize_text:'5 Мб',
		loadimg_default_proportion:false,
		loadimg_change_frame_size:true,
		loadimg_add_patch:''
	},options);
	jse_pathtoloadimg=options.pathtoloadimg;
	
var jse_select_color_html_bg='<br><div class="jse_select_color"><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="000000" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="993300" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="333300" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="003300" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="003366" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="000080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="333399" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="333333" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="800000" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff6600" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="808000" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="808080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="008080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="0000ff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="666699" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="808080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff0000" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff9900" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="99cc00" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="339966" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="33cccc" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="3366ff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="800080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="999999" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff00ff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ffcc00" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ffff00" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="00ff00" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="00ffff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="00ccff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="993366" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="c0c0c0" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff99cc" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ffcc99" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ffff99" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ccffcc" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ccffff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="99ccff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="cc99ff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ffffff" /></div><div class="jseclear"><span></span></div></div>';

var jse_select_color_html_color='<br><div class="jse_select_color"><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="000000" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="993300" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="333300" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="003300" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="003366" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="000080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="333399" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="333333" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="800000" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff6600" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="808000" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="808080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="008080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="0000ff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="666699" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="808080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff0000" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff9900" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="99cc00" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="339966" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="33cccc" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="3366ff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="800080" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="999999" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff00ff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ffcc00" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ffff00" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="00ff00" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="00ffff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="00ccff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="993366" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="c0c0c0" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ff99cc" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ffcc99" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ffff99" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ccffcc" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="ccffff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="99ccff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="cc99ff" /></div><div class="colors"><img src="'+options.pathtoimg+'jse_img/pixel.gif" title="eeeeee" /></div><div class="jseclear"><span></span></div></div>';

	function jse_HexToRGB(hex_clr){
		if(!/^\#?[\da-f]{6}$/i.test(hex_clr))
			return null;
	
		var color = (hex_clr.charAt(0)=="#") ? hex_clr.substring(1):hex_clr;
	
		return {
			"red" : parseInt(color.substring(0,2),16),
			"green" : parseInt(color.substring(2,4),16),
			"blue" : parseInt(color.substring(4,6),16)
		}
	}

	var jse_cut_color='#a60a93';
	var jse_temp_color='#5f6409';
	function jse_go_format(th,w){
		var jseframe_doc=th.find('iframe').get(0).contentWindow.document;
		switch(w){
			case 'bold':
				jseframe_doc.execCommand('bold',false,null);
				break;
			case 'italic':
				jseframe_doc.execCommand('italic',false,null);
				break;
			case 'underline':
				jseframe_doc.execCommand('underline',false,null);
				break;
			case 'lineThrough':
				jseframe_doc.execCommand('strikethrough',false,null);
				break;
			case 'sup':
				jseframe_doc.execCommand('superscript',false,null);
				break;
			case 'sub':
				jseframe_doc.execCommand('subscript',false,null);
				break;
			case 'left':
				jseframe_doc.execCommand('justifyleft',false,null);
				break;
			case 'center':
				jseframe_doc.execCommand('justifycenter',false,null);
				break;
			case 'right':
				jseframe_doc.execCommand('justifyright',false,null);
				break;
			case 'justify':
				jseframe_doc.execCommand('justifyfull',false,null);
				break;
			case 'link':
				var url=prompt('Введите адрес','http://');
				jseframe_doc.execCommand('CreateLink',false,url);
				break;
			case 'img':
				if(!visible_ruf()){
					show_ruf(
							options.loadimg_x,
							options.loadimg_y,
							options.loadimg_hide,
							options.loadimg_default_proportion,
							options.loadimg_width,
							options.loadimg_height,
							options.loadimg_change_frame_size,
							options.loadimg_imgsize,
							options.loadimg_imgsize_text,
							options.loadimg_add_patch,
							options.loadimg_folder,
							'jse_loadimg_done'
					);
					/*
					init_redaktor_upload_foto(
							options.loadimg_x,
							options.loadimg_y,
							options.loadimg_folder,
							'',
							'',
							'',
							'',
							'jse_loadimg_done',
							options.loadimg_width,
							options.loadimg_height,
							true,
							options.loadimg_hide,
							options.loadimg_imgsize,
							options.loadimg_imgsize_text
					);
					*/
					jse_loadimg_folder=options.loadimg_folder;
					jse_now_load_where=jseframe_doc;
					jseframe_doc.execCommand("InsertImage",false,"touploadimg");
					
					var temps=jQuery(jseframe_doc).find("img[src=touploadimg]");
					temps.after("<span class='touploadimg'></span>");
					temps.remove();
				}
				break;
			case 'video':
				var link_to_video=window.prompt("Мы принимаем ссылки на YouTube, RuTube, Google video, vimeo",'');
				if(link_to_video!=null && link_to_video!='' && link_to_video!=0){
					jseframe_doc.execCommand("InsertImage",false,"link_to_video");
					var temps=jQuery(jseframe_doc).find("img[src=link_to_video]");
					temps.attr('src',options.pathtoimg+'jse_img/your_video.gif').attr('title',link_to_video).attr('class','this_video');
				}
				break;
			case 'color':
				if(jQuery(th).find("#jse_to_sel_col_block").html()==''){
					jQuery(th).find("#jse_to_sel_col_block").html(jse_select_color_html_color);
					jQuery(th).find("#jse_to_sel_col_block div.jse_select_color div.colors").each(function(){
						var color=$(this).find("img").attr('title');
						$(this).find("img").css("background-color","#"+color);
					});
					jQuery(th).find("#jse_to_sel_col_block div.jse_select_color div.colors").hover(
						function(){
							jQuery(this).css("background-color","#c4cdd6").css("border-color","#330066")
						},
						function(){
							jQuery(this).css("background-color","white").css("border-color","white")
						}
					);
					jQuery(th).find("#jse_to_sel_col_block").hover(
						function(){;},
						function(){jQuery(this).find("div.jse_select_color").hide();}
					)
					jQuery(th).find("#jse_to_sel_col_block div.jse_select_color div.colors").click(function(){
						var tocolor=jQuery(this).find('img').attr("title");
						
						jseframe_doc.execCommand('ForeColor',false,"#"+tocolor);
						
						var rgb=jse_HexToRGB(tocolor);
						rgb="rgb("+rgb.red+", "+rgb.green+", "+rgb.blue+")";
						
						jQuery(jseframe_doc).find('span').each(function(){
							if(jQuery(this).css('color')==tocolor || jQuery(this).css('color')==rgb){
								jQuery(this).after("<span id='temps'></span>");
								var text=jQuery(this).html();
								var temps=jQuery(this).next();
								var bgc='';
								if(jQuery(this).css('background-color')!=''){
									bgc=jQuery(this).css('background-color');
									bgc='style="background-color:'+bgc+';"';
								}
								jQuery(this).remove();
								temps.before("<font color='"+tocolor+"' "+bgc+">"+text+"</font>");
								temps.remove();
							}
						});
						jQuery(th).find("#jse_to_sel_col_block div.jse_select_color").hide();
					});
				}else{
					jQuery(th).find("#jse_to_sel_col_block div.jse_select_color").show();
				}
				break;
			case 'bgcolor':
				if(jQuery(th).find("#jse_to_sel_bgcol_block").html()==''){
					jQuery(th).find("#jse_to_sel_bgcol_block").html(jse_select_color_html_bg);
					jQuery(th).find("#jse_to_sel_bgcol_block div.jse_select_color div.colors").each(function(){
						var color=$(this).find("img").attr('title');
						$(this).find("img").css("background-color","#"+color);
					});
					jQuery(th).find("#jse_to_sel_bgcol_block div.jse_select_color div.colors").hover(
						function(){
							jQuery(this).css("background-color","#c4cdd6").css("border-color","#330066")
						},
						function(){
							jQuery(this).css("background-color","white").css("border-color","white")
						}
					);
					jQuery(th).find("#jse_to_sel_bgcol_block").hover(
						function(){;},
						function(){jQuery(this).find("div.jse_select_color").hide();}
					)
					
					jQuery(th).find("#jse_to_sel_bgcol_block div.jse_select_color div.colors").click(function(){
						var tocolor=jQuery(this).find('img').attr("title");
		
						if(navigator.userAgent.match(/gecko|opera/i)){
							jseframe_doc.execCommand('hilitecolor',false,tocolor);
						}else{
							jseframe_doc.execCommand('BackColor',false,tocolor);
						}
						jQuery(th).find("#jse_to_sel_bgcol_block div.jse_select_color").hide();
					}); 
					
				}else{
					jQuery(th).find("#jse_to_sel_bgcol_block div.jse_select_color").show();
				}
				break;
			case 'cut':
				if(jQuery(jseframe_doc).find('#ubcut').length==0){ // уже есть кат
					jseframe_doc.execCommand('ForeColor',false,jse_cut_color);
					var rgb=jse_HexToRGB(jse_cut_color);
					rgb="rgb("+rgb.red+", "+rgb.green+", "+rgb.blue+")";
					jQuery(jseframe_doc).find('span').each(function(){
						if(jQuery(this).css('color')==jse_cut_color || jQuery(this).css('color')==rgb){
							jQuery(this).after("<span id='temps'></span>");
							var text=jQuery(this).html();
							var temps=jQuery(this).next();
							jQuery(this).remove();
							temps.before("<font color='"+jse_cut_color+"'>"+text+"</font>");
							temps.remove();
						}
					});
					var this_cut=jQuery(jseframe_doc).find('font[color='+jse_cut_color+']:eq(0)');
					if(this_cut.length==0){
						jseframe_doc.execCommand("InsertImage",false,"thiscut");
						var temps=jQuery(jseframe_doc).find('img[src=thiscut]');
						temps.after("<br/><br/><a href='#' id='ubcut'>Читать дальше<span>&nbsp;(ubcut)</span></a><br/><br/>");
						temps.remove();
						
						// удаляэм левые спани
						jQuery(jseframe_doc).find('span').each(function(){
							if(jQuery(this).css('color')==jse_cut_color || jQuery(this).css('color')==rgb){
								var del=jQuery(this);
								var text=del.html();
								del.after("<span id='temps'></span>");
								var temps=del.next();
								del.remove();
								temps.before(text);
								temps.remove();
							}
						});
						
					}else{				
						var text=this_cut.html();
						if(text=='') text='Читать дальше';
						this_cut.after("<span id='temps'></span>");
						var temps=this_cut.next();
						this_cut.remove();
						temps.before("<br/><br/><a href='#' id='ubcut'>"+text+"<span>&nbsp;(ubcut)</span></a><br/><br/>");
						temps.remove();
					}
				}
				break;
			case 'clear':
				jseframe_doc.execCommand('removeformat',false,null);
				break;
			case 'copyto':
				var sel_text='';
				if (jseframe_doc.getSelection()) {
				  // the Navigator 4.0x code
				  sel_text=jseframe_doc.getSelection();
				} else if (jseframe_doc.selection && jseframe_doc.selection.createRange) {
					var range = jseframe_doc.selection.createRange();
					var sel_text = range.text;
				  // the Internet Explorer 4.0x code
				} else {
				  // the alternative code
				}
				if(jse_array_dom[options.scopytoid]!=null){
					jQuery(jse_array_dom[options.scopytoid]).find("body").html(sel_text);
				}
				break;
			case 'bquote':
				
				var sel_text='';
				if (jseframe_doc.getSelection()){
				  // the Navigator 4.0x code
				  sel_text=jseframe_doc.getSelection();
				} else if (jseframe_doc.selection && jseframe_doc.selection.createRange) {
					var range = jseframe_doc.selection.createRange();
					var sel_text = range.text;
				  // the Internet Explorer 4.0x code
				} else {
				  // the alternative code
				}
				while(sel_text.search(/\n/)!=-1)sel_text=sel_text.replace(/\n/,'</br>');
				sel_text=sel_text.replace(/\n/,'</br>');
				while(sel_text.search(/\r/)!=-1)sel_text=sel_text.replace(/\r/,'');
				sel_text=sel_text.replace(/\r/,'');
			//	alert(sel_text);
				setTimeout(options.sbquote_func+"('"+sel_text+"')",0);
				
				break;
		}
	}
	
	function jseSetEvent(th){
		var tools=th.find("div.jsetools");
		tools.find('img.jsetoolshover')
			.hover(
				   function(){
					   jQuery(this).css('background-position','bottom');
					},
					function(){
						jQuery(this).css('background-position','top')
					}
			);
		tools.find('img.jsebold').click(function(){jse_go_format(th,'bold')});
		tools.find('img.jseitalic').click(function(){jse_go_format(th,'italic')});
		tools.find('img.jseunderline').click(function(){jse_go_format(th,'underline')});
		tools.find('img.jselineThrough').click(function(){jse_go_format(th,'lineThrough')});
		tools.find('img.jsesup').click(function(){jse_go_format(th,'sup')});
		tools.find('img.jsesub').click(function(){jse_go_format(th,'sub')});
		tools.find('img.jseleft').click(function(){jse_go_format(th,'left')});
		tools.find('img.jsecenter').click(function(){jse_go_format(th,'center')});
		tools.find('img.jseright').click(function(){jse_go_format(th,'right')});
		tools.find('img.jsejustify').click(function(){jse_go_format(th,'justify')});
		tools.find('img.jselink').click(function(){jse_go_format(th,'link')});
		tools.find('img.jseimg').click(function(){jse_go_format(th,'img')});
		tools.find('img.jsevideo').click(function(){jse_go_format(th,'video')});
		tools.find('img.jsecolor').click(function(){jse_go_format(th,'color')});
		tools.find('img.jsebgcolor').click(function(){jse_go_format(th,'bgcolor')});
		tools.find('img.jsecut').click(function(){jse_go_format(th,'cut')});
		tools.find('img.jseclear').click(function(){jse_go_format(th,'clear')});
		tools.find('img.jsecopyto').click(function(){jse_go_format(th,'copyto')});
		tools.find('img.jsebquote').click(function(){jse_go_format(th,'bquote')});
		return;
	}
	function jseCreateEditor(th){
		th.html('');
		th.append("<div class='jsetools'></div>");
		var tools=th.find("div.jsetools");
		
		tools.append('<table cellpadding="0" cellspacing="3"><tr></tr></table>');
		if(options.sbold) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsebold jsetoolshover" title="Полужирный" /></td>');
		if(options.sitalic) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jseitalic jsetoolshover" title="Курсив" /></td>');
		if(options.sunderline) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jseunderline jsetoolshover" title="Подчеркнутый" /></td>');
		if(options.slineThrough) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jselineThrough jsetoolshover" title="Перечеркнутый" /></td>');
		if(options.ssup) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsesup jsetoolshover" title="Верхний индекс" /></td>');
		if(options.ssub) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsesub jsetoolshover" title="Нижний индекс" /></td>');
		if(options.sleft) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jseleft jsetoolshover" title="По левому краю" /></td>');
		if(options.scenter) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsecenter jsetoolshover" title="По центру" /></td>');
		if(options.sright) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jseright jsetoolshover" title="По правому краю" /></td>');
		if(options.sjustify) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsejustify jsetoolshover" title="По ширине" /></td>');
		if(options.slink) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jselink jsetoolshover"  title="Ссылка" /></td>');
		if(options.simg) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jseimg jsetoolshover"  title="Рисунок" /></td>');
		if(options.svideo) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsevideo jsetoolshover" title="Видео" /></td>');
		if(options.scolor) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsecolor jsetoolshover"  title="Цвет текста" /><span id="jse_to_sel_col_block"></span></td>');
		if(options.sbgcolor) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsebgcolor jsetoolshover" title="Цвет фона" /><span id="jse_to_sel_bgcol_block"></span></td>');
		if(options.scut) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsecut jsetoolshover" title="Читать дальше (ub.cut)" /></td>');

		if(options.scopyto) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsecopyto jsetoolshover" title="CopyCut" /></td>');
		
		if(options.sbquote) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jsebquote jsetoolshover" title="Цитата" /></td>');
		if(options.sclear) tools.find("table tr").append('<td><img src="'+options.pathtoimg+'jse_img/pixel.gif" class="jseclear jsetoolshover" title="Очистить форматирование" /></td>');
		
		th.append("<iframe frameborder='0'></iframe>");
		var jseframe=th.find('iframe');
		var jseframe_doc=jseframe.get(0).contentWindow.document;
		jseframe.attr({
				height:options.jseheight+'px', 
				width:(th.width()-1)+'px',
				frameborder:'0'
			});
		jseframe_doc.open();
		if(options.init_text=='') options.init_text=' ';
		jseframe_doc.write('<html><head><link href="'+options.pathtoimg+'jse_img/framestyle.css" rel="stylesheet" type="text/css" /></head><body>'+options.init_text+'</body></html>');
		jseframe_doc.close();
		jseframe_doc.designMode='On';
		
		jse_array_dom[jse_array_index]=jseframe_doc;
		jse_array_index++;
		
		var frame_body=false;
		setTimeout(function(){
			frame_body=jQuery(jseframe_doc).find('body');
			frame_body.attr({marginheight:0, marginwidth:0, topmargin:0, leftmargin:0, rightmargin:0,bottommargin:0});
			
			// init img and video
			frame_body.find("img[title!='']").attr('src',options.pathtoimg+'jse_img/your_video.gif').attr('class','this_video');
			frame_body.find("img[title='']").each(function(){
				var imgsrc=$(this).attr('src');
				$(this).attr('src',options.pathtoloadimg+imgsrc);										   
			});
			
		},1);
		
		jseSetEvent(th);
		
		$(jseframe_doc).keyup(function(){
			jse_clear_html(frame_body);
		});
		
	}
	
	return this.each(function() {
		var th=jQuery(this);
		jseCreateEditor(th)
	});

}

function jse_loadimg_done(e,f){
//	alert(f);
	if(e==''){
		var temps=jQuery(jse_now_load_where).find("span.touploadimg:eq(0)");
		temps.after("<img src='"+jse_pathtoloadimg+""+f+"' width='100px'/>");
		jQuery(jse_now_load_where).find("span.touploadimg").remove();
	}else alert(e);
}

jQuery.fn.jseditor_get=function(){	
	var jse_result=new Array();
	var i=0;
	this.each(function() {
		var th_doc=jQuery(this).find('iframe').get(0).contentWindow.document;
		var main_damp=jQuery(th_doc).find('body').html();
		
		jQuery(th_doc).find("a#ubcut span").remove();

		jse_result[i]=new Array();

		//////////////
		// get video
		var j=0;
		var video_array=new Array();
		jQuery(th_doc).find("img.this_video").each(function(){
			var curvideo=jQuery(this);
			if(curvideo.attr('title')!='' && curvideo.attr('title')!=null){
				var link_to_video=curvideo.attr('title');
				curvideo.after("<span class=placeforvideo>"+j+"</span>");
				curvideo.remove();
				video_array[j]=link_to_video;
				j++;
			}
		});
		jse_result[i]['video']=new Array();
		jse_result[i]['video']=video_array;
		// get video
		//////////////
		
		/////////////
		// get img
		jQuery(th_doc).find("span.touploadimg").remove();
		var all_img=jQuery(th_doc).find("img");
		var img_array=new Array();
		var j=0;
		all_img.each(function(){
			var curimg=jQuery(this);
			var text=curimg.attr('src');
			var reg = new RegExp("[0-9a-zA-Z_\.]*$");
			text = reg.exec(text);
			img_array[j]=text;
			curimg.after("<span class=placeforimg>"+j+"</span>");
			curimg.remove();
			j++;
		});
		jse_result[i]['img']=new Array();
		jse_result[i]['img']=img_array;
		// get img
		////////////
		var temp_html=jQuery(th_doc).find('body').html();
	//	temp_html=temp_html.replace(/<[:space]{0,1}(font|Font|FONT)[:space]{0,1}>/,'');
		jse_result[i]['html']=temp_html;
		jQuery(th_doc).find('body').html(main_damp);
		i++;
	});
	return jse_result;
}

