var IS_IE = (document.all) ? true : false;
var CLASS_NAME = (IS_IE) ? "className" : "class";

var regex = new RegExp("[A-Za-z0-9._-]{1,}@[A-Za-z0-9._-]{1,}[.][A-Za-z]{2,}", "gi"); 

// --------------------------------------------------------
//  NEWSBOX -> homepage
// --------------------------------------------------------
function showArticle(_idMot) {
	var t = new Date();
	var time1 = t.getTime();
	//$("#box_newsbox").fadeOut("fast");  
	$("#box_newsbox_wait").show();
	$.get(
		"http://www.baby.be/nl/spip.php?page=ajax_newsbox" , 
		{ idMot: _idMot } , 
		function(data){ 	
			$("#box_newsbox").html(data);  	     
			$("#box_newsbox_wait").hide("fast");		
		}
	);       
	
}

function confirmBox(_content, _titre, _mod, _darg) { 
	new Boxy(_content, {title: _titre, modal: _mod, closeable:true, closeText:'Sluiten', clone:false, draggable:_darg});    
}

// --------------------------------------------------------
// Poster newsletter
// --------------------------------------------------------
function post_newsletter(_idArticle) {
	_email = $("#newsltter_form").val();
	
	$("#envoi_newsletter").show("fast").html("Envoi en cours ...");
	
	var t = new Date();
	$.post(
		"./ajax.php" , 
		{ post_newsletter: 1, email: _email, t: t.getTime()} , 
		function(data){ $("#envoi_newsletter").show("fast").html(data); }
	);	
}

// --------------------------------------------------------
//  NEWSBOX -> homepage FUN
// --------------------------------------------------------
function showArticleFun(_idMot) {
	var t = new Date();
	var time1 = t.getTime();
	//$("#box_newsbox").fadeOut("fast");  
	$("#box_newsbox_wait").show();
	$.get(
		"http://www.famidoo.be/fr2/spip.php?page=ajax_newsbox_fun" , 
		{ idMot: _idMot } , 
		function(data){     
			$("#box_newsbox").html(data);           
			$("#box_newsbox_wait").hide("fast");        
		}
	);       
	
}

// --------------------------------------------------------
//  Show loginbox
// --------------------------------------------------------
function openLogin() {
	$('#loginbox').show('fast');
}

// --------------------------------------------------------
//  Fermer un objet
// --------------------------------------------------------
function fermer(_div) {
	$("#"+_div).fadeOut('fast');	
}

// --------------------------------------------------------
// Envoyer article à un ami
// --------------------------------------------------------
function envoie_ami(_idArticle) {
	var t = new Date();
	
	$("#envoi_ami").show("fast").html("Envoi en cours ...");
	
	$.get(
		"./envoi_ami.php" , 
		{ idArticle: _idArticle, t: t.getTime()} , 
		function(data){ $("#envoi_ami").show("fast").html(data); }
	);	
}

// --------------------------------------------------------
// Poster article
// --------------------------------------------------------
function post_ami(_idArticle) {
	
	_nom = escape(document.inviter.nom.value);
	_email = escape(document.inviter.email.value);
	_email1 = escape(document.inviter.email1.value);
	_email2 = escape(document.inviter.email2.value);
	_email3 = escape(document.inviter.email3.value);
	
	$("#envoi_ami").show("fast").html("Envoi en cours ...");
	
	var t = new Date();
	$.post(
		"./ajax.php" , 
		{ envoiAmi: 1, idArticle: _idArticle, nom: _nom, email: _email, email1: _email1, email2: _email2, email3: _email3, t: t.getTime()} , 
		function(data){ $("#envoi_ami").show("fast").html(data); }
	);	
}


// --------------------------------------------------------
// Envoie d'un commentaire sur un article
// --------------------------------------------------------
function envoiComment() {
	
	_idArticle = document.commentaire.idArticle.value;
	_message = document.commentaire.message.value;
	
	if (_idArticle > 0 && _message != '')
		document.commentaire.submit(); 
	else
	{
		if (_message == '') document.getElementById('message').style.background = "#CCCFFF";
	}
}


