var regexEmail = new RegExp("[A-Za-z0-9._-]{1,}@[A-Za-z0-9._-]{1,}[.][A-Za-z]{2,}", "gi");     

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

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

// --------------------------------------------------------
//  Afficher une image  (Galerie)
// --------------------------------------------------------
function afficheImg(_id, _idi) {
	$("#chargementImg").fadeIn("fast").html('<br><br><br><center><img src="images/loading.gif"></center>');
	$.get(
		"ajax.php" , 
		{ AFFICHEIMG : 1, idi : _idi, id : _id } , 
		function(data){     
			$("#affichePhoto").hide();
			$("#affichePhoto").fadeIn("slow").html(data);
			$("#chargementImg").fadeOut("fast");    
		}
	);
	//comments(_idi);
	//$("#laisser_comment").hide();
	//changeRank(document.getElementById("rate_img").value,_idi);
}

function affichePhotos() {    
	$("#photos").fadeIn('fast').html('<img src="images/attente.gif"> Chargement de la galerie ...');    
	$.post(
		"./ajax.php" , 
		{autresPhotos: 1} , 
		function(data){ $("#photos").html(data); }
	);    
} 


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

// --------------------------------------------------------
//  Récupère la réponse HTTPREQUEST
// --------------------------------------------------------
function getRequestResponse()
{ 
	var html = $.ajax({
		url: "ajax.php?TESTALBUM",
		data: "TESTALBUM",
		cache: false,
		async: false
		}).responseText;
	return html;
}

