// --------------------------------------------------------
//  form ajout d'un wish
// --------------------------------------------------------
function sendQuestion() {
	var _idQuestion = $('#idQuestion').val();
	var _titre = $('#titre_liste').val();
	var _texte = $('#texte_liste').val(); 

	if ((_titre != '' && _idQuestion == '') || (_titre != '' && _idQuestion > 0) || (_titre != '' && _idQuestion != '')) {                
		$('#listForm').submit(); 
	}
	else {
		if (_titre == '') $('#titre_liste').css({'border' : '#CC0000 solid 1px'});
		else $('#titre_liste').css({'border' : '#CCCCCC solid 1px'});
		
		if (_texte == '') $('#texte_liste').css({'border' : '#CC0000 solid 1px'});
		else $('#texte_liste').css({'border' : '#CCCCCC solid 1px'});        		
	}
}

// --------------------------------------------------------
//  Envoi d'un abus
// --------------------------------------------------------
function sendAbus(_idMessage) {
	var _nom = $('#nom_abus').val();
	var _email = $('#email_abus').val();
	var _texte = $('#texte_abus').val(); 

	if (_nom != '' && _email != '' && _texte != '') {                
		$.post(
		"./ajax.php" , 
		{sendAbus : 1, idMessage : _idMessage, nom: _nom, email: _email, texte: _texte} , 
		function(data){ 
			$("#abus_"+_idMessage).html('<h1 style="margin:30px 20px 0px 40px; color:#FFF">Le message a été signalé!<br>Merci pour votre coopération!</h1>');
		}
		); 
	}
	else {
		if (_nom == '') $('#nom_abus').css({'border' : '#CC0000 solid 1px'});
		else $('#nom_abus').css({'border' : '#CCCCCC solid 1px'});

		if (_email == '') $('#email_abus').css({'border' : '#CC0000 solid 1px'});
		else $('#email_abus').css({'border' : '#CCCCCC solid 1px'}); 
		
		if (_texte == '') $('#texte_abus').css({'border' : '#CC0000 solid 1px'});
		else $('#texte_abus').css({'border' : '#CCCCCC solid 1px'});                
	}
}

var messageActu = 0;
// --------------------------------------------------------
//  Form abus
// --------------------------------------------------------
function formAbus(_idMessage) {
	$("#signaler_"+messageActu).fadeOut('fast'); 
	$.get(
	"./ajax.php" , 
	{formAbus : 1, idMessage : _idMessage} , 
	function(data){ 
		$("#signaler_"+_idMessage).fadeIn('fast').html(data);
	}
	); 
	messageActu = _idMessage;
}

// --------------------------------------------------------
//  Changer de liste
// --------------------------------------------------------
function changeLink() {
	i = document.formMalist.maListe.selectedIndex;
	if (i == 0) return;
	url = document.formMalist.maListe.options[i].value;
	parent.location.href = url;
}

function changeCheckbox(_data) {
	if ($("#"+_data).val() == '1') $("#"+_data).val(0);
	else $("#"+_data).val(1);	
}


//-----------------------------------------------------------------------
// Système de vote par points  (RANK)
//            Joni Jokhaji
//            17-09-2007
//-----------------------------------------------------------------------

var NOMBRE_ETOILES = 5;

//------------------------------------------------------------------------
// Initialisation des points
//------------------------------------------------------------------------
function init_votes2()
{             
	var ratings = document.getElementsByTagName('div');
	for (var i = 0; i < ratings.length; i++)
	{
		if (ratings[i].className != 'rating')
			continue;

		var rating = ratings[i].firstChild.nodeValue;
		ratings[i].removeChild(ratings[i].firstChild);
		if (rating > NOMBRE_ETOILES || rating < 0)
			continue;

		// On cherche les étoiles :-)
		for (var j = 0; j < NOMBRE_ETOILES; j++)
		{
			var etoile = document.createElement('img');
			if (rating >= 1)
			{
				etoile.setAttribute('src', 'http://www.babyfrance.com/images/stars/rating_on.gif');
				etoile.className = 'on';
				rating--;
			}
			else if(rating == 0.5)
			{
				etoile.setAttribute('src', 'http://www.babyfrance.com/images/stars/rating_half.gif');
				etoile.className = 'half';
				rating = 0;
			}
			else
			{
				etoile.setAttribute('src', 'http://www.babyfrance.com/images/stars/rating_off.gif');
				etoile.className = 'off';
			}
			var widgetId = ratings[i].getAttribute('id').substr(7);
			etoile.setAttribute('id', 'star_'+widgetId+'_'+j);
			etoile.onmouseover = new Function("evt", "displayHover2("+widgetId+", "+j+");");
			etoile.onmouseout = new Function("evt", "displayNormal2("+widgetId+", "+j+");");
			ratings[i].appendChild(etoile);
		}
	}
}

