$(document).ready(function(){
	/**
	 * Actions reatives au Menu
	 */

	// Initialisation de la ligne 1 du menu
	$('#menu1').find('ul').parent().each(function(){
		var submenu = $(this).find('ul:eq(0)');
		var alink = $(this).find('a:eq(0)');
		$(this).hover(
			function(){
				alink.addClass('menu_selected');
				submenu.show();
			},
			function(){
				submenu.hide();
				alink.removeClass('menu_selected');
			}
		);
	});

	// Initialisation de la ligne 2 du menu
	$('#menu2').find('ul').parent().each(function(){
		var submenu = $(this).find('ul:eq(0)');
		var alink = $(this).find('a:eq(0)');
		$(this).hover(
			function(){
				alink.addClass('menu_selected');
				submenu.show();
			},
			function(){
				submenu.hide();
				alink.removeClass('menu_selected');
			}
		);
	});

	/**
	 * Actions reatives au Moteur de recherche
	 */

	// Saisie d'un mot clé
	var keywords = $("#keywords").val();
	$("#keywords").click(function() {
		$("#keywords").val("");
	});
	$("#keywords").blur(function() {
		if ($("#keywords").val()=="") $("#keywords").val(keywords);
	});

	// Auto-completion
	$('#keywords').attr('autocomplete','off');
	n = -1;
	$('#keywords').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) {
			$('#search_form').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;
				}
			});
		}
	});
	

	$('#keywords').blur(function() {
		fill();
	});
	$('.suggestionList > ul > li').live('click',function() {
		fill($(this).html());
	});

	/**
	 * Actions reatives au Panier d'achat
	 */

	// Vide les champs pré-remplis
	$('#basket_user #users_email').focus(function() {
		$(this).val('');
	});
	$('#basket_user #users_password').focus(function() {
		$(this).val('');
	});
	
	
	// 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_Path+'/basket/addprod/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();
			},
			complete : function() {
				refresh();
			}
		});
		return false;
	});

	/**
	 * Actions reatives au champs Users_Precision des formulaire d'inscriptions
	 */

	// Affiche le champ précision en cas de sélection du professionnel
	if ($("#users_type option:selected").val()!='Pro') {
		$("p.p_users_pro").hide();
	}
	$("#users_type").change(function() {
		if ($(this).val()=='Pro') {
			$("p.p_users_pro").show();
		} else {
			$("p.p_users_pro").hide();
		}
	});
});

/**
 * Fonctions reatives au Moteur de recherche
 */
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_Path + '/index.php?p=search&a=autocompletion&keywords='+inputString, {}, 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) {
	$('#keywords').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
}

/**
 * Fonctions reatives aux boites AJAX
 */

// 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();
});

// Rafraichissement du bloc Mon panier
function refresh() {
	$('#basket_content').remove();
	showBasketLoading();
	$.ajax({
		type: 'POST',
		url :fw_Path+'/basket/refresh/',
		success : function(msg) {
			$('#basket').append(msg);
			hideBasketLoading();
		}
	});
}

// Fonctions d'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()}

