////////////////////////////////////////////////////////////////////
///////////////MESSAGE CACHE CONTENU NON CONNECTER//////////////////
////////////////////////////////////////////////////////////////////

// = > affiche un par niveau
	function errorLogin(){
		
		$('.box_info p.errorLogin:first').show();
		$('.box_info .box_body_sortable').each(function(){
			$(this).find('p.errorLogin').hide();
			$(this).find('p.errorLogin:first').show();
		});
		
		// LINK CONNECTION
		$('.loginFiche').click(function(){
			modalLogin($('.loginFiche').attr('rel'));
			
		});
		
		$('.registerFiche').click(function(){
			modalRegister($(this));
		});
	}
	
///////////////////////////////////////////
////////////MODAL CONNECTION//////////////
////////////////////////////////////////////

// => SET CONNECTION
	function modalLogin(fade){
		//vide les champs
		$('#reminderBox div.errorEmail').html('');
		$('#connectForm div.error').html('');
		$('#reminderBox').css('display', 'none');
		
		//modal de connection
		$('#connectForm').dialog({
			width: 600,
			resizable: false,
			disabled:true,
			draggable:false,	
			buttons: {
				cancel: function() {
					$( this ).dialog( "destroy" );
					backOpac(false);
				},
				connect: function() {
					connect($(this), fade);
				}
			},
			open: function(event, ui) { 
				backOpac(true);
				$(this).css('height', '100%');
				$(this).dialog('option', 'position', 'center');
			},
			close: function(event, ui) { 
				backOpac(false);
				$( this ).dialog('destroy');
			}
		});
		$(".ui-dialog-titlebar").css('display','block'); 
		
		//TRADUCTION CONNECT CANCEL
		$('.ui-button-text').each(function(){
			if($(this).html()=='connect'){
				$(this).html($('#modalConnect').html());
			}else if($(this).html()=='cancel'){
				$(this).html($('#modalCancel').html());
			}
		});
		
		//mdp oubli�
		$('#forgotPass').click(function(){
			$('#reminderBox').fadeIn();
			$('#connectForm').css('height', '100%');
			$('#connectForm').dialog('option', 'position', 'center');
			
		});
		$('#newPassSendEmail').click(function(){
			var data = $('#connectForm form').serialize();
			$.ajax({
				type: "POST",
				url: domain+"/do/login.php",
				data: data+'&reset_pwd=niche',
				success: function(response){
					$('#reminderBox div.errorEmail').html(response);
				}
			});
		});
	}
// => REQUETE LOGIN
	function connect(elm, fade){
		var data = $('#connectForm form').serialize();
		$('#reminderBox').fadeOut();
		$.ajax({
			type: "POST",
			url: domain+"/do/login.php",
			data: data,
			success: function(response){
				if(response=='true'){
					$(elm).dialog( "destroy" );
					backOpac(false);
					if(fade=="noFade"){
						fadeBodyGet();
					}else{
						
						fadeBody();
					}
				}else{
					$('#connectForm div.error').fadeOut(function(){
						$(this).html(response);
						$(this).fadeIn();
					})
					
					if($('#refreshCookie').length>0){
						$('#refreshCookie').click(function(){
							var chaine = window.location.toString();
							if(chaine.lastIndexOf('#')==-1){
								window.location.replace(chaine+'?c=d#validRegister');		
							}else{
								chaine = chaine.substring(0,chaine.lastIndexOf('#')); 
								window.location.replace(chaine+'?c=d#validRegister');	
							}
						});
					}
				}
			}
		});
	}
// => CONNECT SANS MODAL MENU TOP
	var CONNECTPASS =false;
	
	function connectHomeForm(){
		if(CONNECTPASS==false){
			//CONNECTPASS=true;
			
			var data = $('#connect').serialize();
			$('#reminderBox').fadeOut();
			$.ajax({
				type: "POST",
				url: domain+"/do/login.php",
				data: data,
				success: function(response){
					if(response=='true'){
						log('home True');
						fadeBody();
					}else{
						modalLogin();
						$('#connectForm div.error').html(response);
					}
				}
			});
		}
		
	}
// => APPUI SUR ENTRER CONNECT MENU TOP
	function connectHome(){
		$('#password, #login').keyup(function(event){
			if (event.keyCode == '13') {
				connectHomeForm();
				return false;
			}	 								  
		});	
		
	}
	
