//*********** Fonctions Jquery *******************//
$(function() {
	
	/*$("div[id^='pic-']").mouseleave(function(){
		//$("div[id^='pic-']").css({opacity : 1});
			$("div[id^='s-']").fadeOut('fast');

		});	*/
	$("table").mouseleave(function(){$("div[id^='s-']").hide();});
	$("div[id^='s-']").click(function(){$("div[id^='s-']").hide();});
	
});
//##################   fonction add/remove FAV ##############
function checkFav(action,poster,user,attach,topic,post,ipath){
	
	
	    $("#notfav").remove();
	    $("#showfav").remove();
	    $("#ifav-"+attach).html("<img src='"+ipath+"/misc/loader.gif' />");
	    imfav = "<img src='"+ipath+"/misc/fav1.gif' />";
	
		$.post(
			"./astraitements.php",
			{actionFav:action,poster:poster,user:user,attach:attach,topic:topic,post:post,ipath:ipath},
			function (data,status) {
		
				if(typeof(data != 'undefined'))
				{			
					var dat = data.split('|%|');
					if (dat[0]=="addok")
					{
							 
							  $("#nbfav-"+attach).show();
					          $("#ifav-"+attach).after("<span id='notfav'>"+dat[1]+"</span>").slideDown();
					          setTimeout("$('#notfav').fadeOut();",1500);
					          setTimeout("$('#showfav').remove();$('#ifav-"+attach+"').after('<span id=\"showfav\"><a style=\"color:#00F3AD\" href=\"./portal_struc1.php?fav\">Voir mes photos favorites</a></span>').fadeIn();",2000);
					          $("#ifav-"+attach).html("<a href=\"javascript:void(0);\" onclick=\"checkFav('rem',"+poster+","+user+","+attach+","+topic+","+post+",'"+ipath+"');\"><img src='"+ipath+"/misc/fav1.jpg' /></a>");
					          $("#nbfav-"+attach).html(dat[2] + imfav);
					          if (dat[2]==1)  $("#lbfav-"+attach).html(" - <a id='att-"+attach+"' style='color:#00F3AD' href='javascript:void(0);' onclick='showWho(\"showFav\","+attach+");'>Qui ?</a>");
							  $("#wfav-"+attach).html("").hide();
							 
					          
					          
					}else if  (dat[0]=="remok"){
							 
							 
							  $("#ifav-"+attach).after("<span id='notfav'>"+dat[1]+"</span>").slideDown();
					          setTimeout("$('#notfav').fadeOut();",1500);
					          setTimeout("$('#showfav').remove();$('#ifav-"+attach+"').after('<span id=\"showfav\"><a style=\"color:#00F3AD\" href=\"./portal_struc1.php?fav\">Voir mes photos favorites</a></span>').fadeIn();",2000);
					          $("#ifav-"+attach).html("<a href=\"javascript:void(0);\" onclick=\"checkFav('add',"+poster+","+user+","+attach+","+topic+","+post+",'"+ipath+"');\"><img src='"+ipath+"/misc/fav0.jpg' /></a>");
							  $("#nbfav-"+attach).html(dat[2] + imfav);	
							  if (dat[2]==0)  {$("#lbfav-"+attach).html("");	 $("#nbfav-"+attach).hide();}
							  $("#wfav-"+attach).html("").hide();			
					}
				}
					
			}
		);
		
	
}
//##################   fonction montre users FAV ##############
function showWho(action,attach){
	
	
	    $("#wfav").hide();
	    //$("#showfav").remove();
	    //$("#ifav-"+attach).html("<img src='"+ipath+"/misc/loader.gif' />");
	
		$.post(
			"./astraitements.php",
			{actionFav:action,attach:attach},
			function (data,status) {
		
				if(typeof(data != 'undefined'))
				{
                                    
                                    
                                   
                                    
					var dat = data.split('|%|');
					if (dat[0]=="showFav")
					{
							 
							$("#wfav-"+attach).html(dat[1]).slideDown('fast');
							$("#lbfav-"+attach).html(" - <a id='att-"+attach+"' style='color:#00F3AD' href='javascript:void(0);' onclick='showWho(\"hideFav\","+attach+");'>Qui ?</a>");					          
					          
					}else if  (dat[0]=="hideFav"){
						
						$("#wfav-"+attach).html("").slideUp('fast');
						$("#lbfav-"+attach).html(" - <a id='att-"+attach+"' style='color:#00F3AD' href='javascript:void(0);' onclick='showWho(\"showFav\","+attach+");'>Qui ?</a>");			
					}
				}
					
			}
		);
		
	
}
//##################   fonction form pour selection book ##############
function showFormSelBook(sel,attach){
	
	
	    $("#book-"+sel+""+attach).slideDown();
	    //$("#showfav").remove();
	    //$("#ifav-"+attach).html("<img src='"+ipath+"/misc/loader.gif' />");
			
	
}
//##################   retenir une photo pour le book ##############
function validePhotoBook(){
            
            
            
	
                        var action = $("input[name='actionBook']").val();
                        var attachID = $("input[name='attachID']").val();
                        
                        
                if (attachID=="")  {    alert('Veuillez saisir l\'ID de la photo avant de valider...'); return false;  }
	
		$.post(
			"./astraitements.php",
			{actionBook:action,attach:attachID},
			function (data,status) {
		
				if(typeof(data != 'undefined'))
				{
                                    
                                    
                                   
                                    
					var dat = data.split('|%|');
					if (dat[0]=="ok")
					{
                                                window.location.href = "./posting.php?mode=reply&f="+dat[1]+"&t="+dat[2];
					          
					}else {
                                            
                                            alert('Une erreur est survenue...'); return false;
                                            
                                        }    
						
				}
					
                        }
		);
		
	
}

