/*
 * Mootools based suggest framework
 */

window.addEvent('domready',function(){
	// find any inputs in the document with an action property.
	if (!window.ie6) {
	$E('body').getElements('input[autocomplete=off]').each(function(item){
		var div = new Element('div',{
			'class': 'SuggestFramework_List',
			'id': item.id+'_list',
			'styles': {
				'position': 'absolute',
				'z-index': '1',
				'width': item.getStyle('width'),
				'background': '#fff',
				'display': 'none'
			}
		}).injectAfter(item);
		item.addEvent('focus',function(){
			$(item.id+'_list').setStyle('display','block');
		});
		item.addEvent('blur',function(){
			(function(){$(item.id+'_list').setStyle('display','none')}).delay(50);
		});
		item.addEvent('keyup',function(){
			// use ajax to read the information and submit to the handler, then interpret the returned JS array
			var url = item.getProperty('action')+'?type=query&q='+escape(item.value);
			suggestlist = [];
			var aj = new Ajax(url,{
				method: 'get',
				evalResponse: true,
				onComplete: function(){
					div.empty();
					var table = new Element('table', {
					'styles':{
						'width': '100%',
						'border-collapse':'collapse'
						}
					});
					suggestlist.each(function(row){
						var tr = new Element('tr');
						row.each(function(data){
							var td = new Element('td', {
								'class': 'SuggestFramework_Normal'
							}).setHTML(data).injectInside(tr);
						});
						tr.addEvent('mouseenter',function(){
							tr.getChildren().each(function(child){
								child.removeClass('SuggestFramework_Normal');
								child.addClass('SuggestFramework_Highlighted');
							});
						});
						tr.addEvent('mouseleave',function(){
							tr.getChildren().each(function(child){
								child.removeClass('SuggestFramework_Highlighted');
								child.addClass('SuggestFramework_Normal');
							});
						});
						tr.addEvent('mousedown',function(){
							item.value=tr.getFirst().getText();
						});
						tr.injectInside(table);
					});
					table.injectInside(div);
				}
			}).request();
		});
	});
	}
});

