$(document).ready(function(){

//Comments show/hide
$('#comments_bl .h a').click(function () {

	$('#comments_bl .in').slideToggle();

return false;
});


//Buttons show/hide
$('#comments_bl .item .text, #comments_bl .item .inf, #comments_bl .item .buttons, #comments_bl .item .comment_form').live('mouseover', function () {
	$(this).parent().children('.buttons').show();$(this).parent().children('.inf').addClass('sel');
}).live('mouseout', function () {
	$(this).parent().children('.buttons').hide();$(this).parent().children('.inf').removeClass('sel');
});


//Comments first loading
$("#comments_bl").each(function () {
	$id = $(this).attr('data-id');
	$page = $(this).attr('data-page');
	$this = $(this);

	$.ajax({
		url: '/index.php?ajax=comments.ajax',
		data: 'act=show&id='+ $id +'&page='+ $page,
		beforeSend: function(){},
		complete: function(){},
		success: function ($ajaxOutput) {
			$this.append($ajaxOutput).slideDown('fast');
			$('#comments_bl .comment_form:first').slideDown('fast');
		}
	});

});


//Comment add
$("#comments_bl .comment_form a.comment").live('click', function () {
	$id = $(this).attr('data-id');
	$page = $(this).attr('data-page');
	$comment_id = $(this).attr('data-comment-id');
	$comment_text = encodeURIComponent($(this).parents('.comment_form').find('textarea').val());
	$this = $(this).parents('.comment_form');

	if ($comment_text) {
	$.ajax({
		url: '/index.php?ajax=comments.ajax',
		data: 'act=add&id='+ $id +'&page='+ $page +'&comment_id='+ $comment_id +'&comment_text='+ $comment_text,
		beforeSend: function(){},
		complete: function(){},
		success: function ($ajaxOutput) {
			$this.slideUp('fast',  function () {
				$this.before($ajaxOutput);
				$('#comments_bl .item').slideDown('fast');
			});
		}
	});
	}

return false;
});


//Answer show/hide
$('#comments_bl a.answer').live('click', function () {

	$('#comments_bl .comment_form:not(:first)').slideUp('fast');
	$(this).parents('.buttons').siblings('.comment_form').slideDown('fast');

return false;
});


//Skipped show
$('#comments_bl a.skipped').live('click', function () {

	$id = $(this).parents('#comments_bl').attr('data-id');
	$page = $(this).parents('#comments_bl').attr('data-page');
	$parent_id = $(this).parent('.item').attr('data-comment-id');
	$this = $(this);
	$parent = $(this).parent();

	$.ajax({
		url: '/index.php?ajax=comments.ajax',
		data: 'act=show&id='+ $id +'&page='+ $page +'&parent_id='+ $parent_id,
		beforeSend: function(){},
		complete: function(){},
		success: function ($ajaxOutput) {
//			$this.replaceWith($ajaxOutput);
			$this.slideUp('fast');
			$this.after($ajaxOutput);
			$parent.find('.item').hide();
			$parent.find('.item').slideDown('fast');
		}
	});

return false;
});


//Comment voting/abuse reporting
$('#comments_bl a.rate, #comments_bl a.report').live('click', function () {

	if ($(this).hasClass('disabled') == false) {

		var $id = $(this).attr('href').substr(1),
			 $this = $(this);

		if ($this.hasClass('report')) $value = 0;
		else if ($this.hasClass('down')) $value = -1;
		else $value = 1;
		
		if ($value != 0) {
		   $current_value = parseInt($(this).siblings('.rating').children().text());
		   $new_value = parseInt($(this).siblings('.rating').children().text()) + $value;
		   if ($new_value > 0) $new_value = '+'+ $new_value;
		   if ($new_value == 0) $class = 'null';
		   else if ($new_value < 0) $class = 'minus';
		   else $class = 'plus';
		   $(this).siblings('.rating').removeClass('null').removeClass('minus').removeClass('plus').addClass($class);
		   $(this).siblings('.rating').children().text($new_value);
		}

		$.getJSON(
			'/index.php?ajax=comments.ajax&act=vote&id='+ $id +'&value='+ $value,
			function ($jsonOutput) {
				if (!$jsonOutput.resultError) {
					$('#comments_bl a.rate[href=#'+ $id +'], #comments_bl a.report[href=#'+ $id +']').addClass('disabled');
				}
			}
		);

	}

return false;
});


//Comment removing
$('#comments_bl a.remove').live('click', function () {

	var $id = $(this).attr('href').substr(1);

	$.getJSON(
		'/index.php?ajax=comments.ajax&act=remove&id='+ $id,
		function ($jsonOutput) {
			if (!$jsonOutput.resultError) {
				$('.item[data-comment-id='+ $id +']').slideUp('fast', function() {
					$('.item[data-comment-id='+ $id +']').remove();
				});
			}
		}
	);

return false;
});



//MAIN!
});

