var intervalNotif 	= 30000;
var pageNotifs		= 'methods/notifications.post.php';
var idTemporized	= 0;
var timeFadeOut		= 0;
var nbNotifs		= 0;

//////////////////////////////////////////////////
//
//   VUES
//
//////////////////////////////////////////////////

function afficheFlashNotif(pseudoSender) {
var text = "<div class='notifboxflash'>Flash !<br /><a href='"+pseudoSender+"'><span class='boldtitle'>"+pseudoSender+"</span></a><span class='normal'> a flashé sur vous.</span></div>";
return text;	
}

function afficheGiftNotif(pseudoSender) {
var text = "<div class='notifboxgift'>Cadeau !<br /><a href='"+pseudoSender+"'><span class='boldtitle'>"+pseudoSender+"</span></a><span class='normal'> vous a offert un cadeau.</span></div>";
return text;	
}

function afficheVisiteNotif(pseudoSender) {
var text = "<div class='notifboxvisite'>Nouvelle visite !<br /><a href='"+pseudoSender+"'><span class='boldtitle'>"+pseudoSender+"</span></a><span class='normal'> visite votre profil.</span></div>";
return text;	
}

function afficheCommentNotif(pseudoSender) {
var text = "<div class='notifboxcomment'>Nouveau commentaire !<br /><a href='"+pseudoSender+"'><span class='boldtitle'>"+pseudoSender+"</span></a><span class='normal'> a commenté votre tweet.</span></div>";
return text;	
}

function afficheMateRequestNotif(pseudoSender) {
var text = "<div class='notifboxrequest'>Nouveau contact !<br /><a href='"+pseudoSender+"'><span class='boldtitle'>"+pseudoSender+"</span></a><span class='normal'> souhaite faire partie de vos contacts.</span></div>";
return text;	
}

function afficheMateAcceptNotif(pseudoSender) {
var text = "<div class='notifboxrequest'>Nouveau contact !<br /><a href='"+pseudoSender+"'><span class='boldtitle'>"+pseudoSender+"</span></a><span class='normal'> a accepté votre demande de Mate.</span></div>";
return text;
}

function afficheMessageNotif(pseudoSender) {
var text = "<div class='notifboxmessage'>Nouveau Message !<br /><a href='"+pseudoSender+"'><span class='boldtitle'>"+pseudoSender+"</span></a><span class='normal'> vous a envoyé un message privé.</span></div>";
return text;
}

function afficheNbNotif(nbNotifs) {
var text = "<div class='notifboxnotif'>Vous avez "+nbNotifs+" notifications</div>";
return text;
}
////////////////////////////////////////////////////////
//
//  AFFICHAGE DES NOTIFS
//
// On gère selon les specs :
//////
	// 1 : Gift
	// 2 : Comment
	// 3 : flash
	// 4 : visite
	// 5 : request Mate
	// 6 : accept Mate 
	// 7 : Message reçu
//////
////////////////////////////////////////////////////////


function afficheMessageNotifs(data) {
var afficheText = "";
for (var i = 0; i < data.length; i++) {
	
		//On teste quel type de notif c'est
		if(data[i]['type']==1) {
		afficheText += afficheGiftNotif(data[i]['pseudoSender']);
		}
		if(data[i]['type']==2) {
		afficheText += afficheCommentNotif(data[i]['pseudoSender']);
		}
		if(data[i]['type']==3) {
		afficheText += afficheFlashNotif(data[i]['pseudoSender']);
		}
		if(data[i]['type']==4) {
		afficheText += afficheVisiteNotif(data[i]['pseudoSender']);
		}
		if(data[i]['type']==5) {
		afficheText += afficheMateRequestNotif(data[i]['pseudoSender']);
		}
		if(data[i]['type']==6) {
		afficheText += afficheMateAcceptNotif(data[i]['pseudoSender']);
		}
		if(data[i]['type']==7) {
		afficheText += afficheMessageNotif(data[i]['pseudoSender']);
		}
		nbNotifs++;
}
	
$('#notifs').prepend(afficheText);
}

//////////////////////////////////////////////////
//
//   CONTROLEUR
//
//////////////////////////////////////////////////

function getNotifs() {
	$.get(pageNotifs, {action:'notifs'}, function(data) {

							   if(data.status==404) {
								
							   } 
							   else {
								afficheMessageNotifs(data);
								$('#notifs').fadeIn(function() {
										
										if(nbNotifs>3) {
												$('#toomuchnotifs').fadeIn(function() {
												$('#notifs').fadeOut(function() {
												$('#toomuchnotifs').html(afficheNbNotif(nbNotifs));
									  			});
												});
										}
															 
															 });
							   }
							   
							   },"json");
}

///////////////////////////////////////////////////////////////
//
// GESTION DES NOTIFS
//
///////////////////////////////////////////////////////////////

$(document).everyTime(intervalNotif,function(i) {
  getNotifs();
  
  // Si trop de notifications et affichage de la liste des notifs
  $('#toomuchnotifs').click(function() {
	$(this).fadeOut( function() {								 
	$('#notifs').fadeIn();
							  });
									 });	
});


