var icons=new Array("exp","q","#","code","b","i","u","strike","color","size","s","list","img","hr","c","url","smilies","sub","sup","table","no","more","email","chess","help","words"),tags_w=21,ipath2=ipath
var descriptions={"exp":"Фиксация панели","q":"Цитата","#":"Текста, заключенного в эти коды, не будет видно в сообщении","code":"Программный код","b":"Жирный текст","i":"Наклонный текст","s":"Мелкий шрифт","u":"Подчеркнутый текст","strike":"Зачеркнутый текст","c":"Текст по центру","sub":"Нижний регистр","sup":"Верхний регистр","size":"Размер шрифта","color":"Цвет шрифта","no":"Код, который отменяет все коды","url":"Ссылка","email":"Email адрес","img":"Картинка","smilies":"Смайлики","list":"Цифровой или алфавитный cписок","table":"Таблица","more":"Скрывает текст и генерирует линк для доступа к нему","hr":"Горизонтальная линия","chess":"Шахматная комбинация (полная функциональность только в разделе \"Шахматы\")","words":"Наиболее употребляемый список фраз для сообщения модератору (работает только в ПМ)","help":"Список кодов с примерами (в новом окне)"}
var tmp=getById('put_tag_panel')
if(tmp)tmp.innerHTML='<a href="javascript:void(0)" id="tpanel_link" onmouseover="over_panel_els=true;setTimeout(show_tags_panel,500)" onmouseout="over_panel_els=false;setTimeout(hide_tags_panel,500)"><img src="'+ipath2+'/insert_tags/icon.gif" border=0 title="Панель тэгов"></a>'
document.write("<div id='tags_panel' align='right' style='z-index:1000;display:none;position:absolute;background:white;border:#828282 1px solid;"+((document.all&&!window.opera&&!document.createTextRange)?"filter: alpha(opacity=75);":(!window.opera?"-moz-opacity: 0.75;":"opacity: 0.75;"))+"width:"+(6*23+2)+"px' onmouseout='over_panel_els=false;setTimeout(hide_tags_panel,500)' onmouseover='over_panel_els=true'>")
document.write(draw_buttons(6))
document.write('</div>')
if(in_location('messanger.cgi')&&in_location('action=moderpm'))document.write('<br><small><b>Список типичных нарушений:</b>&nbsp;&nbsp;</small><br><iframe style="width:280px;height:160px;border-style:none;overflow:hidden" src="'+ipath2+'/insert_tags/words.htm'+'" scrolling="none" frameborder="0"></iframe>')
if(Get_Cookie('fix_tg_pan'))
{if(document.addEventListener)document.addEventListener("DOMContentLoaded",function(e){fix_tags_panel(true)},false)
 else document.onreadystatechange=function(){if(document.readyState=="complete")fix_tags_panel(true)}}
function draw_buttons(width)
{var out='',count=0
 for(var key=0;key<icons.length;key++)
 {if((icons[key]=='chess'&&!in_location('forum=43'))||(icons[key]=='words'&&!in_location('messanger.cgi')))continue;else out+="<img onmouseover='mover_style(this)' onmousedown='mborder_style(this,2)' onmouseup='mover_style(this)' style='background-image:url(\""+ipath2+"/insert_tags/tags.gif\");background-position: "+((icons.length-key)*tags_w)+" 0; border:solid 1px white;height:21px;width:21px;cursor:pointer' onmouseout='mborder_style(this,0)' onclick='insert_tag(this,\""+icons[key]+"\")' title='"+(descriptions[icons[key]]?descriptions[icons[key]]:"")+"' src='"+ipath2+"/insert_tags/blank.gif'>"
 if(++count%width==0)out+="<br>"}
 return out
}
function in_location(str)
{return window.location.href.indexOf(str)==-1?false:true}
function show_tags_panel()
{if(!over_panel_els)return
var pan=getById('tags_panel'),obj=getById('tpanel_link')
if(pan&&!getById('my_prompt')){var x=getposOffset(obj,'left'),y=getposOffset(obj,'top');pan.style.top=y-30+'px';pan.style.left=x-80+'px';pan.style.display='block'}}
function hide_tags_panel()
{var pan=getById('tags_panel');if(pan&&!over_panel_els)pan.style.display='none'}
var text_tags='',over_panel_els=false,hold_iframe=false
function mover_style(obj)
{over_panel_els=true;el_has_focus=(el_has_focus?el_has_focus:attach2textarea());copy_text();mborder_style(obj,1)}
function mborder_style(obj,st)
{var thk='1px solid ',brd1=thk+'buttonhighlight',brd2=thk+'buttonshadow'
 if(st==0)brd1=brd2=thk+'white';if(st==2){var tmp=brd1;brd1=brd2;brd2=tmp}
 obj.style.borderTop=obj.style.borderLeft=brd1;obj.style.borderBottom=obj.style.borderRight=brd2}
