$(document).ready(function() {

	// $('#link_displayLogin').click(function(){displayLogin('inscription');});
	$('#link_displayLogin').unbind('click').click(function(){displayLogSubscribe(true);});
	
	// ouvre la fancy d'inscription si on arrive sur accueil depuis une offre parrainage
	if(typeof(action_sponsorship) != 'undefined')  displayLogSubscribe(); 
	
	$("#header_login #mdp_oublie #mdp_oublie_link").unbind('click').click(function(){
		displayLogSubscribe(false,'forgot');
	});
	// cleanage
  
	$("#form_register_intro").find("#email").focus( function(){ $(".tag_tooltip_email").remove(); });
	$("#header_login").find("#email").focus( function(){ $(".tag_tooltip_login_header").remove(); });
	$("#header_login").find("#password").focus( function(){ $(".tag_tooltip_login_header").remove(); });

	// behavior header
	$('.tag_login').slideUp();

	// champs login et mdp
	$('#header .member #login_module .ident_input [class$=_label]').click(function(){
		$(this).parent().children('input').focus();
	});
	
	$('#header .member #login_module .ident_input input').live('focus', function(){
		login_label_gest($(this), 'hide');
	}).live('blur', function(){
		login_label_gest($(this), 'show');
	});
	////////////
	
	animate_connection.prototype.main=$('#header .member');
	animate_connection.prototype.login_module=$('#header .member #login_module');
	animate_connection.prototype.login_hide=$('#header .member #login_module .login_hide');
	animate_connection.prototype.close=$('#header .member #login_module .close');
	$('.input_connect_email').unbind('focus').focus(function(){animate_connection('show');});
	$('.open_login').unbind('click').click(function(e){e.preventDefault(); animate_connection('show');});
	$('#header .member #login_module .close').unbind('click').click(function(){animate_connection('hide');});
   
  // Cookie
	var remember = $.cookie('remember');
	var mail_elem=$('#header .member #login_module .ident_input #email');
	var pass_elem=$('#header .member #login_module .ident_input #password');
	
	if ( remember == 'true' ){
		var email = $.cookie('email');
		var password = $.cookie('password');
		
		remember_elem=$('#header .member #login_module .check_remember #remember');
		mail_elem.val(email);
		pass_elem.val(password);
		remember_elem.attr('checked' , 'checked');
		if(typeof $.uniform.update=="function") $.uniform.update(remember_elem);
	}else{
		login_label_gest(mail_elem, 'show');
		login_label_gest(pass_elem, 'show');
	}

	if(typeof(scrollto) != 'undefined') $.scrollTo('#'+scrollto, 300);

});

function login_label_gest(element, type){ // type=show/hide
	var name=element.attr('name');
	var cache=element.parent().children('[class$=_label]');
	cache.css('display', (type=='show' ? (element.val()=='' ? 'block' : 'none') : 'none'));
}

function animate_connection(type){ // type=show/hide
	if (type=='show' && !animate_connection.prototype.ident_open){
		$('.open_login').hide();
		$('.input_connect_email').width('154px');
		$('.input_connect_mdp').width('154px');
		$('#sep').show();
		animate_connection.prototype.ident_open=true;
		animate_connection.prototype.main.css({'z-index':'10'});
		animate_connection.prototype.login_hide.css({'display':'block'});
		animate_connection.prototype.login_module.addClass('ombre').animate({'height':'210px'}, 400, function(){
			animate_connection.prototype.close.show();
			// animate_connection.prototype.login_module.find('.input_connect_email').blur().focus(); // debug firefox perte du curseur
		});
	}else if(type=='hide'){
		$('#sep').hide();
		$('.open_login').show();
		$('.input_connect_email').width('134px');
		animate_connection.prototype.close.css('display','none');
		animate_connection.prototype.login_hide.css('display','none');
		animate_connection.prototype.login_module.animate({'height':'105px'}, 200, function(){
			animate_connection.prototype.main.css({'z-index':'6'});
		}).removeClass('ombre');
		animate_connection.prototype.ident_open=false;
	}
}
animate_connection.prototype.ident_open=false;
animate_connection.prototype.main=false;
animate_connection.prototype.login_module=false;
animate_connection.prototype.login_hide=false;
animate_connection.prototype.close=false;

