
$(document).ready(function(){
	
	/**
	 * Initialisation du menu
	 * Il y a un bug avec IE6 sur le deuxième niveau
	 */
	$('#menu').find('ul').parent().each(function(){
		var submenu = $(this).find('ul:eq(0)');
		var alink = $(this).find('a:eq(0)');
		$(this).hover(
			function(){
				submenu.fadeIn('fast');
			},
			function(){
				submenu.fadeOut('fast');
			}
		);
	});
	
	/** Vide les champs pré-remplis **/
	$('#mailings_email').focus(function() {
		$(this).val('');
	});
	
	$('#search').focus(function() {
		$(this).val('');
	});
	
	
	/** Gestion du formulaire login AJAX */
	$('.link_login').live('click', function(){
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : path2+'/index.php?lang='+$('html').attr('lang')+'&p=login',
			success : function(msg){
				$('body').append(msg);
				hideLoading();

				/** Submit sur le formulaire */
				$('#form_login').live('submit',function(){
					$('#login_ajax_box').remove();
					showLoading();
					
					var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :path2+'/index.php?lang='+$('html').attr('lang')+'&p=login&a=check',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',8000);
						},
						complete : function() {
							refresh();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	
	
	/** Gestion du formulaire mot de passe oublié AJAX */
	$('.link_forgotpassword').live('click', function(){
		$('.ajax_box').remove();
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : path2+'/index.php?lang='+$('html').attr('lang')+'&p=forgotpassword',
			success : function(msg){
				$('body').append(msg);
				hideLoading();

				/** Submit sur le formulaire */
				$('#form_forgotpassword').live('submit',function(){
					$('#forgotpassword_ajax_box').remove();
					showLoading();
					
					var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :path2+'/index.php?lang='+$('html').attr('lang')+'&p=forgotpassword&a=check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',8000);
						},
						complete : function() {
							refresh();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	
});
/** Rafraichissement du bloc "mon panier", "mon compte"  **/
function refresh() {
	$('#basket_content').remove();
	showBasketLoading();
	$.ajax({
		type: 'POST',
		url :path2+'/index.php?lang='+$('html').attr('lang')+'&p=basket&a=refresh',
		success : function(msg) {
			$('#basket').append(msg);
			hideBasketLoading();
		}
	});
}

/** Ferme le bloc ok après quelques secondes et cache l'overlay **/
function showOk() {
	if($('#ok_box').length>0) {
		$('#ok_box').fadeOut('slow');
		hideOverlay();
	}
}
/** Supprime le bloc AJAX **/
$('.close_ajax_box').live('click',function(){
	$(this).parents('.ajax_box').remove();
	hideOverlay();
});



/** Fonctions d'affichage / masquage des loaders et de l'overlay **/
function showOverlay() 		{ $("#overlay").css({'display': 'block','opacity': 0.4});}
function showLoading() 		{ $('#loading').show()}
function showBasketLoading(){ $('#loading_basket').show()}
function showFiltreLoading(){ $('#filtre_loading_ajax').show()}

function hideOverlay() 		{ $("#overlay").css({'display': 'none'});}
function hideLoading() 		{ $('#loading').hide()}
function hideBasketLoading(){ $('#loading_basket').hide()}
function hideFiltreLoading(){ $('#filtre_loading_ajax').hide()}

