$(document).ready(function() 
{
    var addOptions = {
        beforeSubmit:  validateCommentAdd,
        success:	   successAddComment
    };
 
 	var delOptions = {
        success : successDelComment
    };
 
    $('#comment-form').submit(function() { 
        $(this).ajaxSubmit(addOptions); 
        return false;
    });
	
	$('#remove-comment-form').submit(function() { 
        $(this).ajaxSubmit(delOptions); 
        return false;
    });

	$("#remove-comment-dialog-confirm").dialog({
		autoOpen: false,
		resizable: false,
		height:200,
		modal: true,
		buttons: {
			'Remover': function() {
				$('#remove-comment-form').submit();
				$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});
	
	makeBindLinks();
})

function makeBindLinks(){
	$('a.comment_remove_link').click(function()
	{
		$('#commentid').val($(this).attr("mediaid"));		
		$('#remove-comment-dialog-confirm').dialog('open');
		return false;
	});
}
 
function validateCommentAdd(formData, jqForm, options) { 
	
	var _mensagemCommentTextArea = $('textarea[name=mensagemCommentTextArea]').fieldValue();
		
	if(!_mensagemCommentTextArea[0])
	{ 
		alert('Por favor insira um comentário!');
        return false; 
    }
	
    return true;
}
 
function successDelComment(responseText, statusText, xhr, $form)  {
	$('#comment_' + responseText).remove();
} 

function successAddComment(responseText, statusText, xhr, $form)  {
	
	div = $("<div>").html(responseText);	
	$('#comment-list').prepend(div);	
	$('#comment-form').clearForm();

	var url = $(location).attr('href') + '#allComment';
	$(location).attr('href',url);
	
	makeBindLinks();
}