/* (c) 2008-2010, Nikiforov Vladimir aka Volod */


function quote_comment_callback(data)
{
	c = document.getElementById('CommentForm').comment;
	if (c.value != '') c.value += "\n\n";
	c.value += data + "\n";
	quote_name = last_name;
}

function quote_comment(cid, t)
{
	f = document.getElementById('CommentForm').comment.focus();
	$.get('/comment_get_ajax.php',{
		quote: 1,
		comment_id: cid,
		topic_id: t,
		gender: user_names[cid].gender
	}, quote_comment_callback);
}

function rollback_comment(cid)
{
	$.get('/comment_rollback.php', {comment_id: cid}, alert_or_reload);
}

function edit_comment_callback(data)
{
	f = document.getElementById('CommentForm');
	f.comment.value = data;
	f.comment.focus();
}

function edit_comment(c)
{
	f = document.getElementById('CommentForm');
	f.action = '/comment_modify.php';
	f.comment_id.value = c;
	f.b1.value = 'отправить исправленный вариант комментария';
	f.b2.style.visibility = '';
	$.get('/comment_get_ajax.php', {comment_id: c}, edit_comment_callback);	
}

function add_comment()
{
	f = document.getElementById('CommentForm');
	f.action = '/comment_add.php';
	f.comment_id.value = -1;
	f.b1.value = 'Отправить комментарий';
	f.b2.style.visibility = 'hidden';
	f.comment.value = '';
}

function sumbit_comment_callback(data)
{
	(data == 0) ? alert("Код подтверждения неверный") : document.CommentForm.submit();
}

function submit_comment(form)
{
	//var form = document.CommentForm;
	//потому, что этот скрипт используется для создания OTD
	// у которого это поле не comment, а description (name=description id='comment' !!!)
	comment = document.getElementById('comment');
	//для незарегинных поля имя файла нет вообще и его не надо проверять
	//если оно есть И оно заполнено, то коммент можно не вводить!
	ok = false;//допустим, что все плохо
	if ((form.c_image_file != null && form.c_image_file.value != '') || form.file_url != '')//юзер авторизованный и ввел картинку
		ok = true;//хорошо
	if (comment.value != '')//ввел коммент
		ok = true;//тоже хорошо
	if (!ok)//а вот если не сделал ни того ни другого, то шиш ему
	{
		alert('Вы не ввели сообщение!');
		return false;
	}
	if (form.user_id.value == '0')
	{
		setCookie('user_name', form.user_name.value);
		setCookie('user_email', form.user_email.value);
		
		if (form.confirm_num.value == '')
		{
			alert('Вы не ввели код подтверждения!');
			return false;
		}		
		$.get('/check_confirm_img.php', {
			confirm_num: form.confirm_num.value,
			confirm_key: form.confirm_key.value
		}, sumbit_comment_callback);
		return false;
	}
	form.submit();
	return false;
}

function comment_remove_attach(cid, f)
{
	$.get('/comment_remove_attach.php', {comment_id: cid, f: f}, alert_or_reload);
}

function comment_preview()
{
	c = $('#preview');
	if (c.css('display') == 'none')
	{
		$('#preview')
		.show()
		.html(LOAD_INDICATOR)
		.load('/comment_get_ajax.php', {
			bbcode: 1,
			comment: escape(document.getElementById('comment').value)});
	}
	else
	{
		c.hide();
	}
}	

function topic_preview(tid, cid, flag, do_switch)
{
	p = $('#topiccomment' + tid);
	
	if (do_switch == 0 || p.css('display') != 'block')
	{		
		p.fadeIn(300)
			.html(LOAD_INDICATOR)
			.load('/comment_get_ajax.php', {
				bbcode: 1,
				arrows: 1,
				topic_id: tid,
				comment_id: (flag == 'c') ? cid : 0});
		return;
	}
	else
	{
		p.fadeOut(300);
	}
}

function show_parent_comment(tid, cid, n, bbcode)
{
	$('#p_c' + cid).fadeIn(300)
		.html(LOAD_INDICATOR)
		.load('/comment_get_ajax.php', {
			bbcode: bbcode,
			arrows: 1,
			topic_id: tid,
			src_comment_id: cid,
			comment_n: n});
	return;
}

function hide_parent_comment(cid)
{
	$('#p_c' + cid).fadeOut(300);
}


function show_hidden_comment(cid,ask)
{	
	e = $('#hiddencomment' + cid);
	if (e.height() > 0)
		return;

	if ((ask == 1) && !confirm("Этот комментарий может содержать оскорбительные слова или изображения. Вы уверены, что хотите его просмотреть?"))
		return;
	
	$('#c_env' + cid).show();
	$('#c_hidden' + cid).hide();

	e.html(LOAD_INDICATOR).load('/comment_get_ajax.php', {bbcode:1, comment_id: cid});
}

function open_comment_visible_dialog_callback(data)
{
//for future use :)
}

function open_comment_visible_dialog(cid,hbl)
{
	d = $('#commenthidedialog' + cid);
	
	if (d.css('display') == 'block')
	{
		d.fadeOut(500);
	}
	else
	{
		$('.commenthidedialog').hide();
		d.show().load("/comment_hide.php", {
			cid: cid, hbl: hbl, action: 'form'},
			open_comment_visible_dialog_callback);
	}	
}

function comment_hide(hidden, cid)
{
	hbl = document.getElementById('c' + cid + 'hidden_by_level');
	f = document.getElementById('commenthideform' + cid);
	if (f.hidden_by_level.length > 0)
	{
		for (i=0; i < f.hidden_by_level.length; i++)
			if (f.hidden_by_level[i].checked)
				hbl = f.hidden_by_level[i];
	}
	else
	{
		hbl = f.hidden_by_level;
	}
	
	$('#commenthidestatus' + cid)
		.show()
		.load('/comment_hide.php', {
			comment_id: cid,
			hidden: hidden,
			hidden_by_level: hbl.value});
	$('#commenthidediv' + cid).fadeOut(600);
}

function showUserInfo(c)
{
	$('#userinfo' + c).fadeIn(100);
	$('#userinfo' + c).fadeOut(10000);
}

function comment_remove_callback(data)
{
	if (parseInt(data) > 0)
	{
		$('#c_env'+parseInt(data)).fadeOut(500);
	}
	else
	{
		alert(data);
	}
}

function comment_remove(id)
{
	if (confirm("Уверены?"))
	{
		$.get('/comment_remove.php', {comment_id: id, action: 'kill'}, comment_remove_callback);
	}
}