
$(document).ready( function() {
	$('.btnModifyCommentaire').show();
	$('.btnSaveCommentaire').hide();
});



function addAgenda(id_fiche, id_user, date, reload) {
	$( function() {
		var dataString = 'submit=valider&add=agenda&utilisateur_i_id='
				+ id_user + '&fichesmulti_i_id=' + id_fiche + '&date=' + date + '&agenda=1';
		$.ajax( {
			type :"POST",
			url :"/FRONT/CRT_PIDF/include/myspace/ajax/action_selection.php",
			data :dataString,
			success : function(data) {
				if(reload == true) {
					window.location.reload();
				} else {
					$('#btnAgenda_'+id_fiche).fadeOut("slow");
					$('#btnAgenda_'+id_fiche).fadeIn("slow");
					return true;
				}
			}
		});
		return false;
	});
}

function removeAgenda(agenda_i_id, update) {
	$( function() {
		var dataString = 'submit=valider&remove=agenda&agenda_i_id=' + agenda_i_id;
		$.ajax( {
			type :"POST",
			url :"/FRONT/CRT_PIDF/include/myspace/ajax/action_selection.php",
			data :dataString,
			success : function(data) {				
				if(update == true) {				
					$('#bloc_' + agenda_i_id).slideUp("slow");
				} else {
					$('#bloc_' + agenda_i_id).slideUp("slow");
				}
				return true;
			}
		});
		return false;
	});
}

function addSelection (id_fiche, id_user, rub_i_id) {

}

function removeSelection(id_fiche, id_user, rub_i_id) {
	$( function() {
		var dataString = 'submit=valider&remove=selection&utilisateur_i_id='
				+ id_user + '&fichesmulti_i_id=' + id_fiche + '&selection=0';
		$.ajax( {
			type :"POST",
			url :"/FRONT/CRT_PIDF/include/myspace/ajax/action_selection.php",
			data :dataString,
			success : function(data) {
				
				$('#bloc_' + id_fiche).slideUp("slow");
				var nb = $('#nb_selection_' + rub_i_id).html();
				$('#nb_selection_' + rub_i_id).html(nb-1); 
			
				
				$('#ImgCarrHeader'+id_fiche).remove();
				
				//compter le nombre d'elements vides à supprimer
	            var nbElemToDelete = $("li[@id=ImgCarrHeaderVide]").size();
	          //supprimer les blocs vides
	            if(nbElemToDelete > 0)
	            {
    	            $("li[@id=ImgCarrHeaderVide]").each(function(i){	
    	            	//$(this).remove();
    	            });
	            }
	            //calcul nombre element à rajouter
	            var nbElemToAdd = (parseInt(nbElemToDelete) + (1));
	            
	            if(nbElemToAdd >= 1)
	            {
    	            var contenuVide = '';
    	            for(var i=1;i<=nbElemToAdd;i++)
    	            {
    	            	 contenuVide = '<li id="ImgCarrHeaderVide"></li>';
    	            	 $("#selections").append(contenuVide);
    	            }
	            }
				
	            //$("img[@src='"+data+"']").parent().remove();
	            
	            /*var nbItem = tabSelections.length;
	            
	            for(i=0;i<nbItem;i++){
	        		if (tabSelections[i].path==data)
	        		{
	        			--nbItem;
	        			break;
	        		}
	        	}
	        	for (; i< nbItem; i++) {
	        		tabSelections[i] = tabSelections[i+1];
	        	}
	        	delete tabSelections[nbItem];

	        	tabSelections.length--;

		        initCaroussel();*/
		        
	            var newVarElements	=  $("span[@id=nbCharsSelectionHead]").attr("title");
	            newVarElements		= parseInt(newVarElements) - (1);
	            $("#nbCharsSelectionHead").empty();
	            document.getElementById('nbCharsSelectionHead').innerHTML = ' ';
	            $("#nbCharsSelectionHead").append(newVarElements+' ');
	            
	            //effacer lancien contenu le lattribut id
	            $("span[@id=nbCharsSelectionHead]").removeAttr("title");
	            
	            //mettre en place le nouvel attribut id
	            $("span[@id=nbCharsSelectionHead]").attr("title",newVarElements);     
	            
	            /*if(parseInt(newVarElements)==0) {
	            	 $("#spanMessageSel").hide();
	            	 $("#mySelectionEmpty").show();
	            }*/	
	            
				return true;
			}
		});
		return false;
	});
	
}

