function is_ie() {
	return $.browser.msie;
}

var editor_base_field='comment';
var editor_type='blog';

//wysiwyg
var editor={};

function editor_init(field, id, mode, is_save) {
	if (field.substr(0,1)=='!') {
		editor_base_field=field.substr(1);
		field=editor_base_field+'_'+id;
	}
	if (!id) id='0';
	if ($("#"+field)[0] && $("#iframe_"+field)[0]) {
		if (typeof(editor[field])!='object') {
			editor[field]=new WC_editor(field, id, mode);
		} else {
			if (typeof(is_save)=='undefined' || (is_save && editor[field].mode!=mode)) {
				editor[field].focus(1);
			}
		}
	}
  $(document).click(function(){
     toggleButtons(id, 6, 0);
  });
     
  $('#fcol_btn_'+id).click(function(event){
    event.stopPropagation();
  });  
  $('#box_color_1_'+id).click(function(event){
    event.stopPropagation();
  });  
  $('#bcol_btn_'+id).click(function(event){
    event.stopPropagation();
  });    
  $('#box_color_2_'+id).click(function(event){
    event.stopPropagation();
  });      
  $('#button_image_'+id).click(function(event){
    event.stopPropagation();
  });
  $('#box_img_'+id).click(function(event){
    event.stopPropagation();
  });
  $('#button_link_'+id).click(function(event){
    event.stopPropagation();
  });
  $('#box_link_'+id).click(function(event){
    event.stopPropagation();
  });
  $('#button_smile_'+id).click(function(event){
      event.stopPropagation();
  });
  $('#smiles_'+id).click(function(event){
      event.stopPropagation();
  });
}

function checkStyle(f){
	var o=editor[f];
	if (!o) return;
	o.sel=o.getSel(true);
	o.rng=o.getRng(true);
	o.updateStyle();
	$('.noclass').hide();
	o.interval=0;
}

//class
WC_editor=function (field, id, mode) {
	this.version=1;
	this.field=field;
	this.id=id;
	this.win=$('#iframe_'+this.field)[0].contentWindow;
	this.doc=this.win.document;
	this.mode=mode;
	this.timeout=200;
	this.interval=0;

	this.ie=is_ie();
	this.is_b=null;
	this.id_u=null;
	this.is_u=null;
	this.is_fcol=null;
	this.is_bcol=null;

	this.blank_html=
	'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
	+'<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1251"></head>'
	+'<style>body{font-size:10pt;color:#000;text-align:left; font-family: "Trebuchet MS", Arial, Tahoma, Verdana}p{margin: 0px 0px 0px 0px}'
	+'span.bold{font-weight:bold} span.italic{font-style:italic} span.underline{text-decoration:underline}</style>'
	+'<body style="margin: 3px 3px 3px 3px; background-color: #fff"></body></html>';
	this.std_classes=[];

	this.smiles_path='/img/blog/pregnant/';//'/ticket/smiles/';
	this.buttons_path='/img/blog/';//'/ticket/';
	this.emoticons=[
	':smile1:',
	':smile2:',
	':smile3:',
	':smile4:',
	':smile5:',
	':smile6:',
	':smile7:',
	':smile8:',
	':smile9:',
	':smile10:',
	':smile11:',
	':smile12:',
	':smile13:',
	':smile14:',
	':smile15:',
	':smile16:',
	':smile17:',
	':smile18:',
	':smile19:',
	':smile20:',
	':smile21:',
	':smile22:',
	':smile23:',
	':smile24:',
	':smile25:',
	':smile26:',
	':smile27:',
	':smile28:',	
	':smile29:',
	':smile30:'		
	];
	this.init();
}


