function UpdateCommentList()
{
	filter=0;
	page=$('#comment-filter #comment-page').val();
	$('#comment-filter select option:selected').each(function(){filter=$(this).val()});
	news_id=$('#news-id').html();
	$('#comment-list').load(
		'/news/comment.ajax.php',
		{
			filter:filter,
			news_id:news_id,
			p:page
		},
		function()
		{
			SetCommentListAction();
		}
	);
}

function SetCommentListAction()
{
	$('#comment-list .navigate a').click(function()
	{
		page=$(this).attr('href');
		page=page.replace(/^.*#p(\d+)$/,"$1");
		$('#comment-filter #comment-page').val(page);
		UpdateCommentList();
		return false;		
	});
	$('#comment-list .comment-item a').click(function()
	{
		c_sel=$(this);
		if ($(this).hasClass('up')) direction='up';
		else direction='down';
		comment_id=$(this).attr('href').substr(1);
		$.ajax({
			type:"POST",
			url:"/news/comment.ajax.php",
			data:{
				act:'vote',
				n:comment_id,
				d:direction
			},
			cache: false,
  			dataType: "json",
  			success:function(msg)
  			{
  				if (msg.er==0)
  				{
  					UpdateCommentList();
  				}
  				ShowCommentMessage(msg.msg);
  			}
		});
		return false;
	});
}
function ShowCommentMessage(msg)
{
	$('#comment-msg').html(msg);
  	$('#comment-msg').show();
  	$('#comment-msg').fadeOut(4000);
}

$(document).ready(function()
{
	SetCommentListAction();
	
	$('select',$('#comment-filter')).change(function()
	{
		UpdateCommentList();
	});
	
	$('#comment-add a').click(function()
	{
		$('img',$('#comment-add')).attr('src','/l/i/captcha/image.jpg?c='+Math.random(255));
		return false;
	});
	
	$('#comment-add').submit(function()
	{
		news_id=$('#news-id').html();
		$.ajax({
  			type: "POST",
  			url: "/news/comment.ajax.php",
  			data:
  			{
  				t:$('textarea',$(this)).val(),
  				c:$('input:text',$(this)).val(),
  				act:'add',
  				n:news_id
  			},
  			cache: false,
  			dataType: "json",
  			success: function(msg)
  			{
  				ShowCommentMessage(msg.msg);
  				if (msg.er==0)
  				{
  					UpdateCommentList();
  					$('textarea',$('#comment-add')).val('');
  					$('input:text',$('#comment-add')).val('');
  				}
  			}
		});
		return false;
	});
	
});