function checkNumSel(f){
	
		fid = $("#"+f);
		
		var post = $("#"+f+" :input[name='book_post_id']").val();
		var forum = $("#"+f+" :input[name='book_forum_id']").val();
		var photo = $("#"+f+" :input[name='book_attach_id']").val();
		var bm = $("#"+f+" :input[name='book_bm_id']").val();
		var auteur = $("#"+f+" :input[name='book_auteur_id']").val();
		var book = $("#"+f+" :input[name='book_num']").val();
		var photo_num = $("#"+f+" :input[name='book_photo_num']").val();
		var bm_username = $("#"+f+" :input[name='book_bm_username']").val();
		var action = $("#"+f+" :input[name='book_action']").val();
		
		//alert(post+" "+forum+" "+photo+" "+bm+" "+auteur+" "+book+" "+action); return false;
		
		$.post(
			"./astraitements.php",
			{actionBook:action,post:post,forum:forum,photo:photo,bm:bm,auteur:auteur,book:book,photo_num:photo_num,bm_username:bm_username},
			function (data,status) {
		
				if(typeof(data != 'undefined'))
				{
                                                                        
					var dat = data.split('|%|');
					if (dat[0]==1)
					{
						 
                         window.location.href = "./portal_struc8.php?view=votes";
					          
					}else if (dat[0]==0){
                                            
                          alert(dat[1]);
                          window.location.reload();
                           
                          
                        
                                            
                    }    
						
				}
					
           }
		);
		
		
	
	
}
function vote4book(f){
	
		fid = $("#"+f);
		
		var bm = $("#"+f+" input[name='bm']").val();
		var photo = $("#"+f+" input[name='photo']").val();
		var book = $("#"+f+" input[name='book']").val();
		var bm_name = $("#"+f+" input[name='bm_name']").val();
		var nb_votants = $("#"+f+" input[name='nb_votants']").val();
		var score_book = $("#"+f+" input[name='score_book']").val();
		var vote = $("#"+f+" :input[name='monvote']:checked").val();
		var action = "vote4book";
		
		
		
		$.post(
			"./astraitements.php",
			{actionBook:action,photo:photo,bm:bm,book:book,bm_name:bm_name,vote:vote,nb_votants:nb_votants,score_book:score_book},
			function (data,status) {
		
				if(typeof(data != 'undefined'))
				{
                                                                        
					var dat = data.split('|%|');
					if (dat[0]==1)
					{
						 
                         //window.location.href = "./portal_struc8.php?view=votes";
                        // $("#noformbm-"+photo).slideUp('fast');
                        $("#formbm-"+photo).before('<span  id="noformbm-"'+photo+' style="padding:10px;color:#EFDB00;font-weight:bold;font-size:14px">Vous avez vot&eacute; pour cette photo...</span>');
                         $("#formbm-"+photo).slideUp('');
                         
					          
					}else if (dat[0]==0){
                                            
                          alert(dat[1]);
                          if (dat[2]=='reload') window.location.reload();
                           
                          
                        
                                            
                    }    
						
				}
					
           }
		);
		
		
	
	
}
function bookActionAdm(f){
	
		fid = $("#"+f);
		
		var photo = $("#"+f+" input[name='photo']").val();
		var book = $("#"+f+" input[name='book']").val();
		var choix = $("#"+f+" select[name='actionAdm']").val();
		var notif = $("#"+f+" input[name='notif']").val();
		var action = "adm4book";
		var ok = true;
		
		
		if (choix=="") return false;
		
		if (choix=="-1") {ok = confirm('Etes-vous sûr de vouloir supprimer cette photo de la sélection ?');}
		if (choix=="3") {ok = confirm('Etes-vous sûr de vouloir marquer cette photo comme "NON-EXCLU" ?');}
		
		if (ok){
			$.post(
				"./astraitements.php",
				{actionBook:action,photo:photo,book:book,choix:choix,notif:notif},
				function (data,status) {
			
					if(typeof(data != 'undefined'))
					{
																			
						var dat = data.split('|%|');
						if (dat[0]==1)
						{
							 
							 window.location.href = "./portal_struc8.php?view=votes";
								  
						}else if (dat[0]==0){
												
							   if(dat[2]!=''){
									if(dat[2]=='reload') {
										window.location.reload(); 
										}else{
										alert("Vous allez être redirigé vers le topic de cette photo en mode 'édition'\r\nafin de notifier sa sélection au book...");	
										window.location.href="./posting.php?mode=reply"+dat[2];
									}
									
								}
												
						}    
							
					}
						
			   }
			);
		}
		
	
	
}
function bookRepeche(photo,book){
	
		
		var action = "repecheBook";
		var ok = true;
		
		
		
		ok = confirm('Etes-vous sûr de vouloir repécher cette photo pour le book en cours ?');
		
		if (ok){
			$.post(
				"./astraitements.php",
				{actionBook:action,photo:photo,book:book},
				function (data,status) {
			
					if(typeof(data != 'undefined'))
					{
																			
						var dat = data.split('|%|');
						if (dat[0]==1)
						{
							 
							 window.location.reload();
								  
						}else if (dat[0]==0){
												
                                                            alert("Une erreur est survenue...");
                                                             window.location.reload();
								
												
						}    
							
					}
						
			   }
			);          
            }
		
	
	
}
function addOldBook(f){
	
		fid = $("#"+f);
		
		var photo = $("#"+f+" input[name='photo']").val();
		var book = $("#"+f+" input[name='book']").val();
		var action = "addOldBook";
		
		
		
		$.post(
			"./astraitements.php",
			{actionBook:action,photo:photo,book:book},
			function (data,status) {
		
				if(typeof(data != 'undefined'))
				{
                                                                        
					var dat = data.split('|%|');
					if (dat[0]==1)
					{
						 
					$("#formadd input[name='photo']").val('');
					$("#formadd input[name='book']").val('');
					$("#checkadd").prepend(dat[1]);
					          
					}else if (dat[0]==0){
                                            
						$("#checkadd").prepend(dat[1]);
  
                                              
                    }    
						
				}
					
           }
		);
		
		
	
	
}
function exclu4book(f){
	
		fid = $("#"+f);
		
		var notif = $("#"+f+" textarea[name='doublon-notif']").val();
		var doublon = $("#"+f+" input[name='doublon']").val();
		var action = "exclu4book";
		
		
		
		$.post(
			"./astraitements.php",
			{actionBook:action,notif:notif,doublon:doublon},
			function (data,status) {
		
				if(typeof(data != 'undefined'))
				{
                                                                        
					var dat = data.split('|%|');
					if (dat[0]==1)
					{
					var maj = $("#doublon-maj-"+doublon);	 
					maj.html("Maj ok !");
					setTimeout("$('span[id^=\"doublon-maj\"]').html('');",2000);
					          
					}else if (dat[0]==0){
                                            
						$("#doublon-maj-"+doublon).html("<span style='color:red'>Erreur dans la maj...</span>");
						setTimeout("$('span[id^=\"doublon-maj\"]').html('');",2000);
  
                                              
                    }    
						
				}
					
           }
		);
		
		
	
	
}
function usersByRegions(){
	
		
		var choix = $("select[name='list_regions']").val();
                var action = "checkUsersRegions";
		
		
		
		if (choix=="") {$("textarea[name='username_list']").val(''); return false;}
		
		
			$.post(
				"./astraitements.php",
				{actionPM:action,choix:choix},
				function (data,status) {
			
					if(typeof(data != 'undefined'))
					{
																			
						var dat = data.split('|%|');
						if (dat[0]==1)
						{
							 
							$("textarea[name='username_list']").val(dat[1]);
								  
						}else if (dat[0]==0){
									
                                                                        
                                                                        
                                                           $("textarea[name='username_list']").val('');        			
									
                                                }
												
						  
							
					}
                                }		
			   
			);
		
		
	
	
}

function showSeries(s){
	
	//$("div[id^='s-']").fadeOut('fast');
	
	if(s==0) {
	
    $("div[id^='s-']").hide();
    
    }else{
    
	$("#s-"+s).show();//function(){
  $("div[id^='s-']").not("#s-"+s).hide();
  
  //}); 
	}
}

