var comment_form_now=-1;
var host='/socium/';
var monitor_updater;

function rate_topic(id, rating, preview)
{
	loadXMLDoc("action=rate_topic&id="+id+"&rating="+rating+"&preview="+preview);
}

function rate_comment(id, rating)
{
	loadXMLDoc("action=rate_comment&id="+id+"&rating="+rating);
}

function rate_user(id, rating)
{
	loadXMLDoc("action=rate_user&id="+id+"&rating="+rating);
}

function comment_form(id)
{
	if (comment_form_now==id)
	{
		return '';
	}

	if (document.getElementById("comment_reply_"+id)==null)
	{
		return false;
	}

	if (comment_form_now>=0)
	{
		document.getElementById("comment_reply_"+comment_form_now).innerHTML='';
	}

	comment_form_now=id;
	comment_form_now_obj=document.getElementById("comment_reply_"+id);
	comment_form_now_obj.innerHTML='<div class="comment_item"><textarea class="comment_form" id="comment_form"></textarea><br/><input type="hidden" id="comment_parent_id" value="'+comment_form_now+'"><input type="submit" id="comment_submit" onclick="reply()" value="Отправить"></div>';
}

function el(id) {return document.getElementById(id);}

function reply()
{
	el('comment_submit').disabled=true;
	comment_form_now=-1;
	loadXMLDoc("action=reply&comment="+encodeURIComponent(el("comment_form").value)+"&parent="+el("comment_parent_id").value+"&id="+el("topic_id").value);
}

function loadXMLDoc(url, encode) {

	if (encode)
	{
		data=encodeURI(url);
	}
	else
		data=url;
	url=host;
//	alert(url);

	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(data);
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("POST", url, true);
			req.setRequestHeader('Content-Type',	'application/x-www-form-urlencoded');
			req.send(data);
		}
	}

}

function XMLValue(doc, tag, n)
{n = n ? n : 0 ; return doc.getElementsByTagName(tag)[n].firstChild.data;}

function processReqChange() 
{
	var n=0;
	var r=null;

	if (req.readyState == 4) {
		   if (req.status == 200) {
			  response = req.responseXML.documentElement;
				
			  while(r=response.getElementsByTagName('action')[n])
			  {
				  if (!r)
				  {
					  break;
				  }

				  action = XMLValue(response,'action', n);
				  switch (action)
				  {
					  case 'error': alert(XMLValue(response,'data', n)); break;
					  case 'replace': document.getElementById(XMLValue(response,'id', n)).innerHTML=XMLValue(response,'data', n); tooltip.d(); break;
				  }
				  n++;
			  }
	        }
		  tooltip.d();
    }
}

new Ajax.Updater('gloom_in_air', host+'?action=refresh_air', 
	{ method: 'get' 
	}
);

monitor_updater=new Ajax.PeriodicalUpdater('gloom_in_air', host+'?action=refresh_air', 
	{ method: 'get' ,
		frequency: 2,
	    decay: 1
	}
);

function stop_monitor()
{
	monitor_updater.stop();
	el('monitor_control').innerHTML='<a href="#" onclick="start_stop_monitor(\'start\'); return false;">Start scan</a>';
}

function start_monitor()
{
	monitor_updater.start();
	el('monitor_control').innerHTML='<a href="#" onclick="start_stop_monitor(\'stop\'); return false;">Stop scan</a>';
}

function start_stop_monitor(set)
{
	new Ajax.Request(host+"?action=monitor_start_stop&set="+set, {
	  method: 'get',
	  onSuccess: function(transport) {
		if (transport.responseText.match('1'))
		  stop_monitor();
		else
		  start_monitor();
	  }
	});
}

function show_user_box(el, user_id)
{
	return false;
	user_box = jQuery('<div id="user_box" class="user_box" style="position: absolute; z-order: 2; top:' + el.style.top + 'px; left: ' + el.style.left + 'px;"></div>');
	user_box.appendTo(jQuery("#subline_div"));
	user_box.load(host+'?action=show_user_box&id=' + user_id, {}, function(){user_box.fadeIn("fast");});
	
	user_box.mouseout(function(){jQuery("div.user_box").fadeOut();});
}