
$( function() {
	$("#add_comment").dialog({ autoOpen: false, modal: true, width: 400 })
		.removeClass( 'hidden' )
		.find( 'input[type=button]' ).button();
} );

function showAddComment( el )
{
	var ta = $('#ta_comment');
	
	ta.val( '' );
	ta.attr( 'rel', el.rel );

	$("#add_comment").dialog( 'option', 'title', el.title );
	$("#add_comment").dialog( 'open' );

	ta.focus();
}

function cancelAddComment()
{
	$("#add_comment").dialog( 'close' );
}

function doAddComment()
{
	var ta = $('#ta_comment');

	if( ta.val() )
	{
		$("#add_comment").dialog( 'close' );
		startProgress();
		$.post( "/w/index.php?action=ajax&rs=ajaxJAddComment",
			{ page: $('#comment_page').val(), parent: ta.attr( 'rel' ), text: ta.val() },
			onAddCommentComplete );
	}
}

function onAddCommentComplete( data )
{
	var div = document.createElement( 'div' );
	div.className = 'result_block messageEntry';
	var json = $.parseJSON( data );
	div.innerHTML = json.text;
	
//	if( json.parent != 0 )
//		$("#div_comment_" + json.parent).parent().after( div );
//	else
		$('#messageList').append( div );
		
	$('html,body').animate({ scrollTop: $(div).offset().top }, { duration: 'slow', easing: 'swing'});

	$('#no_comments').remove();
	endProgress();
	cancelAddComment();
}

function onDeleteCommentComplete( resp )
{
	if( resp.status == 200 ) 
	{
		if( resp.responseText )
		{
			var div = $('#div_comment_' + resp.responseText.trim() ).parent();
			if( div )
				div.remove();
			else
				alert( resp.responseText );
		}			
	}
	
	endProgress();
}

function deleteComment( el )
{
	if( confirm( el.title + "?" ) )
	{
		startProgress();
		sajax_do_call( "ajaxJDeleteComment", [ el.rel ], onDeleteCommentComplete );
	}
}
