// --------------------------------------------------------
//  Affiche le formualire des comments
// --------------------------------------------------------
function laisser_comment(_idi, _idCompte) {
	$("#commentForm").fadeIn("fast");
	$.post(
		"ajax.php" , 
		{ FORMCOMMENT : 1, idi : _idi, idCompte: _idCompte } , 
		function(data){ $("#laisser_comment").fadeIn("fast").html(data); }
	); 
}

// --------------------------------------------------------
//  Envoyer le comments
// --------------------------------------------------------
function postComment(_idi, _idCompte) {
	$("#commentForm").fadeIn("fast");
	
	_texte = $('#textComment').val();
	
	$.post(
		"ajax.php" , 
		{ POSTCOMMENT : 1, idVideo : _idi, texte: _texte, idCompte: _idCompte } , 
		function(data){ $("#laisser_comment").fadeIn("fast").html(data); }
	); 
}

// --------------------------------------------------------
//  Afficher les comments d'une image
// --------------------------------------------------------
function comments(_idi) {
	$.get(
		"ajax.php" , 
		{ COMMENTS : 1, idi : _idi } , 
		function(data){ $("#comments").fadeIn("fast").html(data); }
	); 
}

// --------------------------------------------------------
//  Supprimer le comment
// --------------------------------------------------------
function supprimerComment(_idComment, _idv) {    
	if(confirm('Êtes-vous sûr de vouloir supprimer ce commentaire?')) {
		$.post(
			"ajax.php" , 
			{ SUPPRIMERCOMMENT : 1, idComment: _idComment, idVideo : _idv} , 
			function(data){ $("#commentaire_"+_idComment).fadeOut("fast"); }
		); 
	}
}


// --------------------------------------------------------
//  Afficher une image  (Galerie)
// --------------------------------------------------------
function afficheImg(_id, _idi) {
	$.get(
		"ajax.php" , 
		{ AFFICHEIMG : 1, idi : _idi, id : _id } , 
		function(data){ 
			$("#affichePhoto").hide();
			$("#affichePhoto").fadeIn("slow").html(data); 
		}
	); 
	comments(_idi);
	$("#laisser_comment").hide();
	
	//changeRank(document.getElementById("rate_img").value,_idi);
}

function fermer(_id) {
	$("#"+_id).fadeOut("fast");
}

// --------------------------------------------------------
//  Récupère la réponse HTTPREQUEST
// --------------------------------------------------------
function getRequestResponse(_request)
{ 
	date = new Date();
	var req = null;
	if(window.XMLHttpRequest)
	   req = new XMLHttpRequest();
	else if (window.ActiveXObject)
	   req  = new ActiveXObject("Microsoft.XMLHTTP");

	 req.open("GET",_request+"&"+date.getTime(), true);
	req.send(null);

	if(req.readyState == 4)
	{
		return req.responseText;
	}
}

// --------------------------------------------------------
//  TEST si dossier tmp OK
// --------------------------------------------------------
function testAlbum() {
	var rep = '';
	rep = getRequestResponse('ajax.php?TESTALBUM');
	if (rep == 'ok') return 1;
	else return 0;
	
}

// --------------------------------------------------------
//  Test formulaire Album
// --------------------------------------------------------
function form_album(images)
{
   var enfant = 0;
   var erreur = '';
   if(document.getElementById("titre").value == "") {
		document.getElementById("titre").style.background = "#ffcccc";
		enfant++;
		erreur += '- Vous n\'avez pas saisi un titre pour votre album!<br>'; 
   }
   
  if (images != 0) {
	  if (testAlbum() == 0) {
			enfant++;
			erreur += '- Vous n\'avez pas uploadé vos images!<br>Une fois vos images séléctionnées, cliquez sur le bouton "Envoyer les photos"';
	   }
  }
 
   if(enfant == 0) {
	document.getElementById('formulaire_album').submit();    
   }
   else {
	$("#Suite_album").fadeIn("fast").html('<div align="right"><a href="javascript:fermer(\'Suite_album\')">Fermer</a></div><br><img src="images/attention.gif" align="left" style="margin-right:20px">'+erreur);
   }

}