// --------------------------------------------------------
// Mettre à jour l'affichage du vote
// --------------------------------------------------------
function resultatVote2(_div) {

	var ratings = document.getElementById(_div);

		var rating = ratings.firstChild.nodeValue;
		ratings.removeChild(ratings.firstChild);

		for (var j = 0; j < NOMBRE_ETOILES; j++)
		{
			var etoile = document.createElement('img');
			if (rating >= 1)
			{
				etoile.setAttribute('src', 'http://www.babyfrance.com/images/stars/rating_on.gif');
				etoile.className = 'on';
				rating--;
			}
			else if(rating == 0.5)
			{
				etoile.setAttribute('src', 'http://www.babyfrance.com/images/stars/rating_half.gif');
				etoile.className = 'half';
				rating = 0;
			}
			else
			{
				etoile.setAttribute('src', 'http://www.babyfrance.com/images/stars/rating_off.gif');
				etoile.className = 'off';
			}
			var widgetId = ratings.getAttribute('id').substr(7);
			etoile.setAttribute('id', 'star_'+widgetId+'_'+j);
			etoile.onmouseover = new Function("evt", "displayHover2("+widgetId+", "+j+");");
			etoile.onmouseout = new Function("evt", "displayNormal2("+widgetId+", "+j+");");
			ratings.appendChild(etoile);
		}

}

// --------------------------------------------------------
// Afficher le hover des étoiles
// --------------------------------------------------------
function displayHover2(ratingId, etoile)
{
	for (var i = 0; i <= etoile; i++)
	{
		document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', 'http://www.babyfrance.com/images/stars/rating_over.gif');
	}
}

// --------------------------------------------------------
// Afficher normalement les étoiles
// --------------------------------------------------------
function displayNormal2(ratingId, etoile)
{
	for (var i = 0; i <= etoile; i++)
	{
		var status = document.getElementById('star_'+ratingId+'_'+i).className;
		document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', 'http://www.babyfrance.com/images/stars/rating_'+status+'.gif');
	}
}

// --------------------------------------------------------
// Voter
// --------------------------------------------------------
function vote2(evt) {
	var tmp = evt.target.getAttribute('id').substr(5);
	var _id = tmp.substr(0, tmp.indexOf('_'));
	var etoileNbr = tmp.substr(tmp.indexOf('_')+1);

	$("#resultat_"+_id).html("Attendez SVP ...");

	// AJAX --> mise à jour des données
	$.get(
		  "http://www.babyfrance.com/questions_reponses/ajax.php" ,
		  { action: 'vote' , ID: _id , points: parseInt(etoileNbr)+1 } ,
		  function(data){ $("#resultat_"+_id).show("fast").html(data); }
		 );

	// Mise à jour du vote
	$("#rating_"+_id).show("fast").html(parseInt(etoileNbr)+1);
	resultatVote2("rating_"+_id);
}

// --------------------------------------------------------
// Changerle rank d'une image
// --------------------------------------------------------
function changeRank2(_idi,_nidi) {

	$("#rating_"+_idi).html(_nidi);

	init_votes2();
}

// --------------------------------------------------------
// Initialiser l'affichage des étoiles
// --------------------------------------------------------
function init2() {
	init_votes2();
	$('div.rating img').click(vote2);
}

// --------------------------------------------------------
// Voter pour un commentaire
// --------------------------------------------------------
function voterMessage(_idMessage, _point) {

	$("#result_"+_idMessage).fadeIn("fast").html('<img src="images/attente_mini.gif">');
	
	$.get(
	"http://www.babyfrance.com/questions_reponses/ajax.php" , 
	{voterMessage:1, idMessage : _idMessage, point: _point} , 
	function(data){ 
		$("#result_"+_idMessage).html(data).fadeIn("fast");
	}
	);        
}