// --------------------------------------------------------
// Sondages : Voter
// --------------------------------------------------------
function sondagesVoter(_poll_id) {
	var _option_id = '';
	_option_id = $("#sondages input:checked").val();

	$("#chargeSondage").fadeIn("fast").html('<center><img src="http://www.famidoo.be/fr2/images/loading.gif"></center>');
	
	$.post(
	"ajax.php" , 
	{poll_ident : _poll_id, action : "vote", option_id : _option_id} , 
	function(data){ 
		$("#ajaxSondage").html(data).fadeIn("fast"); 
		$("#chargeSondage").fadeOut("fast");  
	}
	);		
}

// --------------------------------------------------------
// Sondages : Voir les resultats
// --------------------------------------------------------
function sondagesResults(_poll_id) {
	$("#chargeSondage").fadeIn("fast").html('<center><img src="http://www.famidoo.be/fr2/images/loading.gif"></center>');
	
	$.post(
	"ajax.php" , 
	{poll_ident : _poll_id, action : "voir"} , 
	function(data){ 
		$("#ajaxSondage").html(data).fadeIn("fast"); 
		$("#chargeSondage").fadeOut("fast"); 
	}
	);		
}

// --------------------------------------------------------
// Sondages : Retour
// --------------------------------------------------------
function sondagesRetour(_poll_id) {
	$("#chargeSondage").fadeIn("fast").html('<center><img src="http://www.famidoo.be/fr2/images/loading.gif"></center>');
					 
	$.post(
	"ajax.php" , 
	{poll_ident : _poll_id, action : "autre"} , 
	function(data){   
		$("#ajaxSondage").html(data).fadeIn("fast"); 
		$("#chargeSondage").fadeOut("fast");
	}
	);		
}

// --------------------------------------------------------
// Form commentaire
// --------------------------------------------------------
function formComment(_action, _id, _template) {
	$("#chargeSondage").fadeIn("fast").html('<center><img src="http://www.famidoo.be/fr2/images/loading.gif"></center>');
	
	$.post(
	"./ajax.php" , 
	{formComment: 1, id : _id, action : "send"} , 
	function(data){ 
		$("#ajaxSondage").html(data); 
		$("#chargeSondage").fadeOut("fast");  
	}
	);		
}

// --------------------------------------------------------
// LOGIN babyclub
// --------------------------------------------------------
function loginBabyclub(_fichier) {
	_login = $("#login").val();
	_pass = $("#pass").val();
	
	$.post(
	_fichier , 
	{login_babyblub : 1, login : _login, pass : _pass} , 
	function(data){ 
		$("#contentLoginForm").html(data); 
		location.reload(false);
	}
	);
}

// --------------------------------------------------------
// Form oublié mon mot de passe
// --------------------------------------------------------
function formOublie(_fichier, _div) {
  
	if (_div == null) _div = 'login_centre';
	$("#"+_div).html('<center><img src="images/loading.gif"></center>').fadeIn("fast");
	$.get(
	""+_fichier+"" , 
	{formOubie : 1, fichier : _fichier, div : _div} , 
	function(data){ 
	  //alert(data);
		$("#"+_div).html(data).fadeIn("fast"); 
	}
	);
}

// --------------------------------------------------------
// Envoie Form oublié mon mot de passe
// --------------------------------------------------------
function envoiFormOublie(_fichier, _div) {
	if (_div == null) _div = 'login_centre';  
	_email = $("#email").val();
	if (_email.search(regex) != -1) {
		$("#"+_div).html('<center><img src="images/loading.gif"></center>').fadeIn("fast");  
		$.post(
		""+_fichier+"" , 
		{envoiFormOublie : 1, email:_email, div : _div} , 
		function(data){ 
		  //alert('-->'+data);
			$("#"+_div).html(data).fadeIn("fast"); 
		}
		);
	}
	else
		document.getElementById('email').style.background = "#ffcccc";
}