function failure_header_login (form , response){
	trigger = $(form).find('#email') ;	
	info = new Object ; 
	info.type = 1 ; 
	info.pos = 'left,center' 
	info.opacity = 1 ;
	info.decal_x = 51 ;
	info.decal_y = -3 ;
	info.arrow = 'right' ; 
	info.text = '';
	
	if(response.error_collection.email!=true  ) { info.text+="L'email semble invalide.<br/>" ; }
	else {
		if(response.error_collection.email=='empty') { info.text+="Le champ email est vide.<br/>" ; }
		if(response.error_collection.password=='empty') { info.text+="Le champ Mot de passe est vide.<br/>" ; }
		
		info.text += "Si vous avez oublié vos identifiants,<br/> <span class='link_tt_1 lien_rose'><a class='mdp_oublier_member_link'>Cliquez Ici</a></span>" ; 
	}
	info.text_class = new Object ({'padding_left' : 'pad_left_5' , 'padding_right' : 'pad_right_5'  , 'my_class_add' : 'bold' , 'font_size' : 'fs_08', 'color' : 'blanc_text'}) ; 
	info.id_tooltip='tt_verif_login_header' ;
	info.tag_tooltip = 'tag_tooltip_login_header';
	info.color = 'rose1'
	
	tooltip(trigger , info, function(){
		$('#tt_verif_login .mdp_oublier_member_link').unbind('click').click(function(){
			displayLogSubscribe(false,'forgot');
		});
	}) ;
}

function failure_form_register_intro(form , response){
	trigger = $(form).find('#submit_bt').parent() ;	
	info = new Object ; 
	info.type = 1 ; // type de pop_up  passé dans _html_stuff, a dev pour juste preciser option de fleches et basculer automatiquement sur bon type
	info.pos = 'left,center'
	info.opacity = 1 ;
	info.decal_x = 3 ;
	info.decal_y = 0 ;
	info.arrow = 'right' ; // ex :  'left bottom top' => 3 arrows
	info.text="Cet email semble invalide" ;
	
	// cas doublon
	if(response.error_collection.email == 'doublon') {info.text="Cet email existe déja.<br/>Si vous êtes déja inscrit, utilisez<br/> le formulaire juste à gauche" ;}
	
	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_verif_email' ;
	info.tag_tooltip = 'tag_tooltip_email'
	info.color = 'rose1'
	
	tooltip(trigger , info) ;
}

function do_on_submit_login(form , response){
	// cleanage
	$('.tag_tooltip_login').fadeOut(400 , function(){$(this).remove()} );
	
	var form_id = '#'+$(form).attr('id') ; 
	
	login_values = new Object ;
	login_values.trigger =  'ajax_call';
	login_values.email = $(form_id).find('#email').val();
	login_values.password = $(form_id).find('#password').val();
	
	page_login = '/common/lab/components/_login' ;
	
	$.post( page_login , login_values,
		function(response){ 
			if(response == true){ 
				// Remember-Me Code si login correct
				if ($('#member #remember').attr('checked') || $('#form_login_intro').find('#remember').attr('checked')  ){
					var email = $(form_id + ' #email').attr("value");
					var password = $(form_id + ' #password').attr("value");
					  
					// durée du cookie
					$.cookie('email', email, { expires: 30, path:'/' });
					$.cookie('password', password, { expires: 30, path:'/' });
					$.cookie('remember', true, { expires: 30, path:'/' });
				} else {
					// reset cookies
					$.cookie('email', null, { expires: 0, path:'/' });
					$.cookie('password', null, { expires: 0, path:'/' });
					$.cookie('remember', null, { expires: 0, path:'/' });
				}
				   
				// SUBMIT 
				$(form).submit() ;
				
			} else if(response == false) {
				// alert('response_login : ' + response) ;
				switch($(form).attr('id')){
				  case 'form_login' : // log depuis Je suis Deja inscris , ou en mode recup d'email oublié
					  message = 'Email ou mot de passe incorrect' ;
					  $(form).find('input').addClass("pink3");
					  $(form).find('input').bind('focus', function(){  
							$(this).removeClass("pink3");
							$('.tag_tooltip_login').fadeOut(400 , function(){$(this).remove()} );
						});
					  
					  trigger = $(form).find('#submit_bt').parent() ;	
					  info = new Object ; 
					  info.type = 1 ; // type de pop_up  passé dans _html_stuff, a dev pour juste preciser option de fleches et basculer automatiquement sur bon type
					  info.pos = 'left,center'
					  info.opacity = 1 ;
					  info.decal_x = 3 ;
					  info.decal_y = 0 ;
					  info.arrow = 'right' ; // ex :  'left bottom top' => 3 arrows					  
					  info.text="Email ou mot de passe invalide" ;
					  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_verif_login' ;
					  info.tag_tooltip = 'tag_tooltip_login'
					  info.color = 'rose1'
					  
						var message_panic = false;
					  tooltip(trigger , info) ;
					break ;
				  case 'header_login' :
					  trigger = $(form).find('#submit_bt').parent() ;	
					  info = new Object ; 
					  info.type = 1 ; // type de pop_up  passé dans _html_stuff, a dev pour juste preciser option de fleches et basculer automatiquement sur bon type
					  info.pos = 'left,center'
					  info.opacity = 1 ;
					  info.decal_x = 3 ;
					  info.decal_y = 0 ;
					  info.arrow = 'right' ; // ex :  'left bottom top' => 3 arrows
					  info.text="Ce couple email/mot de passe ne correspond à aucun membre.<br/>Merci de vérifier les informations" ;
					  info.text += "<br/>Si vous avez oublié vos identifiants, <span class='link_tt_1 lien_rose'><a  class='mdp_oublier_member_link'>Cliquez Ici</a></span>"
					  info.text_class = new Object ({'padding_left' : 'pad_left_5' , 'padding_right' : 'pad_right_5'  , 'my_class_add' : 'bold' , 'font_size' : 'fs_08' , 'color' : 'blanc_text'}) ; 
					  info.id_tooltip='tt_verif_login' ;
					  info.tag_tooltip = 'tag_tooltip_login_header' ;
					  info.color = 'rose1'
					  
					  tooltip(trigger , info, function(){
							// debug($('#tt_verif_login'));
							// $('#tt_verif_login').html(info.text);
							
							$('#tt_verif_login .mdp_oublier_member_link').unbind('click').click(function(){
								$('#tt_verif_login').remove();
								displayLogSubscribe(false,'forgot');
							});
						}) ;
						var message_panic = true;
						// $('.mdp_oublier_member_link').trigger('click');
					break ;
				  default :
					  alert('Mot de passe ou Email incorrect' ) ; 
				}  
			} else if (response.optin===false){
				displayOnePop('/common/lab/log_subscriber/_optin_pop?email='+$(form_id + ' #email').attr("value"));
			} else if (response.optin_sms===false){
				displayOnePop('/common/lab/payment_pop/_valid_sms?action=reload');
			}
		}, "json");
}

