function ajusterHauteur() {
	$("#boite-onglets").height($("#boite-" + tOnglets[idActuel]).height() + 35);
}

// onglets populaires, récents, commentaires
var tOnglets = ["tous-buzz", "adjuges", "rejetes"];
var idActuel = 0;
var idAncien = 0;

$(document).ready(function(){
	for (var i = 0; i < tOnglets.length; i++) {
		$("#boite-" + tOnglets[i]).css("position", "absolute");
		$("#" + tOnglets[i]).bind("click", function(){
			if (idActuel != tOnglets.arrayIndexOf(this.id)){
				idAncien = idActuel;
				idActuel = tOnglets.arrayIndexOf(this.id);
				$("#" + tOnglets[idActuel]).removeClass().addClass(tOnglets[idActuel] + "-actif");
				$("#" + tOnglets[idAncien]).removeClass().addClass(tOnglets[idAncien]);
				if ($("#boite-" + tOnglets[idActuel]).height() <= $("#boite-" + tOnglets[idAncien]).height()){
					$("#boite-" + tOnglets[idActuel]).fadeIn(300, ajusterHauteur);
					$("#boite-" + tOnglets[idAncien]).fadeOut(500);
				} else {
					ajusterHauteur();
					$("#boite-" + tOnglets[idActuel]).fadeIn(300);
					$("#boite-" + tOnglets[idAncien]).fadeOut(300);
				}
			}
		})
	};
	
	// DONNE UNE BONNE GROSSEUR AUX DIV CACHÉS POUR LAJOUT DE COMMENTAIRES
	var hauteurFormulaire = $('#formAjoutCommentaire').height();
	$('#enregistrementEnCours').height(hauteurFormulaire);
	$('#messageConfirmation').height(hauteurFormulaire);
	
	toggleText("textareaCommentaire");
	toggleText("diffCourriel1");
	toggleText("diffCourriel2");
	
	// Suivi
	$("#btnSuivi").bind("click", function(){
		if (!$("#btnSuivi").hasClass("off")){
			$("#field_suivi").attr('checked', true);
			setSuivi();
		} else {
			$("#field_suivi").attr('checked', false);
			enleverSuivi();
		}
	});
	
});	

// CODE POUR AJOUT DE COMMENTAIRES
function ajouteCommentaire(f,divId) {
	if ($('#textareaCommentaire').val() == '' || $('#textareaCommentaire').val() == 'Laisse ton message...') {
		alert('Tu dois composer un commentaire!');
		return false;
	}
	var data = $(f).serialize();
	data += '&ajax=true';
	if ($("#field_suivi").attr("checked")){	setSuivi();	} else { enleverSuivi(); }
	$('#formAjoutCommentaire').fadeOut('fast',function() {
		$('#enregistrementEnCours').fadeIn('fast',function() {
			$.post('/script/ajax/commentaireBuzz.php',
				data,
				function(commentaires) {
					$('#enregistrementEnCours').fadeOut('fast',function() {
						$('#messageConfirmation').fadeIn('fast');
						$('#'+divId).html(commentaires);
						$("#ajaxCommentaires").css("height","auto");
					});
				}
			);
		});
	});

	return false;
}
function remontreFormCommentaire() {
	$('#textareaCommentaire').val('Laisse ton message...');
	$('#messageConfirmation').fadeOut('fast',function() {
		$('#formAjoutCommentaire').fadeIn('fast');
	});
}

function toggleTextArea(selecteur) {
	var cible = $('#'+selecteur);
	cible.click(function(e) {
		if (cible.val() == cible.attr('title')) { alert('OK'); }
	});
}

// Pour cacher les commentaires
function cacheComment(intId_Commentaire) {
	$.post('/script/ajax/supprimerCommentaires.php','&commentaire='+intId_Commentaire,function(data) {
		if (data == '1') {
			if (!$('#commentaire_'+intId_Commentaire).prev().length) {
				$('#commentaire_'+intId_Commentaire).next().fadeOut('slow',function() { $(this).remove(); }); // Enleve le HR suivant parce que cest le premier commentaire
			} else {
				$('#commentaire_'+intId_Commentaire).prev().fadeOut('slow',function() { $(this).remove(); }); // Enleve le HR précédent
			}
			$('#commentaire_'+intId_Commentaire).fadeOut('slow',function() {
				$(this).remove();
				if (!$('#ajaxCommentaires div.commentaire').length) {
					$('#ajaxCommentaires').html("<p>Aucun commentaire n'a été posté. Sois le premier à commenter !<"+"/p>");
				}
			});
		} else {
			alert("Un problème s'est produit et le message n'a pu être caché.");
		}
	});
}

function voter(intBuzz,cassePasse,principal) {
	data = 'buzz='+intBuzz+'&opinion='+cassePasse;
	$.post('/script/ajax/vote.php',
		data,
		function(commentaires) {
			if (principal) {
				$('#voteChoix').hide();
				if (cassePasse == 1) {
					$('#votePasse').css({display:"block", position:"relative", left:"0px"}).show();
					publishOnFacebook("passe");
				} else {
					$('#voteCasse').css({display:"block", position:"relative", left:"0px"}).show();
					publishOnFacebook("casse");
				}
				$('embed.sIFR-flash').show();
			} else {
				$('#vote'+intBuzz).fadeOut('fast',function() {
					if (cassePasse == 1) {
						$(this).before('<span class="buzz-deja-passe">Je viens de voter: <'+'/span>');
					} else {
						$(this).before('<span class="buzz-deja-casse">Je viens de voter: <'+'/span>');
					}
					$(this).next('br.clear').remove();
					$(this).remove();
				});
			}
		}
	);
}

function abonDiffusion(form,p) {
	var infosAbon = $('#'+form).serialize();
	$("#"+p).height($("#"+p).height());
	$("#"+p).html("Envoi en cours...");
	$.post('/script/ajax/diffusionAbonBuzz.php',infosAbon,function(data) {
		if (data == 'OK') {
			$("#"+p).html("Merci, tu recevras les résultats de ce buzz par courriel dès qu'ils seront disponibles!");
		} else if (data == 'ERR_EMAIL') { 
			$("#"+p).html("Une erreur s'est produite, réessaie plus tard.");
		}
	});
	return false;
}

function abonDiffusionMembre(form,p,check){
	var infosAbon = $('#' + form).serialize();
	$("#" + p).height($("#" + p).height());
	$("#" + p).html("Envoi en cours...");
	$.post('/script/ajax/diffusionAbonBuzz.php',infosAbon,function(data) {
		if (data == "OK") {
			$("#" + p).html("Merci, tu recevras les résultats de ce buzz par courriel dès qu'ils seront disponibles!");
		} else { 
			$("#" + p).html("Une erreur s'est produite, réessaie plus tard.");
		}
	});
}
function desabonDiffusionMembre(form,p,check){
	var infosAbon = $('#' + form).serialize();
	$("#" + p).height($("#" + p).height());
	$("#" + p).html("Envoi en cours...");
	$.post('/script/ajax/diffusionDesabonBuzz.php',infosAbon,function(data) {
		if (data == "OK") {
			$("#" + p).html("Tu ne recevras plus les résultats de ce buzz par courriel.");
		} else { 
			$("#" + p).html("Une erreur s'est produite, réessaie plus tard.");
		}
	});
}