function envoiForm(formulaire) {
	_nom = $("#nom").val();
	_email = $("#email").val();
	_societe = $("#societe").val();
	_fonction = $("#fonction").val();
	_telephone = $("#telephone").val();
	_message = $("#message").val();
	_type = $("#type").val();
	
	$("#formulaire").html("<br><br><center><b>Envoi en cours ...</b></center>"); 
	
	$.post(
		"./ajax.php" , 
		{ nom: _nom, email: _email, societe: _societe, fonction: _fonction, telephone: _telephone, message: _message, type: _type } , 
		function(data){ $("#formulaire").html(data); }
	);    
}   

function formCommentSondages(_id, _titre) {    
	var _t = $("#titreSondage").html();
	if (_t == null)
		_t = _titre;
	$("#tiers3").html('<br><br><br><br><center><img src="images/loading.gif"> Formulaire en chargement ...</center>').fadeIn("fast");    
	$.get(
		"./ajax.php" , 
		{ formSondages: 1, id:_id, titre:_t} , 
		function(data){ $("#tiers3").html(data).fadeIn("fast"); }
	);    
}

function envoiFormCommentSondages(_id, _titre) {
	_message = $("#messageSondage").val();
	
	var erreur = 0;
	if (_message == '') {
		document.getElementById('messageSondage').style.background = "#ffcccc";
		erreur++;
	}
	else
		document.getElementById('messageSondage').style.background = ""; 
	
	if (erreur == 0) {
		$("#formOnDenonce").html('<img src="images/loading.gif"> Envoi en cours ...').fadeIn("fast"); 
		$.post(
			"./ajax.php" , 
			{envoiFormCommentSondages:1, message: _message, id:_id, titre:_titre } , 
			function(data){ $("#tiers3").html(data); }
		);  
	}
}


function apercu() {
	_texte = $("#texte").val();
	if(_texte != '') {
		$.post(
			"http://www.famidoo.be/fr2/babyclub/forum/ajax_apercu.php" , 
			{ texte: _texte} , 
			function(data){ $("#apercu").html(data); }
		);
	}
	else
		$("#apercu").hide();
}

var actualUser = 0;

function infoBullUser(_idCompte, _pseudo) {
	
	if (actualUser > 0)
		$("#userProfil_"+actualUser).fadeOut('fast');

	$("#userProfil_"+_idCompte).fadeIn("fast");
	//$("#bullPseudo_"+_idCompte).html(_pseudo);
	
	actualUser = _idCompte;  
	
	$.ajax({
	  type: "GET",
	  url: "http://www.famidoo.be/fr2/ajax.php",
	  cache: true,
	  data: "INFOBULLUSER&idCompte="+_idCompte,
	  success: function(data){
		$("#userProfil_"+_idCompte).html(data);
	  }
	});    

}

function masquer(_div1, _div2) {
	$("#"+_div2).html('<a href="javascript:afficher(\''+_div1+'\', \''+_div2+'\')">réafficher</a>');
	$("#"+_div1).slideUp("slow");    
}

function afficher(_div1, _div2) {
	$("#"+_div2).html('<a href="javascript:masquer(\''+_div1+'\', \''+_div2+'\')">Masquer</a>');
	$("#"+_div1).slideDown("slow");
}

function showDashBoard(_idCompte, _page) {
	$.get(
		"http://www.famidoo.be/fr2/babyclub/dashboard.php" , 
		{ idCompte: _idCompte, page: _page} , 
		function(data){ $("#dashBoardList").html(data); }
	);  
}

// --------------------------------------------------------
// Add to NOD list
// --------------------------------------------------------
function addToNodList(_date, _div) {
	$("#"+_div).html('<img src="http://www.famidoo.be/fr2/images/loading.gif">').fadeIn("fast");
	$.get(
	"http://www.famidoo.be/fr2/ajax.php" , 
	{addToNodList : 1, date : _date} , 
	function(data){ 
		$("#"+_div).html(data).fadeIn("fast"); 
		//showNodList(_date, _div);
	}
	);
}

