
function Comments (module, module_id, url) {
    this.module = module;
    $commentsContainer = $('#srvComments_'+module);
	$commentsFormClone = $commentsContainer.find('.srvCommentsFormContainer');
	
	$('.srvCommentsForm', $commentsContainer).live('submit',function(){
		var $thisForm = $(this)
		var text = $thisForm.find('textarea[name=text]');
		var reply_to = $thisForm.attr('reply_to') != undefined ?  $thisForm.attr('reply_to') : 0;
		if (text.val().length > 0)
			$.post(url,
				{module:module, module_id:module_id, ajax:1, action:'comment_send', text:text.val(), reply_to: reply_to}, 
				function(html){
					if (reply_to > 0) $thisForm.closest('.srvCommentItem').append(html);
					else $commentsContainer.find('.srvCommentsList').append(html); text.val('');
					if ($thisForm.attr('cloned')) $thisForm.closest('.srvCommentsFormContainer').remove(); 
				}
			);
		return false;
	});
	
	$commentsContainer.find('.srvReply').live('click',function(){
		var $form_container = $commentsFormClone.clone('true');
		var $form = $form_container.find('form');
		$commentsContainer.find('.srvCommentsForm[cloned=true]').closest('.srvCommentsFormContainer').remove();
		$form_container.removeAttr('id');
		$form.attr('cloned', true);
		$form.attr('reply_to', $(this).attr('comment_id'));
		$(this).closest('.srvCommentItem').append($form_container);
		$form.find('textarea[name=text]').focus();
		return false;
	});

	$commentsContainer.find('.srvDelete').live('click',function(){
		$thisButton = $(this);
		if (!confirm("Вы уверены?")) return false;
		var comment_id = $(this).attr('comment_id') != undefined ? parseInt($(this).attr('comment_id')) : 0;
		var del_childs = $(this).attr('del_childs') != undefined ? parseInt($(this).attr('del_childs')) : 0;
		if (!comment_id) return false;
		$.post(url, {module:module, module_id:module_id, ajax:1, action:'comment_delete', id:comment_id, del_childs: del_childs}, function(data){ if (data.status == 1) $thisButton.closest('.srvCommentItem').remove(); else if (data.status == 2) $commentsContainer.html(data.html); }, 'json');
		return false;
	});

	$commentsContainer.find('.srvReport').live('click',function(){
		$thisButton = $(this);
		var comment_id = $(this).attr('comment_id') != undefined ? parseInt($(this).attr('comment_id')) : 0;
		if (!comment_id) return false;
		$.post(url, {module:module, module_id:module_id, ajax:1, action:'comment_report', id:comment_id}, function(data){ $thisButton.after('Модератор извещён'); $thisButton.remove(); }, 'json');
		return false;
	});

	$commentsContainer.find('.srvQuickLink').bind('click',function(){
		$commentsFormClone.find('textarea[name=text]').focus();
		return false;
	});
		
	$commentsContainer.find('.srvCommentsForm textarea[name=text]').live('keyup', function(e){
		if(e.keyCode == 13 && e.ctrlKey){
			e.stopPropagation();
			$(this).closest('.srvCommentsForm').submit();
		}
		return false;
	});
	
}

