//////////////////////////////////////////////////////////////////////
//

$(document).ready(function()
{	
	$('#identification .submit').click(function()
	{
		// alert('yop');
		login_do();
		return false;
	});	
	$('#identification-popup #submit').click(function()
	{
		login_popup_do();
		return false;
	});
	
	$('#identification :input').focus(function()
	{
		$('#identification #feedback').html('&nbsp;');
	});
	
	
	$('#identification .forgotten').click(function()
	{
		login_forgotten_open();
		return false;
	});
	
	$('#identification-popup .forgotten').click(function()
	{
		window.parent.login_forgotten_open();
		return false;
	});
	
	$('#login_forgotten_form #submit').click(function()
	{
		login_forgotten_submit();
		return false;
	});	

	$('#login_forgotten_form .btn_close').click(function()
	{
		// alert('yop');
		login_forgotten_close();
		return false;
	});
	
	$('#identification-popup .btn_close').click(function()
	{
		// alert('yop');
		login_forgotten_close();
		return false;
	});
	
	
	$('.unlogged').click(function()
	{
		login_popup_open();
		return false;
	});	
	
	$('.subscribe').click(function()
	{
		window.parent.location = window.parent.$('#subscribe a').attr('href');
		return false;
	});	

	$('input, textarea').click(function(){
		$('#feedback').slideUp(500, null);
	});
	
	$('#identification input').keydown(function()
	{
		if (event.keyCode == '13') {
			login_do();
			event.preventDefault();	
		}
  	});
	
});


//////////////////////////////////////////////////////////////////////
//

function login_do ()
{
	var data_str = $("#identification :input").serialize();
	data_str 	+= '&as_ajax=1&do=login';
	
	$.ajax({
		cache: false,
		type: "POST",
		url: "admin_/include_global/login/login-actions.php",
		data: data_str, 
		success: function(feedback)
		{
			feedback = feedback.split('|');
			
			if (feedback[0] == 1)
			{
				window.location = feedback[1]; 
			}
			else
			{
				login_error_display(feedback[1]);
			}
		}
	});	
}

//////////////////////////////////////////////////////////////////////
//
function login_popup_do ()
{
	
	var data_str = $("#identification-popup :input").serialize();
	data_str 	+= '&as_ajax=1&do=login';

	$.ajax({
		cache: false,
		type: "POST",
		url: "../login/login-actions.php",
		data: data_str, 
		success: function(feedback)
		{
			feedback = feedback.split('|');
				
			if (feedback[0] == 1)
			{
				window.parent.location = feedback[1]; 
				login_forgotten_close();
			}
			else
			{
				login_popup_error_display(feedback[1]);
			}
		}
	});	
}
/*
*/
//////////////////////////////////////////////////////////////////////
//

function login_error_display (message)
{
	$('#identification .feedback').html(message);
}

function login_popup_error_display (message)
{
	$('#identification-popup #message').slideUp(500, null);
	$('#identification-popup #feedback').html(message).slideDown(500, null);
}

function login_forgotten_succes_display (message)
{
	$('#login_forgotten_form .form').slideToggle();
	$('#login_forgotten_form #feedback').html(message).fadeIn(500, null);
}

function login_forgotten_error_display (message)
{
	// alert(message);
	$('#login_forgotten_form #feedback').html(message).fadeIn(500, null);
}

//////////////////////////////////////////////////////////////////////
//

function login_popup_open()
{
	// 
	Shadowbox.open({
		player : 	'iframe',
		title:      ' ',
        content:    'admin_/include_global/login/login-popup.php',
        height:     300,
        width:      500
    });
}


//////////////////////////////////////////////////////////////////////
//

function login_forgotten_open()
{
	//
	// alert('login_forgotten_open'); 
	Shadowbox.open({
		player : 	'iframe',
		title:      ' ',
        content:    'admin_/include_global/login/login-forgotten.php?do=login_forgotten',
        height:     300,
        width:      500
    });
}

//////////////////////////////////////////////////////////////////////
// Fermeture de l'iframe ouverte via Shadowbox

function login_forgotten_close()
{
	// window.parent.form_succes_end();
	window.parent.Shadowbox.close();
}


//////////////////////////////////////////////////////////////////////
//

function login_forgotten_submit()
{
	$('#login_forgotten_form #feedback').fadeOut(200, null);
	// 
	var data_str = $("#login_forgotten_form :input").serialize();
	data_str 	+= '&as_ajax=1&do=login_forgotten_submit';

	$.ajax({
		cache: false,
		type: "POST",
		url: "login-actions.php",
		data: data_str, 
		success: function(feedback)
		{
			feedback = feedback.split('|');
			
			if (feedback[0] == 'ok')
			{	
				// login_forgotten_succes_display(feedback[1]);
				// $('.form').css('visibility','hidden');
				$('#login_forgotten_form .form, #login_forgotten_form .message').css('visibility','hidden');
				$('#feedback').html(feedback[1]).slideDown(500, null);
				// $('#feedback').html(feedback[1]).slideDown(500, null);
			}
			else
			{
				$('#feedback').html(feedback[1]).slideDown(500, null);
				// login_forgotten_error_display(feedback[1]);
			}
		}
	});	
}

//////////////////////////////////////////////////////////////////////
//
/*
function signin_do ()
{
	var data_str 	= '&as_ajax=1&do=addressbook_load_remote';

	$.ajax({
		cache: false,
		type: "POST",
		url: "../../../../admin_/include_global/addressbook/addressbook-actions.php",
		data: data_str, 
		success: function(feedback)
		{
			var feedback = feedback.split('|');
			$('#addressbook-list').html(feedback[1]);
		}
	});	
}
*/