function insert_tag(obj,tag,value,fin)
{var intag='',x=getposOffset(obj,'left'),y=getposOffset(obj,'top') 
 switch(tag)
 {
 case'help':window.open("http://i.ru-board.com/codes.html");break
 case'img':if(!value)ask_for_data(tag,"Введите ссылку на картинку\n(Свою можно залить, к примеру, <small><a href='http://www.imageshack.us' target=_blank>сюда</a></small>)","http://",x,y,2);else intag=value;break
 case'table':if(!value)show_iframe(obj,ipath2+'/insert_tags/make_table.htm',500,430,x,y);else intag='[table]'+value+'[/table]';break
 case'color':if(!value)show_iframe(obj,ipath2+'/insert_tags/color_picker.htm',400,200,x,y);else intag='[color='+value+']'+text_tags+'[/color]';break
 case'more':if(!value)ask_for_data(tag,"Введите название для скрываемого куска текста\nМожно оставить пустым","",x,y);else intag=value;break
 case'list':if(!value)show_iframe(obj,ipath2+'/insert_tags/list.htm',500,450,x,y);else intag=value;break
 case'email':if(!value)ask_for_data(tag,"Введите email","user@host.ru",x,y,3);else intag='[img]mailpng.cgi?word='+value+'[/img]';break
 case'size':if(!value)ask_for_data(tag,"Выберите размер шрифта","",x,y);else intag=value;break  
 case'words':if(!value)show_iframe(obj,ipath2+'/insert_tags/words.htm',300,150,x,y);else intag=value;break;
 case'url':if(!value)ask_for_data(tag,"Ввведите url","http://",x,y,3);else {if(text_tags=='')text_tags='Ссылка';if(value!='')intag='[url='+value+']'+text_tags+'[/url]'}break
 case'chess':if(!value)ask_for_data(tag,"Введите шахматную комбинацию","W:Ke1,Qd1,Ra1,h1,Nb1,g1,Bc1,f1,Pa2,b2,c2,d2,e2,f2,g2,h2/B:Ke8,Qd8,Ra8,h8,Nb8,g8,Bc8,f8,Pa7,b7,c7,d7,e7,f7,g7,h7",x,y,2);else intag=value;break
 case'smilies':show_iframe(obj,ipath2+'/insert_tags/smilies.htm',450,370,x,y);break
 case'hr':intag='[hr]';break
 case'return':intag=value;break
 case'exp':fix_tags_panel(false);break
 default: intag="["+tag+"]"+text_tags+"[/"+tag+"]";break  
 }
 if(!el_has_focus)el_has_focus=attach2textarea()
 if(intag!=''&&el_has_focus) {insertAtCaret(el_has_focus,intag);text_tags='';if(document.all)document.selection.empty()}
}
function fix_tags_panel(state)
{var tas=document.getElementsByTagName('textarea'),pan=document.createElement('div')
 pan.id='fix_tg_pan';pan.onmouseover=function(){over_panel_els=false}
 if(tas&&tas.length>0){if(!getById('fix_tg_pan')||state){Set_Cookie('fix_tg_pan',1,365);tas=tas[tas.length-1];pan.innerHTML=draw_buttons(99);tas.parentNode.insertBefore(pan,tas)}
 else{Set_Cookie('fix_tg_pan',0,-1);tas=tas[tas.length-1];tas.parentNode.removeChild(getById('fix_tg_pan'))}
 }}