/////////////////////////////////////////////////
/////////////// MODAL INSCRIPTION ///////////////
/////////////////////////////////////////////////

// => SET SIGN IN
	function modalRegister(elm){
		var url = window.location.toString().replace("&", "!*ET*!");
		var dataEmail = "";
		if($.trim($(elm).attr('rel'))!="")dataEmail = "&email="+$.trim($(elm).attr('rel'));
		$.ajax({
			type: "POST",
			url: domain+"/do/modal/gdm_modal_register.php",
			data: 'language='+$(elm).attr('lang')+'&redirect='+url+dataEmail,
			success: function(modal){
				$('#modal').html(modal);
				if($('#registerForm').length==0){
					errorModal();
				}else{
					$('#registerForm').dialog({
						width: 600,
						resizable: false,
						disabled:true,
						draggable:false,	
						buttons: {
							cancel: function() {
								closeModal($(this));
								backOpac(false);
							},
							register: function() {
								
								var pass=require($('#registerForm'));
								
								if(pass==true){
									pass = register($(this));
								}
							}							
						},
						open: function(event, ui) { 
							backOpac(true);
							$(this).css('height', '100%');
							$(this).dialog('option', 'position', 'center');
						},
						close: function(event, ui) { 
							backOpac(false);
							closeModal($(this));
						}
					});
					$(".ui-dialog-titlebar").css('display','block'); 
		
					//TRADUCTION CONNECT CANCEL
					$('.ui-button-text').each(function(){
						if($(this).html()=='register'){
							$(this).html($('#modalRegister').html());
						}else if($(this).html()=='cancel'){
							$(this).html($('#modalCancel').html());
						}
					});
				}
			}
		});	
	}
// => REGISTER

	function register(elm){
		$.ajax({
			type: "POST",
			url: domain+"/do/modal/gdm_modal_register.php",
			data: $('.ui-dialog #registerForm').serialize(),
			success: function(modal){
				var pass=require($('#registerForm'));
				if(pass==true){
					$('#modal').html(modal);
					//non valide
					if($('#modal #registerForm').length>0){
						$('.ui-dialog #registerForm').html($('#modal #registerForm').html());
					//valide
					}else{
						closeModal($(elm));
						registerOk();
					}
				}
			}
		});
	
	}
// => VALID

	function registerOk(){
		$('#registerOk').dialog({
			width: 400,
			resizable: false,
			disabled:true,
			draggable:false,
			buttons: {
				ok: function() {
					closeModal($(this));
					backOpac(false);
				}
			},
			open: function(event, ui) { 
				backOpac(true);
				$(this).css('height', '100%');
				$(this).dialog('option', 'position', 'center');
				
			},
			close: function(event, ui) { 
				backOpac(false);
				closeModal($(this));
			}
		});
		$(".ui-dialog-titlebar").css('display','block'); 	
	}
	
// => ERROR MAX LOGIN
	function modalLoginWait(message){
		$('#maxLoginWait').dialog({
			width: 400,
			resizable: false,
			disabled:true,
			draggable:false,
			buttons: {
				ok: function() {
					closeModal($(this));
					backOpac(false);
				}
			},
			open: function(event, ui) { 
				backOpac(true);
				$(this).css('height', '100%');
				$(this).dialog('option', 'position', 'center');
				
			},
			close: function(event, ui) { 
				backOpac(false);
				closeModal($(this));
			}
		});
		$(".ui-dialog-titlebar").css('display','block'); 	
		
		//décompte avant conexion
	
   Timer();
	}
	function Timer() {
       var dt=new Date()
       if($('#waitForLogin').html()==0){
		  	var chaine = window.location.toString();
			if(chaine.lastIndexOf('?')==-1){
				if(chaine.lastIndexOf('#')==-1){
					window.location.replace(chaine+'?fade=body');	
				}else{
					chaine = chaine.substring(0,chaine.lastIndexOf('#')); 
					window.location.replace(chaine+'?fade=body');	
				}
			}else{
				chaine = chaine.substring(0,chaine.lastIndexOf('?')); 
				window.location.replace(chaine+'?fade=body');	
			} 
	   }else{
		  $('#waitForLogin').html($('#waitForLogin').html()-1);
		  setTimeout("Timer()",1000);
	   }
   }
