// JavaScript Document


 
$(document).ready( function(){

	
	// In Membre part
	
	$('#error_reporting #recup_id_link').click( function () {
			var email_to_send  = $(this).attr('alt') ;		
			var response = send_id_amnesia(email_to_send);
			
				if(response=='ok')
					{	
						var mess_confirm = "Un email contenant vos identifiants a été envoyé à :<br/> <span id='email_doublon' class='orange'>" +  email_to_send  + "</span> <br/> ...";		  
						$("#error_reporting #recup_id").fadeOut(350, function () { $(this).html(mess_confirm); $(this).fadeIn(700); });
				 }
			
			}	);
	
	


	// In Login part (index.php)
	
	$("form[id='form_register_intro']  *[id$='_bt']").click(function(){ $("*[id^='tt']").remove(); } );
	$("form[id='form_login']  *[id$='_bt']").click(function(){ $("*[id^='tt']").remove(); } );

	
	
	$(".tag_forget_password").click(function(){ 
											   $("*[id^='tt']").remove();
											   $(this).parents("form").find("input[type!='hidden']").val('').removeClass("pink3");
											   $(".tag_remember_me").hide() ;
											  });
	
	$(".tag_forget_email").click(function(){ 
											$("*[id^='tt']").remove();
											$(this).parents("form").find("input[type!='hidden']").val('').removeClass("pink3");
											$(".tag_remember_me").hide() ;
											});
	
	
	
	
	
	
	
	// CANCEL 
	$('#cancel_bt').click(function(){  // retablit le formulaire Login de base 
    
	 $(".tag_remember_me").show() ;
	
	$(this).parents('.tag_cancel_line').hide() ;
    $("#head_message_login").hide().html('Je suis déja inscrit').removeClass('rose1_text').fadeIn(300) ;


    // case modepasse oublié 
    $('#pass_line').children().show(function(){$(this).fadeTo(400,1);});					
    $(this).parents('form').find("a[id='send_id_bt']").hide();


   // case email oublié 
    $(this).parents('form').find("input[id='email'],input[id='login'],input[id='password']").removeAttr('readonly' ) ;	
	$('#row_4 > ul').fadeTo(200,1);
	
	$(".tag_submit_contener").find('#member_access_bt').remove() ;
	$(".tag_submit_contener").find('#send_email_bt').remove() ;
	
	$('#email_recup_line').remove() ;
	$(".tag_submit_contener").find('#submit_bt').show() ;
	
	$(this).parents('form').find("a[id='send_email_bt']").hide();																																			
    $(this).parents('#form_login').find('#email_line').fadeTo(300, 0.1 , function() { 	
																					$(this).html($(this).data('content')) ;
																					$(this).fadeTo(300,1) ;	
																			        }
																		  );


	// Pour les 2 cas 
	  $(this).parents('form').find("a[id='submit_bt']").show();
	  $(this).parents('form').find('#forgotten_pass').show();
	  $(this).parents('form').find('#forgotten_email').show();
	  
	  $(this).parents("form").find("input[type!='hidden']").val('');
	  $(this).parents("form").find('input').unbind();
	  }
	  );
	
	
	
	
	// click sur les messages de recup
	$('.tag_forget_password').click( forget_password ) ;
	$('.tag_forget_email').click( forget_email) ;
	
	
	function forget_email()
	{
			// change head
			$("#head_message_login").hide().html('Email oublié').addClass('rose1_text').fadeIn(300) ;
			
			
			$(this).parents('#form_login').find('#email_line').fadeTo(300, 0.1, 
																	  function() { 
																				  $(this).data('content' , $(this).html() )   ;	
																				  $(this).children('label').html('Pseudo :') ;	
																				  $(this).children("input[id='email']").attr('id','login').attr('name','login'); 
																				  $(this).children("input[id='login']").focus(); 	
																				  $(this).fadeTo(300,1) ;	
                                                                                 }
	        									                      );
			
			
			// hide submit_bt, affiche son clone avec un autre behavior on click  ( not check form )
			var submit_bt =  $(this).parents('form').find("a[id='submit_bt']");
			var send_email_bt = submit_bt.clone(false);
		    send_email_bt.attr('id','send_email_bt').unbind() ;
			send_email_bt.click(send_forget_email) ; 
			
			$(submit_bt).after(send_email_bt) ;

			submit_bt.hide();
			
			$('#forgotten_email').hide();
		    $('#forgotten_pass').hide();
			
		    $('.tag_cancel_line').fadeIn(300); 
 
				trigger = $('#email_line') ;	
				info = new Object ; 
				info.pos = 'center,bottom' ;
				info.decal_x =  -70 ;
				info.decal_y = 35 ;
				info.arrow = 'right' ; 
				
				info.text = "Indiquez vos <span class='bold'>Pseudo</span> et <span class='bold'>Mot de Passe</span><br/>et cliquez sur <span class='bold rose1_text'>Valider</span>" ;
				info.id_tooltip='tt_recup_id' ;
				info.tag_tooltip = 'tag_tt_recup_id'  ;
				info.color = 'vert_pomme';
				
				tooltip(trigger , info) ;
				
			

	}
	
	 function forget_password() 
	 {
		var submit_bt =  	$(this).parents('form').find("a[id='submit_bt']");
	
	  // hide submit_bt, affiche son clone avec un autre behavior on click  ( not check form )
		 var send_id_bt = submit_bt.clone(false);
		 send_id_bt.attr('id','send_id_bt').unbind() ;
		 send_id_bt.click(send_forget_id) ;
			
		 $(submit_bt).after(send_id_bt) ;
		 submit_bt.hide();
				
		
	  $(this).parents('#forgotten_pass').fadeOut(400,function(){$('.tag_cancel_line').fadeIn(300)});
	  $('#forgotten_email').fadeOut(400,function(){$(this).hide()});

	  $("#head_message_login").hide().html('Mot de passe oublié').addClass('rose1_text').fadeIn(300) ;
		
	  $('#pass_line').children().fadeTo('300', 0.1, function() {$(this).hide();});
		
		  trigger = $('#email_line') ;	
		  info = new Object ; 
		  info.pos = 'center,bottom' ;
		  info.decal_x =  -75 ;
		  info.decal_y = 10 ;
		  info.arrow = 'right top' ; 
		  
		  info.text = "Indiquez votre <span class='bold'>Email</span> et cliquez sur <span class='bold rose1_text'>Valider</span>.<br/><span class='center'>Vos identifiants vous seront renvoyés</span>" ;
		  info.id_tooltip='tt_recup_id' ;
		  info.tag_tooltip = 'tag_tt_recup_id'  ;
		  info.color = 'vert_pomme';
		  
		  tooltip(trigger , info) ;
																		
		}
		

		$('.tag_send_id_amnesia').click( send_forget_id ) ;	
		
		function send_forget_id() 
		{		
				
		  var email_to_send = $(this).parents("form").find("#email").val();
		  var response = send_id_amnesia(email_to_send);
				
					if(response=='ok')
					{	
					
					$("#tt_recup_id").find(".content div").fadeOut(200 ,function(){																																																												                    $(this).html("Un email contenant vos identifiants a été<br/>envoyé à <span class='pink1 bold'>" + email_to_send  + "</span>").fadeIn(500)
																																																																		                                                                                   }) ;  
				   $("#send_id_bt").unbind() ;
					
					} else {
					$("#tt_recup_id").find(".content div").html("Cet <span class='bold'>email</span> ne correspond à aucun abonné.<br/>Corrigez l'adresse et recommencez.") ;
					$(this).parents("form").find('input').bind('focus', 
															   function()
															   { 																																																													                                                                $("#tt_recup_id").find(".content div").fadeOut(200 ,function(){$(this).html($("#tt_recup_id").data('content')).fadeIn(500)} )     																																																												                                                               });					
					}	
		}	
		
		
		
	  
	  
	  //----------------------------------------
	  
	  function send_forget_email() 
	  {				
		 var login = $(this).parents("form").find("#login").val();
		 var password = $(this).parents("form").find("#password").val();
		 var response = send_email_amnesia(login, password) ;
		 
		 
	 // Patch : cas user old inscrit sans  email, redirect espace membre avec forcing de renseigner email 
	 
	  if(  typeof(response.uncomplete_registration) != 'undefined'  && response.uncomplete_registration == 'email' )
	  {
		  window.location.href ='/membre/info_modif?uncomplete_registration=email#row_1'; 		  
		  return false ;
	  }	 
		 
	  
	  if(response.email == false)
	  {
  
		  error_message = '' ;	  
		  concate = '';
		  jQuery.each( response.error , 
					  function(i, val) {
		                               error_message += concate +  val ;
		                               concate = '<br/>';
                                        });
		  
 
		$("#tt_recup_id").find(".content div").html(error_message) ;
		 
		$(this).parents("form").find('input').bind('focus',
													function(){ 																																																													                                                              $("#tt_recup_id").find(".content div").fadeOut(200 ,function(){$(this).html($("#tt_recup_id").data('content')).fadeIn(500)} ) 
                                                              $(this).parents("form").find('input').unbind();  
															  });		
	  } else {
	  
	 
	 // note : '#email_line' = ancienne ligne email, transformé en Login pour recup
	  var email_recup_line = $('#email_line').clone() ;
	  email_recup_line.attr('id','email_recup_line') ;
	  email_recup_line.css('opacity' , 0 ) ;
	  $('#email_line').before(email_recup_line) ;
	  
	   $("#email_recup_line input[type='hidden']").remove(); // workaround :id de form contenu dans cete ligne , donc on le duplique pas 
	   $('#email_recup_line label').html('Email :');
	   $("#email_recup_line input[id='login']").attr('id','email').attr('name','email').val(response.email).addClass('input_selected') ;
	   $('#email_recup_line').fadeTo(1100, 1);
	   $("*[id^='tt_']").hide();
	   
	   
		// attach_to = $('#email_recup_line #email') ;	
		
		 attach_to = $('#email_recup_line') ;	
		// $("#head_form")
		
		
		info = new Object ; 
		info.pos = 'center,top' ;
		info.decal_x =  50 ;
		info.decal_y = 52 ;
		info.arrow = 'center bottom' ;
		info.opacity = 1 ;
		
		info.text = "Voici l'<span class='bold'>email</span> associé à ce compte. Si vous souhaitez le modifier,<br/> rendez-vous sur votre <span class='tag_link_identifiant'><span class='rose1_text bold'>Espace Membre > Mes identifiants</span></span>" ;
		// info.text_class = new Object ({'padding_left' : 'mon_padding_left' , 'padding_right' : 'mon_padding_riiiiiiight'  , 'ma_class' : 'une_class' , 'des_class' : ' maclass1 , maclass2'}) ; 
		// info.text_class = new Object ({'padding_left' : 'pad_left_5' , 'padding_right' : 'pad_right_5'  , 'my_class_add' : 'bold' , 'font_size' : 'fs_11' }) ; 
		info.id_tooltip='tt_recup_id' ;
		info.tag_tooltip = 'tag_tt_recup_id'  ;
		info.color = 'vert_pomme';
		
		tooltip(attach_to , info) ;
		
		$('#row_4 > ul').fadeTo(200,0.1);
		
		
		// create new button => `accéder a votre espace membre`
		
		var page =  '/lab/ajax/_html_stuff' ; 					
		info = new Object();
		info.ask_for = 'button' ;
		info.option_button = new Object();
		info.option_button.text =  'Accéder à votre Espace Membre' ;
		info.option_button.color =  'bleu' ;
		info.option_button.id =  'member_access_bt' ;
		
		var button =  $.ajax({ type: "POST", url: page, async: false , data: info, dataType: 'html' }).responseText ;
		//alert( button ) ;
		
		$('.tag_submit_contener').fadeTo(400,0.1 , 
										 function()
										 {    
											// $(this).data('content' , $(this).html() )   ; // rollback
											$(this).children('#send_email_bt').replaceWith(button);
										    $('#member_access_bt').click( function() { $('#form_login').find('#submit_bt').click() ; } );	  // new button prend le comportement de submit_bt
											$(this).fadeTo(400,1) ; 
											$(this).parents('form').find("input[id='email'],input[id='login'],input[id='password']").attr('readonly' ,'readonly') ;		
										 });
		
		
	// link ver Espace Membre > Mes Identifiants
	
	
	$(".tag_link_identifiant").hover(  function(){$(this).children('span').css({'color':'#FA7AC0' , 'cursor':'pointer'}) } ,
									   function(){$(this).children('span').css({'color':'#F73FA5' , 'cursor':'inherit'}) }   
									 );
	
	$(".tag_link_identifiant").click(
									 function() { 
										          $('#form_login').attr('action' , $('#form_login').attr('action') + '&goto=identifiant' ) ;  
										          $('#form_login').find('#submit_bt').click() ;
									            } 
									 );
	
	
	
	}
	  
	   
	
	
	 }	
	

												
							
}); // End Document Ready

function send_email_amnesia(login, password)
{
	var page =  '/lab/ajax/_send_id_amnesia' ; 					
	info = new Object();
	info.trigger = 'forget_email' ;
	info.login = login ;
	info.password = password ;
	
	var result =  jQuery.parseJSON( $.ajax({ type: "POST", url: page, async: false , data: info, dataType: 'json' }).responseText);
	return result ;		
}



function send_id_amnesia(email_to_send)
{

				var page =  '/lab/ajax/_send_id_amnesia' ; 					
				info = new Object();
				info.trigger = 'forget_mdp' ;
				info.email_to_send = email_to_send ;
				var result =     $.ajax({ type: "POST", url: page, async: false , data: info, dataType: 'text' }).responseText; 
				return result ;		
}
	


