str_length = 26;
suggestion = true;
	function lookup(inputString) {
		if(inputString.length == 0 || inputString.length == 1 && suggestion) {
			$('#suggestions').hide();
		} else {
			if(inputString.length <= str_length) {suggestion = true;}
			if(str_length > 0 && suggestion) {
			$.get(fw_uri+'/'+$('html').attr('lang')+"/search/autocompletion/index."+inputString+".html", {/*variable : ""+valeur+""*/}, function(data){
				if(data.length == 0) {
					$('#suggestions').hide();
					suggestion = false;
					str_length = inputString.length;
				}
				if(data.length >1) {
					n = -1;
					suggestion = true;
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
			}	
		}
	} 
	
	function fill(thisValue) {
		$('#search').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
	}

$(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('');
	});
	/** Auto-completion **/
	$('#search').attr('autocomplete','off');
	n = -1;
	$('#search').live('keyup', function(event) {
		if(event.keyCode == 40) {
 			if(n+1 < $("#autoSuggestionsList > ul > li").length) {
				n = n + 1;
				$("#autoSuggestionsList > ul > li").eq(n).attr('class','hover').focus();
				if(n > 0 && n < $("#autoSuggestionsList > ul > li").length) {
					$("#autoSuggestionsList > ul > li").eq(n-1).removeClass('hover');
				}
			}
		} else if(event.keyCode == 38) {
			if(n > 0) {
				n = n - 1;
				if(0 <= n <= $("#autoSuggestionsList > ul > li").length) {
					$("#autoSuggestionsList > ul > li").eq(n).attr('class','hover').focus();
				}
				if(n >= 0) {
					$("#autoSuggestionsList > ul > li").eq(n+1).removeClass('hover');
				}
			} 
		}
		else {
			if(event.keyCode != 13) {
				lookup($(this).attr('value'));
			}

		}
	});
	
	$('body').keydown(function(event) {
		if(event.keyCode == 13) {
			$('#form_search').submit(function() {
				if($('#autoSuggestionsList > ul > li.hover').length > 0){
					fill($('#autoSuggestionsList > ul > li.hover').eq(0).html());
					$('#autoSuggestionsList > ul > li.hover').removeClass('hover');
					return false;
				} else {
					return true;
				}
			});
		}
	});
	

	$('#search').blur(function() {
		fill();
	});
	$('.suggestionList > ul > li').live('click',function() {
		fill($(this).html());
	});
	
	/** Gestion du formulaire login AJAX */
	$('.link_login').live('click', function(){
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : fw_uri+'/'+$('html').attr('lang')+'/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 :fw_uri+'/'+$('html').attr('lang')+'/login/check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',4000);
						},
						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 : fw_uri+'/'+$('html').attr('lang')+'/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 :fw_uri+'/'+$('html').attr('lang')+'/forgotpassword/check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',4000);
						},
						complete : function() {
							refresh();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	/** Gestion du formulaire mot de passe oublié AJAX */
	$('.link_validaccount').live('click', function(){
		$('.ajax_box').remove();
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : fw_uri+'/'+$('html').attr('lang')+'/validaccount/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();

				/** Submit sur le formulaire */
				$('#form_validaccount').live('submit',function(){
					$('#validaccount_ajax_box').remove();
					showLoading();
					
					var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :fw_uri+'/'+$('html').attr('lang')+'/validaccount/check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',4000);
						},
						complete : function() {
							refresh();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	/** Gestion d'ajout au panier en AJAX */
	$('.add_basket').live('submit', function(){
		showOverlay();
		showLoading();
		
		var s = $(this).serialize();
		
		$.ajax({
			type: 'POST',
			data : s,
			url : fw_uri+'/'+$('html').attr('lang')+'/basket/addprod/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();
			},
			complete : function() {
				refresh();
			}
		});
	return false;
	});
});
/** Rafraichissement du bloc "mon panier", "mon compte"  **/
function refresh() {
	$('#basket_content').remove();
	showBasketLoading();
	$.ajax({
		type: 'POST',
		url :fw_uri+'/'+$('html').attr('lang')+'/basket/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()}