function addMap(id_fiche, id_user, reload) {
	$( function() {
		var dataString = 'submit=valider&add=map&utilisateur_i_id='
				+ id_user + '&fichesmulti_i_id=' + id_fiche + '&carte=1';
		$.ajax( {
			type :"POST",
			url :"/FRONT/CRT_PIDF/include/myspace/ajax/action_selection.php",
			data :dataString,
			success : function(data) {
				//map.addOverlay(__createMarkerBis(new GLatLng(40, 2), 'Big', 1, __buildText(dataJSON['1'])));				
				if(reload == true) {
					window.location.reload();
				} else {
					$('#btnMap_'+id_fiche).hide();
					//afficher grisé.
					$('#btnMapOff_'+id_fiche).fadeIn("slow");
					return true;
				}
			}
		});
		return false;
	});
}

function removeMap(id_fiche, id_user, reload, index) {
	$( function() {
		var dataString = 'submit=valider&remove=map&utilisateur_i_id='
				+ id_user + '&fichesmulti_i_id=' + id_fiche + '&carte=0';
		$.ajax( {
			type :"POST",
			url :"/FRONT/CRT_PIDF/include/myspace/ajax/action_selection.php",
			data :dataString,
			success : function(data) {	
				map.removeOverlay(__markers[index]);	
				$('#selection_'+index).fadeOut("slow");		
				/*if(reload == true) {
					window.location.reload();
				} else {
					$('#btnMap_'+id_fiche).fadeOut("slow");
					return true;
				}*/
			}
		});
		return false;
	});
}

function showCommentaire(id_fiche) {
	$( function() {
		$('#fiche_' + id_fiche).slideDown("slow");
		/* $('#btnCommentaire_'+id_fiche).hide(); */
	
		var commentaire = $('#commentaire_'+id_fiche).val();
		if(commentaire=="")
		{
			$('#btnModifyCommentaire_'+id_fiche).hide();
			$('#btnSaveCommentaire_'+id_fiche).show();
			$('#commentaire_'+id_fiche).removeAttr("readonly");
		}
	});
}

function addCommentaire(id_fiche, id_user) {
	
	$( function() {
		var commentaire = $('#commentaire_'+id_fiche).val();
		var dataString = 'submit=valider&add=commentaire&utilisateur_i_id='
			+ id_user + '&fichesmulti_i_id='+id_fiche+'&commentaire='+commentaire;
		$.ajax( {
			type :"POST",
			url :"/FRONT/CRT_PIDF/include/myspace/ajax/action_selection.php",
			data :dataString,
			success : function(data) {
				$('#btnModifyCommentaire_'+id_fiche).show();
				$('#btnSaveCommentaire_'+id_fiche).hide();
				$('#commentaire_'+id_fiche).removeClass('on');
				$('#commentaire_'+id_fiche).addClass('off');

				
				//$('#btnCommentaireOff_'+id_fiche).show();
				
				
				$('#commentaire_'+id_fiche).addClass('off');
				
				//document.getElementById('commentaire_'+id_fiche).addAttribute("readonly","readonly");
				
				/********************************/
				var readonly = document.createAttribute("readonly");
				readonly.nodeValue = "readonly";
				document.getElementById('commentaire_'+id_fiche).setAttributeNode(readonly);
				/********************************/
				
				//if(commentaire=="")$('#fiche_' + id_fiche).slideUp("slow");	
					
				return true;
			}
		});
		return false;
	});
}

function editCommentaire(id_fiche) {
	$( function() {
			$('#btnModifyCommentaire_'+id_fiche).hide();
			$('#btnSaveCommentaire_'+id_fiche).show();
			//$('#commentaire_'+id_fiche).removeClass('toggleTextAreaMySpace off');
			//$('#commentaire_'+id_fiche).addClass('toggleTextAreaMySpace on');		
			//document.getElementById('commentaire_'+id_fiche).removeAttribute("readonly");
			$('#commentaire_'+id_fiche).removeAttr("readonly");
			
		
	});
}

