function FFSuggest(){var pDebug=false;var pInstanceName="";var pSearchURL="";var pQueryParamName="";var pFormname="";var pLayerName="";var pQueryInput;var pSuggest=new Array();var pLastQuery;var submitted=false;var pShowImages=false;var pSuggestImageClass="suggestImage";var pSuggestQueryClass="suggestTextQuery";var pSuggestTypeClass="suggestTextType";var pSuggestAmountClass="suggestTextAmount";var pSuggestQueryTypedClass="suggestTextQueryTyped";var pSuggestFooterClass="suggestFooter";var pSuggestHeaderClass="suggestHeader";var pSuggestRowClass="suggestRow";var pSuggestHighlightClass="suggestHighlight";var ptranslation;this.init=function(searchURL,formname,queryParamName,divLayername,instanceName,debugMode,channelParamName,channel,showImages){pSearchURL=searchURL;pFormname=formname;pQueryParamName=queryParamName;pChannelParamName=channelParamName;pChannel=channel;pLayerName=divLayername;pInstanceName=instanceName;pDebug=debugMode;pShowImages=showImages;if(pSearchURL==""){if(pDebug){alert("no searchurl defined")}return null}else{if(pInstanceName==""){if(pDebug){alert("no instancename defined")}return null}else{if(pFormname==""){if(pDebug){alert("no formname defined")}return null}else{if(pQueryParamName==""){if(pDebug){alert("no queryparamname defined")}return null}else{if(pLayerName==""){if(pDebug){alert("need a layer for output")}}}}}}pQueryInput=document[pFormname][pQueryParamName];if(pQueryInput.value==""){pQueryInput.value=$("#tranHeaderEnterSearchText").val()}$(pQueryInput).keyup(function(ev){handleKeyPress(ev)});$(pQueryInput).focus(function(){if(this.value==$("#tranHeaderEnterSearchText").val()){this.value=""}showLayer()});$(pQueryInput).blur(function(){if(this.value==""){this.value=$("#tranHeaderEnterSearchText").val()}hideLayer()});$(document[pFormname]).submit(function(){handleSubmit()});ptranslation=new Object();ptranslation.unspecified="Sonstiges";ptranslation.productName="Produktname";ptranslation.brand="Hersteller";ptranslation.content="Inhalt";ptranslation.searchTerm="Suchbegriff";ptranslation.category="Kategorie"};function handleSubmit(){var id=$("tr."+pSuggestHighlightClass).attr("id");if(id!=undefined){var id=$("tr."+pSuggestHighlightClass).attr("id");var query;for(var i=0;i<pSuggest.length;i++){if(pSuggest[i].search(id)==0){query=pSuggest[i].substring(id.length);break}}addInputToForm("userInput",document[pFormname][pQueryParamName].value);document[pFormname][pQueryParamName].value=query;addInputToForm("queryFromSuggest","true")}}this.handleClick=function(){$(document[pFormname]).submit()};this.handleMouseOver=function(id){unmarkAll();$("#"+pLayerName+' tr[id="'+id+'"]').removeClass(pSuggestRowClass).addClass(pSuggestHighlightClass)};this.handleMouseOut=function(id){$("#"+pLayerName+' tr[id="'+id+'"]').removeClass(pSuggestHighlightClass).addClass(pSuggestRowClass)};function handleKeyPress(evt){evt=(evt)?evt:((event)?event:null);var keyCode=evt.keyCode;if(keyCode==38){moveUp()}else{if(keyCode==27){hideLayer()}else{if(keyCode==40){moveDown()}else{if(pQueryInput.value==""){hideLayer();pLastQuery="";return null}if(pLastQuery!=pQueryInput.value){getSuggestions()}pLastQuery=pQueryInput.value}}}}function moveUp(){if($("tr."+pSuggestHighlightClass).length==0){$("tr."+pSuggestRowClass+":last").removeClass(pSuggestRowClass).addClass(pSuggestHighlightClass)}else{if($("tr."+pSuggestHighlightClass).prev("tr."+pSuggestRowClass).length==0){$("tr."+pSuggestHighlightClass).removeClass(pSuggestHighlightClass).addClass(pSuggestRowClass);$("tr."+pSuggestRowClass+":last").removeClass(pSuggestRowClass).addClass(pSuggestHighlightClass)}else{$("tr."+pSuggestHighlightClass).removeClass(pSuggestHighlightClass).addClass(pSuggestRowClass).prev("tr."+pSuggestRowClass).removeClass(pSuggestRowClass).addClass(pSuggestHighlightClass)}}}function moveDown(){if($("tr."+pSuggestHighlightClass).length==0){$("tr."+pSuggestRowClass+":first").removeClass(pSuggestRowClass).addClass(pSuggestHighlightClass)}else{if($("tr."+pSuggestHighlightClass).next("tr."+pSuggestRowClass).length==0){$("tr."+pSuggestHighlightClass).addClass(pSuggestRowClass).removeClass(pSuggestHighlightClass);$("tr."+pSuggestRowClass+":first").removeClass(pSuggestRowClass).addClass(pSuggestHighlightClass)}else{$("tr."+pSuggestHighlightClass).addClass(pSuggestRowClass).removeClass(pSuggestHighlightClass).next("tr."+pSuggestRowClass).removeClass(pSuggestRowClass).addClass(pSuggestHighlightClass)}}}function getSuggestions(){var query=$("input[name="+pQueryParamName+"]").attr("value");if(pLastQuery==query){return}else{pLastQuery=query}var requestURL=pSearchURL+"?"+pQueryParamName+"="+encodeURIComponent(query)+"&"+pChannelParamName+"="+pChannel+"&format=json";$.ajax({type:"GET",url:requestURL,contentType:"application/x-www-form-urlencoded; charset=UTF-8",success:function(json,textStatus){var jsonObj=eval(json);if(jsonObj.length>0){var outputText='<table cellpadding="0" cellspacing="0" class="'+pLayerName+'" width="100%" border="0" onMouseDown="'+pInstanceName+'.handleClick();">';outputText+='<tr class="'+pSuggestHeaderClass+'" ><td nowrap="nowrap" colspan="'+(3+pShowImages)+'">'+$("#tranSuggestionsToYourSearch").val()+"</td></tr>";pSuggest=new Array();for(var i=0;i<jsonObj.length;i++){var suggestQuery=jsonObj[i].query;var suggestCount=jsonObj[i].hitCount;if(suggestCount==0){suggestCount=""}else{if(suggestCount==1){suggestCount=$("#tranOneProduct").val()}else{suggestCount=suggestCount+" "+$("#tranProducts").val()}}var suggestType=ptranslation[jsonObj[i].type];if(!suggestType){suggestType=""}var suggestImageUrl=jsonObj[i].imageURL;var id=i;outputText+='<tr id="'+id+'" class="'+pSuggestRowClass+'" onMouseOver="'+pInstanceName+".handleMouseOver("+id+');" onMouseOut="'+pInstanceName+".handleMouseOut("+id+');">'+(pShowImages?'<td nowrap="nowrap" class="'+pSuggestImageClass+'"><img src="'+suggestImageUrl+'" alt=""/></td>':"")+'<td nowrap="nowrap" class="'+pSuggestQueryClass+'">'+suggestQuery.replace(new RegExp("("+query+")","ig"),'<span class="'+pSuggestQueryTypedClass+'">$1</span>')+'</td><td nowrap="nowrap" class="'+pSuggestAmountClass+'">'+suggestCount+"</td></tr>";pSuggest[i]=id+suggestQuery}outputText+="</table>";$("div#"+pLayerName).html(outputText).show()}else{$("div#"+pLayerName).hide()}},error:function(e,xhr,settings,exception){if(pDebug){alert("Error:\nHTTP result code: "+e.status+"\nrequested URL: "+requestURL)}}})}function hideLayer(){unmarkAll();$("div#"+pLayerName).hide();fireSuggestLayerHidden()}this.hideLayerOutsideCall=function(){hideLayer()};function showLayer(){$("div#"+pLayerName).show()}function fireSuggestCompleted(suggestLayerIsVisible){if(typeof(onSuggestCompleted)=="function"){onSuggestCompleted(suggestLayerIsVisible)}}function fireSuggestLayerHidden(){if(typeof(onSuggestLayerHidden)=="function"){onSuggestLayerHidden()}}function unmarkAll(){$("tr."+pSuggestHighlightClass).each(function(i){$(this).removeClass(pSuggestHighlightClass).addClass(pSuggestRowClass)})}function addInputToForm(name,value){var element=document.createElement("input");element.name=name;element.type="hidden";element.value=value;document[pFormname].appendChild(element)}};
