window.addEvent('domready', function() {
 	
	var input_value = $('motcle_search');
	
	input_value.addEvent('click',function() {
		var value = input_value.get('value')
		if(value == _RECHERCHE) {
			input_value.set('value','');
		};
		
	});
 
	// Our instance for the element with id "demo-word2"
	var form_activation = true;
	new Autocompleter.Request.HTML(input_value, _ROOT_URL+'routines_recherche.php', {
		'indicatorClass': 'autocompleter-loading',
		width:390,
		filterSubset:true,
		zIndex:200,
		multiple:false,
		'postData': {
			'extended': '1' // send additional POST data, check the PHP code
		},
		autoSubmit:true,
		onSelection:function(s1,s2,s3,s4) {
			window.location.href = s2.getElements('a')[0].get('href');
		},
		overflowMargin: 500,
		'injectChoice': function(choice) {
			// choice is one <li> element
			//var text = choice.getFirst();
			if(choice.hasClass('empty') == true) {
				$('mdr').getElement('.input_submit').fade('out');
				form_activation = false;
			} else {
				$('mdr').getElement('.input_submit').fade('in');
				form_activation = true;
			}
			var text = choice.getElement('span');
			// the first element in this <li> is the <span> with the text
			var value = text.innerHTML;
			//alert(value)
			
			// inputValue saves value of the element for later selection
			choice.inputValue = value;
			// overrides the html with the marked query value (wrapped in a <span>)
			text.set('html', this.markQueryValue(value));
			
			// add the mouse events to the <li> element
			this.addChoiceEvents(choice);		
		}
	});
	$('mdr').getElement('form').addEvent('submit',function(ev) {
		if(form_activation == false) {
			ev.preventDefault();	
		} else {
			/*ev.stop();
			if($('recherche_ajax')!=null) {
				window.location.href = $('recherche_ajax').get('href');
			}*/
			return true;
		}
	})
	
});
