
/**
 * Global Comments
 * Version 1.0
 *
*/

COMMENTS_AJAX_URL = '/common/ajax/front/components/Comments/ajax.php';

/* Load */
function fncComments_Load(offset, comment_type_id, parent_id, scroll_to_id){
	
	$('Comments_Parent'+parent_id).update('<center><br><img src="/common/templates/default/images/icons_modules/loading.gif" /><br><br></center>');

	new Ajax.Updater({ success : $('Comments_Parent'+parent_id)}, COMMENTS_AJAX_URL, {
		parameters: { 
			action             	: 'load_comments_ajax',
			offset				: offset,
			comment_type_id		: comment_type_id,
			parent_id           : parent_id
		},
		evalScripts : true,
							
		onComplete: function(transport){
			if(scroll_to_id) {
				// go up to view it
				$('comment'+scroll_to_id).scrollTo();
			}
		},
		
		onFailure: function(transport){
			$('Comments_Parent'+parent_id).update('Error retrieving comments. Please refresh the page.'); 
		}
	});
}

/* Insert */
function fncComments_Add(uniq_form_id){
	
	var Parameters   = "action=post_comment&" + Form.serialize($(uniq_form_id));
	var clsValidator = new Validation ($(uniq_form_id));

	if(clsValidator.validate()){
		
		new Ajax.Request(COMMENTS_AJAX_URL, {
			parameters  : Parameters,
			evalScripts : true,
						
			onSuccess: function(transport){
				var arrComment = transport.responseJSON;
				
				// reload comments
				fncComments_Load(0, arrComment.comment_content_type_id, arrComment.comment_parent_id, arrComment.comment_id);
				
				// show success msg (these are in list_wrapper tpl)
				if(arrComment.comment_status == '0') {
					$('Comments_Parent'+arrComment.comment_parent_id+'_Moderated').show();
					setTimeout("Effect.Fade('Comments_Parent"+arrComment.comment_parent_id+"_Moderated', { duration: 2.0 });", 5000);
					
				} else if(arrComment.comment_status == '1') {
					$('Comments_Parent'+arrComment.comment_parent_id+'_Posted').show();
					setTimeout("Effect.Fade('Comments_Parent"+arrComment.comment_parent_id+"_Posted', { duration: 2.0 });", 5000);
				}
				
				// reset form
				$('Comments_Parent'+arrComment.comment_parent_id+'_Form').reset();
				$('Comment_Errors').hide();
			},
			
			onFailure: function(transport){
				// uses the general div (in form tpl) for errors. 
				// erros come from $lang var in php
				$('Comment_Errors').update(transport.responseText).show().scrollTo();
				
			}
		});
		
	}
}