function SendSelectionToFriend()
{
	
	var nbSelectionActu = $("input[@type=checkbox][@checked][@name=id_fiche]").length;
	
	if(nbSelectionActu >= 1)
	{
		$('#BlocEnvoyerAmi').slideDown("slow");
		var contenu = nbSelectionActu + " sélections à envoyer";
		$("#compteurSelection").empty();
		$("#compteurSelection").append(contenu);
		
		//cacher tous les items
		$("input[@type=checkbox][@name=id_fiche]").each(function(i){
			$("input[@type=checkbox][@name=id_fiche]").parent().parent().hide();
			$("input[@type=checkbox][@name=id_fiche]").parent().parent().parent().parent().parent().hide();//cacher tt les blocs
			 });
		
		//cacher le nombre de resultat
		$("span[@class='ZiEffaceEnvoi']").hide();
		
		//cacher les option de selection des fiches
		$("ul[@class='tools']").hide();
		
		//afficher les items selectionés uniquement 
		$("input[@type=checkbox][@checked][@name=id_fiche]").parent().parent().parent().parent().parent().slideDown("slow");//afficher les nn vides
		$("input[@type=checkbox][@checked][@name=id_fiche]").parent().parent().slideDown("slow");

		//sans les cases a cocher
		$("input[@type=checkbox][@checked][@name=id_fiche]").hide();
		
		$('#ConfirmEnvoiAmi').hide();

		$("a[@class=btnModifyCommentaire]").hide();
		
		/****CACHER LES INTERETS**/
		$("ul[@class=opts]").hide();
		$("h2[@class=main]").hide();
		$("p[@class=intro]").hide();
		$("div[@id=interets_full]").hide();
		/************************/
		
		
	}
	else
	{	
		var MessageSelectionCase = $("#messageAuMoins").val();
		alert( MessageSelectionCase );
	}
}

function CacherSendSelectionToFriend()
{
	$('#BlocEnvoyerAmi').slideUp("slow");
	$("input[@type=checkbox][@name=id_fiche]").parent().parent().slideDown("slow");
	$("input[@type=checkbox][@name=id_fiche]").parent().parent().parent().parent().parent().slideDown("slow"); //afficher tt les blocs
	
	//monter les cases a cocher 
	$("input[@type=checkbox][@checked][@name=id_fiche]").show();
	
	//montrer le nombre de resultat
	$("span[@class='ZiEffaceEnvoi']").show();
	
	//montrer les option de selection des fiches
	$("ul[@class='tools']").show();
	
	$('#ConfirmEnvoiAmi').hide();
	
	/****MONTRER LES INTERETS**/
	$("ul[@class=opts]").show();
	$("h2[@class=main]").show();
	$("p[@class=intro]").show();
	$("div[@id=interets_full]").show();
	/************************/

	$("a[@class=btnModifyCommentaire]").show();
	
}

function EnvoyerSelectionAmis(id_user)
{	
	var StringSelections = "";
	var StringEmails	 = "";
	var StringIntitules	 = "";
	
	
	//recuperation des id des fiches
	$("input[@type=checkbox][@checked][@name=id_fiche]").each(function(i){	
		StringSelections = StringSelections + "-" + $(this).val();
		StringIntitules = StringIntitules + "-" + $(this).parent().attr("name");
		 });
	
	//recuperation du texte
	var txtSendFriend = $("textarea[@name=msg]").val();
	
	//recuperation des adresse mail
	var email1 = $("input[@class=inputText][@name=email1]").val();
	var email2 = $("input[@class=inputText][@name=email2]").val();
	var email3 = $("input[@class=inputText][@name=email3]").val();
	var email4 = $("input[@class=inputText][@name=email4]").val();
	var email5 = $("input[@class=inputText][@name=email5]").val();
	
	StringEmails = email1 + "-*-" + email2 + "-*-" + email3 + "-*-" + email4 + "-*-" + email5 ;
	
	CacherSendSelectionToFriend();
	
	$('#ConfirmEnvoiAmi').slideDown("slow");
	
	
	$( function() {
		var dataString = 'submit=valider&add=sendFriendListe&utilisateur_i_id='
				+ id_user + '&fiches=' + StringSelections + '&texte=' + txtSendFriend + '&mails=' + StringEmails + '&titres=' + StringIntitules ;
		$.ajax( {
			type :"POST",
			url :"/FRONT/CRT_PIDF/include/myspace/ajax/action_selection.php",
			data :dataString,
			success : function(data) {		
			}
		});
		return false;
	});
	
	
}



/*******AGENDA*********/