WC_editor.prototype.init=function (mode) {
	if (typeof(mode)!='undefined') {
		this.mode=mode;
	}
	var d='';
	switch (this.mode) {
		case 'wysiwyg':
		d=this.getDoc();
		d.designMode='On';
		this.updateWYSIWYG();
		this.body=this.doc.body;
		this.getBody().contentEditable = true;

		if (this.ie) {
			$('#iframe_'+this.field).css('overflow', 'hidden');
			this.getBody().style.overflow='scroll';
		}
		try {
			if (d.queryCommandEnabled('styleWithCSS')) d.execCommand("styleWithCSS", false, false);
		} catch(e) {}

		function myFunction1(ev, stop) {
			if (obj.interval) clearInterval(obj.interval);
			obj.interval=setTimeout('checkStyle("'+obj.field+'")', obj.timeout);
			if (stop) return false;
		};
		function myFunction2(ev, stop) {
			return myFunction1(ev, true);
		}

		var obj=this;
		$(this.body).click(myFunction1);
		
		if (this.ie) {
			$(this.body).bind('keyup', myFunction1);
		} else {
			this.doc.addEventListener('keyup', myFunction1, false);
		}

		$(this.body).bind('contextmenu', myFunction2);
		this.focus(1);
		break;
		case 'html':
		if (typeof(mode)!='undefined') {
			this.updateHTML();
		}
		$("#"+this.field)[0].focus();
		break;
	}
}

WC_editor.prototype.updateStyle=function (switch_thetag) {
	var s;
	if (!switch_thetag) s=this.checkStyle();
	if ($('#bold_btn_'+this.id)[0] && $('#underline_btn_'+this.id)[0] && $('#italic_btn_'+this.id)[0] && $('#fcol_btn_'+this.id)[0] && $('#bcol_btn_'+this.id)[0]) {
		if (switch_thetag=='b') {
			this.is_b=!this.is_b;
			s={'b':this.is_b};
			$('#bold_btn_'+this.id)[0].src = this.buttons_path + (s.b ? 'button-bold-act.gif' : 'button-bold.gif');
		} else if (switch_thetag=='u') {
			this.is_u=!this.is_u;
			s={'u':this.is_u};
			$('#underline_btn_'+this.id)[0].src = this.buttons_path + (s.u ? 'button-underline-act.gif' : 'button-underline.gif');
		} else if (switch_thetag=='i') {
			this.is_i=!this.is_i;
			s={'i':this.is_i};
			$('#italic_btn_'+this.id)[0].src = this.buttons_path + (s.i ? 'button-italic-act.gif' : 'button-italic.gif');
		} else if (switch_thetag=='fcol') {
			this.is_fcol=!this.is_fcol;
			s={'fcol':this.is_fcol};
			$('#fcol_btn_'+this.id)[0].src = this.buttons_path + (s.fcol ? 'button-fcol-act.gif' : 'button-fcol.gif');
		} else if (switch_thetag=='bcol') {
			this.is_bcol=!this.is_bcol;
			s={'bcol':this.is_bcol};
			$('#bcol_btn_'+this.id)[0].src = this.buttons_path + (s.bcol ? 'button-bcol-act.gif' : 'button-bcol.gif');
		} else {
			$('#bold_btn_'+this.id)[0].src = this.buttons_path + (s.b ? 'button-bold-act.gif' : 'button-bold.gif');
			$('#italic_btn_'+this.id)[0].src = this.buttons_path + (s.i ? 'button-italic-act.gif' : 'button-italic.gif');
			$('#underline_btn_'+this.id)[0].src = this.buttons_path + (s.u ? 'button-underline-act.gif' : 'button-underline.gif');
			$('#fcol_btn_'+this.id)[0].src = this.buttons_path + (s.fcol ? 'button-fcol-act.gif' : 'button-fcol.gif');
			$('#bcol_btn_'+this.id)[0].src = this.buttons_path + (s.bcol ? 'button-bcol-act.gif' : 'button-bcol.gif');
		}
	}
}

