var SearchEngine = new Class({
	
	 initialize: function() {
	 	if (!$('main_result_list') || !$('main_search_div'))
	 		return;	
	 	this.list = $('main_result_list');
	 	this.lastValue = '';
	 	this.objVisible = false;
	 	$('main_search_div').addEvent('mouseenter',this.DoIt.bind(this));
    	$('main_result_list').addEvent('mouseleave',this.CheckListFadeOut.bind(this));
	 },

	DoIt:function() {
		var searchValue = $('input_search').value;
		try {
			var searchLength = searchValue.length;
			if(searchLength > 1) {
	
				if (!this.objVisible) {
					var posTop=GetTopPosition($('bloc_title'));
					var posLeft=GetLeftPosition($('bloc_title'));			

					this.list.style.top=(posTop+45)+'px';
					this.list.style.left=(posLeft-204)+'px';
					
					this.ResultFadeIn();
				}
				if (this.lastValue==searchValue) {
					return;
				}
				else {
					this.lastValue=searchValue;
				}
				 
				 var myAjax = new Request({
					          url:"/getSearchResult.php?item="+searchValue,
					          method: 'get',
					          encoding:'ISO-8859-1',
					          onComplete: function(responseText) { // no default update element, we do it manually
					 			
					          $('main_result_list').className='searchVisible';
					          $('search_list_result').set('html',responseText);
								var elem_rechercher		= $('search_box');
								var style_rechercher 	= elem_rechercher.style;
					          }
					      });
				 
				 
				 myAjax.send();
				 
			}
			if (searchLength <= 1) {
				if (this.objVisible)
					this.ResultFadeOut();
			}
		}
		catch(e) {
			alert('DoIt ' + e.message);
		}
	},
	
	ResultFadeIn:function() {
		try {
		var searchEffect = new Fx.Tween('main_result_list', {duration: 1000, transition: Fx.Transitions.elastic});
		
		searchEffect.start('opacity',0, 1);
		$('input_search').focus();
		this.objVisible = true;
		} catch(e){
			alert('ResultFadeIn ' + e.message);
		}	
	},
	
	ResultFadeOut:function() {
		if (this.objVisible) {
			var searchEffect = new Fx.Tween('main_result_list', {duration: 1000, transition: Fx.Transitions.elastic});
			searchEffect.start('opacity',1, 0);
			this.objVisible = false;
		}
		$('input_search').value = '';
	},
	
	CheckListFadeOut:function() {
		if (!$('input_search').focus)
			this.ResultFadeOut();
	},

	SearchBoxFadeIn:function() {
		var searchEffect = new Fx.Tween('main_search_div', {duration: 1000, transition: Fx.Transitions.elastic});
		
		searchEffect.start('opacity',0, 1);
	},

	SearchBoxFadeOut:function() {
		var searchEffect = new Fx.Tween('main_search_div', {duration: 1000, transition: Fx.Transitions.elastic});
		
		searchEffect.start('opacity',1, 0);
	}

});
