function FFSuggest() {
	// RGB values because of jQuery css color comparing
	var gStandardBgColor;
	var gStandardTextColor;
	var gHighlightBgColor;
	var gHighlightTextColor;
	var cInstanceName			= "gSuggestObj";
	
	var gSearchUrl				= "";
	var gLastQuery;
	var gCurrentSelection		= -1;
	var g$HeaderSuggestLayer 	= $('#headerSuggestLayer');
	var gLayerName				= g$HeaderSuggestLayer.attr('id');
	var g$SuggestElements		= new Object();
	var g$CurrentElement 		= new Object();	
	// header search input
	var g$SearchQueryINPUT 		= $('#searchQueryInput');
	// header search input event handler
	g$SearchQueryINPUT.keyup(handleKeyPress);
	g$SearchQueryINPUT.focusin(handleFocusIn);
	g$SearchQueryINPUT.focusout(handleFocusOut);
	g$SearchQueryINPUT.click(handleFirstClick);
	
	this.init = function() {
		setRGBColorValues(); // depending on current browser version
		if(window.location.protocol == "https:") {
			gSearchUrl = tHttpsSuggestPipelineUrl;
		} else {
			gSearchUrl = tHttpSuggestPipelineUrl;
		}
	}
	
	this.handleClick = function(pLayerNumber) {
		g$SearchQueryINPUT.val(g$CurrentElement.text());
		// call function from basic.js
		headerSearchCheckInput();
	}
	
	this.handleMouseOver = function(pLayerNumber) {
		unmarkAll();
		gCurrentSelection = pLayerNumber;
		g$CurrentElement = $("#" + gLayerName + "_" + pLayerNumber);
		highlightCurrentElement();
	}
	
	function setRGBColorValues () {
		gStandardBgColor 	= "rgb(255, 255, 255)"; // hex: #ffffff;
		gStandardTextColor 	= "rgb(0, 0, 0)"; 	    // hex: #000000
		gHighlightBgColor 	= "rgb(224, 244, 244)"; // hex: #e0e0e0
		gHighlightTextColor = "rgb(112, 112, 112)"; // hex: #707070
		
		var lAgent = navigator.userAgent;
		var lVersionOffset;
		if ((lVersionOffset = lAgent.indexOf("MSIE"))!= -1) {
			var lFullVersion = lAgent.substring(lVersionOffset + 5);
			// trim the fullVersion string at semicolon/space if present
			if ((lVersionOffset = lFullVersion.indexOf(";"))!= -1) lFullVersion = lFullVersion.substring(0, lVersionOffset);
			if ((lVersionOffset = lFullVersion.indexOf(" "))!= -1) lFullVersion = lFullVersion.substring(0, lVersionOffset);
			// for all IE browser versions under 9.0
			if (lFullVersion < 9) {
				gStandardBgColor 	= "rgb(255,255,255)"; // hex: #ffffff;
				gStandardTextColor 	= "rgb(0,0,0)"; 	  // hex: #000000
				gHighlightBgColor 	= "rgb(224,244,244)"; // hex: #e0e0e0
				gHighlightTextColor = "rgb(112,112,112)"; // hex: #707070
			}
		}
	}

//	this.handleMouseOut = function(pLayerNumber) {}
	
	function handleKeyPress(pEvt){
		pEvt = (pEvt) ? pEvt : ((event) ? event : null);
		var lKeyCode = pEvt.keyCode;
		
		if (lKeyCode == 38) {
			moveSelection("up");
		} else if(lKeyCode == 40) {
			moveSelection("down");
		} else if(lKeyCode == 13) { // key code for 'enter'
			if(g$CurrentElement.length > 0) {
				g$SearchQueryINPUT.val(g$CurrentElement.text());
			}
			headerSearchCheckInput();
		} else {
			if(g$SearchQueryINPUT.val() == "") {
				hideLayer();
				if (g$HeaderSuggestLayer.length > 0){
					g$HeaderSuggestLayer.html("");
				}
				if(g$SuggestElements.length > 0){
					g$SuggestElements = new Object();
				}
				return null;
			}
			if (gLastQuery != g$SearchQueryINPUT.val()) {
				startAjax();
			}
			gLastQuery = g$SearchQueryINPUT.val();
		}
	}
	
	function handleFocusIn(event){
		if(g$SuggestElements.length > 0){
			showLayer();
		} else {
			hideLayer();
		}
	}
	
	function handleFocusOut(event){
		hideLayer();
	}
	
	function handleFirstClick(event){
		handleFocusIn();
	}
	
	function moveSelection(pDirection) {
		if (pDirection == "up")	{
			if(gCurrentSelection == -1) {// if no highlighting yet, set last suggest element
				gCurrentSelection = g$SuggestElements.length - 1;
			} else {
				gCurrentSelection = g$SuggestElements.length - 1;
				try {
					g$SuggestElements.each(function(pIndex) {
						if($(this).css("color") == gHighlightTextColor) {
							var lNextPosition = pIndex - 1;
							var l$NextElement = $("#" + gLayerName + "_" + lNextPosition);
							// if previous suggest element exists
							if(l$NextElement.length > 0){
								// break
								throw(lNextPosition);
							}
						}
					});
				} catch (pNextPosition) {
					gCurrentSelection = pNextPosition;
				}
			}
		} else if (pDirection == "down") {
			if (gCurrentSelection == -1) {// if no highlighting yet, set first suggest element
				gCurrentSelection = 0;
			} else {
				gCurrentSelection = 0;
				try {
					g$SuggestElements.each(function(pIndex) {
						if($(this).css("color") == gHighlightTextColor) {
							var lNextPosition = pIndex + 1;
							var l$NextElement = $("#" + gLayerName + "_" + lNextPosition);
							// if next suggest element exists
							if(l$NextElement.length > 0){
								// break
								throw(lNextPosition);
							}
						}
					});
				} catch (pNextPosition) {
					gCurrentSelection = pNextPosition;
				};
			}
		}
		unmarkAll();
		highlightCurrentElement();
	}
	
	function unmarkAll() {
		g$SuggestElements.each(function(pIndex) {
			unmarkSuggest($(this));
		});
	}
	
	function unmarkSuggest(p$SuggestElement) {
		p$SuggestElement.css("background-color", gStandardBgColor);
		p$SuggestElement.css("color", gStandardTextColor);
	}
	
	function highlightCurrentElement() {
		g$CurrentElement = $("#" + gLayerName + "_" + gCurrentSelection);
		g$CurrentElement.css("background-color", gHighlightBgColor);
		g$CurrentElement.css("color", gHighlightTextColor);
		g$CurrentElement.css("cursor", "pointer");
	}
	
	function hideLayer() {
		if (g$HeaderSuggestLayer.length > 0) {
			g$HeaderSuggestLayer.hide();
		}
	}
	
	function showLayer() {
		if (g$HeaderSuggestLayer.length > 0) {
			g$HeaderSuggestLayer.show();
		}
	}
	
	function startAjax() {
		var lQueryParam = replaceRegExp(g$SearchQueryINPUT.val());
		var lAjaxUrl = gSearchUrl + lQueryParam;
		
		// ajax request
	    $.ajax({
	        url: lAjaxUrl,
	        dataType: "json",
	        cache: false,
	        processData: false,
	        success: 
        		function (pAjaxJSONResponse){
    				handleAjaxResponse(pAjaxJSONResponse.suggestResults);
    			}
	    });
	}
	
	function handleAjaxResponse(pSuggestResultArray) {
		gCurrentSelection = -1; // reset suggest highlighting
		var lNewSuggest = new Array();
		for (var i in pSuggestResultArray) {
			var lFirstChar = pSuggestResultArray[i].charCodeAt(0);
			if (lFirstChar != 13 && lFirstChar != 10 && pSuggestResultArray[i].length >= 1) {
				lNewSuggest.push(pSuggestResultArray[i]);
			}
		}
		var lOutputText = '';
		for (var i in lNewSuggest) {
			lOutputText += '<div id="' + gLayerName + '_' + i + '" class="' + gLayerName + '" onMouseDown="' + cInstanceName + '.handleClick(' + i + ');" onMouseOver="' + cInstanceName + '.handleMouseOver(' + i + ');">';			
			lOutputText += lNewSuggest[i];
			lOutputText += '</div>';
		}
		if (lNewSuggest.length >= 1) {
			showLayer();
			g$HeaderSuggestLayer.html(lOutputText);
			g$SuggestElements = $('.' + gLayerName);
		} else {
			hideLayer();
			g$HeaderSuggestLayer.html("");
		}
		unmarkAll();
	}
	
	function replaceRegExp(pQuery) {
		pQuery = pQuery.replace('Ü','ue');
		pQuery = pQuery.replace('ü','ue');
		pQuery = pQuery.replace('Ä','ae');
		pQuery = pQuery.replace('ä','ae');
		pQuery = pQuery.replace('Ö','oe');
		pQuery = pQuery.replace('ö','oe');
		pQuery = pQuery.replace('ß','ss');
		pQuery = pQuery.replace(' ','');
		pQuery = pQuery.replace('`','');
		pQuery = pQuery.replace('?','');
		pQuery = pQuery.replace('+','');
		pQuery = pQuery.replace('-','');
		pQuery = pQuery.replace('%','');
		return pQuery;
	}	
}

function headerSearchCheckInput(pSrc) {
	var lSearchQueryValue = (typeof(pSrc) != "undefined") ? $('#search-no-match-input').val() : $('#searchQueryInput').val(); // decision which INPUT field is used
	lSearchQueryValue = ("2782742" == lSearchQueryValue || "278 274 2" == lSearchQueryValue) ? "278274X" : lSearchQueryValue;
	var lUrlParameters = "?query=" + escape(trim(lSearchQueryValue)) + "&host=" + window.location.host + "&From=Search" + "#lmPromo=la,1,hk,home,fl,header_search";
	var lSubmitUrl = tSearchPipelineUrl + lUrlParameters; // 'tSearchPipelineUrl' is defined inside header.isml
	document.location.href = lSubmitUrl;
}

function initGivenSearchWordHandling() {
	$('.js-site-content-search-word')
		.click(function() {
			var lUrlParameters = "?query=" + escape(trim($(this).text())) + "&host=" + window.location.host + "&From=Search" + "#lmPromo=la,1,hk,home,fl,header_search";
			var lSubmitUrl = tSearchPipelineUrl + lUrlParameters; // 'tSearchPipelineUrl' is defined inside header.isml
			document.location.href = lSubmitUrl;
		});
}