WC_editor.prototype.checkStyle=function () {
	this.sel=this.getSel(true);
	this.rng=this.getRng(true);
	var tag, is_b, is_u, is_i, is_fcol, is_bcol;
	var parentEl=this.rng?(this.ie?(this.rng.parentElement && this.rng.parentElement()):this.rng.commonAncestorContainer.parentNode):null;
	while (parentEl && (!is_b || !is_u || !is_i || !is_fcol || !is_bcol) && parentEl.tagName && $.inArray(parentEl.tagName.toLowerCase(), ['html', 'body'])<0)
	{
		tag=parentEl.tagName.toLowerCase();
		is_b=(is_b?is_b:(tag=='strong' || tag=='b' || parentEl.className=='bold' || parentEl.style.fontWeight=='bold' || parentEl.style.fontWeight>400));
		is_i=(is_i?is_i:(tag=='em' || tag=='i' || parentEl.className=='italic' || parentEl.style.fontStyle=='italic'));
		is_u=(is_u?is_u:(tag=='u' || parentEl.className=='underline' || parentEl.style.textDecoration=='underline'));
		is_fcol=(is_fcol?is_fcol:(tag=='font' || tag=='span' || parentEl.color!='' || parentEl.style.color!=''));
		is_bcol=(is_bcol?is_bcol:(tag=='font' || tag=='span' || parentEl.style.background!='' || parentEl.style.background-color!=''));
		parentEl=parentEl.parentNode;
	}
	this.is_b=is_b;
	this.id_i=is_i;
	this.is_u=is_u;
	this.is_fcol=is_fcol;
	this.is_bcol=is_bcol;
	return {'b': is_b, 'i': is_i, 'u': is_u, 'fcol': is_fcol, 'bcol': is_bcol};
}

WC_editor.prototype.focus=function (r) {
	this.getWin().focus();
	if (this.ie) {
		this.getRng(r).select();
	}
}
WC_editor.prototype.getWin=function () {
	return this.win;
}
WC_editor.prototype.getDoc=function () {
	return this.doc;
}
WC_editor.prototype.getBody=function () {
	return this.body;
}

WC_editor.prototype.getSel=function (r) {
	var sel=null;
	if (!r && this.sel)
	{
		return this.sel;
	}
	try
	{
		sel=this.getWin().getSelection();
	}
	catch (e)
	{
		try
		{
			sel=this.getDoc().selection;
		}
		catch (e2) {}
	}
	this.sel=sel;
	return sel;
}
WC_editor.prototype.getRng=function (r) {
	var rng=null;
	if (!r && this.rng && this.rng!='')
	{
		return this.rng;
	}
	try
	{
		rng=this.getSel().getRangeAt(0);
	}
	catch (e)
	{
		try
		{
			rng=this.getSel().createRange();
		}
		catch (e2) {}
	}
	this.rng=rng;
	return rng;
}
WC_editor.prototype.pasteRngHTML=function (html) {
}

WC_editor.prototype.updateHTML=function () {
	var root=this.getBody();
	if (root!=null) {
		tmp=root.innerHTML;
		tmp=this.replaceSpecTags(tmp, 'html');	
		$("#"+this.field).val(tmp);
	}
	
}

WC_editor.prototype.updateWYSIWYG=function () {
	var d=this.getDoc();
	var tmp=$("#"+this.field).val();
	if (typeof(d.body)=='undefined' || d.body==null || d.body.className!='content_iframe') {
		d.open();
		d.write(this.blank_html);
		d.close();
	}
	d.body.className='content_iframe';
	d.body.style.height="90px";
	d.body.innerHTML=$.trim(tmp)?tmp:(this.ie?'':'<br clear="none"/>');
	this.updateStyle();
}

WC_editor.prototype.cleanStr=function (str) {
	return str.replace(/( |\t){2,}/g, ' ');
}

WC_editor.prototype.escapeRegExp=function (str) {
	return str.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
}

function rgbToHex(rgb) { 
  var rgbvals = /rgb\((.+),(.+),(.+)\)/i.exec(rgb); 
  var rval = parseInt(rgbvals[1]); 
  var gval = parseInt(rgbvals[2]); 
  var bval = parseInt(rgbvals[3]); 
  return '#' + ( 
    rval.toString(16) + 
    gval.toString(16) + 
    bval.toString(16) 
  ).toUpperCase(); 
}

