function sb_show_results(data,myid){
	jQuery(function($) {
		$('#sb_results_div').remove();
		$('<div id="sb_results_div"></div>')
			.append(data)
			.insertAfter('#'+myid)
			.css('top',$('#'+myid).css('top'))
			.css('left',$('#'+myid).css('left'))
			.children('.sb_cell')
				.click(function(){				
					$(this).attr('value',$('.sb_cell_selected:first').html());
					$('#'+myid).attr('value',$(this).html());
					$('#sb_results_div').remove();
				})
				.mouseenter(function(){
					$(this).addClass('sb_cell_selected').siblings().removeClass('sb_cell_selected');
				})
				.mouseleave(function(){
					$(this).removeClass('sb_cell_selected');
				});
		$('.sb_cell').css('width',$('#'+myid).css('width')).css('height',$('#'+myid).css('height'));
	});	
}

function sb_find(table,keyfield,searchfield,keyword,myid,event){
	jQuery(function($) {
	
	if (keyword.length < 2) {		
		$('#sb_results_div').remove();
		return false;
	}
	
	if ((event.keyCode == 9) || (event.keyCode == 13) || (event.keyCode == 27)) { // Tab // Enter // Esc
		    	$('#sb_results_div').remove();
		    	return true;
	}
	if ((event.keyCode == 40) || (event.keyCode == 38)){					
		
		if ($('.sb_cell_selected').length > 0){
		    if (event.keyCode == 38) { // Up Arrow
		        $('.sb_cell_selected').prev('.sb_cell').addClass('sb_cell_selected').siblings().removeClass('sb_cell_selected');
		    }
		    if (event.keyCode == 40) { // Down Arrow
		        $('.sb_cell_selected').next('.sb_cell').addClass('sb_cell_selected').siblings().removeClass('sb_cell_selected');
		    }		    
		} else {
			$('#sb_results_div').children(':first').addClass('sb_cell_selected');
		}
		
		$('#'+myid).attr('value',$('.sb_cell_selected:first').html())
		return true;
	}
	
		$.post(
			'/xml/searchbox.php',
			{attribute: searchfield,
			identificator: keyfield,
			table: table,
			keyword: keyword,
			limit: 5
			},
			function(data, textStatus){
				sb_show_results(data,myid);				
			}
		)
	});
}

// initialize inputs
jQuery(function($) {
	if ($('.sb_inputbox').length > 0){
		$('.sb_inputbox').click(function (){
			$('#sb_results_div').remove();
		});
	}
});
