$(document).ready(function() {
	//gets the presentation id and minisite id from the component_presentation_transcriptsearch.php which uses the 
	//presentation_transcriptsearch.html fragment.  The component must be registered in the view.
	var presentation_id = $('#presentation_id').val();
	var minisite_id = $('#minisite_id').val();
	$('#presentation_commentform form').ajaxForm({
		target: null,
		beforeSubmit: comment_progress,  
		success: comment_render, 
		url: '/ajax/comments/add/index.php?presentation_id=' + presentation_id + '&minisite_id=' + minisite_id, 
		type: 'post', 
		dataType: 'json', 
		clearForm: false, 
		resetForm: false
	});
});

var comment_progress = function () {
	//$('#comment_button').css('display', 'none');
	
	var comment_count = parseInt($('.number_of_comments').html());
	$('.number_of_comments').html(comment_count + 1);
};

var comment_render = function (comment) {
	var comment_date_format = $('#comment_date_format').val();
	var date = $.PHPDate(comment_date_format, new Date());
		
	//read the tempalte
	var commentTemplate = $('#dummycomment .comment').clone();
	
	//populate the template
	$(commentTemplate).find('.comment_body').html(comment['body']);
	$(commentTemplate).find('.comment_name').html(comment['author']);
	$(commentTemplate).find('.date').html(date);
	
	//add the populate template to the list of comments
	commentTemplate.prependTo('.presentation_comments_item');

	//show fields
	$('#presentation_commentform textarea[name=comment]').val('');
	$('.presentation_comments_zero').remove();
	$('#comment_button').css('display', 'block');
	
	return true;	
};