// --------------------------------------------------------
//  Test formulaire de connexion
// --------------------------------------------------------
function form_connexion() {
   var enfant = 0;
   var erreur = '';
   
   if(document.getElementById("login").value == "") {
		document.getElementById("login").style.background = "#ffcccc";
		enfant++;
   }
   
   if(document.getElementById("pass").value == "") {
		document.getElementById("pass").style.background = "#ffcccc";
		enfant++;
   }
   
   if(enfant == 0) {
	document.getElementById('formulaire_connexion').submit();    
   }
}


// --------------------------------------------------------
// Editer FORM  photo
// --------------------------------------------------------
function editFormPhoto(_idVideo) {
	$("#editPhoto").show();
		
	$.post(
		  "./ajax.php" ,
		  { editFormPhoto: 1 , idVideo: _idVideo } ,
		  function(data){ $("#editPhoto").slideDown().html(data); }
		 );
}


// --------------------------------------------------------
// Editer une photo
// --------------------------------------------------------
function editPhoto(_idImage) {
	$("#formEdit").fadeIn().html("Enregistrement...");

	_titre = $("#titre_"+_idImage).val();
	_description = $("#description_"+_idImage).val();
	_idCategorie = $("#idCategorie_"+_idImage).val();
	_tags = $("#tags_"+_idImage).val(); 
		
	if (_titre == '' || _description == '' || _idCategorie == '') {
		$("#formEdit").fadeIn().html('Veuillez remplir tous les champs!');
	}
	else {
		$.post(
			  "./ajax.php" ,
			  { editPhoto: 1 , idImage: _idImage , titre: _titre, description: _description, idCategorie: _idCategorie, tags: _tags } ,
			  function(data){ $("#formEdit").fadeIn().html(data); }
			 );
	}
}

// --------------------------------------------------------
// Supprimer une photo
// --------------------------------------------------------
function deletePhoto(_idImage) {
		
	$.post(
		  "./ajax.php" ,
		  { deletePhoto: 1 , idImage: _idImage} ,
		  function(data){ $("#resultat_"+_idImage).html(data); }
		 );
		 
	$("#cadre_"+_idImage).fadeOut("slow");	
}

// --------------------------------------------------------
// Editer une video
// --------------------------------------------------------
function editVideo(_idVideo) {
	$("#formEdit").fadeIn().html("Enregistrement...");

	_titre = $("#titre_"+_idVideo).val();
	_description = $("#description_"+_idVideo).val();
	_idCategorie = $("#idCategorie_"+_idVideo).val();
	_tags = $("#tags_"+_idVideo).val();  
		
	if (_titre == '' || _description == '' || _idCategorie == '') {
		$("#formEdit").fadeIn().html("Veuillez remplir tous les champs!");
	}
	else {
		$.post(
			  "./ajax.php" ,
			  { editVideo: 1 , idVideo: _idVideo , titre: _titre, description: _description, idCategorie: _idCategorie , tags: _tags} ,
			  function(data){ $("#formEdit").fadeIn().html(data); }
			 );
	}
}

// --------------------------------------------------------
// Supprimer une Video
// --------------------------------------------------------
function deleteVideo(_idVideo) {
		
	$.post(
		  "./ajax.php" ,
		  { deleteVideo: 1 , idVideo: _idVideo} ,
		  function(data){ $("#resultat_"+_idVideo).html(data); }
		 );
		 
	$("#cadre_"+_idVideo).fadeOut("slow");	
}

// --------------------------------------------------------
// ajouter des photos
// --------------------------------------------------------
function ajouterPhotos(_idAlbum, _div) {
		
	$.post(
		  "./ajax.php" ,
		  { ajouterPhotos: 1 , idAlbum: _idAlbum} ,
		  function(data){ $("#"+_div).fadeIn("fast").html(data); }
		 );
}

// --------------------------------------------------------
// Sous-menu album (Photo, video)
// --------------------------------------------------------
function choixAlbum(_div) {
	$('#'+_div).slideDown('fast');
}

// --------------------------------------------------------
// ajouter un lien Video
// --------------------------------------------------------
function ajouterVideo() {
	if (document.getElementById("urlVideo").value == '') {
		document.getElementById("urlVideo").focus();
		document.getElementById("urlVideo").style.background = '#FFCCCC';
	}
	else {
		document.getElementById("urlVideo").value = '';
		document.getElementById("urlVideo").focus();
		document.getElementById("urlVideo").style.background = '#FFCCCC';
	}
}