function show_iframe(obj,url,width,height,x,y) 
{var ifr=document.createElement('iframe')
 ifr.src=url 
 ifr.id='hid_iframe'
 ifr.frameBorder="no"
 set_style(ifr,'z-index:1001;position:absolute;display:none;background:white;border: #828282 solid 1px;display:none') 
 ifr.style.left=((x>width/2)?x-parseInt(width/2):10)+'px';ifr.style.top=((y>height/2)?y-parseInt(height/2):10)+'px'
 ifr.style.width=width+'px';ifr.style.height=height+'px'                                     
 document.body.appendChild(ifr)
 ifr.style.display='block' 
 setTimeout(function(){document.body.onmouseover=function(evt){if(!hold_iframe&&who_fired_event(evt)!=ifr){delete_iframe();hide_tags_panel();document.body.onmouseover=''}}},500)  
}
function attach2textarea()
{var els=document.getElementsByTagName('textarea')
if(els)
 {
  for(var i=0;i<els.length;i++)
  {els[i].onkeyup=function(evt){setCaret(this)}
   els[i].onclick=function(evt){setCaret(this)}
   els[i].onselect=function(evt){setCaret(this)}
   }
//IE & Opera bug fix
  var el=els[els.length-1]
  if(document.selection&&textar_sel)
  {var range=document.selection.createRange()
   range.moveStart('character',-range.text.length)
   setCaret(el)}
  if(el.createTextRange)
   {var r=el.createTextRange();r.collapse(false);r.select()}
  return el
 }
return false
}
function ask_for_data(tag,txt,value,x,y,type)
{
var div=document.createElement('div')
div.id='my_prompt'
set_style(div,'padding:10px;z-index:1001;position:absolute;background:#dddddd;text-align:center;border:#828282 solid 2px;visibility:hidden')
div.innerHTML="<b><pre>"+txt+"</pre></b>"
if(tag=='size'){for(var i=1;i<8;i++) div.innerHTML+='<font style="cursor:pointer" size='+i+' onclick="retvalue(this,\''+tag+'\','+i+');document.body.removeChild(this.parentNode)">'+i+'</font>&nbsp;';div.innerHTML+="<br>"}
else{div.innerHTML+="<input type=text size=30 id='prompt_data' value='"+(value?value:'')+"'><br><input type=button class=button value='Дальше' onclick='retvalue(this,\""+tag+"\""+(value?",\""+value+"\"":"")+(type?",\""+type+"\"":"")+");document.body.removeChild(this.parentNode)'>&nbsp;&nbsp;&nbsp;&nbsp;"}
div.innerHTML+="<input type=button class=button value='Отмена' onclick='document.body.removeChild(this.parentNode)'>"
document.body.appendChild(div)
div.style.left=x-div.clientWidth/2+'px';div.style.top=y-div.clientHeight/2+'px'
div.style.visibility='visible'
if(getById('prompt_data'))getById('prompt_data').select()
}
function retvalue(obj,tag,def,type)
{var dat=getById('prompt_data')?getById('prompt_data').value:def
 if(type==3&&dat){insert_tag(obj,tag,dat);return}
 if(type==2&&dat){insert_tag(obj,tag,'['+tag+']'+dat+'[/'+tag+']');return}
 if(dat||(!def&&dat==''))insert_tag(obj,tag,'['+tag+(def||dat?'='+dat:'')+']'+text_tags+'[/'+tag+']')
}
function delete_iframe()
{var tmp=getById('hid_iframe');if(tmp)document.body.removeChild(tmp);hold_iframe=false;over_panel_els=false}
function copy_text()
{if(!el_has_focus)return
 if(document.all){text_tags=document.selection.createRange().text;document.selection.createRange().collapse()}
 else if(document.getElementById)
 {var selLength=el_has_focus.textLength
  var selStart=el_has_focus.selectionStart
  var selEnd=el_has_focus.selectionEnd
  if (selEnd==1||selEnd==2)selEnd=selLength
  text_tags=(el_has_focus.value).substring(selStart,selEnd)
 }
}
function getById(id)
{return document.getElementById(id)}
function set_style(obj,style)
{if(typeof obj.style.cssText=='string')obj.style.cssText=style;else obj.setAttribute('style',style)}
//IE bug fix
var textar_sel=false
if(document.all&&typeof myEvent=='function') myEvent(document,'selectstart',tags_where,"+")
function tags_where(e)
{e=get_e(e);var el=who_fired_event(e);textar_sel=(el.tagName=='TEXTAREA')?true:false}
