/**
 * Init the comments
 */
$(function(){

	init_comment();


	// The button to toggle the PublishGameStats
	$("#togglePublishGameStats").click(function(){

		$(".comment_unit").each(function(){
			if ($(this).find(".game_stats_bloc").length > 0){
				$(this).toggle();
			}
		});
	});


	// The display replies not displayed
	//-----------------------------------
	$(".btn_replies_not_displayed").click(function(){
		var parent = $(this).parent().parent();
		var page_id = $("#page_id").text();

		var comment_ids= $(".replies_not_displayed_content_result", parent).text();

		$(this).hide();
	
		
		displayWaiter($(".replies_not_displayed_content_result", parent), "big4");
		

		$(".replies_not_displayed_content_result", parent).load("plugins/vp_comments/03_replies_hidden_display.php?page_id="+page_id+"&comment_ids="+comment_ids, function(){
				init_comment();
				
				$(".replies_not_displayed_content_result", parent).fadeIn();
				
		});
	});
});

/**
 * [init_comment description]
 * @return {[type]} [description]
 */
function init_comment(){
	$(".btn_vote_down").attr("data-toggle",'tooltip');
	$(".btn_vote_down").attr("data-placement",'bottom');
	$(".btn_vote_down").attr("title",$("#LANG_comments_vote_down_note").text());

	$(".btn_vote_up").attr("data-toggle",'tooltip');
	$(".btn_vote_up").attr("data-placement",'bottom');
	$(".btn_vote_up").attr("title",$("#LANG_comments_vote_up_note").text());

	$(".btn_reply").attr("data-toggle",'tooltip');
	$(".btn_reply").attr("data-placement",'bottom');
	$(".btn_reply").attr("title",$("#LANG_comments_reply").text());

	avatar_refresh();
	try{ // To avoid an error in mini_site pge dev.
		$("span.timeago").timeago();
	}catch(e){
		console.log(e);
	}
	tooltips_refresh();
}

/**
 * [comments_form_verif description]
 * @param  {[type]} form_id       [description]
 * @param  {[type]} free_comments [description]
 * @return {[type]}               [description]
 */
function comments_form_verif(form_id, free_comments){
	
	// we define -----------------------
	form = document.getElementById(form_id);
	
	// we init -----------------------
	var keAlerte = "";
	
	if (free_comments){
		// we verify -----------------------
		if (form.name.value == "" || form.name.value == " ") keAlerte+="\n"+_LANG_comments_plz_fill_pseudo+"\n";
	}
	// we verify -----------------------
	if (form.content.value == "" || form.content.value == " ") keAlerte+="\n"+_LANG_comments_plz_fill_message+"\n";
	
	if (!form.accept_comments_rules.checked) keAlerte+="\n"+_LANG_comments_accept_rules+"\n";
	
	// we act -----------------------
	if (keAlerte!=""){
		alert(keAlerte);
		return false;
	}else{
		var tmp = "";
		if (form.post_all_pages && form.post_all_pages.checked) tmp = "&post_all_pages=1"; // for the superadmin
		// ajax_send_form(	"comments_content_result",
		// 							"plugins/vp_comments/php_scripts/01_comments_action.php",
		// 							"content="+form.content.value+"&page_id="+form.page_id.value+"&name="+form.name.value+tmp, // care : look at the "+tmp" at end
		// 							""
		// 							);

		displayWaiter($(".content_result", $("#"+form_id).parent()), "big4");

		var cr = $(".content_result", $("#"+form_id).parent());
		cr.load("plugins/vp_comments/php_scripts/01_comments_action.php",
									{"content":form.content.value, "parent_id":form.parent_id.value, "page_id":form.page_id.value, "name":form.name.value+tmp}, 
									function(){
										
										// If there has been an error, we display back the form.
										//-----------------------------------
										if ($(".dont_close_texterea", $(this)).length > 0){

											$("#"+form_id).slideDown();

											form.content.value = $(".dont_close_texterea", $(this)).text();
										}

									});
		// We hide the form
		//-----------------------------------
		$("#"+form_id).slideUp();
		// reset
		form.name.value = "";
		form.content.value = "";
		
	}

}

// -----------------------
// -----------------------

/**
 * [vote_comment description]
 * @param  {[type]} comment_id [description]
 * @param  {[type]} page_id    [description]
 * @param  {[type]} type       'up', 'down'
 * @return {[type]}            [description]
 */
function vote_comment(comment_id,page_id, type){
		var cr = $("#comment_"+comment_id+"_content_result");
		cr.load("plugins/vp_comments/php_scripts/02_votes_action.php", {"comment_id":comment_id, "page_id":page_id, "type":type},
									function(){
										// We hide the text result
										//-----------------------------------
										hideAfterDelay($(".alert", cr));
										    

									})
}

/**
 * [reply_comment description]
 * @param  {[type]} parent_id The first comment to be replied. (means a first level comment, even if the reply button is a second level one.)
 * @param  {[type]} comment_id The comment id to know where to display the form.
 * @param  {[type]} page_id    [description]
 * @return {[type]}            [description]
 */
function reply_comment(parent_id, comment_id, page_id, author_name){
	
	// The comment_form duplicated in the comment_unit
	//-----------------------------------
	var comment_cr = "#comment_"+comment_id+"_content_result";
	$(comment_cr).hide();
	$(comment_cr).html("<div class='reply_form_bloc'></div>");
	$(".reply_form_bloc", comment_cr).html($("#comment_form").html());
	$("h2", comment_cr).text($("#LANG_comments_reply").text());
	$("h2", comment_cr).append("<i class='fa fa-times-circle ico_btn btn_close'></i>");

	//$("#comments_form_note", comment_cr).text($("#LANG_comments_reply_note").text());
	$("#comments_form_note", comment_cr).hide();

	// The @login
	//-----------------------------------
	$("textarea", comment_cr).val("@"+author_name+" : ");

	// We change the form id
	//-----------------------------------
	$("form", comment_cr).attr("id", "form_comment_"+comment_id);

	// Display
	//-----------------------------------
	$(comment_cr).slideDown(function(){
		$("textarea", comment_cr).focus();
	});
	

	// The parent_id
	//-----------------------------------
	$(".parent_id", comment_cr).val(parent_id);

	// Action of close button
	//-----------------------------------
	$(".btn_close", comment_cr).click(function(){
		$(comment_cr).slideUp(function(){
			$(comment_cr).empty();
			$(comment_cr).show();
		});
		
	});
}

// -----------------------
// -----------------------
function display_comments_rules(){
	document.getElementById("msg_comments_rules").style.display = "block";
}
// -----------------------
// -----------------------
// usefull for href
function func1(){ alert('test'); }


/**
 * To add class to the current_user
 * @return {[type]} [description]
 
function init_comments(){

	if (IS_MEMBER){

		$(".comment_content").each(function(){

			if ($(".ihCommentUserId", $(this).parent().parent() ).val() == USER_ID){
				$(this).addClass('current_user');
			}

		}
	}

}

*/


