// ---------------------------------
// --- Design Patterns sexys -------
// ---------------------------------

var url_site="comments/";

/* Vide l'input quand on le focus
obj : chaine permettant de reperer l'input (ex : "#prenom", "input.lol", etc)
*/
function clear_input_init(obj)
{
	function clear_input_bind(obj, val)
	{
		$(obj).focus(function()
		{
			if ($(this).val() == val)
			{
				$(this).html('');
				$(this).val('');
				$(this).addClass('filled');
			}
		});
		$(obj).blur(function()
		{
			if ($(this).val() == '')
			{
				$(this).html(val);
				$(this).val(val);
				$(this).removeClass('filled');
			}
		});
	}
	$(obj).each(function()
	{
		// Utilise une closure pour retenir la valeur initiale
		clear_input_bind(this, $(this).val());
	});
}

// ---------------------------------
// --- Fin Design Patterns sexys ---
// ---------------------------------


// ---------------------------------
// ----------- Star Rate -----------
// ---------------------------------


function star_init(star_on, star_off, star_none)
{
	$('.input_star').each(function()
	{
		var div = this;
		$(div).attr('nonesrc', star_none);
		$('img', div).attr('val', '');
		$('img', div).each(function(i, e)
		{
			$(e).mouseover(function()
			{
				$('img', div).each(function(j, e)
				{
					if (j <= i)
						$(this).attr('src', star_on)
					else
						$(this).attr('src', star_off)
				});
			});
			$(e).click(function()
			{
				$('input', div).attr('val', i + 1 );
			});
			$(e).mouseout(function()
			{
				if ($('input', div).attr('val') == '')
					$('img', div).attr('src', star_none);
				else
				{
					var val = $('input', div).attr('val');
					$('img', div).each(function(j, e)
					{
						if (j < val)
							$(this).attr('src', star_on)
						else
							$(this).attr('src', star_off)
					});
				}
			});
		});
	});
}

// ---------------------------------
// ------------ Captcha ------------
// ---------------------------------


function captcha_init()
{
	$('#comment_form_captcha img').mousedown(function(e)
	{
		$('#comment_form_captcha img').removeClass('selected');
		$('#comment_form_captcha .title').removeClass('input_error');
		$('#comment_form_captcha_val').val($(this).attr('val'));
		$('#comment_form_captcha').prev('.input_notif').fadeOut(300,function(){$(this).remove();});
		$(this).addClass('selected');
		e.preventDefault();
	});
}

function captcha_reload()
{
	$.get(url_site + 'ajax_captcha_reload.php', {}, function(data)
	{
		$('#comment_form_captcha').html(data);
		captcha_init();
	});
}

// Déroule la liste des commentaires
$(document).ready(function()
{
	$('#comment_list_all_button').toggle(function()
	{
		$.get(url_site + 'ajax_comment_list_all.php',
			{
				type:$(this).attr('fiche_type'),
				id:$(this).attr('fiche_id')
			}, function(data)
			{
				var div = $('<div>' + data + '</div>').hide();
				$('#comment_list_contener').append(div);
				div.slideDown(500, function()
				{
					$('.comment_elt', div).unwrap();				
				});
				$('#comment_list_all_button').html('Replier');
			});
	}, function()
	{
		$('#comment_list_contener .comment_elt:gt(1)').slideUp(500, function(){$(this).remove();})
		$('#comment_list_all_button').html('Voir tout');

	});
});

// ---------------------------------
// -- Formulaire de commentaires ---
// ---------------------------------

function comment_form_init()
{
	$('#comment_form_submit').click(function() // Soumet le formulaire
	{
		$('#comment_form .input_notif').fadeOut(500,function(){
			$(this).remove();
		});
	
		var ok = true;
		var param = new Object();
		
		param.fiche_id = $('#comment_fiche_id').val();
		param.fiche_type = $('#comment_fiche_type').val();
		if ($('#comment_form_pseudo').hasClass('filled'))
			param.pseudo = $('#comment_form_pseudo').val();
		else
			param.pseudo = '';
		if ($('#comment_form_message').hasClass('filled'))
			param.message = $('#comment_form_message').val();
		else
			param.message = '';
		param.captcha_val = $('#comment_form_captcha_val').val();
		param.captcha_id = $('#comment_form_pseudo_captcha_id').val();

		// Check param supplémentaires
		$('#comment_form_supp div.input_text, #comment_form_supp div.input_star').each(function()
		{
			var input = $('input',this);
			if ($(this).hasClass('input_text'))
			{
				if (input.hasClass('filled'))
					param[input.attr('id')] = input.val();
			}
			else if ($(this).hasClass('input_mark'))
			{
				if (input.attr('val') != '')
					param[input.attr('id')] = input.val();
			}
		});

		// Lance la requete si tout est conforme
		$.getJSON(url_site + 'ajax_comment_add.php',
		param,
		function(result)
		{
			if (result.error)
			{
				alert(result.msg);
				return;
			}
			if (!result.sent) // Erreur dans le formulaire
			{
				for(var key in result.list)
				{
					if (key == 'comment_form_captcha')
						captcha_reload();
					var notif = $('<div class="input_notif"><img src="img/bulle_arrow_left.png"/>'+result.list[key]+'</div>');
					var pos = {
						comment_form_pseudo : {marginLeft : '-215px', marginTop: '10px', position: 'absolute'},
						comment_form_message : {marginLeft : '-215px', marginTop: '20px', position: 'absolute'},
						comment_form_captcha : {marginLeft : '-215px', marginTop: '15px', position: 'absolute'}
					};
					if (pos[key])
					{
						notif.css(pos[key]);
						$('#'+key).before(notif);
						notif.fadeIn(300);
					}
				}
			}
			else // Commentaire ajouté
			{
				// Vide le formulaire
				$('#comment_form input').val('').removeClass('filled').focus().blur();
				$('#comment_form textarea').val('').html('').removeClass('filled').focus().blur();
				captcha_reload();
				$('.input_star').each(function()
				{
					$('input',this).val('');
					var nonesrc = $(this).attr('nonesrc');
					$('img',this).each(function()
					{
						$(this).attr('src',nonesrc);
					});
				});
				
				// Ajoute le commentaire à la liste
				//$.scrollTo('#comment_list',200);
				$('#comment_list_contener .comment_elt').removeClass('last_inserted');
				var newcom = $(result.last).hide();
				if ($('#comment_list .comment_elt').size() > 1)
					$('.comment_elt:last').slideUp(300, function(){
						$(this).remove();
					});
				$('#comment_list_contener').prepend(newcom);
				newcom.addClass('last_inserted');
				newcom.slideDown(300);
			}
		});
	});
}

