function load_comment(id, url)
{
	$.post( 
		url + '/comment/index/' + id,
		
		{
			product_id: id
		},
		
		function (data){
			var hash = document.location.hash;
			
			$('#comment_data').html(data);
			$('#comment_valid').hide().html('');
			$('#cadd_comment').click(function () { add_comment(id, url) });
			
			if(hash != '')
			{
				//alert('#post'+hash);
				$('#post8').focus();
			}
		},
		
		'html'
	);
}

function add_comment(id, url)
{
	$.post(
		url + '/comment/add/' + id,
		{
			parent_id: $('#cparent_id').attr('value'),
			product_id: id,
			username: $('#cusername').attr('value'),
			email: $('#cemail').attr('value'),
			message: $('#ccomment').attr('value')
		},
		function (obj){
			//alert(obj);
			if(obj.validation == 1)
			{
				$('#comment_valid').hide().html('');
				$('#cparent_id').attr('value', '');
				$('#ccomment').attr('value', '');
				load_comment(id, url);
			}
			else
			{
				$('#comment_valid').html(obj.msg).show();
			}
		}
		,
		'json'
	);
}

function recomment(id)
{
	$('#comment_valid').html('<p><b>RE:</b> #' + id + ' <a href="javascript:recomment_off()">Убрать</a></p>').show();
	$('#cparent_id').attr('value', id);
	$('#cadd_comment').focus();
	$('#cusername').focus();
}

function recomment_off(id)
{
	$('#comment_valid').html('').hide();
	$('#cparent_id').attr('value', '');
	$('#cadd_comment').focus();
	$('#cusername').focus();
}

function editcomment(id)
{
	var text = $('#comment_'+id).text();
	if($('#edit_comment_text').text() == '')
	{
		$('#comment_'+id).html('<textarea id="edit_comment_text" rows="7" cols="20">'+text+'</textarea><br><a href="#" id="edit_save_button">Сохранить</a><a href="#" id="cansel_save_button">Отмена</a>');
		$('#edit_save_button').click(function () 
										{
											var new_text = $('#edit_comment_text').attr('value');
											$('#comment_'+id).text(new_text);
											//alert('save'); 
											return false;
										}
									);
		$('#cansel_save_button').click(function ()
										{
											$('#comment_'+id).text(text);
											return false;
										});
	}
}