// --------------------------------------------------------
//  TEST si dossier tmp OK
// --------------------------------------------------------
function testAlbum() {
	var rep = '';
	rep = getRequestResponse();
	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").show("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 une photo
// --------------------------------------------------------
function editPhoto(_idImage) {
	_titre = $("#titre_"+_idImage).val();
	_description = $("#description_"+_idImage).val();
	_idCategorie = $("#idCategorie_"+_idImage).val();
	_tags = $("#tags_"+_idImage).val();
				   
	if (_titre == '' || _description == '' || _idCategorie == '') {
		$("#resultat_"+_idImage).show().html("Veuillez remplir tous les champs!");
	}
	else {   
		$("#resultat_"+_idImage).show().html("Enregistrement...");
		$.post(
			  "./ajax.php" ,
			  { editPhoto: 1 , idImage: _idImage , titre: _titre, description: _description, idCategorie: _idCategorie, tags: _tags } ,
			  function(data){ $("#resultat_"+_idImage).show().html(data); }
			 );
	}
}

// --------------------------------------------------------
// Supprimer une photo
// --------------------------------------------------------
function deletePhoto(_idImage) {
	if(confirm('Êtes-vous sûr de vouloir supprimer cette photo?')) {		
		$.post(
			  "./ajax.php" ,
			  { deletePhoto: 1 , idImage: _idImage} ,
			  function(data){ $("#cadre_"+_idImage).html(data); }
			 );
			 
		$("#cadre_"+_idImage).fadeOut("slow");	
	}
}

// --------------------------------------------------------
// Editer une video
// --------------------------------------------------------
function editVideo(_idVideo) {
	$("#resultat_"+_idVideo).show().html("Enregistrement...");

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

// --------------------------------------------------------
// Supprimer une Video
// --------------------------------------------------------
function deleteVideo(_idVideo) {
	if(confirm('Êtes-vous sûr de vouloir supprimer cette vidéo?')) {    	
		$.post(
			  "./ajax.php" ,
			  { deleteVideo: 1 , idVideo: _idVideo} ,
			  function(data){ $("#resultat_"+_idVideo).html(data); }
			 );
			 
		$("#cadre_"+_idVideo).fadeOut("slow");	
	}
}

// --------------------------------------------------------
// ajouter des  photo
// --------------------------------------------------------
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();
	_titreVideo = $("#titreVideo").val(); 
	if (_urlVideo != '' && _titreVideo != '' ) {
		$.post(
			  "./ajax.php" ,
			  { validerVideo: 1, urlVideo: _urlVideo, titreVideo: _titreVideo} ,
			  function(data){ 
				$("#fd").fadeIn("fast").html(data); 
				afficheVideos();
			  }
		);
	}
	else {               
		if (_urlVideo == '') {
			document.getElementById("urlVideo").focus();
			document.getElementById("urlVideo").style.background = '#FFCCCC';	
		}
		if (_titreVideo == '') {
			document.getElementById("titreVideo").focus();
			document.getElementById("titreVideo").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);
}

// --------------------------------------------------------
// Afficher le popup d'un album photos
// --------------------------------------------------------
function popupAlbum(_id) {
	$("#popupAlbum").fadeIn("fast").html('<img src="images/attente.gif"> En chargement ...');
	$.get(
	  "./ajax.php" ,
	  { popupAlbum: 1, id: _id} ,
	  function(data){ $("#popupAlbum").fadeIn("fast").html(data); }
	 );
}

// --------------------------------------------------------
// Check les inputs
// --------------------------------------------------------
function checkInputField(field)
{
		var type = field.getAttribute("type");
		switch(type)
		{
			case "text" : 
				if (field.value != "" )    return true;
				break;
			case "checkbox" :
				if (field.checked && field.value != "" )    return true;
				break;
			case "radio" :
				if (field.checked && field.value != "" ) return true;
				break;
			case "button" :
				if (field.value != "" )    return true;
				break;
			case "submit" :
				if (field.value != "" )    return true;
				break;
			default :
				return false;
		}
		return false;
}
	var val;
  
// --------------------------------------------------------
// check les chmaps d'un tableau
// --------------------------------------------------------   
function checkTabFields()
{    
	val = '';
	if (document.autresPhotos.checkbox_photos.length > 0) {
		for (i=0; i<document.autresPhotos.checkbox_photos.length; i++){
			if (document.autresPhotos.checkbox_photos[i].checked==true){
				
				val += document.autresPhotos.checkbox_photos[i].value+',';
			}
		}
	}
	else 
		if ((document.autresPhotos.checkbox_photos.length == null || document.autresPhotos.checkbox_photos.length == 'undefined') && document.autresPhotos.checkbox_photos.checked==true) 
			val = document.autresPhotos.checkbox_photos.value+",";
		else val = '';
	return val;
}

// --------------------------------------------------------
// Déplacer une photo vers un album au choix
// --------------------------------------------------------   
function moveToAlbum(_idAlbum) {    
	checkTabFields();
	
	$("#resultPhotos").html("Chargement ..."); 
	
	if (val != '') _val = val;
	else _val = 0;
	
	if (_val != 0) {	
		$.post(
			"./ajax.php" , 
			{ moveToAlbum: 1, idAlbum: _idAlbum, images: _val } , 
			function(data){ 
				$("#resultPhotos").html(data);
				affichePhotos(); 
				$("#choixAlbum").fadeOut("slow");
			}
		);  
	}  
	else 
		$("#resultPhotos").html('<img src="images/warning.png"> Choisissez des photos!');
	
}

// --------------------------------------------------------
// Form d'édition d'une photo
// --------------------------------------------------------   
function editUnePhoto(_idImage) {
	$("#editUnePhoto").fadeIn('fast').html("Chargement ...");
	$.get(
		"./ajax.php" , 
		{ formEditUnePhoto: 1, idImage: _idImage } , 
		function(data){ $("#editUnePhoto").html(data); }
	);    	
}

// --------------------------------------------------------
// Lise de choix des albums photos
// -------------------------------------------------------- 
ouvert = 0;  
function afficheMoveAlbums() {
	//if (ouvert == 0) {    
		$("#choixAlbum").fadeIn('fast').html("Chargement ...");
		$.get(
			"./ajax.php" , 
			{ afficheMoveAlbums: 1} , 
			function(data){ $("#choixAlbum").fadeIn('fast').html(data); }
		); 
		ouvert = 1;
	//}
	/*else {
		$("#choixAlbum").fadeOut('fast');
		ouvert = 0;
	} */      
}

// --------------------------------------------------------
// Créer un album
// --------------------------------------------------------   
function createAlbum() {

	_titreAlbum = $("#titreAlbum").val(); 

	if (_titreAlbum != '') {
		$("#choixAlbum").fadeIn('fast').html("Chargement ...");
		$.post(
			"./ajax.php" , 
			{ createAlbum: 1, titreAlbum: _titreAlbum } , 
			function(data){ 
				$("#choixAlbum").fadeIn('fast').html(data);
				afficheMoveAlbums();      
			}
		);   
	}
	else {
		document.getElementById("titreAlbum").focus();
		document.getElementById("titreAlbum").style.background = '#FFCCCC'; 	
	}	
	  
}

// --------------------------------------------------------
// Poste les photos + Affichage après
// --------------------------------------------------------   
function postPhotos() {    
	$("#photos").fadeIn('fast').html('<img src="images/attente.gif"> Photos en traitement ...');    
	$.post(
		"./ajax_post_img.php" , 
		{} , 
		function(data){ 
			$("#photos").html(data); 
			affichePhotos();
		}
	);    
} 


//-----------------------------------------------------------------------
// 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);
}

