/*
 *	@titre: postit.
 *	@description: javascript post-it - dream on.
 *	@auteur: neov - http://www.neov.net.
 *	@creation: 20091109.
 *	@modification: 20091218
 *	@required: jQuery 1.3.2
 *
 */

var textePostit = new Array();
textePostit['fr_FR'] = ['Afficher les Post-its', 'Masquer les Post-its'];
textePostit['en_EN'] = ['Show Post-it', 'Hide the Post-its'];
textePostit['es_ES'] = ['Mostrar los Post-it', 'Ocultar los Post-it'];
textePostit['de_DE'] = ['Post-its anzeigen', 'Post-its verbergen'];

var contenuPostit = '';
var mailPostit = '';
var nomPostit = '';
var publicPostit = '';
var deletedId = 0;


$(
	function()
	{

		// cache saisie post-it au chargment.
		$('.post-it').find('a').removeClass('active');
		$('.post-it').find('form .ecrire').hide();
		
		// ecrire post-it.
		$('.post-it').find('a').click(
			function()
			{
				$('.post-it #post-it').html('');
				if(!$(this).hasClass('running')) {
					var $btn = $(this);
					$(this).addClass('running');
					
					$(this).toggleClass('active');
					if($(this).hasClass('active')) {
						$(this).parent().next('form').find('.ecrire').slideDown({queue:true, duration:300, easing:"backEaseOut"});   
					} else {
						$(this).parent().next('form').find('.ecrire').slideUp({queue:true, duration:300, easing:"backEaseIn"});
					}
					
					$(this).parent().next('form').find('.ecrire').queue(
						function ()
						{
							$btn.removeClass('running');
							$(this).dequeue();
						}
					);
				}
				return false;
			}
		);
		
		// textarea resizer.
		$('.post-it-textarea').TextAreaResizer();
		
		
	// enchainement affichage ecrire post it
		//validation contenu postit
		$('.post-it .ecrire input[type=submit]').click(
			function()
			{
			    //tmtvalidator
			    bErreur = tmt_validateForm($("#ecrirePostit").get(0));
				if (!bErreur){
	            	return false;
	      		}
			
			   // enregistrement des valeurs saisies     
			  	contenuPostit = $('.post-it #post-it').val();
			  	mailPostit = $('.post-it #post-mail').val();
			  	nomPostit = $('.post-it #post-nom').val();

			  	// test si un dossier est déjà ouvert
			  	if (email == '' ) // || pageActuelle == '1'
			  		//pas dossier ouvert : pas de choix entre publique et privé
			  	{
			  		  publicPostit = 1;                  // tout de suite publique
							var chemin = j_basepath + 'ajax.php?module=postit&action=postitFo:ajoutPostit';
							
							$('.post-it .post-it-message').slideDown({queue:true, duration:gtime, easing:"backEaseOut"});
							$('.post-it .ecrire').slideUp({queue:true, duration:300, easing:"backEaseIn"});				
							
							$.ajax(
								{
									type: 'GET',
									url: chemin,
									data: { zMail: mailPostit, ztext: contenuPostit, zNom: nomPostit, iPublic: publicPostit},
									dataType: 'json',
									success: function(response)
									{
										
												$('.post-it').find('a').removeClass('active');
												$('.post-it').find('a')
													.unbind('click')
													.click(
														function()
														{	
															$('.post-it #post-it').val('');
															return false;
														}
													);										
												
												$('.post-it').find('a')
													.unbind('click')
													.click(
														function()
														{
															//	alert("here");
																$('.post-it .post-it-message').slideUp({queue:true, duration:300, easing:"backEaseIn"});
																if(!$(this).hasClass('running')) {
																	var $btn = $(this);
																	$(this).addClass('running');
																	
																	$(this).toggleClass('active');
																	if($(this).hasClass('active')) {
																		$(this).parent().next('form').find('.ecrire').slideDown({queue:true, duration:300, easing:"backEaseOut"});   
																	} else {
																		$(this).parent().next('form').find('.ecrire').slideUp({queue:true, duration:300, easing:"backEaseIn"});
																	}
																	
																	$(this).parent().next('form').find('.ecrire').queue(
																		function ()
																		{
																			$btn.removeClass('running');
																			$(this).dequeue();
																		}
																	);
															} 
															
															return false;
														}
													);													
				
												$('.post-it #post-it').val('');			  
					  						$('.post-it #post-nom').val($('.post-it #post-nom').attr('title'));
												$('.post-it #post-choix-publique').attr('checked', true);
												$('.post-it #post-choix-prive').attr('checked', false);
												
												setTimeout(function(){
												$('.post-it .post-it-message').slideUp({queue:true, duration:300, easing:"backEaseIn"});
												}, 4000);
												
												return false;
		
									}
								}
							);			  		 
					}
					else
						// dossier ouvert : choix entre publique et privé 
					{
							
						
							$('.post-it .choix-publication').slideDown({queue:true, duration:gtime, easing:"backEaseOut"})
							$('.post-it .ecrire').slideUp({queue:true, duration:300, easing:"backEaseIn"});
							$('.post-it').find('a').removeClass('active');
							$('.post-it').find('a')
								.unbind('click')
								.click(
									function()
									{
										return false;
									}
								);

					}

				return false;
			}
		);
		
		//validation type confidentialité
		$('.post-it .choix-publication input[type=submit]').click(
			function()
			{	
				publicPostit = $('.choix-publication input:radio:checked').val();     //0 si Privé , 1 si publique
				
				if (publicPostit == 0)
				{
						$('.post-it').find('a').removeClass('active');
						$('.post-it').find('form .ecrire').hide();
						$('.post-it .choix-publication').slideUp({queue:true, duration:300, easing:"backEaseIn"});
				}
				else
				{
					$('.post-it .post-it-message').slideDown({queue:true, duration:gtime, easing:"backEaseOut"})
					$('.post-it .choix-publication').slideUp({queue:true, duration:300, easing:"backEaseIn"});
				}
				
				
				$('.post-it').find('a')
					.unbind('click')
					.click(
						function(publicPostit)
						{
							if (publicPostit == 1)
							{
									$('.post-it .post-it-message').slideUp({queue:true, duration:300, easing:"backEaseIn"});
							}

								if(!$(this).hasClass('running')) {
								var $btn = $(this);
								$(this).addClass('running');
								
								$(this).toggleClass('active');
								if($(this).hasClass('active')) {
									$(this).parent().next('form').find('.ecrire').slideDown({queue:true, duration:300, easing:"backEaseOut"});   
								} else {
									$(this).parent().next('form').find('.ecrire').slideUp({queue:true, duration:300, easing:"backEaseIn"});
								}
								
								$(this).parent().next('form').find('.ecrire').queue(
									function ()
									{
										$btn.removeClass('running');
										$(this).dequeue();
									}
								);
							} 
							
							return false;
						}
					);

					var chemin = j_basepath + 'ajax.php?module=postit&action=postitFo:ajoutPostit';				
					$.ajax(
						{
							type: 'GET',
							url: chemin,
							data: { zMail: mailPostit, ztext: contenuPostit, zNom: nomPostit, iPublic: publicPostit},
							dataType: 'json',
							success: function(response)
							{

								$('.post-it #post-it').val('');			  
			  				$('.post-it #post-nom').val($('.post-it #post-nom').attr('title'));
								$('.post-it #post-choix-publique').attr('checked', true);
								$('.post-it #post-choix-prive').attr('checked', false);         
								
								if (publicPostit == 0)
								{
									location.reload();
										return false;
								}
								else
								{
									setTimeout(function(){
									$('.post-it .post-it-message').slideUp({queue:true, duration:300, easing:"backEaseIn"});
									}, 4000);
									return false;
								}
								

							}
							//return false;
						}
					);

					
					return false;
				
			}
		);
		
		// effacer post it.
		$('.post-erase a').click(
			function()
			{
				//var l = $('.post-it-bloc .content > div').length;
				var l = $('#hiddenresult div.result').length;
				if (l > 1) {
					var deletedId = $('.post-it-bloc .content > div').attr('id');
					$('#' + deletedId, $('#hiddenresult')).remove();
					var lr = $('#hiddenresult div.result').length;					
					$('.post-it-hide span').text('('+lr+')');
					supprimerPostit(deletedId);
					
					initPaginationPostIt();					
				} else {
					var deletedId = $('.post-it-bloc .content > div').attr('id');
					$('#' + deletedId, $('#hiddenresult')).remove();
					$('.post-it-hide span').text('('+0+')');
					supprimerPostit(deletedId);					
					$('.post-it-bloc').remove();
					$('.post-it-hide').hide();
				}
				return false;
			}
		);
		
		// affiche/cache post it.
		$('.post-it-hide a').click(
			function()
			{
				if(!$(this).hasClass('running')) {
					var $btn = $(this);
					$(this).addClass('running')
					$(this).toggleClass('inactive');
					
					if($(this).hasClass('inactive')) {
						$('.post-it-bloc').animate({opacity: 0}, {queue:true, duration:gtime, easing:"sineEaseOut"});
						$(this).removeAttr('title');
						$(this).attr({title:textePostit[lang][0]});
						$(this)
							.html(textePostit[lang][0] + ' <span>('+ $('#hiddenresult div.result').length +')</span>');
					} else {
						$(this).removeAttr('title');
						$('.post-it-bloc').animate({opacity: 1}, {queue:true, duration:gtime, easing:"sineEaseOut"});
						$(this).attr({title:textePostit[lang][1]});
						
						$(this)
							.html(textePostit[lang][1] + ' <span>('+ $('#hiddenresult div.result').length +')</span>');
					}
				}
				
				$('.post-it-bloc').queue(
					function()
					{
						$btn.removeClass('running');
						$(this).dequeue();
					}
				);

				return false;
			}
		);
		
		// affichage post it.
		initPaginationPostIt();
		return false;
		
	}
	
	
);