WC_editor.prototype.replaceSpecTags=function (content, mode) {
	if (typeof(mode)=="undefined") {
		mode=this.mode;
	}
	if (mode=="wysiwyg") {
		var replaces=[
		[/\r/g, ""],
		[/\n/g, "<br>"],
		[/\[(\/?)i\]/ig, "<$1em>"],
		[/\[(\/?)u\]/ig, "<$1u>"],
		[/\[(\/?)b\]/ig, "<$1strong>"],
		[/\[url=(?:"|&quot;)(.+?)(?:"|&quot;)\](.+?)\[\/url\]/ig, "<a target=\"_blank\" href=\"$1\">$2</a>"],
		[/\[fcol=(?:"|&quot;)#([\da-f]{3,6})(?:"|&quot;)\](.+?)\[\/fcol\]/ig, "<font color=\"#$1\">$2</font>"],
		[/\[bcol=(?:"|&quot;)#([\da-f]{3,6})(?:"|&quot;)\](.+?)\[\/bcol\]/ig, "<span style=\"background-color: #$1\">$2</span>"],
		[/\[img\](\d+)\[\/img\]/ig, "<img id=\"img_$1\" style=\"display:block\" src=\"/"+editor_type+"_image?id=$1\"/>"]
		];
		for (i=0; i<replaces.length; i++) {
			content=content.replace(replaces[i][0], replaces[i][1]);
		}
        
		var obj=this;

		$.each(this.emoticons, function(i, el) {
			content=content.replace(new RegExp("\\s"+obj.escapeRegExp(el)+"\\s", "ig"), '<img id="smile_'+(i+1)+'_'+(Math.random().toString().substr(2))+'" src="'+obj.smiles_path+'smile'+(i+1)+'.gif"/>')
		});

		content=this.cleanStr(content);
	} else if (mode=="html") {
			var txt=content;
			var obj=document.createElement('div');
			var color='';
			obj.innerHTML=txt;		 
			if (obj.innerHTML.indexOf('<BR clear="none">')!=-1)
			    obj.innerHTML = obj.innerHTML.substring(0, obj.innerHTML.indexOf('<BR clear="none">')) + obj.innerHTML.substring(obj.innerHTML.indexOf('<BR clear="none">') + 17);
			if (obj.innerHTML.indexOf('<BR><P>')!=-1)
			    obj.innerHTML = obj.innerHTML.substring(0, obj.innerHTML.indexOf('<BR><P>')) + obj.innerHTML.substring(obj.innerHTML.indexOf('<BR><P>') + 4);  
			if (obj.innerHTML.indexOf('<BR></P>')!=-1)
			    obj.innerHTML = obj.innerHTML.substring(0, obj.innerHTML.indexOf('<BR></P>')) + obj.innerHTML.substring(obj.innerHTML.indexOf('<BR></P>') + 4);  			    
			if($("span", obj)[0]){
			var el=$("span", obj)[0];
			}else{
			var el=$("font", obj)[0];
			}
			while (el) {
				var r='';
				if (el.getAttribute('bgcolor')||el.style.backgroundColor!='') {
					color=(el.getAttribute('bgcolor') ? el.getAttribute('bgcolor') : el.style.backgroundColor);
					if (color.substr(0, 3)=='rgb') {
						color = rgbToHex(color);
					}
					$(el).replaceWith('[bcol="'+color+'"]'+el.innerHTML+'[/bcol]');
				}
				if (el.getAttribute('color') || el.style.color){
				color=(el.getAttribute('color') ? el.getAttribute('color') : el.style.color);
					if (color.substr(0, 3)=='rgb') {
						color = rgbToHex(color);
					}
					$(el).replaceWith('[fcol="'+color+'"]'+el.innerHTML+'[/fcol]');
				}
				if (el.className=='bold' || el.style.fontWeight=='bold' || el.style.fontWeight>400){					
				    $(el).replaceWith('<strong>'+el.innerHTML+'</strong>');				  
				}
				if (el.className=='italic' || el.style.fontStyle=='italic'){
				    $(el).replaceWith('<em>'+el.innerHTML+'</em>');
				}
				if (el.className=='underline' || el.style.textDecoration=='underline'){
				    $(el).replaceWith('<u>'+el.innerHTML+'</u>');
				}			
				if (el.face){
				    $(el).replaceWith(el.innerHTML);
				}			
				if (el.size){
				    $(el).replaceWith(el.innerHTML);
				}			
				if($("span", obj)[0])
				{
					el=$("span", obj)[0];
				} else
				{
					if($("font", obj)[0])
					{
						el=$("font", obj)[0];
					} else
					{
						el=null;
					}
				}
			}
			content=obj.innerHTML;
		var replaces=[
		[/\r|\n/g, " "],
		[/(<(p|div|h[1-6])(?:\s[^>]*)?>)\s*<br>\s*(<\/\2>)/ig, "$1 $3"],
		[/<\/(p|div|h[1-6])>\s*<(p|div|h[1-6])(\s[^>]*)?>/ig, "\n"],
		[/(.)<\/?(?:p|div|h[1-6]).*?>(.)/ig, "$1\n$2"],
		[/\&nbsp\;/ig, " "],
		[/<br(\s[^>]*)?>/ig, "\n"],
		[/<(\/?)(?:em|i)>/ig, "[$1i]"],
		[/<(\/?)u>/ig, "[$1u]"],
		[/<fcol(\s[^>]*)?c=(?:"|&quot;)#([\da-f]{3,6})(?:"|&quot;)>(.+?)<\/fcol>/ig, "[fcol=\"#$2\"]$3[/fcol]"],
		[/<bcol(\s[^>]*)?c=(?:"|&quot;)#([\da-f]{3,6})(?:"|&quot;)>(.+?)<\/bcol>/ig, "[bcol=\"#$2\"]$3[/bcol]"],
		[/<(\/?)(?:strong|b)(\s[^>]*)?>/ig, "[$1b]"],
		[/<a\s[^>]*?href="(.+?)".*?>(.+?)<\/a>/ig, "[url=\"$1\"]$2[/url]"],
		[/<img\s[^>]*?id=.?img_(\d+).*?>/ig, "[img]$1[/img]"]
		];
		for (i=0; i<replaces.length; i++){
			content=content.replace(replaces[i][0], replaces[i][1]);
		}
		content=this.cleanStr(content);
		$.each(this.emoticons, function(i, el) {
			content=content.replace(new RegExp("<img\\s[^>]+?smile_"+(i+1)+"_\\d+[^>]*?>", "ig"), " "+el+" ");
		});
		content=this.stripTags(content);
	}
	return content;
}

