var SearchBoxClear = false;
var SearchSuggestion = null;
var LastSearch = "";
var SuggestionSelected = null;

var SetSuggestionSelected = function(el){
	if(!$chk(el)){
		ClearSuggestionSelected(el);
		return ;
	}
	if($chk(SuggestionSelected))
		ClearSuggestionSelected(SuggestionSelected);
	el.setStyles({'border':'1px solid #039', 'background':'#eef', 'color':'#000'});
	SuggestionSelected=el;
};
var ClearSuggestionSelected = function(el){
	if($chk(el))
		el.setStyles({'border':'1px solid #fff', 'background':'transparent', 'color':'#666'});
	if($chk(SuggestionSelected))
		SuggestionSelected.setStyles({'border':'1px solid #fff', 'background':'transparent', 'color':'#666'});
	SuggestionSelected=null;
};

window.addEvent('domready', function(e){
	$('InputSearchField').addEvents({
		'focus': function(ev){
			if(SearchBoxClear) return ;
			this.set('value','');
			SearchBoxClear = true;
		}, 'keyup': function(ev){

			if($('DivHeaderSearchSuggestion').getStyle('display')=='block'){

				if(ev.key=='up'){

					if($chk(SuggestionSelected)){
						var PreviousEntry = SuggestionSelected.getPrevious('.DivHSSEntry');
						if($chk(PreviousEntry))
							SetSuggestionSelected(PreviousEntry);
						else
							SetSuggestionSelected($('DivHeaderSearchSuggestion').getLast());
					} else 
						SetSuggestionSelected($('DivHeaderSearchSuggestion').getLast());

				} else if(ev.key=='down'){

					if($chk(SuggestionSelected)){
						var NextEntry = SuggestionSelected.getNext('.DivHSSEntry');
						if($chk(NextEntry))
							SetSuggestionSelected(NextEntry);
						else
							SetSuggestionSelected($('DivHeaderSearchSuggestion').getFirst());
					} else 
						SetSuggestionSelected($('DivHeaderSearchSuggestion').getFirst());

				} else if(ev.key=='enter'){
					if($chk(SuggestionSelected))
						$('InputSearchField').set('value', SuggestionSelected.get('text'));
					ClearSuggestionSelected(null);
					$('DivHeaderSearchSuggestion').setStyle('display', 'none');
					return ;
				}

			}

			var SearchValue = this.get('value');
			if(SearchValue.length<=1){
				$('DivHeaderSearchSuggestion').setStyle('display', 'none');
				return ;
			}
			if(SearchValue==LastSearch){
				return ;
			}

			SuggestionSelected = null;
			LastSearch = SearchValue;

			if($chk(SearchSuggestion))
				SearchSuggestion.cancel();

			SearchSuggestion = new Request.JSON({
				method: 'get',
				url: '/ajax_search_suggestion.php?loja='+escape(SearchValue),
				encoding: 'ISO-8859-1',
				onSuccess: function(json){

					SearchSuggestion = null;

					var Container = $('DivHeaderSearchSuggestion');

					Container.empty().setStyle('display', 'block');

					if((!$defined(json.items))||($type(json.items)!="array")||(json.items.length<=0)){
						Container.set('html', '&nbsp; &nbsp; <b style="color:#b00;">Sem sugestões</b> &nbsp; &nbsp;');
						return ;
					}

					json.items.each(function(el){
						new Element('div', {'class':'DivHSSEntry', 'html':el.label, 'events': {
							'mouseenter': function(){SetSuggestionSelected(this);},
							'mouseleave': function(){ClearSuggestionSelected(this);},
							'click': function(){
								$('InputSearchField').set('value', this.get('text'));
								$('DivHeaderSearchSuggestion').setStyle('display', 'none');
							}
						}}).inject(Container);
					});

				}
			}).send();
		}, 'keydown': function(ev){
			if((ev.key=='enter')&&($('DivHeaderSearchSuggestion').getStyle('display')=='block'))
				StopBubblingEvent(ev);
		}
	});
});

