/* (c) 2008, Nikiforov Vladimir aka Volod. This file may be used freely. */

var result = '';
var is_popup = false;
var current_form;
var current_id;
var result;

function toggle_im_line_callback()
{
	document.location.reload();
}

function toggle_im_line(mode)
{
	$.get('/user_settings.php', {k:'show_im_events', v: mode}, toggle_im_line_callback);
}

function skip_message_callback(data)
{
	$('#instantmessage').html(data[1]);
	if (data[0] == 0)
		$('#instantmessage').fadeOut(6000);
}

function skip_message(mid)
{
	$.getJSON('/instant_messages.php', {action: 'skip', id: mid}, skip_message_callback);
	$('#skip_link' + mid).hide();
}

function get_next_im()
{
	$.getJSON('/instant_messages.php', {action: 'get'}, skip_message_callback);
}

function close_im_form(id)
{
	$('#im_area' + id).fadeOut(200);
}

function open_im_form(id)
{
	$('#im_area'+id).fadeIn(200);
	document.getElementById('im_form'+id).body.focus();
}

function send_im_callback(data)
{
	if (data != 'OK')
	{	//an error occured!
		alert(data);
	}
	else
	{
		document.current_form.body.value = '';
		if (document.close_after)
		{
			$('#im_area' + document.current_id).hide();
			get_next_im();
		}
		else
		{	//длинный вариант фразы alert('Сообщение отправлено');
			$('#instantmessage').show().html("Сообщение отправлено").fadeOut(6000);
		}

	}	
}

function send_im(close_after, id)
{
	document.close_after = close_after;
	f = document.getElementById('im_form' + id);
	document.current_form = f;
	document.current_id = id;

	if (f.dst.value == 0)
	{
		alert("Кому шлём?");
		return false;
	}
	if (f.body.value == '')
	{
		alert("Введите текст сообщения!");
		return false;
	}
	
	$.post('/instant_messages.php',
		{
			action: 'send',
			dst: f.dst.value,
			body: escape(f.body.value),
			replay: (f.replay)?f.replay.value:0
		}, send_im_callback);
	return false;		
}

function clean_messages()
{
	if (confirm("Операция необратима. Вы уверены?"))
		$.get('/instant_messages.php', {action:'clean'}, alert_or_reload);
}