function ajax_filtre(familles_id) {
  $.ajax({
           type: "GET",
           url:   $('base').attr('href')+"/index.php?a=ajaxfiltre",
           data: "familles_id="+familles_id,
           error:function(msg){
            alert("Erreur durant la requete");
				   },      
           success:function(data){
    				   	//affiche le contenu du fichier dans le conteneur dédié
				$('#sousfamilles_id').empty();
				$('#sousfamilles_id').append(data);
				$('#first_option').remove();
              }
		});
   return false;
}
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("index.php?p=results&a=autocompletion", {queryString: ""+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) {
		$('#search_freetext').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
	}



$(document).ready(function(){
	$('#callback_societe').focus( function() { 
		$(this).attr('value','');
	});
	$('#callback_nom').focus( function() { 
		$(this).attr('value','');
	}); 
	$('#callback_tel').focus( function() { 
		$(this).attr('value','');
	});	
	$('#search_freetext').focus( function() { 
		$(this).attr('value','');
	});
	// Menu déroulant
	$('#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');
			}
		);
	});
	$('#menu_quisommesnous').click(function() { return false; });
	$('#menu_produits').click(function() { return false; });
	$('#menu_produits+ul:first>li>a').click(function() { return false; });
	$('#menu_support').click(function() { return false; });
	$('#menu_nouscontacter').click(function() { return false; });
	/*
	* Liste liée
	*/
	$('#familles_id').change(function () {
	  var familles_id = $(this).attr('value');
	  ajax_filtre(familles_id)
	});
	
	
	
	$("#add_favoris").click(function(event){
	  if(window.opera) {
        if ($(this).attr("rel") != ""){ // don't overwrite the rel attrib if already set
            $(this).attr("rel","sidebar");
        }
    }
 
 
        event.preventDefault(); // prevent the anchor tag from sending the user off to the link
        var url = 'http://www.etiqazur.com';
        var title = this.title;
       
        if (window.sidebar) { // Mozilla Firefox Bookmark
		
            window.sidebar.addPanel(title, url, "");
        } else if( window.external) { // IE Favorite
            window.external.AddFavorite( url, title);
        } else if(window.opera) { // Opera 7+
            return false; // do nothing - the rel="sidebar" should do the trick
        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
            alert('Désolé, votre navigateur ne supporte pas l\'ajout au favori automatique, ajoutez-le manuellement');
        }
		
		return false;
    });
/** Auto-completion **/
	$('#search_freetext').attr('autocomplete','off');
	n = -1;
	$('#search_freetext').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_freesearch').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_freetext').blur(function() {
		fill();
	});
	$('.suggestionList > ul > li').live('click',function() {
		fill($(this).html());
	});
});