
function Browserr() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }


  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    return;
  }


  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browserr = new Browserr();

var dragObj = new Object();
dragObj.zIndex = 0;



var __wisiwyg_drop_mode=false;
var __wisiwyg_drop_dx;
var __wisiwyg_drop_dy;


function wisiwyg(id,win_name,width,height,params)
{
//	alert("loading");
	this.win;
	this.pid=id;
	this.win_name=win_name;
	this.left;
	this.height;
	this.width=width;
	this.height=height+20;
	this.selection;
	this.tb;
    this.root_win;
	this.tb_mode;
	this.trigger;
	this.value;
	this.insert_value;
	this.edit_link;
	this.button=Array();
	this.bts_desc=Array();
	this.bts_desc[0]=Array('bold',0,'Жирный шрифт');
	this.bts_desc[1]=Array('italic',1,'Наклонный шрифт');
	this.bts_desc[2]=Array('underline',2,'Подчеркнутый шрифт');
	this.bts_desc[3]=Array('justifyleft',3,'Выравнивание по левому краю');
	this.bts_desc[4]=Array('justifycenter',4,'Выравнивание по центру');
	this.bts_desc[5]=Array('justifyright',5,'Выравнивание по правому краю');
	this.bts_desc[6]=Array('justifyfull',6,'Выравнивание по обоим краям');
	this.bts_desc[7]=Array('outdent',7,'Уменьшить отступ');
	this.bts_desc[8]=Array('indent',8,'Увеличить отступ');
	this.bts_desc[9]=Array('insertunorderedlist',9,'Неупорядоченный список');
	this.bts_desc[10]=Array('insertorderedlist',10,'Упорядоченный список');
//	this.bts_desc[11]=Array('fontsize',11,'Размер шрифта');
//	this.bts_desc[12]=Array('fontname',12,'Стиль шрифта');
//	this.bts_desc[11]=Array('hilitecolor',13,'Цвет фона');
//	this.bts_desc[12]=Array('forecolor',14,'Цвет шрифта');
	this.bts_desc[11]=Array('insertimage',15,'Вставить картинку');
	this.bts_desc[12]=Array('inserttable',16,'Вставить/редактировать таблицу');
	this.bts_desc[13]=Array('createlink',17,'Вставить/редактировать ссылку');
	this.bts_desc[14]=Array('clearhtml',18,'Очистить HTML текст');
//	alert(params.length);
	if(params){
		for (var i=0;i<params.length;i++){
			this.bts_desc[i+15]=Array('_insertspan_'+params[i]['services_id'],18+i,params[i]['descr'],'services',params[i]['img'],params[i]['services_id']);
//			alert(i);
		}
	}
	
	
//	this.bts_desc[19]=Array('null',19,'');
	
	
	
	
	
	
//	this.fontsizes=Array("1","2","3","4","5");
	this.fontnames=Array("Arial","Courier","Tahoma","Verdana","Impact","Fixedsys");
	
	this.opera=function()
	{
		alert('opera');
		uploader_go('act=im_opera');
//		div_id=document.getElementById(this.pid);
//		div_id.innerHTML="<textarea name=\""+this.win_name+"\" style=\"width:"+this.width+"; height:"+this.height+"\">"+div_id.innerHTML+"</textarea>";
	}
	
	this.init=function(){
		co=Array(0,0);
		try{
		this.value=document.getElementById(this.pid).innerHTML;
		}catch(e){
			try{
			thispid=this.pid+"1";
			this.value=document.getElementById(thispid).innerHTML;
			this.pid=thispid;
			}catch(e){
				thispid=this.pid+"2";
				this.value=document.getElementById(this.pid+"2").innerHTML;
				this.pid=thispid;
				}}
		document.getElementById(this.pid).innerHTML="<table cellpadding=0 cellspacing=0></table>";
		obj_div=document.getElementById(this.pid).firstChild;
		obj_div.style.cssText="width:"+(this.width+4)+"px; height:"+this.height+"px; border:1px solid #eee;text-align:center";
		obj_div.className="editor";
		obj_div.insertRow(0);
		obj_div.rows[0].insertCell(0);
		co=this.getCoordsObj(document.getElementById(this.pid),co);
		this.left=co[0];
		this.top=co[1];
		obj_div.rows[0].cells[0].className="wys_menu";
		obj_div.rows[0].cells[0].cssText="background:#cdcdcd;";
		obj_div.rows[0].cells[0].appendChild(this.getMenu());
		obj_div.insertRow(1);
		obj_div.rows[1].insertCell(0);
		obj_div.rows[1].cells[0].style.cssText="position:relative;top:0;left:0";
		root_win=document.createElement("div");
        this.root_win=root_win;
		root_win.style.cssText="position:relative; top:0px; left:0px; width:"+this.width+"height:"+(this.height-20);
		obj_div.rows[1].cells[0].appendChild(root_win);
		root_win.innerHTML="<iframe id="+this.win_name+" frameborder=0 width="+this.width+" height="+(this.height-20)+" style=\"position:absolute; left:0; top:0; display:block\"></iframe>";
		
		
		
		
		html_source=document.createElement("textarea");
		html_source.setAttribute("id",this.win_name+"_source");
		html_source.setAttribute("name",this.win_name);
		html_source.style.cssText="position:absolute; left:0;top:0; border-top:1px solid #eee; overflow:scroll; background-color:#fafafa; width:"+this.width+"; height:"+(this.height-20)+"; display:none; font-family:arial; font-size:12px";
		root_win.appendChild(html_source);
/*		
		comment_source=document.createElement("input");
		comment_source.setAttribute=('type','text');
		comment_source.setAttribute=('id',this.win_name+'_comment');
		comment_source.setAttribute=('name',this.win_name+'_comment_');
		comment_source.value='Комментарий';
		comment_source.style.cssText="border-top:1px solid #eee;background-color:#fafafa; width:"+this.width+";";
		root_win.appendChild(comment_source);
*/		
		submit_button=document.createElement("SPAN");
		submit_button.innerHTML='<input type="button" value="Сохранить" style="border-top:1px solid #017001;background-color:#eee; width:200px;" onClick="savefile(window[\''+this.win_name.replace(/::/,"__")+'\'].save(),\''+this.win_name.replace(/::/,"__")+'\',\'' + this.win_name+'_captcha_val'+'\');"> <input type="button" value="Отмена" style="border-top:1px solid #017001;background-color:#eee; width:200px;" onClick="cancelFile(\''+this.win_name.replace(/::/,"__")+'\');">';
		root_win.appendChild(submit_button);
		
		obj_div.insertRow(2);
		obj_div.rows[2].insertCell(0);
		obj_div.rows[2].cells[0].style.cssText="font-size:10px;font-family:arial;text-align:left;border-top:1px solid #eee;padding-left:5px;padding-top:2px";

		trigger=document.createElement("div");
		trigger.className='wisiwyg_captcha';
		trigger.setAttribute("id",this.win_name+"_captcha");
		trigger.innerHTML='<table><tr><td>Введите результат операции: </td><td><img id="captchaimg" onclick="this.src=\'/code.php?\'+Math.random();"  style="cursor:pointer;" title="Кликните, чтобы обновить картинку" src="/code.php"></td><td><input type="text" id="'+this.win_name+'_captcha_val" value=""></td></tr></table>';
 		obj_div.rows[2].cells[0].appendChild(trigger);

		this.win=document.getElementById(this.win_name);
		this.win.style.cssText="border-top:1px solid #eee; background-color:#fff;";
 		this.win.contentWindow.document.write('<LINK rel="stylesheet" type="text/css" href="/styles/reset.css">');
		this.win.contentWindow.document.write('<LINK rel="stylesheet" type="text/css" href="/styles/content.css">');
		this.win.contentWindow.document.write('<LINK rel="stylesheet" type="text/css" href="/styles/advanced.css">');
		this.win.contentWindow.document.write('<LINK rel="stylesheet" type="text/css" href="/styles/buttons.php">');
		this.win.contentWindow.document.write(this.value);
		this.win.contentWindow.document.close();
		

		if (!document.all) this.win.contentWindow.setTimeout(function() {this.document.designMode="on";}, 10);
		else this.win.contentWindow.document.designMode="on";
		//this.win.onload=function() { this.contentWindow.document.designMode="on"; }

/*		trigger_checkbox.onclick=function()
		{
			val=this.value.replace(/::/,"__");
			obj=eval(val);
			if (obj.trigger)
			{
				win=document.getElementById(obj.win_name);
				win_source=document.getElementById(obj.win_name+"_source");
				val_tag=win.contentWindow.document.getElementsByTagName("body");
				val_tag[0].innerHTML=win_source.value;
				win_source.style.display="none";
				win.style.display="block";
				win.contentWindow.document.designMode="on";
				obj.trigger=0;
				this.src="/i/im_trigger_off.gif";
			}
			else
			{
				win=document.getElementById(obj.win_name);
				win_source=document.getElementById(obj.win_name+"_source");
				val_tag=win.contentWindow.document.getElementsByTagName("body");
				win_source.style.display="block";
				win_source.value=val_tag[0].innerHTML;
				obj.trigger=1;
				this.src="/i/im_trigger_on.gif";
			}
		return false;
		}
*/
	}
	
	
	this.save=function()
	{
		val_obj=this.win.contentWindow.document.getElementsByTagName("body")[0];
		
		if (val_obj.firstChild.tagName=='PRE'.toUpperCase())
		{
		  val=val_obj.firstChild.innerHTML;
		}
		else
		{
		  val=val_obj.innerHTML;
		}
		win_source=document.getElementById(this.win_name+"_source");
		win_source.value=val;
		return val;
	}

	this.getMenu=function()
	{
		menu_tb=document.createElement("table");
		menu_tb.setAttribute("align","center");
		menu_tb.setAttribute("height","30");
		menu_tb.insertRow(0);
		menu_tb.className="wys_menu";
		for (i=0;i<this.bts_desc.length;i++){
			if(this.bts_desc[i][3]!='services'){
				menu_tb.rows[0].insertCell(i);
				menu_tb.rows[0].cells[i].appendChild(this.getButton(this.bts_desc[i][0],this.bts_desc[i][1],this.bts_desc[i][2]));
				menu_tb.rows[0].cells[i].style.cssText="text-align:center;width:21px";
			}
			else{
				menu_tb.rows[0].insertCell(i);
				menu_tb.rows[0].cells[i].appendChild(this.getButton2(this.bts_desc[i][0],this.bts_desc[i][1],this.bts_desc[i][2],this.bts_desc[i][4],this.bts_desc[i][5]));
				menu_tb.rows[0].cells[i].style.cssText="text-align:center;width:21px";
			}
		}
		return menu_tb;
	}


	this.getButton2=function(name_id,inner,title,img,id)
	{
		count=this.button.length;
		this.button[count]=document.createElement("div");
		this.button[count].style.cssText="position:relative;top:0;left:0;overflow:hidden;height:22px;width:27px;background-image:url('/i/btl_.gif');";
		this.button[count].setAttribute("id",name_id);
		this.button[count].setAttribute("title",title);
		this.button[count].setAttribute("name",this.win_name);
		this.button[count].onmousemove='return '+id+'___';
		this.button[count].onmouseover=function(){ this.style.backgroundImage="url('/i/btl.gif')"; };
		this.button[count].onmouseout=function(){ this.style.backgroundImage="url('/i/btl_.gif')"; };
		this.button[count].onclick = this.tbclick;
		this.button[count].innerHTML="<img src='"+ img +"' style='position:absolute;top:0px;left:0px;'>";
		return this.button[count];
	}

	
	this.getButton=function(name_id,inner,title)
	{
		count=this.button.length;
		this.button[count]=document.createElement("div");
		width=27*inner;
		this.button[count].style.cssText="position:relative;top:0;left:0;overflow:hidden;height:22px;width:27px;background-image:url('/i/btl_.gif');";
		this.button[count].setAttribute("id",name_id);
		this.button[count].setAttribute("title",title);
		this.button[count].setAttribute("name",this.win_name);
		this.button[count].onmouseover=function(){ this.style.backgroundImage="url('/i/btl.gif')"; };
		this.button[count].onmouseout=function(){ this.style.backgroundImage="url('/i/btl_.gif')"; };
		this.button[count].onclick = this.tbclick;
		this.button[count].innerHTML="<img src='/i/bt_all.gif' style='position:absolute; left:-"+width+";height:22px'>";
		return this.button[count];
	}

	this.tbclick=function(event)
	{
		
		ifr=this.getAttribute("name");
		ifr=ifr.replace(/::/,"__");
/*
		str='';
		for (key in window){
			if (typeof window[key]=='object'){
				str+=key+':' + window[key] + '\n';
			}
		}
		alert (str);
*/
//		alert(ifr=='settings__text')
		obj=window[ifr.toString()];
//		alert(obj);
		obj.win.contentWindow.focus();

		if (this.id=="fontsize")
		{
			object=document.createElement("table");
			object.style.cssText="width:100%; height:100%";
			for (i=0;i<obj.fontsizes.length;i++)
			{
				object.insertRow(i);
				object.rows[i].insertCell(0);
				object.rows[i].cells[0].setAttribute("alt",ifr+","+obj.fontsizes[i]);
				object.rows[i].cells[0].style.cssText="text-align:center;font-family:tahoma;";
				font=document.createElement("font");
				object.rows[i].cells[0].appendChild(font);
				font.setAttribute("size",obj.fontsizes[i]);
				if (document.all)
				{
					img=document.createElement("img");
					img.src="/i/tr.gif";
					object.rows[i].cells[0].style.cssText="position:relative; top:0;left:0;text-align:center";
					img.style.cssText="position:absolute; top:0;left:0;width:100%;height:100%";
					object.rows[i].cells[0].appendChild(img);
				}
				
				font.appendChild(document.createTextNode("размер шрифта "+obj.fontsizes[i]));
				object.rows[i].cells[0].onmouseover=function() { this.style.background="#EFE7EF";}
				object.rows[i].cells[0].onmouseout=function() { this.style.background="#E7EFEF";}
				object.rows[i].cells[0].onclick=function() 
				{
					vrs=this.getAttribute("alt").split(",");
					nm=vrs[0].replace(/__/,"::");
					win_id=nm+"_window";
					document.getElementById(nm).contentWindow.document.execCommand("fontsize", false, vrs[1]); 
					document.getElementById(win_id).parentNode.removeChild(document.getElementById(win_id));
					document.getElementById(nm).contentWindow.focus();
					}
			}
			obj.showWindow(230,170,"Размер шрифта",object);
		}
else if (this.id=="fontname")
        {
            object=document.createElement("table");
            object.style.cssText="width:100%; height:100%";
            for (i=0;i<obj.fontnames.length;i++)
            {
                object.insertRow(i);
                object.rows[i].insertCell(0);
                object.rows[i].cells[0].setAttribute("alt",ifr+","+obj.fontnames[i]);
                object.rows[i].cells[0].style.cssText="text-align:center;font-size:14px;";
                font=document.createElement("font");
                font.style.cssText="font-size:16px; font-family:"+obj.fontnames[i]+";";
                object.rows[i].cells[0].appendChild(font);
                font.setAttribute("name",obj.fontnames[i]);
                if (document.all)
                {
                    img=document.createElement("img");
                    img.src="/i/tr.gif";
                    object.rows[i].cells[0].style.cssText="position:relative; top:0;left:0;text-align:center";
                    img.style.cssText="position:absolute; top:0;left:0;width:100%;height:100%";
                    object.rows[i].cells[0].appendChild(img);
                }
               
                font.appendChild(document.createTextNode("стиль шрифта "+obj.fontnames[i]));
                object.rows[i].cells[0].onmouseover=function() { this.style.background="#EFE7EF";}
                object.rows[i].cells[0].onmouseout=function() { this.style.background="#E7EFEF";}
                object.rows[i].cells[0].onclick=function()                 {
                    vrs=this.getAttribute("alt").split(",");
                    nm=vrs[0].replace(/__/,"::");
                    win_id=nm+"_window";
                    document.getElementById(nm).contentWindow.document.execCommand("fontname", false, vrs[1]);
					document.getElementById(win_id).parentNode.removeChild(document.getElementById(win_id));
                    document.getElementById(nm).contentWindow.focus();
                    }
            }
            obj.showWindow(230,170,"Название шрифта",object);
        }



		else if (this.id=='clearhtml')
		{
			if (document.all)
			{
				obj.selection=obj.win.contentWindow.document.selection.createRange();
				obj.insert_value=obj.selection;
				if (obj.win.contentWindow.document.selection.type=="Control") parent_el=obj.selection.item(0);
				else	parent_el=obj.selection.parentElement();
			}
			else
			{
				obj.selection = obj.win.contentWindow.getSelection();
				parent_el=obj.selection.focusNode;
			}
			
			//eval(this.value);
			val_tag=obj.win.contentWindow.document.getElementsByTagName("body");
			str=val_tag[0].innerHTML;
			//alert (str);
			str=str.replace(/<pre>/ig,'');
			str=str.replace(/<\/pre>/ig,'');
			str=str.replace(/<font .*?>/ig,'');
			str=str.replace(/<\/font>/ig,'');
			str=str.replace(/style=\".*?\"/ig,'');
			val_tag[0].innerHTML=str;
			//alert (str);
		}
		else if (this.id=="forecolor")
		{
			object=obj.getColorTable(this.id);
			obj.showWindow(230,170,"Цвет текста",object);
		}
		else if (this.id=="createlink")
		{
			if (document.all)
			{
				obj.selection=obj.win.contentWindow.document.selection.createRange();
				obj.insert_value=obj.selection;
				if (obj.win.contentWindow.document.selection.type=="Control") parent_el=obj.selection.item(0);
				else	parent_el=obj.selection.parentElement();
			}
			else
			{
				obj.selection = obj.win.contentWindow.getSelection();
				parent_el=obj.selection.focusNode;
			}

			obj_link=obj.getParentTag(parent_el,"a");
			obj.edit_link=obj_link;
			object=document.createElement("table");
			object.setAttribute("align","center");
			object.insertRow(0);
			object.rows[0].insertCell(0);
			object.rows[0].cells[0].style.cssText="font-size:12px;font-family:arial;text-align:right";
			object.rows[0].cells[0].appendChild(document.createTextNode("URL: "));
			url_input=document.createElement("input");
			url_input.setAttribute("type","text");
			url_input.setAttribute("id",ifr+"_link_url");
			//alert (ifr+"_link_url");
			url_input.setAttribute("value",(obj_link) ? obj_link.href : "http://");
			url_input.style.cssText="font-family:arial;font-size:12px;width:230";
//			url_input.style.cssText="border:1px solid #000; font-family:arial;font-size:12px;width:230";
			object.rows[0].cells[0].appendChild(url_input);
			object.insertRow(1);
			object.rows[1].insertCell(0);
			object.rows[1].cells[0].setAttribute("align","right");
			but=document.createElement("input");
			but.setAttribute("type","submit");
			but.setAttribute("name",ifr);
			but.setAttribute("value","Вставить");
			but.style.cssText="border:1px solid #eee; font-family:arial;font-size:12px;";
			object.rows[1].cells[0].appendChild(but);
			if (!obj_link && !document.all)
			{
				but.onclick=function() 
				{
                    ifr2=this.name;
					ifr=ifr2.replace(/__/,"::");
					link=document.getElementById(ifr2+"_link_url").value;
					win=document.getElementById(ifr+"_window");
					win.parentNode.removeChild(win);
					ret=document.getElementById(ifr).contentWindow.document.execCommand("createlink", false, link);
				}
			}
			else if (!obj_link && document.all)
			{
				but.onclick=function() 
				{
					ifr2=this.name;
					obj=eval(ifr);
					ifr=ifr2.replace(/__/,"::");
					link=document.getElementById(ifr2+"_link_url").value;
					win=document.getElementById(ifr+"_window");
					win.parentNode.removeChild(win);
					obj.insert_value.pasteHTML("<a href=\""+link+"\">"+obj.insert_value.htmlText+"</a>");
				}
			}
			else if (obj_link)
			{
				but.onclick=function() 
				{
					ifr2=this.name;
					obj=eval(ifr);
					ifr=ifr2.replace(/__/,"::");
					link=document.getElementById(ifr2+"_link_url").value;
					win=document.getElementById(ifr+"_window");
					win.parentNode.removeChild(win);
					obj.edit_link.href=link;
				}
			}
			obj.showWindow(300,300,"Вставка/редактирование ссылки",object);
		}
		else if (this.id=="hilitecolor")
		{
			object=obj.getColorTable(this.id);
			obj.showWindow(230,170,"Цвет фона",object);
		}
		else if (this.id=="insertimage")
		{
		if (document.all)
			{
				obj.selection=obj.win.contentWindow.document.selection.createRange();
			}
			else
			{
				obj.selection = obj.win.contentWindow.getSelection();
			}
			object=obj.getImages(this.id);
			obj.showWindow(320,240,"Вставка картинки",object);
			window.wisiwyg_object=obj;
		}
		else if (this.id=="inserttable")
		{
			if (document.all)
			{
				obj.selection=obj.win.contentWindow.document.selection.createRange();
				if (obj.win.contentWindow.document.selection.type=="Control")
				{
					parent_el=obj.selection.item(0);
					new_string="вставить";
				}
				else
				{
					parent_el=obj.selection.parentElement();
					new_string="<a href='javascript:void(0); onClick="+ifr+".setNewTable(\""+ifr+"\");'>вставить</a>";
				}
			}
			else
			{
				obj.selection = obj.win.contentWindow.getSelection();
				parent_el=obj.selection.focusNode;
				new_string="<a href='javascript:void(0); onClick="+ifr+".setNewTable(\""+ifr+"\");'>вставить</a>";
			}
			obj.tb=obj.getParentTag(parent_el,"table");
			if (!new_string)
			{
				new_string="<a href='javascript:void(0); onClick="+ifr+".setNewTable(\""+ifr+"\");'>вставить</a>";
			}
			if (obj.tb)
			{
				ed_tb_cols=obj.tb.rows[0].cells.length;
				ed_tb_rows=obj.tb.rows.length;
				edit_string="<a href='javascript:void(0); onClick="+ifr+".setEditTable(\""+ifr+"\","+ed_tb_cols+","+ed_tb_rows+");'>редактировать</a>";
			}
			else
			{
				tb_cols=2;
				tb_rows=2;
				edit_string="редактировать";
			}
			object=document.createElement("div");
			object.style.cssText="width:100%; height:100%";
			cols_id=ifr+"_cols";
			rows_id=ifr+"_rows";
			object.innerHTML="<table width=100% height=100% cellpadding=0 cellspacing=0 style=\"margin:3px\">"
			+"<tr><td class=win_page_open id="+ifr+"_newtable>"+new_string+"</td><td class=win_page_close  id="+ifr+"_edittable>"+edit_string+"</td><td width=90% class=win_page_block>&nbsp;</td></tr>"
			+"<tr><td colspan=3 class=win_page_main><table width=100% cellpadding=0 cellspacing=2></td></tr>"
			+"<tr><td align=right width=50% class=win_string>Колонок:</td><td><input type=text class=win_text style=\"width:20px\" id="+ifr+"_cols value="+tb_cols+"></td></tr>"
			+"<tr><td align=right class=win_string>Строк:</td><td><input type=text class=win_text style=\"width:20px\" id="+ifr+"_rows value="+tb_rows+"></td></tr>"
			+"<tr><td align=center colspan=2>"
			+"<input type=submit class=win_button_v value=\"Применить\" id="+ifr+"_submit_new"+" onClick="+ifr+".insertTable('"+ifr+"');>"
			+"<input type=submit class=win_button_h value=\"Применить\" id="+ifr+"_submit_edit"+" onClick="+ifr+".updateTable('"+ifr+"');>"
			+"</td></tr></table>"
			+"</td></tr></table>";
			obj.showWindow(230,200,"Вставить/редактировать таблицу",object);
			if (obj.tb)
			{
				ed_tb_cols=obj.tb.rows[0].cells.length;
				ed_tb_rows=obj.tb.rows.length;
				edit_string="<a href='javascript:void(0); onClick="+ifr+".setEditTable(\""+ifr+"\","+ed_tb_cols+","+ed_tb_rows+");'>редактировать</a>";
				obj.setEditTable(ifr,ed_tb_cols,ed_tb_rows);
			}
			else
			{
				tb_cols=2;
				tb_rows=2;
				edit_string="редактировать";
				obj.setNewTable(ifr);
			}

		}
		else {
			if(this.id.indexOf('insertspan') >= 0){
			//это наша кнопка
				object=eval(ifr);
//				if (document.all) object.selection.select();
				div=document.createElement("span");
				table=document.createElement("input");
				table.setAttribute("type", "text");
				table.disabled=true;
				tid=this.id;
				spanval = tid.match(/\d+/);
				table.className='value'+spanval;
				div.appendChild(table);
//				alert(ifr);
				ifr=ifr.replace(/__/,"::");
				iframe=document.getElementById(ifr);
				object.insertNodeAtSelection(iframe.contentWindow,table,div);
			}
			else
			{
				ifr=ifr.replace(/__/,"::");
				ret=document.getElementById(ifr).contentWindow.document.execCommand(this.id, false, null);
			}
		}
	}

	this.setEditTable=function(ifr,cols,rows)
	{
	edTab=document.getElementById(ifr+"_edittable");
	newTab=document.getElementById(ifr+"_newtable");
	subm_new=document.getElementById(ifr+"_submit_new");
	subm_edit=document.getElementById(ifr+"_submit_edit");
	subm_new.className="win_button_h";
	subm_edit.className="win_button_v";
	this.tb_mode=1;
	edTab.className="win_page_open";
	newTab.className="win_page_close";
	cols_id=document.getElementById(ifr+"_cols");
	rows_id=document.getElementById(ifr+"_rows");
	cols_id.value=cols;
	rows_id.value=rows;
	}

	this.setNewTable=function(ifr)
	{
	this.tb_mode=0;
	edTab=document.getElementById(ifr+"_edittable");
	newTab=document.getElementById(ifr+"_newtable");
	subm_new=document.getElementById(ifr+"_submit_new");
	subm_edit=document.getElementById(ifr+"_submit_edit");
	subm_new.className="win_button_v";
	subm_edit.className="win_button_h";
	edTab.className="win_page_close";
	newTab.className="win_page_open";
	cols_id=document.getElementById(ifr+"_cols");
	rows_id=document.getElementById(ifr+"_rows");
	cols_id.value="2";
	rows_id.value="2";
	}

	this.getParentTag=function(tag,tagName)
	{
		if (tag)
			if (tag.nodeName.toUpperCase()==tagName.toUpperCase()) return tag;
			else return this.getParentTag(tag.parentNode,tagName);
	}

	this.getImages=function(action)
	{
	  top.window.wisiwyg_object=this;
	  if (top.window.image_id)
	  {
		current_id=top.window.image_id;
	  }
	  else
	  {
		current_id=0;
	  }
      if (top.window.interface_type)
	  {
		interface_type=top.window.interface_type;
	  }
	  else
	  {
		interface_type='';
	  }
	  object=document.createElement("div");
	  object.innerHTML="<iframe name='set_image' src='/editor/imageinsert.php?id="+current_id+"&interface="+interface_type+"' width='100%' height='220' frameborder=0 scrolling='no'></iframe>";
	  return object;
	}
	
	this.getColorTable=function(action)
	{
		object=document.createElement("table");
		object.style.cssText="width:100%; height:100%;position:relative;top:0;left:0";
		object.insertRow(0);
		object.rows[0].insertCell(0);
		object.insertRow(1);
		object.rows[1].insertCell(0);
		div=document.createElement("div");
		object.rows[0].cells[0].style.cssText="height:90%;text-align:center";
		object.rows[0].cells[0].appendChild(div);
		div.style.cssText="width:100%; height:100%;position:relative;top:0;left:4";

		var sz, cPick = new Array("00","33","66","99","CC","FF"), iCnt=2
		var iColors = cPick.length, szColor = ""

		dx=7;
		dy=16;
		for (var r=0;r<iColors;r++)
		{
			for (var g=iColors-1;g>=0;g--)
				for (var b=iColors-1;b>=0;b--)
				{
					indx=(iColors-1)*(iColors-1)-(g-1)*(iColors-1)-b;
					szColor = cPick[r]+cPick[g]+cPick[b] 
					img=document.createElement("div");
					img.style.cssText="width:"+dx+";height:"+dy+";background-color:#"+szColor+"; position:absolute; top:"+(r*dy)+"; left:"+(indx*dx)+";";
					div.appendChild(img);
					if (document.all)
					{
						img_tr=document.createElement("img");
						img_tr.src="/i/tr.gif";
						img_tr.style.cssText="position:absolute; top:0;left:0;width:100%;height:100%";
						img.appendChild(img_tr);
					}
					img.setAttribute("alt",ifr+","+szColor);
					img.onmouseover=function()
					{
						vrs=this.getAttribute("alt").split(",");
						document.getElementById(vrs[0]+"_color").style.background="#"+vrs[1];
					}
					img.onclick=function() 
					{
						vrs=this.getAttribute("alt").split(",");
						nm=vrs[0].replace(/__/,"::");
						win_id=nm+"_window";
						action =(document.all && action=="hilitecolor") ? "backcolor" : action;
						document.getElementById(nm).contentWindow.document.execCommand(action, false, vrs[1]); 
						document.getElementById(win_id).parentNode.removeChild(document.getElementById(win_id));
						document.getElementById(nm).contentWindow.focus();
					}
				}
		}
		object.rows[1].cells[0].style.cssText="text-align:center";
		div2=document.createElement("div");
		div2.setAttribute("id",ifr+"_color");
		div2.style.cssText="border:1px solid #eee; width:100%;height:40";
		object.rows[1].cells[0].appendChild(div2);
		return object;
	}

	this.insertTable=function(ifr)
	{
		cols_id=ifr+"_cols";
		rows_id=ifr+"_rows";
		cols=document.getElementById(cols_id).value;
		rows=document.getElementById(rows_id).value;
		object=eval(ifr);
		if (document.all) object.selection.select();
		div=document.createElement("div");
		table=document.createElement("table");
		div.appendChild(table);
//		table.setAttribute("border","1");
		table.className='editortable';
		for (i=0;i<rows;i++)
		{
			table.insertRow(i);
			for (j=0;j<cols;j++)
			{
			table.rows[i].insertCell(j);
			table.rows[i].cells[j].innerHTML="&nbsp;";
			table.rows[i].cells[j].style.width="70px";
			}
		}
		ifr=ifr.replace(/__/,"::");
		iframe=document.getElementById(ifr);
		object.insertNodeAtSelection(iframe.contentWindow,table,div);
		win=document.getElementById(ifr+"_window");
		win.parentNode.removeChild(win);
	}

	this.updateTable=function(ifr)
	{
		cols_id=ifr+"_cols";
		rows_id=ifr+"_rows";
		cols=document.getElementById(cols_id).value;
		rows=document.getElementById(rows_id).value;
		tb_cols=this.tb.rows[0].cells.length;
		tb_rows=this.tb.rows.length;
		max_cols=(tb_cols>cols) ? tb_cols : cols;
		max_rows=(tb_rows>rows) ? tb_rows : rows;
		for (i=0;i<max_rows;i++)
		{
			if (!this.tb.rows[i] && i<rows) { this.tb.insertRow(i); }
			if (this.tb.rows[i])
			{
				for (j=0;j<max_cols;j++)
				{
					if (!this.tb.rows[i].cells[j] && j<cols)
					{
						this.tb.rows[i].insertCell(j);
						this.tb.rows[i].cells[j].innerHTML="&nbsp;";
					}
					if (this.tb.rows[i].cells[this.tb.rows[i].cells.length-1] && j>=cols)
					{
						this.tb.rows[i].deleteCell(this.tb.rows[i].cells.length-1);
					}
				}
			}
			if (this.tb.rows[this.tb.rows.length-1] && i>=rows) this.tb.deleteRow(this.tb.rows.length-1);
		}
		win=document.getElementById(ifr+"_window");
		win.parentNode.removeChild(win);
	}

	this.insertNodeAtSelection=function (win, insertNode,insertParent)
	{
		if (document.all)
		{
			sel = win.document.selection.createRange()
			sel.pasteHTML(insertParent.innerHTML);
		}
		else
		{
			var sel = win.getSelection();
			var range = sel.getRangeAt(0);
			sel.removeAllRanges();
			range.deleteContents();
			var container = range.startContainer;
			var pos = range.startOffset;
			range=document.createRange();
			if (container.nodeType==3 && insertNode.nodeType==3)
			{
				container.insertData(pos, insertNode.nodeValue);
				range.setEnd(container, pos+insertNode.length);
				range.setStart(container, pos+insertNode.length);
			}
			else
			{
				var afterNode;
				if (container.nodeType==3)
				{
					var textNode = container;
					container = textNode.parentNode;
					var text = textNode.nodeValue;
					var textBefore = text.substr(0,pos);
					var textAfter = text.substr(pos);
					var beforeNode = document.createTextNode(textBefore);
					var afterNode = document.createTextNode(textAfter);
					container.insertBefore(afterNode, textNode);
					container.insertBefore(insertNode, afterNode);
					container.insertBefore(beforeNode, insertNode);
					container.removeChild(textNode);
				} 
				else
				{
					afterNode = container.childNodes[pos];
					container.insertBefore(insertNode, afterNode);
				}
				range.setEnd(afterNode, 0);
				range.setStart(afterNode, 0);
			}
		  sel.addRange(range);
		}
	}

	this.showWindow=function(width,height,header,object)
	{
		x=this.width/2-width/2;
		y=this.height/2-height/2;
		window.status="x="+x+" y="+y+"this.left:"+this.left;
		//y=(y>=this.top) ? y : this.top;
		wnd=document.createElement("div");
		wnd.setAttribute("id",this.win_name+"_window");
		//wnd.onmousemove=this.drag;
		wnd.style.cssText="position:absolute; top:"+y+"; left:"+x+"; width:"+width+"; height:"+height+"; border:1px solid #eee; z-index:2;";
		this.root_win.appendChild(wnd);
		//document.body.appendChild(wnd);
		wnd.innerHTML="<table width=100% height=100% cellpadding=0 cellspacing=0></table>";
		tb=wnd.firstChild;
		tb.insertRow(0);
		tb.rows[0].className="win_header";
		tb.rows[0].insertCell(0);
		tb2=document.createElement("table");
		tb2.className="win_header";
		tb2.setAttribute("alt",this.win_name+"_window");
		tb.rows[0].cells[0].appendChild(tb2);
		tb2.insertRow(0);
		tb2.rows[0].insertCell(0);
		tb2.rows[0].cells[0].appendChild(document.createTextNode(header));
		tb2.rows[0].cells[0].style.cssText="width:95%";
		tb2.rows[0].insertCell(1);
		tb2.rows[0].cells[1].style.cssText="width:15;padding-right:4px";
		tb.rows[0].cells[0].onmousedown=this.dragStart;
		div_close=document.createElement("div");
		im_close=document.createElement("img");
		im_close.src="/i/bt_close.gif";
		im_close.alt=this.win_name+"_window";
		div_close.appendChild(im_close);
		div_close.style.cssText="background-image: url(/i/btc_l.gif)";
		div_close.onmouseover=function() { this.style.cssText="background-image: url(wisiwyg/btc_d.gif)";}
		div_close.onmouseout=function() { this.style.cssText="background-image: url(wisiwyg/btc_l.gif)";}
		im_close.onclick=function() { document.getElementById(this.alt).parentNode.removeChild(document.getElementById(this.alt));}
		tb2.rows[0].cells[1].appendChild(div_close);
		tb.insertRow(1);
		tb.rows[1].insertCell(0);
		tb.rows[1].cells[0].className="win_body";
		dv=document.createElement("div");
		dv.style.cssText="overflow:hidden;width:100%;height:"+(height-20);
		tb.rows[1].cells[0].appendChild(dv);
		dv.appendChild(object);
	}
	
	
	this.dragStart=function(event){
		//obj=this;
		dragObj.elNode=getParentTag(this,"div");
		var el;
		var x, y;
		if (browserr.isIE) {
			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop  + document.body.scrollTop;
		}
		if (browserr.isNS) {
			x = event.clientX + window.scrollX;
			y = event.clientY + window.scrollY;
		}

		dragObj.cursorStartX = x;
		dragObj.cursorStartY = y;
		dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
		dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

		if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
		if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

		dragObj.elNode.style.zIndex = ++dragObj.zIndex;
  
		if (browserr.isIE) {
			document.attachEvent("onmousemove", dragGo);
			document.attachEvent("onmouseup",   dragStop);
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (browserr.isNS) {
			document.addEventListener("mousemove", dragGo,   true);
			document.addEventListener("mouseup",   dragStop, true);
			event.preventDefault();
		}
	}

	function dragGo(event) {
		dr = 0;
		var x, y;
		if (browserr.isIE) {
			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		}
		if (browserr.isNS) {
			x = event.clientX + window.scrollX;
			y = event.clientY + window.scrollY;
		}
		dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
		dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
		if (browserr.isIE) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (browserr.isNS) event.preventDefault();
	}

	function dragStop(event) {
		if (browserr.isIE) {
			document.detachEvent("onmousemove", dragGo);
			document.detachEvent("onmouseup",   dragStop);
		}
		if (browserr.isNS) {
		    document.removeEventListener("mousemove", dragGo,   true);
		    document.removeEventListener("mouseup",   dragStop, true);
		}
	}
	
	
	
	this.getCoordsObj=function(obj,c)
	{
		c[0]+=obj.offsetLeft;
		c[1]+=obj.offsetTop;
		if (obj.offsetParent) 
			return this.getCoordsObj(obj.offsetParent,c);
		else
			return c;
	}

	if (window.opera){
		// Gecko (Opera 9 tries to behave like Gecko at this point).
		if (navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) ){
			this.init();
		}
		// Opera 9.50+
		else if ( window.opera && window.opera.version && parseFloat( window.opera.version() ) >= 9.02 ){
			this.init();		
		}
		else{
			this.opera();
		}
	}
	else
	{
		this.init();
	}
}

this.getParentTag=function(tag,tagName)
{
  if (tag)
	if (tag.nodeName.toUpperCase()==tagName.toUpperCase()) return tag;
	else return getParentTag(tag.parentNode,tagName);
}


function show_info(obj)
{
str="";
wn=window.open("about:blank");
for (val in obj)
	{
	str+=val+": "+obj[val]+"<hr>";
	}
wn.document.write(str);
wn.document.close();
}