WC_editor.prototype.stripTags=function (text) {
	return text.replace(/<\/?[a-z][a-z0-9]*.*?>/ig, "");
}

WC_editor.prototype.stripBB=function (text) {
	return text.replace(/\[\/?(img|url|b|u|i|fcol|bcol)(\s[^\]]*)?\]/ig, "");
}

WC_editor.prototype.getTag=function (tag_find, tag_stop) {
	var sel=this.getSel();
	var rng=this.getRng();
	if (typeof(rng.commonAncestorContainer)!='undefined') {
		node=rng.commonAncestorContainer;
	} else if (typeof(sel.type)!='undefined') {
		if (sel.type.toLowerCase() != "control") {
			node = rng.parentElement();
		} else {
			node = rng[0];
		}
	} else {
		return null;
	}
	if (!tag_find) {//parent
		return node;
	}
	if (typeof(tag_find)!='object') {
		tag_find=[tag_find];
	}
	if (typeof(tag_stop)=='undefined') {
		tag_stop=new Array();
	} else if (typeof(tag_stop)!='object') {
		tag_stop=[tag_stop];
	}
	tag_stop.push('BODY');
	for (i=0; i<tag_find.length; i++) {
		tag_find[i]=tag_find[i].toLowerCase();
	}
	for (i=0; i<tag_stop.length; i++) {
		tag_stop[i]=tag_stop[i].toLowerCase();
	}
	while ($.inArray(node.nodeName.toLowerCase(), tag_find)<0 && $.inArray(node.nodeName.toLowerCase(), tag_stop)<0) {
		node=node.parentNode;
	}
	if ($.inArray(node.nodeName.toLowerCase(), tag_find)>=0) {
		return node;
	} else {
		return null;
	}
}

