function Bds_Suggest() {

	var iSearchParamMinLength = 2;
	var bds_pos = 0;
	var bds_lastQuery;
	var bds_listSize;
	
	function openEvent() {
		$('#bds_subNavigation .bds_subCatVisible').slideUp(200, function() {
				$('#bds_suggestions').slideDown(200);				
			}
		);	
	}
	
	function closeEvent() {
		$('#bds_suggestions').slideUp(200, function() {
				$('#bds_subNavigation .bds_subCatVisible').slideDown(200);
			}
		);
	}

	// bind events to search input field
	this.setup = function() {
		$('.bds_searchInput').bind('keyup', function(event) {
			handleKeyEvents(event);
		});
		$('.bds_searchInput').blur(function() {
			if ($('#bds_suggestions').length) {
				closeEvent();
			}
		});
		$('#f\\.search').bind("keypress", function(e) {
			if (e.keyCode == 13 && bds_pos != 0) {
				return false;
			}
			else if(e.keyCode == 13 && bds_pos == 0){
				$('#f\\.search').submit();
			}
		});
		
	}

	function getSuggestions(param) {
		$.ajax({
			type : "POST",
			url : BDS_BASE_URL,
			data : {
				cl : 'search',
				fnc : 'getSuggestions',
				sSuggestParam : param
			},
			success : function(suggestions) {
				response = $.parseJSON(suggestions);
				if ($("#bds_suggestList").length) {
					$("#bds_suggestList").remove();
				}
				printSuggestResults(response);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				console.debug('error');
			}
		});
	}

	function printSuggestResults(response) {
		if (response != '') {
			var output = '<ul id="bds_suggestList">';
			$.each(response, function(key, hit) {
				output += '<li>' + hit + '</li>';
			});
			output += '</ul>';
			$(output).appendTo('#bds_suggestions');
			openEvent();
			bds_listSize = $('#bds_suggestList li').size();
			handleMouseEvents();	
			$('#bds_suggestList').blur(function() {
				closeEvent();
			});
		} else {
			closeEvent();
		}
	}

	function handleKeyEvents(event) {
		if (event.which == '13' && bds_pos != 0) {
			// console.log("handle enter event");
			var search_text = getSelectedText(bds_pos);
			if (search_text != null) {
				triggerSubmit(search_text);
			}
		} else if (event.which == '27') {
			closeEvent();
			unmarkAll();
		} else if (event.which == '38') {
			setCurrentPosition('inc');
		} else if (event.which == '40') {
			setCurrentPosition('dec');
		} else {
			sSearchParam = $('.bds_searchInput').val();
			if (bds_lastQuery != sSearchParam
					&& sSearchParam.length > iSearchParamMinLength) {
				bds_pos = 0;
				getSuggestions(sSearchParam);
				bds_lastQuery = sSearchParam;
			} else if (sSearchParam.length == 0) {
				closeEvent();
			}

		}
	}

	function setCurrentPosition(direction) {
		currentPos = bds_pos;
		if (direction == 'inc') {
			if (currentPos > 0) {
				currentPos--;
				unmarkAll();
				markItem(currentPos);
			}
			if(currentPos == 0) {
				closeEvent();
			}
		} else {
			if (currentPos < bds_listSize) {
				currentPos = ++currentPos;
				unmarkAll();
				markItem(currentPos);
			}
		}
		bds_pos = currentPos;
	}

	function markItem(currentPos) {
		$("#bds_suggestList li:nth-child(" + currentPos + ")").addClass(
				"selected");
	}

	function unmarkAll() {
		$('#bds_suggestList li').each(function() {
			$(this).removeClass("selected");
		});
	}

	function getSelectedText(listPosition) {
		// return cleaned string
		text = $("#bds_suggestList li:nth-child(" + listPosition + ")").text();
		if (text != null) {
			text = text.replace(/<\/?[^>]+>/gi, '');
		}
		return text;
	}

	function handleMouseEvents() {
		if ($('#bds_suggestList').length) {
			$('#bds_suggestList li').mouseover(function() {
				bds_pos = 0;
				unmarkAll();
				$(this).addClass("selected");
			});
			$('#bds_suggestList li').click(function() {
				var search_text = $(this).text();
				if (search_text != null) {
					triggerSubmit(search_text);
				}
			});
		}
	}

	function triggerSubmit(search_text) {
		$('.bds_searchInput').val(search_text);
		// console.log("search text " + search_text);
		$("#f\\.search").submit();
	}

}