var cliked_credit_info = false;

function handleShortLogin() {
	$(".tag_short_login.credit_info span").click( function(e) {  
		e.preventDefault();
		var credit_info = $('#login_module #credit_info')
		if($(this).hasClass('open') && $(this).attr('id')!="credit_info"){
			$(this).removeClass('open');
			credit_info.animate({'height':'0px'},function(){$(this).css({'padding':'0px'});});
			$('#login_module').animate({'height':'102px'},function(){$('#header .member').css('z-index','6');});
		} else {
			$(this).addClass('open');
			var h = 0;
			if($('ul',credit_info).length>0) h = $('ul',credit_info).outerHeight(true);
			var h2 = 0;
			$('.nav_membre',credit_info).each(function(i,v){
				h2+=$(v).outerHeight(true);
			});
			$('.ss_titre',credit_info).each(function(j,z){
				h2+=$(z).outerHeight(true);
			});
			if($('ul .zodiac_achat',credit_info).length>0) h+=$('ul .zodiac_achat',credit_info).outerHeight(true);
			//else $('#header .member #login_module .ss_titre').css('margin-top','2px');
			
			h+=h2+parseInt($('#header .member #login_module .ss_titre').css('margin-top').replace(/px/, ''));
			var hh = parseInt(102+h);
			if($.browser.msie) hh+=5; 
			$('#header .member').css({'z-index':'10'});
			$('#login_module').animate({'height':hh+'px'});
			credit_info.animate({'height':h+'px','padding-top':'2px'});
		}
	});
	$('#login_module #credit_info').click(function(e){ cliked_credit_info = true;});
	$('body').click(function(e){
		if($(".tag_short_login.credit_info span").hasClass('open') && !$(e.target).hasClass('open') && cliked_credit_info==false){
			$(".tag_short_login.credit_info span").removeClass('open');
			$('#login_module').animate({'height':'102px'},function(){$('#header .member').css('z-index','6');});
			$('#login_module #credit_info').animate({'height':'0px'},function(){$(this).css({'padding':'0px'});});
		} else cliked_credit_info = false;
	});
	$(".tag_short_login.navig_membre").click( function() { $(this).find('option').eq(0).hide() ; });
	$(".tag_short_login.navig_membre").change(
																																	
		function() {			

			var page_path = [];
			page_path = {'1':'/membre/infos_perso' , '2':'/membre/credits_historic' , '3':'/membre/abonnement'  , '4':'/membre/factures' , '5':'/membre/identifiant', '6':'/membre/parrainage'};		
		
			var page_index = $(this).val();							
			var page = page_path[page_index] ;
								
			if(page_index == '0'){return false;}					
			window.location = page;
		}
	);

	
	$("#link_member").hover(
		function () {
						$('#'+this.id + ' div').css("opacity",0.1).animate({ opacity: 0.5}, 500 ) ; 
		}, 
		function () {
						$('#'+this.id + ' div').css("opacity",0.5).animate({ opacity: 1}, 500 ) ;
		}
  );

	$('#header_login').keydown(function(event) { if (event.keyCode == 13) { $('#header_login #submit_bt').click(); return false; } });
}