function supprimerPostit(deletedId) {

			
 		 var cheminSuppressionPostit = j_basepath + 'ajax.php?module=postit&action=postitFo:suppressionPostit';
 		 //var theId = 
						
					$.ajax(
						{
							type: 'GET',
							url: cheminSuppressionPostit,
							data: { i_Id : deletedId},
							dataType: 'json',
							success: function(response)
							{
								//alert ("true");
							//	updateNumber(response.iNbFavoris);
							//	modeAjoutFavori = 0;
		 						getSessionFavoris();
		 						return false;
							}
						}
				); 
						
			 
			
			return false;		
}


function displayErrorPostIt(frm, invalidFields) {
	for(i=0;i<invalidFields.length;i++){    	
       switch(invalidFields[i].name){           
    	   case 'post-mail':    	   		
    	   		$('input[name='+invalidFields[i].name+']').val(erreurEmail); //'Adresse non valide');
    	       	break;
       }
	}
}


 function initPaginationPostIt() {
    var num_entries = $('#hiddenresult div.result').length;
    // Create pagination element
    $("div.post-it-bloc div.bottom p.pagination").paginationPostIt(num_entries, {
        num_edge_entries: 0,
        num_display_entries: gNbPostIt,
        callback: pageselectCallbackPostIt,
        items_per_page:1,
        prev_show_always :true,
        next_show_always : true
          
    });
 }
             
function pageselectCallbackPostIt(page_index, jq){
    var new_content = $('#hiddenresult div.result:eq('+page_index+')').clone();
    $(new_content).attr()
    
    var classNew = $('#hiddenresult div.result:eq('+(page_index)+')').attr('class');
					
	if ($('.post-it-bloc').hasClass('blanc')) {
		var oldClass = $('.post-it-bloc').attr('class');
		var newClass = oldClass.replace('blanc', classNew);
		$('.post-it-bloc').attr({'class': newClass});
	} else {
		var oldClass = $('.post-it-bloc').attr('class');
		var newClass = oldClass.replace('rose', classNew);
		$('.post-it-bloc').attr({'class': newClass});
	}
	
	if ($('#hiddenresult div.result:eq('+(page_index)+')').hasClass('mien') )
	{ $('.post-erase').show(); }
	else
	{ $('.post-erase').hide(); } 
					
    $('div.post-it-bloc div.content').empty().append(new_content);
    return false;
}