// --------------------------------------------------------
// Valider et enregistrer un lien Video
// --------------------------------------------------------
function validerVideo() {
	_urlVideo = $("#urlVideo").val();
	if (_urlVideo != '') {
		$.post(
			  "./ajax.php" ,
			  { validerVideo: 1, urlVideo: _urlVideo} ,
			  function(data){ 
				$("#fd").fadeIn("fast").html(data); 
				afficheVideos();
			  }
		);
	}
	else {
		document.getElementById("urlVideo").focus();
		document.getElementById("urlVideo").style.background = '#FFCCCC';	
	}
}

// --------------------------------------------------------
// Afficher les videos enregistrées
// --------------------------------------------------------
function afficheVideos() {
	$("#videos").fadeIn("fast").html('<img src="images/attente.gif"> En chargement ...');
	$.post(
	  "./ajax.php" ,
	  { afficherVideo: 1} ,
	  function(data){ $("#videos").fadeIn("fast").html(data); }
	 );
}

// --------------------------------------------------------
//  Afficher une image  (Galerie)
// --------------------------------------------------------
function afficheVideo(_id, _idi) {
	$.get(
		"ajax.php" , 
		{ AFFICHEVIDEO : 1, idi : _idi, id : _id } , 
		function(data){ 
			$("#affichePhoto").hide();
			$("#affichePhoto").fadeIn("slow").html(data); 
		}
	); 
	comments(_idi);
	$("#laisser_comment").hide();
	
	//changeRank(document.getElementById("rate_img").value,_idi);
}

// --------------------------------------------------------
// Ecrire un mp
// --------------------------------------------------------
function ecrireMp(_idCompte) {
	$("#ecrireMp").fadeIn('fast'); 
	$.post(
	  "./ajax.php" ,
	  { ecrireMp: 1 , idCompte: _idCompte} ,
	  function(data){ 
		$("#boiteForm").fadeIn('fast').html(data); 
	  }
	 );      
}

// --------------------------------------------------------
// Envoyer mp
// --------------------------------------------------------
function envoyerMp(_idCompte) {  
	_texte = $("#texte").val();  
	$("#boiteForm").html('<img src="images/attente.gif"> Envoi en cous ...'); 
	$.post(
		  "./ajax.php" ,
		  { envoyerMp: 1 , idCompte: _idCompte, texte: _texte} ,
		  function(data){ $("#boiteForm").html(data); }
		 );  
}

// --------------------------------------------------------
// Envoyer la photo à une amie
// --------------------------------------------------------	
function envoie_ami(_id) {
	var t = new Date();
	$("#FormAmi").fadeIn('fast');      
	$.post(
		"./envoi_ami.php" , 
		{ id: _id, t: t.getTime()} , 
		function(data){ $("#envoi_ami").show().html(data); }
	);    
}	

// --------------------------------------------------------
// POSTER la photo à une amie
// --------------------------------------------------------    
function post_ami(_id) {
	_nom = $("#nom").val();
	_email = $("#email").val();
	_email1 = $("#email1").val();
	_email2 = $("#email2").val();
	_email3 = $("#email3").val();
	
	$("#envoi_ami").fadeIn('fast').html("Envoi en cours ...");
	
	var t = new Date();
	$.post(
		"./ajax_envoi_ami.php" , 
		{ id: _id, nom: _nom, email: _email, email1: _email1, email2: _email2, email3: _email3, t: t.getTime()} , 
		function(data){ $("#envoi_ami").fadeIn('fast').html(data); }
	);    
}
	
//-----------------------------------------------------------------------
// Système de vote par points  (RANK)
//			Joni Jokhaji
//			17-09-2007
//-----------------------------------------------------------------------

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.gif');
				etoile.className = 'on';
				rating--;
			}
			else if(rating == 0.5)
			{
				etoile.setAttribute('src', './images/stars/rating_half.gif');
				etoile.className = 'half';
				rating = 0;
			}
			else
			{
				etoile.setAttribute('src', './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", "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.gif');
				etoile.className = 'on';
				rating--;
			}
			else if(rating == 0.5)
			{
				etoile.setAttribute('src', './images/stars/rating_half.gif');
				etoile.className = 'half';
				rating = 0;
			}
			else
			{
				etoile.setAttribute('src', './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", "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.gif');
	}
}

// --------------------------------------------------------
// 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+'.gif');
	}
}

// --------------------------------------------------------
// 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).html(data); }
		 );

	// Mise à jour du vote
	$("#rating_"+_id).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);
}
