/*********************************************************
 * Appel des fonctions au chargement
 ********************************************************/
$(function(){
	initCommentaires();
	initAlerts();
});

/*********************************************************
 * initCommentaires
 * 
 * Gestion des formulaires de réponse / alerte dans les commentaires
 ********************************************************/
function initCommentaires(){
	/* Repondre */
	$(".comment_group .comment .com .repondre").click(function(){
		var $reponse_panel = $(this).parents(".comment_group");
		$reponse_panel.next().slideDown('fast');
		// $.scrollTo($reponse_panel.find(".comment:last-child"), 500);
		ajustColHeight("up");
		return false;
	});
	
	$(".reactions .reponse_form .close a").click(function(){
		$(this).parents(".reponse_form").slideUp(500, function(){
			ajustColHeight("down");
		});
		return false;
	});
	
	/* Reaction - fin de formulaire */
	$(".sidebar_left_2cols .bouton").click(function(){
		$.scrollTo($(".votrereaction.lastchild"), 500);
		return false;
	});
}

/**
 * Alerte lancée pour des commentaires
 * 
 * @name	initAlerts
 * @author	Rémy Vuong
 * @version	1.0
 * @date	2009-09-28
 */
function initAlerts() {
	// Ouvrir le panneau d'alerte
	$('.alerter').click(function(){
		var $alert_panel = $(this).parents('.comment');
		$alert_panel.next().slideDown(500, function() {
			ajustColHeight("up");
		});
		return false;
	});
	
	// Fermer le panneau d'alerte
	$('.alert_msg .close a').click(function() {
		$(this).parents('.alert_msg').slideUp(500, function() {
			ajustColHeight("down");
		});
		return false;
	});
	
	// Click sur le bouton "alerte"
	$('.alert_msg .submit').click(function() {
		// Valide le formulaire si et seulement si un message a été posté.
		var msg = $.trim($(this).parent().children('textarea').val());
		var msg_len = msg.length;
		if (msg_len==0) {
			alert('Vous devez obligatoirement saisir un message.');
			return false;
		}
		else {
			return true;
		}
	});
	
	return false;
}

/*********************************************************
 * ajustColHeight
 * Ajuste la taille de la colonne de droite
 ********************************************************/
function ajustColHeight(dir){
	var leftColHeight = $(".sidebar_left_2cols").height();
	var rightColHeight = $(".sidebar_right_2cols").height();
	if(dir == "up"){
		$(".sidebar_right_2cols").height(leftColHeight);
	}else if(dir == "down"){
		if(leftColHeight > $(".sidebar_right_2cols-inner").height())
		$(".sidebar_right_2cols").height(leftColHeight);
	}
}
