var document_id;
$(document).ready(function() {
	$("div.comment_block a.re").click(function() {
		$("#addformcomment").remove().insertAfter($(this)).show();
		BindCommentLogin();
		$("#addformcomment input[name='parent']").val($(this).attr("cid"));
		return false;
	});
	
	$("div.comment_block a.del").click(function() {
		par = $(this).parents("div.comment_block");
		$.get('/system/php/service.php', 
				{f : 'comment_delete', id : $(this).attr("cid")}, 
				function (data) {
					comment_reload();
					//location.reload();
				}
		)
		
		return false;
	});
	
	$("div.comment_block a.bot").click(function() {
		par = $(this).parents("div.comment_block");
		if(confirm('Хотите занести этот ip в черный список?')){
			$.get('/system/php/service.php', 
				{f : 'its_a_bot', id : $(this).attr("cid")}, 
				function (data) {
//					location.reload();
			});
		}
		return false;
	});

	$("#addcomment").click(function() {
		$("#addformcomment").remove().insertAfter($(this)).show();
		$("#addformcomment input[name='parent']").val(0);
		BindCommentLogin();
		return false;
	});
	
	$("#addcomment3").click(function() {
		$("#addformcomment").remove().insertAfter($(this)).show();
		$("#addformcomment input[name='parent']").val(0);
		BindCommentLogin();
		return false;
	});
	
	
	$("#addcomment2").click(function() {
	  $("#addformcomment").remove().insertAfter($("#addcomment")).show();
	  $("#addformcomment input[name='parent']").val(0);
	  BindCommentLogin();
	  return false;
	});
	
	$("#addcomment_news1").click(function() {
	  $("#addcomment").css('display','block');
	  $("#addformcomment").remove().insertAfter($("#addcomment")).show();
	  $("#addformcomment input[name='parent']").val(0);
	  BindCommentLogin();
	  return false;
	});
	
	document_id = $("#addformcomment input[name='id']").val();

	
});
function BindCommentLogin() {
	$("#addformcomment input[name='logtype']").click(function() {
		if ($(this).val() != 'login') {
			$("#comment_guest").show();
			$("#comment_login").hide();
		} else {
			$("#comment_guest").hide();
			$("#comment_login").show();
		}
	});
	
	$("#addformcomment form").submit(function() {
		$("#comment_error_login").hide();
		$('#submit_button').attr('disabled', true);
		$('#submit_button').val('Комментарий добавляется');
		$(this).ajaxSubmit({
			success:	function(data) {
							switch (data) {
								case 'error_login':
									$("#comment_error_login").show();
								break;
								
								default:
									location.href = location.href.replace(/#.+$/, '') + '#comment'+data;
									comment_reload();
//									location.reload();
								break;
							}
						}
		});
		return false;
	});
	
	$("#addformcomment_close").click(function() {
		$("#addformcomment").hide();
		return false;
	});
}
function comment_reload(){
		$('#submit_button').attr('disabled', false);
		$('#submit_button').val('Добавить');
		$.get('/system/includes/53320.phtml', 
				{id : document_id}, 
				function (data) {
					$('div#comments').html(data);
					bind_functions();
				}
		)
	
}

function bind_functions(){
	$("div.comment_block a.re").click(function() {
		$("#addformcomment").remove().insertAfter($(this)).show();
		BindCommentLogin();
		$("#addformcomment input[name='parent']").val($(this).attr("cid"));
		return false;
	});
	
	$("div.comment_block a.del").click(function() {
		par = $(this).parents("div.comment_block");
		$.get('/system/php/service.php', 
				{f : 'comment_delete', id : $(this).attr("cid")}, 
				function (data) {
					comment_reload();
					//location.reload();
				}
		)
		
		return false;
	});
	
	$("div.comment_block a.bot").click(function() {
		par = $(this).parents("div.comment_block");
		if(confirm('Хотите занести этот ip в черный список?')){
			$.get('/system/php/service.php', 
				{f : 'its_a_bot', id : $(this).attr("cid")}, 
				function (data) {
//					location.reload();
			});
		}
		return false;
	});

	$("#addcomment").click(function() {
		$("#addformcomment").remove().insertAfter($(this)).show();
		$("#addformcomment input[name='parent']").val(0);
		BindCommentLogin();
		return false;
	});
	
	$("#addcomment3").click(function() {
		$("#addformcomment").remove().insertAfter($(this)).show();
		$("#addformcomment input[name='parent']").val(0);
		BindCommentLogin();
		return false;
	});
	
	
	$("#addcomment2").click(function() {
	  $("#addformcomment").remove().insertAfter($("#addcomment")).show();
	  $("#addformcomment input[name='parent']").val(0);
	  BindCommentLogin();
	  return false;
	});
	
	$("#addcomment_news1").click(function() {
	  $("#addcomment").css('display','block');
	  $("#addformcomment").remove().insertAfter($("#addcomment")).show();
	  $("#addformcomment input[name='parent']").val(0);
	  BindCommentLogin();
	  return false;
	});
}