function SendSelectionToFriendAgenda()
{
	
	var nbSelectionActu = $("input[@type=checkbox][@checked][@name=id_fiche]").length;

		$('#BlocEnvoyerAmi').slideDown("slow");
		var contenu = nbSelectionActu + " sélections à envoyer";
		$("#compteurSelection").empty();
		$("#compteurSelection").append(contenu);
		
		//cacher tous les items
		$("input[@type=checkbox][@name=id_fiche]").each(function(i){
			$("input[@type=checkbox][@name=id_fiche]").parent().parent().hide();
			$("input[@type=checkbox][@name=id_fiche]").parent().parent().parent().parent().parent().hide();//cacher tt les blocs
			 });	
		//cacher les obljets inutiles
		$("div[@id='myCalendar']").hide();
		$("select[@id='envies']").hide();
		$("input[@class='submit']").hide();
		$("ul[@id='boutonLiensAgenda']").hide();
		//cacher les obljets inutiles
		
		$('#ConfirmEnvoiAmi').hide();
		
		/****CACHER LES INTERETS**/
		$("ul[@class=opts]").hide();
		$("h2[@class=main]").hide();
		$("p[@class=intro]").hide();
		$("div[@id=interets_full]").hide();
		/************************/
}

function CacherSendSelectionToFriendAgenda()
{
	$('#BlocEnvoyerAmi').slideUp("slow");
	//montrer les option de selection des fiches
	$("ul[@class='tools']").show();
	
	$('#ConfirmEnvoiAmi').hide();
	
	/****MONTRER LES INTERETS**/
	$("ul[@class=opts]").show();
	$("h2[@class=main]").show();
	$("p[@class=intro]").show();
	$("div[@id=interets_full]").show();
	/************************/
	
	/**********montrer les obljets inutiles*************/
	$("div[@id='myCalendar']").show();
	$("select[@id='envies']").show();
	$("input[@class='submit']").show();
	$("ul[@id='boutonLiensAgenda']").show();
	/***********montrer les obljets inutiles*************/
	
}
function print_r(theObj){
	  if(theObj.constructor == Array ||
	     theObj.constructor == Object){
	    document.write("<ul>");
	    for(var p in theObj){
	      if(theObj[p].constructor == Array||
	         theObj[p].constructor == Object){
	document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
	        document.write("<ul>");
	        print_r(theObj[p]);
	        document.write("</ul>");
	      } else {
	document.write("<li>["+p+"] => "+theObj[p]+"</li>");
	      }
	    }
	    document.write("</ul>");
	  }
	}


function Array2Get(tbl,nom) {
	//tbl est une variable tableau
	//nom est le nom du tableau (la même chose que tbl mais entre guillemet)
	var r='&';
	for (var i in tbl)
		r+=nom+'['+i+']='+tbl[i]+'&';
	return r;
}

function EnvoyerSelectionAmisAgenda(id_user)
{	
	var StringSelections = "";
	var StringEmails	 = "";
	var StringIntitules	 = "";
	var nom_evenement,date_evenement = "";
	var event = new Array();
	var tabsend;
	
	
	$("h4[@id=ficheAgendaEvenement]").each(function(i){	
		
		var nom_evenement  =  $(this).attr("name");
		var date_evenement = $(this).text();
		
		event[date_evenement] = nom_evenement;		
	});
	
	tabsend = Array2Get(event,"tabsend");

	//print_r(event);

	//recuperation du texte
	var txtSendFriend = $("textarea[@name=msg]").val();
	
	//recuperation des adresse mail
	var email1 = $("input[@class=inputText][@name=email1]").val();
	var email2 = $("input[@class=inputText][@name=email2]").val();
	var email3 = $("input[@class=inputText][@name=email3]").val();
	var email4 = $("input[@class=inputText][@name=email4]").val();
	var email5 = $("input[@class=inputText][@name=email5]").val();
	
	StringEmails = email1 + "-*-" + email2 + "-*-" + email3 + "-*-" + email4 + "-*-" + email5 ;
	
	CacherSendSelectionToFriendAgenda();
	
	$('#ConfirmEnvoiAmi').slideDown("slow");
	
	
	$( function() {
		var dataString = 'submit=valider&add=sendFriendListeAgenda&utilisateur_i_id='
				+ id_user + '&mails=' + StringEmails  + tabsend + '&texte=' + txtSendFriend ;
		$.ajax( {
			type :"POST",
			url :"/FRONT/CRT_PIDF/include/myspace/ajax/action_selection.php",
			data :dataString,
			success : function(data) {		
			}
		});
		return false;
	});
	
	
}
/********************************/