function simpletag(thetag, id, props) {
	editor[editor_base_field+'_'+id].focus();
	var st={'b': 'Bold', 'i': 'Italic', 'u': 'Underline', 'url': 'CreateLink', 'fcol' : 'foreColor', 'bcol' : 'BackColor'};

	var val=null;
	if (thetag=='url')
	{
		val=props['href'];
		var rng=editor[editor_base_field+'_'+id].getRng();
		var sel;
		if (is_ie())
		{
			sel=$.trim(rng.htmlText);
		}
		else
		{
			sel=$.trim(rng.toString());
		}

		if (!sel)
		{
			var obj=document.createElement('a');
			obj.href=val;
			obj.innerHTML=val;
			pasteTag(id, obj);
			return true;
		}
	}

	if(thetag == 'fcol')
	{
		val = props;
	}
		
	var ua = navigator.userAgent.toLowerCase();
	if(thetag == 'bcol')
	{
		if(ua.indexOf("opera") != -1 || ua.indexOf("gecko") != -1)st[thetag] = 'hilitecolor';
		val = props;
	}
	
	var d = editor[editor_base_field+'_'+id].getDoc();
	
	if (st[thetag] == 'hilitecolor' && (ua.indexOf("gecko") != -1 || ua.indexOf("opera") != -1)) {
		try {
			if (d.queryCommandEnabled('styleWithCSS')) d.execCommand("styleWithCSS", false, true);
		} catch(e) {}
	}
	d.execCommand(st[thetag], false, val);
	if (st[thetag] == 'hilitecolor' &&  ua.indexOf("gecko") != -1) {
		try {
			//if (d.queryCommandEnabled('styleWithCSS')) d.execCommand("styleWithCSS", false, false);
		} catch(e) {}
	}
	if (thetag!='url') {
		editor[editor_base_field+'_'+id].updateStyle(thetag);
	}
	return true;
}

function emoticon(smile,id,title)
{
			var obj=document.createElement('img');
			obj.src='/img/blog/pregnant/smile'+smile+'.gif';
			obj.id='smile_'+smile+'_1';
			pasteTag(id, obj);
			toggleButtons(id, 0);
			return true;
}

function pickColor(type, id)
{
	$('#col_type-'+id).val(type);
    if (type=='fcol')
	    toggleButtons(id, 3);	
    if (type=='bcol')
	    toggleButtons(id, 4);
}

function pasteTag(id, obj) {
	var t=editor[editor_base_field+'_'+id], d=t.getDoc();
	t.focus();
	var r=t.getRng();
	var obj2=document.createElement('div');
	obj2.appendChild(obj);
	var h=obj2.innerHTML;
	if (r.insertNode) {
		if (d.queryCommandEnabled('InsertHTML'))
		return d.execCommand('InsertHTML', false, h);
		r.deleteContents();
		r.insertNode(t.getRng().createContextualFragment(h));
	} else {
		if (r.item)
		r.item(0).outerHTML = h;
		else
		r.pasteHTML(h);
	}
}

function toggleSmiles(id, obj) {
	toggleButtons(id, 0);
}

function insertImg(id, ins) {
	if (!$('#box_img_'+id)[0]) return false;
	$('#err_img_'+id).html("");
	if (ins) {
		if ($('#txt_file_'+id).val()) {
			uploadFile(id);
			return true;
		} else {
			$('#err_img_'+id).html("<span class='erorki'>"+lang['blog']['choose-file']+"</span>");
			return false;
		}
	} else {
		$('#fsubmit_'+id).removeAttr('disabled');
		toggleButtons(id, 1);
	}
}