// --------------------------------------------------------
// Show NOD list
// --------------------------------------------------------
function showNodList(_date, _div) {
	$("#"+_div).html('<img src="http://www.famidoo.be/fr2/images/loading.gif">').fadeIn("fast");
	$.get(
	"http://www.famidoo.be/fr2/ajax.php" , 
	{showNodlist : 1} , 
	function(data){ 
		$("#"+_div).html(data).fadeIn("fast"); 
	}
	);
}

//-----------------------------------------------------------------------
// Système de vote par points  (RANK)
//-----------------------------------------------------------------------

var NOMBRE_ETOILES = 5;

//------------------------------------------------------------------------
// Initialisation des points
//------------------------------------------------------------------------
function init_votes()
{
	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', './images/stars/rating_on.png');
				etoile.className = 'on';
				rating--;
			}
			else if(rating == 0.5)
			{
				etoile.setAttribute('src', './images/stars/rating_half.png');
				etoile.className = 'half';
				rating = 0;
			}
			else
			{
				etoile.setAttribute('src', './images/stars/rating_off.png');
				etoile.className = 'off';
			}
			var widgetId = ratings[i].getAttribute('id').substr(7);
			etoile.setAttribute('id', 'star_'+widgetId+'_'+j);
			etoile.onmouseover = new Function("evt", "displayHover("+widgetId+", "+j+");");
			etoile.onmouseout = new Function("evt", "displayNormal("+widgetId+", "+j+");");
			ratings[i].appendChild(etoile);
		}
	}
}

// --------------------------------------------------------
// Mettre à jour l'affichage du vote
// --------------------------------------------------------
function resultatVote(_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', './images/stars/rating_on.png');
				etoile.className = 'on';
				rating--;
			}
			else if(rating == 0.5)
			{
				etoile.setAttribute('src', './images/stars/rating_half.png');
				etoile.className = 'half';
				rating = 0;
			}
			else
			{
				etoile.setAttribute('src', './images/stars/rating_off.png');
				etoile.className = 'off';
			}
			var widgetId = ratings.getAttribute('id').substr(7);
			etoile.setAttribute('id', 'star_'+widgetId+'_'+j);
			etoile.onmouseover = new Function("evt", "displayHover("+widgetId+", "+j+");");
			etoile.onmouseout = new Function("evt", "displayNormal("+widgetId+", "+j+");");
			ratings.appendChild(etoile);
		}

}

// --------------------------------------------------------
// Afficher le hover des étoiles
// --------------------------------------------------------
function displayHover(ratingId, etoile)
{
	for (var i = 0; i <= etoile; i++)
	{
		document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_over.png');
	}
}

// --------------------------------------------------------
// Afficher normalement les étoiles
// --------------------------------------------------------
function displayNormal(ratingId, etoile)
{
	for (var i = 0; i <= etoile; i++)
	{
		var status = document.getElementById('star_'+ratingId+'_'+i).className;
		document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_'+status+'.png');
	}
}

// --------------------------------------------------------
// Voter
// --------------------------------------------------------
function vote(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(
		  "./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);
	resultatVote("rating_"+_id);
}

// --------------------------------------------------------
// Changerle rank d'une image
// --------------------------------------------------------
function changeRank(_idi,_nidi) {

	$("#reting_"+_idi).html(_nidi);

	init_votes();
}

// --------------------------------------------------------
// Initialiser l'affichage des étoiles
// --------------------------------------------------------
function init() {
	init_votes();
	$('div.rating img').click(vote);
}


function showDivs() {
	$('#ad').hide(); 
	$('#container').show(); 
	$('#footer_wrapper').show();    
}

function hideDivs() {
	$('#container').hide();
	$('#footer_wrapper').hide(); 
}


// --------------------------------------------------------
//  NEWSBOX -> homepage
// --------------------------------------------------------
function registerEmail() {
  _email = $('#email_concours').val();
  if (_email != '') {
  	$("#box_email").html('Chargement ...').fadeIn('fast');
  	$.post(
  		"http://www.famidoo.be/fr/concours/register.php" , 
  		{ email_concours: _email } , 
  		function(data){ 	     
  			$("#box_email").html(data);
  		}
  	);       
	}
	else 
	 $("#box_email").html('Veuillez indiquer votre mot de passe SVP!').fadeIn('fast');
}