function getCookie(Name)
{
 var p = Name + "=";
 var si = document.cookie.indexOf(p);
 if (si == -1) return null;
 var ei = document.cookie.indexOf(";", si + p.length);
 if (ei == -1) ei = document.cookie.length;
 return unescape(document.cookie.substring(si + p.length, ei));
}

function mousePageXY(e)
{
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}

var uint = {};

function nick_over_do(userid, x, y)
{
	if($defined( $('usernick-'+userid) ))
	{
		var ue = $('usernick-'+userid);
		ue.effect('opacity').start(1);
		ue.style.display = 'block';
	} else {
		var ue = new Element('div', {'id': 'usernick-'+userid,
			'class': 'usernick-div'
		})
		$('content-zone').adopt(ue);
		ue.makeDraggable();
		ue.setHTML('<img src="http://i.mirari.ws/img/loading-tiny.gif" alt="Загрузка"/>');
		
		ue.addEvent('mouseleave', function(){ $('usernick-'+userid).style.display='none'; });
		
		new Ajax('/xscript/ajax/usernick', {'data': {'user': userid}, 'update': ue}).request();
	}
		
	ue.style.left = x-30;
	ue.style.top = y+2;
}

function nick_over(userid)
{
	var coord = mousePageXY();
	uint[userid] = setTimeout('nick_over_do('+userid+', '+coord["x"]+', '+coord["y"]+')', 1300);
}

function nick_out(userid)
{
	$clear(uint[userid]);
}

function attachment_insert(disable_att)
{
	if(!disable_att){
	document.write("<a href=\"javascript:void(0)\" onClick=\"javascript:window.open('/xmod/lc-attachments/new'+lc_sessid('?'),'','menubar=0,scrollbars=1,status=0,width=350,height=200'); return false\">Добавить вложение</a>");
		document.write(" &nbsp; ");
	}
	document.write("<a href=\"javascript:void(0)\" title=\"Наклонный текст\" onclick=\"javascript:format_sel('i')\">[ <i>i</i> ]</a>");
		document.write(" &nbsp; ");
	document.write("<a href=\"javascript:void(0)\" title=\"Полужирный текст\" onclick=\"javascript:format_sel('b')\">[ <b>b</b> ]</a>");
		document.write(" &nbsp; ");
	document.write("<a href=\"javascript:void(0)\" title=\"Зачёркнутый текст\" onclick=\"javascript:format_sel('s')\">[ <s>s</s> ]</a>");
		document.write(" &nbsp; ");
	document.write("<a href=\"javascript:void(0)\" title=\"Подчёркнутый текст\" onclick=\"javascript:format_sel('u')\">[ <u>u</u> ]</a>");
		document.write(" &nbsp; ");
	document.write("<a href=\"javascript:void(0)\" title=\"Осветлённый текст\" onclick=\"javascript:format_sel('ot')\">[ ot ]</a>");
		document.write(" &nbsp; ");
	document.write("<a href=\"javascript:void(0)\" title=\"Ссылка\" onclick=\"javascript:insert_link()\">[ url ]</a>");
		document.write(" &nbsp; ");
	document.write("<a href=\"javascript:void(0)\" title=\"По левому краю\" onclick=\"javascript:format_sel('align-left',1)\">[left]</a>");
		document.write(" &nbsp; ");
	document.write("<a href=\"javascript:void(0)\" title=\"По центру\" onclick=\"javascript:format_sel('align-center',1)\">[center]</a>");
		document.write(" &nbsp; ");
	document.write("<a href=\"javascript:void(0)\" title=\"По правому краю\" onclick=\"javascript:format_sel('align-right',1)\">[right]</a>");
		document.write(" &nbsp; ");
	document.write("<a href=\"javascript:void(0)\" title=\"Подзаголовок\" onclick=\"javascript:format_sel('c')\">[подзаголовок]</a>");
	
}

