
var commentVisible = 0;

function showComments() {
	if (commentVisible == 0) {
		$('#zone-comments h4, #zone-comments h5').fadeIn();
		$('#view-comments div.comment-u:lt(10)').css('display','block');
		$('#view-comments, #post-comment').slideDown();
		commentVisible = 1;
	}
}

// Une fois tous montrés, je peux cacher un certain nombre d'éléments
function showedComments() {
	$('#zone-comments h4, #zone-comments h5').fadeOut();
}

// Change le status d'un élément
function switchStatus(selecteur, status){
	$(selecteur).parent('div').children('span').removeClass().addClass(status);
}

function submitComments(){
	$.post("actions/ajaxActions.php", {
		'ajaxAction': 'comment',
		'fbid'		: $('#post-comment input[name=fbid]').val(),
		'fbcheck'	: $('#post-comment input[name=fbcheck]').val(),
		'pour'		: $('#post-comment input[name=pour]').val(),
		'ref'		: $('#post-comment input[name=ref]').val(),
		'prenom'	: $('#post-comment input[name=prenom]').val(),
		'email'		: $('#post-comment input[name=email]').val(),
		'commentaire'	: $('#post-comment textarea[name=commentaire]').val()
	},
	function(data){
		if (data.erreur == '0')
		{
			$('#post-comment').fadeOut();
			$('#view-comments').prepend(data.html);
			$('#view-comments div.comment-u:first').show();
			$('html, body').animate({scrollTop: $('#zone-comments').offset().top}, 800);
			$('#post-comment textarea[name=commentaire]').val();
		}
		else
		{
			alert('erreur');
		}
	}, "json");
}

$(function() {

	// Afficher le bloc commentaire
	$('A.showComments').click(function(){
		showComments();
		$('html, body').animate({scrollTop: $('#zone-comments').offset().top}, 800);
		$('#post-comment').css('display','block');
		return false;
	});

	// Afficher le formulaire commentaire
	$('A.showPostComments').click(function(){
		// je vais tout en bas de la page
		$('#view-comments, #post-comment').css('display','block');
		showComments();
		$('html, body').animate({scrollTop: $('#post-comment').offset().top}, 800);
		//$('#post-comment input[name=prenom]').focus();
		return false;
	});

	// Afficher 10 commentaires de plus
	$('A.showMoreComments').click(function(){
		showComments();
//		$('html, body').animate({scrollTop:$('#view-comments div.comment-u:hidden:first')}, 800);
		$('#view-comments div.comment-u:hidden:lt(10)').slideDown();
		if ($('#view-comments div.comment-u:hidden').size() < 10) {
			switch($('#view-comments div.comment-u:hidden').size()){
				case 0:
					showedComments();
					break;
				case 1:
					$('A.showMoreComments').html('Afficher 1 commentaire supplémentaire');
					break;
				default:
					$('A.showMoreComments').html('Afficher '+$('#view-comments div.comment-u:hidden').size()+' commentaires supplémentaires');
			} // switch
		}
		return false;
	});


	// Afficher tout les commentaies
	/*$('A.showAllComments').click(function(){
		$('html, body').animate({scrollTop:$('#view-comments div.comment-u:hidden:first')}, 800);
		$('#view-comments div.comment-u:hidden').slideDown();
		showedComments();
		return false;
	});*/

	$('A.showAllComments').click(function(){
		$('html, body').animate({scrollTop:$('#view-comments').offset().top}, 800);
		$('#view-comments div.comment-u:hidden').slideDown();
		showedComments();
		return false;
	});

	// Vérifs dans le formulaire Commentaires
	$('#post-comment input[name=prenom], #post-comment textarea[name=commentaire]').keyup(function()
	{
		if ($(this).val() == '' || $(this).val() == null) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});

	$('#post-comment input[name=email]').bind('keyup blur', function() {
		if (!checkEmail($(this).val())) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});


	// Poster un commentaire
	$('A.postComment').click(function(){
		// reste-t-il des erreurs ?
		if (countErrors($('#post-comment')) == 0) {
			submitComments();
		}
		else
		{
			blinkError($('#post-comment'));
		}
		return false;
	});



});