function insertLink(id, ins) {
	if (!$('#box_link_'+id)[0]) return false;
	$('#err_link_'+id).html("");
	if (ins)
	{
		if ($('#txt_link_'+id).val())
		{
			var url=$('#txt_link_'+id).val();
			if (!url.match(/^[a-z]{3,5}:\/\//i)) url="http://"+url;
			var tag_a=editor[editor_base_field+'_'+id].getTag("A");
			if (tag_a)
			{
				tag_a.href=url;
				editor[editor_base_field+'_'+id].focus();
				toggleButtons(id, 2, 0);
			}
			else
			{
				if (simpletag('url', id, {'href': url})) toggleButtons(id, 2, 0);
			}
		}
		else
		{
			$('#err_link_'+id).html("<span class='erorki-link'>"+lang['blog']['filled']+"</span>");
		}
	}
	else
	{
		toggleButtons(id, 2);
		$('#txt_link_'+id).val('');
	}
}

function toggleButtons(id, i, vis) {
	//smiles, box_img, box_link
	var btns=['smiles', 'box_img', 'box_link', 'box_color_1', 'box_color_2'];
	if ($("#"+btns[i]+'_'+id)[0] && typeof(vis)=='undefined') {
		vis=($("#"+btns[i]+'_'+id).css('display')=='none')?1:0;
	}
	$.each(btns, function(ind, el) {
		if ($("#"+btns[ind]+'_'+id)[0]) $("#"+btns[ind]+'_'+id).css('display', (ind==i && vis)?'':'none');
	});
}
function onSave(form,id)
{
	var ef=false;
	editor[editor_base_field+'_'+id].updateHTML();
	return true;
}

//uploader
var timeInterval=0;
var rel_path_to_uploader="/img/";

uploadGetWin=function (id) {
	return $('#iframe_'+id)[0].contentWindow;
}
uploadGetDoc=function (id) {
	return uploadGetWin(id).document;
}
uploadGetBody=function (id) {
	return uploadGetDoc(id).body;
}

function checkLoading(id) {
	if (!timeInterval) return;
	uploadCheck(id);
}

function uploadCheck(id) {
	timeInterval=0;
	var b=uploadGetBody(id);
	var t1=(b.innerHTML.search('UploadingComplete')>=0);
	var t2=(b.innerHTML.search('UploadingError')>=0);
	var t3=(b.innerHTML.search('Error')>=0);
	var t4=(b.innerHTML.search('nginx')>=0);
	var t5=(b.innerHTML.search('!islogin')>=0);
	if (t2 && t5) {
		t2=0;
	}
	var img_id;
	if (t1) {
		img_id=parseInt(b.innerHTML.replace('UploadingComplete:', ''));
	}

	var e_txt;
	if (t1 && img_id) {
		e_txt=lang['blog']['u-complete'];
	} else {
		e_txt=lang['blog']['u-error'];
		if (t2) {
			var err_type=$.trim(b.innerHTML.replace('UploadingError: ', ''));
			if (lang['blog'][err_type]) {
				e_txt+=': '+lang['blog'][err_type];
			}
		}
	}

	$('#err_img_'+id).html("<span class='erorki'>"+e_txt+"</span>");

	b.innerHTML='';
	$('#iframe_'+id)[0].src='';
	var f=$('<input type="file"/>').attr({'id': 'txt_file_'+id, 'name': 'txt_file_'+id});
	$('#txt_file_'+id).replaceWith(f);
	if (img_id) {
		var obj=document.createElement('img');
		obj.style.display='block';
		if (location.href.indexOf('community')>-1) editor_type = 'comm';
		obj.src='/'+editor_type+'_image?id='+img_id;
		obj.id='img_'+img_id;
		editor[editor_base_field+'_'+id].focus();
		var rng=editor[editor_base_field+'_'+id].getRng();
		pasteTag(id, obj);
		editor[editor_base_field+'_'+id].focus();
		toggleButtons(id, 1, 0);
	}
	$('#fsubmit_'+id)[0].disabled=false;
}

function uploadFile(id) {
	$('#fsubmit_'+id)[0].disabled=true;
	$('#err_img_'+id).html("<span class='erorki'>Загрузка...<br/><img src='"+rel_path_to_uploader+"loading_small.gif' alt='loading...'/></span>");
	timeInterval=1;
	return true;
}
