var sending = 0, replyContainerTmp = '';

function OpenReply(id) {
	Request('Topic', 'GetReply', { id : id }, function (reply) {
		txc = $('rtext'+id);
		txc.innerHTML = reply.htext;
	});
	if ($('replyContainer'+id))
		$('replyContainer'+id).style.display = 'block';
}

function DeleteTopic(id) {
	Request('Topic', 'DeleteTopic', { id : id }, function (reply) {
		document.location.href = '/Forums/'+groupId;
	});
}

function DeleteTopicAsSpam(id) {
	Request('Message', 'Complain', { id : id, reason: 'спам (модерация в сообществе)' }, function (result) { alert('Спасибо, ваша жалоба принята'); });
	DeleteTopic(id);
}

function CloseTopic(id) {
	Request('Topic', 'CloseTopic', { id : id }, function (reply) {
		document.location.reload();
	});
} 

function UncloseTopic(id) {
	Request('Topic', 'UncloseTopic', { id : id }, function (reply) {
		document.location.reload();
	});
} 

function StickTopic(id) {
	Request('Topic', 'StickTopic', { id : id }, function (reply) {
		document.location.reload();
	});
} 

function UnstickTopic(id) {
	Request('Topic', 'UnstickTopic', { id : id }, function (reply) {
		document.location.reload();
	});
} 

function ApproveTopic(id) {
	Request('Topic', 'ApproveTopic', { id : id }, function (reply) {
		document.location.reload();
	});
} 

function DeleteReply(id) {
	
	ReplyTo(topicId + 'hidden');
	
	Request('Topic', 'DeleteReply', { id : id }, function (reply) {
	
		$('rcapt'+id).innerHTML = '(сообщение удалено)';
		$('rtext'+id).parentNode.removeChild($('rtext'+id));
		$('redit'+id).parentNode.removeChild($('redit'+id));
		$('replyContainer'+id).parentNode.removeChild($('replyContainer'+id));
		
	});
	
}

function EditReply(id) {
	$('rtext' + id).style.display = 'none';
	$('redit' + id).style.display = '';
}

function EditReplyDone (id) {
	Request('Topic', 'EditReply',
		{
			id : id,
			text : $('reditarea' + id).value
		}, function (reply) {
		
		if (reply.ok) {
			$('rtext' + /*reply.*/id).innerHTML = $('rimage' + id).innerHTML + reply.processed;;
			$('reditarea' + /*reply.*/id).value = reply.plain;
		}
		
	});
	
	EditReplyCancel(/*reply.*/id);
}

function EditReplyCancel (id) {
	$('rtext' + id).style.display = '';
	$('redit' + id).style.display = 'none';
}


function ReplyTo (id) {
	if (!$('replyContainer' + id))
		return;
	var node = $('replyBox'), parent = node.parentNode;
	parent.removeChild(node);
	parent.innerHTML = replyContainerTmp;
	replyContainerTmp = $('replyContainer' + id).innerHTML;
	$('replyContainer' + id).innerHTML = '';
	$('replyContainer' + id).appendChild(node);
	$('replyTo').value = id;
	try {
		$('replyTextarea').focus();
	}
	catch (e) {
		//IE my ass
	}
}

function Abuse (id) {
	if (!$('replyContainer' + id))
		return;
	var node = $('abuseBox'), parent = node.parentNode;
	parent.removeChild(node);
	parent.innerHTML = replyContainerTmp;
	replyContainerTmp = $('replyContainer' + id).innerHTML;
	$('replyContainer' + id).innerHTML = '';
	$('replyContainer' + id).appendChild(node);
	$('abuse').value = id;
	$('abuseForm').action = '#thread' + id;
}

function ShowAdmin (id) {
	if (!$('replyContainer' + id))
		return;
	var node = $('groupadminBox'), parent = node.parentNode;
	parent.removeChild(node);
	parent.innerHTML = replyContainerTmp;
	replyContainerTmp = $('replyContainer' + id).innerHTML;
	$('replyContainer' + id).innerHTML = '';
	$('replyContainer' + id).appendChild(node);
	$('gaMsg').value = id;
	$('gaAction').selectedIndex = 0;
}

function MemberChange() {
	Request('Topic', 'MemberChange',
		{
			msg: $('gaMsg').value,
			action: $('gaAction').options[$('gaAction').selectedIndex].value,
			group: groupId
		}, function (result) {
			if (result.list['error'])
				alert(result.list['error']);
			else
				alert('Выполнено!');
		}
	);
}

function Fave(topic) {
	Request('Topic', 'Fave',
		{
			topic: topic
		}, function () {
			alert('Добавлено!');
		}
	);
}

function RemoveFromInbox(topic, refurl) {
	Request('Message', 'Delete', { id : topic }, function (result) { if (refurl.search(/inbox/i)) document.location.href = refurl; });
}
