$(document).ready(function(){
	if($('#comment-text').length > 0)
	{
		$('#comment-text').autogrow();
	}
});

function comment_ajax_add_comment(application, application_entry){
  var page_url = document.getElementsByTagName('base')[0].href;
  var comment_text = $('#comment-text').val();
                        
  $.post(page_url + 'ajax/comment/add-comment/', {comment_text : comment_text, comment_application : application, comment_application_entry : application_entry}, function(data){
    if(data == 'false'){
      comment_init_error_message('comment-add-error');
     }  
     else{
          if($('#comment-no-comments').length > 0){
	var translation = $('#comment-opinions-translation').val();
	$('#comment-no-comments').html(translation);
          }
      comment_append_comment(data);
     }
  });
     
     $('#comment-text').val('');
}

function comment_init_error_message(id){
	$('#' + id).dialog({
		modal : true,
		resizable : false,
		buttons : {
			'OK' : function()
			{
				$(this).dialog('close');
			}
		}
	});
	
	if(! $('#' + id).dialog('isOpen'))
	{
		$('#' + id).dialog('open');
	}
}

function comment_append_comment(data){
  $(data).hide().appendTo('#comments-container').fadeIn();
}