function subscr_window()
{
  	window.open('/xmod/lc-user-subscribe/list'+lc_sessid("?"),'','menubar=0,scrollbars=1,status=0,width=350,height=500');
  	return false;
}

function format_sel(tagName, nL) {
  var str = document.selection.createRange().text;
  if(!str) return;
  document.all.attachment_insert.focus();
  var sel = document.selection.createRange();
  if(str.indexOf("\n")>0) nL=true;
  newLine = nL?"\n":"";
  sel.text = newLine + "[" + tagName + "]" + newLine + str + newLine + "[/" + tagName + "]" + newLine;
  return;
}

function insert_link() {
  var str = document.selection.createRange().text;
  document.all.attachment_insert.focus();
  var my_link = prompt("Введите адрес:");
  if (my_link != null) {
    var sel = document.selection.createRange();
	sel.text = "[url href=\"" + my_link + "\"]" + str + "[/url]";
  }
  return;
}

var Key;

function listen_ctrl_13()
{
	switch(navigator.appName) {
		case "Microsoft Internet Explorer": case "Opera":
			document.onkeydown = press_ctrl_13;
			Key = "event.ctrlKey && event.keyCode == 13";
		break;
		case "Netscape":
			document.captureEvents(Event.KEYDOWN);
			document.onkeydown = press_ctrl_13;
			Key = "(e.modifiers == 2 && e.which == 10) || (e.ctrlKey && e.which == 13)";
		break;
	}
}

function press_ctrl_13(e)
{
	if(eval(Key))
	{
	  var str = document.selection.createRange().text;
	  var nL = false;
	  if(str.indexOf("\n")>0) nL=true;
	  newLine = nL?"\n":"";
	  document.all.attachment_insert.value = document.all.attachment_insert.value + newLine + "[quote]" + newLine + str + newLine + "[/quote]\n";
	}
}

function persone_search(q)
{
  	window.open('/xscript/users/search?q='+q+lc_sessid("&"),'','menubar=0,scrollbars=1,status=0,width=300,height=400');
  	return false;
}

var hm_now_showing = 0;

function hm_show(sub_id)
{
	if(hm_now_showing) eval("document.all."+hm_now_showing+".className = 'hm-main'");
	eval("document.all."+sub_id+".className = 'hm-main-hover'");
	eval("document.all.hm_sub.innerHTML = document.all."+sub_id+"_sub.innerHTML");
	hm_now_showing = sub_id;
}

function ajax_online_request()
{
  if(!$defined(ajax_online)) var ajax_online = new Ajax('/xscript/ajax/online', {update: $('on_the_site')});
  ajax_online.request();
  setTimeout('ajax_online_request()', 1000*80);
}

var msgs_timeout;
var chat_loader = '<center><img src="http://i.mirari.ws/img/loading.gif" alt="Loading"/></center>';
function ajax_chat_msgs_request()
{
  if(!$defined(ajax_chat_msgs)) var ajax_chat_msgs = new Ajax('/xscript/ajax/chat/msgs', {update: $('mr_chat')});
  if($('mr_chat').innerHTML.indexOf('/loading.gif') >0) return;  
  $('mr_chat').setHTML(chat_loader + $('mr_chat').innerHTML);
  clearTimeout(msgs_timeout);
  msgs_timeout = setTimeout('ajax_chat_msgs_request()', 1000*40);
  ajax_chat_msgs.request();
}
        
function ajax_chat_msg_new()
{
   var msg = prompt('Введите текст сообщения:','');
   if(!$defined(msg) || !msg) return;
   var ajax_chat_new = new Ajax('/xscript/ajax/chat/new'+lc_sessid('?'), {evalResponse: true, data: {chatmsg: msg}, method: 'get'});
   ajax_chat_new.request();
}
       
function ajax_updates_check()
{
    var ajax_upd = new Ajax('/xscript/ajax/updates'+lc_sessid('?'), {evalResponse: true});
    ajax_upd.request();
  	setTimeout('ajax_updates_check()', 1000*100);
}