/**
 * Package declaration for this file
 */
if(langeweb == undefined) var langeweb = new Object();

/**
 * This is the main class of the web site. The constructor
 * initializes it.
 */
langeweb.langewebSearch = function(i_database, i_categoryNames, i_topfive, i_promoKeywordGetter) {	
	
	$('body').append('<div id="debug"></debug>');
	
	
	// general variables
	this.topKeyword = "topkeyword";
	this.maxTopKeywords = 10;
	this.maxTopKeywordRows = 2;
	this.resultsColumnWidth = 256;
	
	this.database = i_database;
	this.categoryNames = i_categoryNames;
	
	this.databaseByCategoryArray = new Array();		
	this.databaseCategories = new Array();
	
	if(i_topfive != undefined) this.topfive = i_topfive;
	this.promoKeywordGetter = i_promoKeywordGetter;
	
	// Constant: url for tracking top5 keywords
	this.top5TrackingUrl = "";
	
	for (var i = 0; i < this.categoryNames.length; i++) {
		this.databaseCategories[i] = new Array();
	}
	
	this.buildSearchLinks();
	
	this.htmlKeywordList = document.getElementsByTagName("ul").topList;
	
	this.categoryQueryArray = new Array();
	this.tmpCategoryQuery = 0;
	
	for (var i = 0; i < this.categoryNames.length; i++) {
		this.tmpCategoryQuery = '#resultList > ul.category' + i + ' > li';
		this.categoryQueryArray[i] = dojo.query(this.tmpCategoryQuery);
	}
	
	this.resultsField = new langeweb.langewebResultField(
		this.categoryQueryArray,
		dojo.query("#resultList > ul > h2"),
		dojo.query("#resultListTopKeywords > ul > li"),
		this.categoryNames,
		this.databaseByCategoryArray,
		this.databaseCategories,
		this.top5TrackingUrl,
		this.topKeyword
	);
	this.searchField = new langeweb.langewebSearchField(
		document.getElementsByTagName("input").searchField,
		this.resultsField,
		this.topKeyword,
		this
	);
	this.resultsField.searchField = this.searchField;	
}

/**
 * Builds the list of max 10 links above the search field. This list
 * is based partly of links, which are fix in the html (coming from the cms)
 * and partly from the top5 keywords database. This function collects
 * as many links as necessary to make up 10 links in all. Also, the fix
 * links from html are connected via onClick to the onTopKeywordClicked
 * handler, as well as the dynamically created top5 links.
 */
langeweb.langewebSearch.prototype.buildKeywordLinks = function(){
	
	var listElement;
	var anchorElement;
	var textElement;
	var promoKeywords = this.promoKeywordGetter();
	
	//promo keywords
	for(var i = 0; i < promoKeywords.length; i++) {
		listElement = document.createElement("li");
		anchorElement = document.createElement("a");
		anchorElement.href = "javascript://;";
		textElement = document.createTextNode(promoKeywords[i].toString().toLowerCase());
		anchorElement.appendChild(textElement);
		listElement.appendChild(anchorElement);
		dojo.connect(anchorElement, "onclick", this, "onTopKeywordClicked");
		this.htmlKeywordList.appendChild(listElement);
	}
	
	// frequently used keywords
	if(this.topfive != undefined && this.topfive.length > 0) {
		var tempSearchIndex = 0;
		for(var j = 0; j < 10 - promoKeywords.length; j++) {
			
			//search, if the current top5Keyword is already contained in the promokeyword
			willSkipThisTop5Keyword = false;
			for(var n = 0; n < promoKeywords.length; n++) {
				if(this.topfive[tempSearchIndex].toLowerCase() == promoKeywords[n].toLowerCase()) {
					willSkipThisTop5Keyword = true;
					break;
				}
			}
			if(willSkipThisTop5Keyword) {
				j--;
				tempSearchIndex++;
				continue;
			}
			listElement = document.createElement("li");
			anchorElement = document.createElement("a");
			anchorElement.href = "javascript://;";
			textElement = document.createTextNode(this.topfive[tempSearchIndex].toString().toLowerCase());
			anchorElement.appendChild(textElement);
			listElement.appendChild(anchorElement);
			dojo.connect(anchorElement, "onclick", this, "onTopKeywordClicked");
			this.htmlKeywordList.appendChild(listElement);
			tempSearchIndex++;
		}
	}
}

/**
 * Once one the 10 keywordLinks above the search field has been clicked,
 * this function is called. It simply puts the clicked keyword into the
 * search field.
 */
langeweb.langewebSearch.prototype.onTopKeywordClicked = function(evt){
	
	this.searchField.focusClearMute = true;
	this.searchField.inputField.value = evt.target.firstChild.nodeValue;
	this.searchField.onChangeInInput({keyCode:0});
}

/**
 * Builds up the search result links below the search field. This function
 * is only called initially and builds ALL links regardless of any search.
 * By default then, all these links are set invisible.
 */
langeweb.langewebSearch.prototype.buildSearchLinks = function(){
	
	var divElement;
	var uListElement;
	var listElement;
	var categoryElement;
	var anchorTextElement;
	var anchorImageElement;
	var textElement;
	var imageElement;
	var imageDivElement;
	var clearDivElement;
	var h2Element;
	var index = 0;
	var indexTopKeywords = 0;
	var categoryIndex = 0;
	
	var tmpResultListLayerWidth = (this.categoryNames.length * this.resultsColumnWidth) + 38;
	var tmpResultListLayerTopKeywordsWidth = (this.maxTopKeywordRows * this.resultsColumnWidth) + 38;
	var tmpResultListWidth = (this.categoryNames.length * this.resultsColumnWidth) + 8;
	var tmpResultListTopKeywordsWidth = (this.maxTopKeywordRows * this.resultsColumnWidth) + 8;
	var tmpHeadlineTopResultsWidth = (this.maxTopKeywordRows * this.resultsColumnWidth) + 2;
	var tmpShowAllResultsWidth = (this.categoryNames.length * this.resultsColumnWidth) - 18;
	
	var tmpResultListLayer = document.getElementsByTagName("div").resultListLayer;
	var tmpResultListLayerTopKeywords = document.getElementsByTagName("div").resultListLayerTopKeywords;

	tmpResultListLayer.innerHTML = '<table cellpadding="0" cellspacing="0" border="0" width="' + tmpResultListLayerWidth + '" height="1"><tr><td class="shadowLayerContent"></td><td class="shadowLayerContent"></td><td class="shadowTopRight"></td></tr><tr><td class="shadowLayerContent"></td><td class="shadowLayerContent" id="resultListLayerContent" valign="top"><div id="resultList"></div><div class="clear"></div><div id="showAllResults"><a href="javascript:submitSearch(\'searchform\');" class="arrow">' + keywordSearchShowAllResults + '</a></div></td><td class="shadowRight"></td></tr><tr><td class="shadowBottomLeft"></td><td class="shadowBottom"></td><td class="shadowBottomRight"></td></tr></table><div id="closeButton"><a href="javascript:closeSearchLayer();"><img src="' + imgBlindGif + '" width="18" height="18" alt="" border="0" /></a></div>';
	
	tmpResultListLayerTopKeywords.innerHTML = '<table cellpadding="0" cellspacing="0" border="0" width="' + tmpResultListLayerTopKeywordsWidth + '" height="1"><tr><td class="shadowLayerContent"></td><td class="shadowLayerContent"></td><td class="shadowTopRight"></td></tr><tr><td class="shadowLayerContent"></td><td class="shadowLayerContent" id="resultListLayerContent" valign="top"><div id="headlineTopResults">' + keywordSearchHeadlineTopResults + '</div><div id="resultListTopKeywords"></div><div class="clear"></div></td><td class="shadowRight"></td></tr><tr><td class="shadowBottomLeft"></td><td class="shadowBottom"></td><td class="shadowBottomRight"></td></tr></table><div id="closeButton"><a href="javascript:closeSearchLayerTopKeywords();"><img src="' + imgBlindGif + '" width="18" height="18" alt="" border="0" /></a></div>';
	
	var tmpResultList = document.getElementsByTagName("div").resultList;
	var tmpResultListTopKeywords = document.getElementsByTagName("div").resultListTopKeywords;
	
	var tmpHeadlineTopResults = document.getElementsByTagName("div").headlineTopResults;
	var tmpShowAllResults = document.getElementsByTagName("div").showAllResults;	
	
	tmpResultList.style.width = tmpResultListWidth + 'px';
	tmpResultListTopKeywords.style.width = tmpResultListTopKeywordsWidth + 'px';
	tmpHeadlineTopResults.style.width = tmpHeadlineTopResultsWidth + 'px';
	tmpShowAllResults.style.width = tmpShowAllResultsWidth + 'px';	
	
	//Result List
	for(var j = 0; j < this.categoryNames.length; j++) {
	
		uListElement = document.createElement("ul");
		dojo.addClass(uListElement, "category"+j);	
		
		h2Element = document.createElement("h2");
		dojo.addClass(h2Element, "searchCategory");
		categoryElement = document.createTextNode(langeweb.categoryNames[j]);
		h2Element.appendChild(categoryElement);
		uListElement.appendChild(h2Element);	
	
		for(var i = 0; i < this.database.length; i++) {
				
				if ((this.database[i].nav == j) && (this.database[i].keywords != this.topKeyword)) {		
				
					this.databaseByCategoryArray[index] = langeweb.database[i];
					this.databaseCategories[j][categoryIndex] = langeweb.database[i];
					
					listElement = document.createElement("li");
					listElement.id = 'searchLI-'+i; 
					
					imageDivElement = document.createElement("div");
					dojo.addClass(imageDivElement, "image");
					anchorImageElement = document.createElement("a");
					if (langeweb.database[i].ml == false){
						anchorImageElement.href = langeweb.database[i].purl;
					} else {
						anchorImageElement.href = "javascript:void(openApplication('" + langeweb.database[i].purl + "',0,0,'" + langeweb.database[i].appurl + "'))";
					}	
					
					
					imageElement = document.createElement("img");
					//imageElement.src = langeweb.database[i].iurl;
					imageElement.border = 0;
					imageElement.id = 'searchIMG-'+i;
					anchorImageElement.appendChild(imageElement);
					
					imageDivElement.appendChild(anchorImageElement);
					listElement.appendChild(imageDivElement);
					
					anchorTextElement = document.createElement("a");
					dojo.addClass(anchorTextElement, "arrow");
					if (langeweb.database[i].ml == false){
						anchorTextElement.href = langeweb.database[i].purl;
					} else {
						anchorTextElement.href = "javascript:void(openApplication('" + langeweb.database[i].purl + "',0,0,'" + langeweb.database[i].appurl + "'))";
					}
					textElement = document.createTextNode(langeweb.database[i].lt);
					anchorTextElement.appendChild(textElement);
					listElement.appendChild(anchorTextElement);
					
					clearDivElement = document.createElement("div");
					dojo.addClass(clearDivElement, "clear");
					listElement.appendChild(clearDivElement);
					
					
					//dojo.connect(anchorTextElement, "onclick", this, "onSearchLinkClicked");
					uListElement.appendChild(listElement);
					
					index++;	
					categoryIndex++;				
				}	
		}
		
		categoryIndex = 0;
		tmpResultList.appendChild(uListElement);
	}	
	
	//Result List Top Keywords
	for(var i = 0; i < this.database.length; i++) {
			
			if ((this.database[i].keywords == this.topKeyword) && (indexTopKeywords < this.maxTopKeywords)) {	
			
				if ((indexTopKeywords == 0) || (indexTopKeywords == 5)){
					uListElement = document.createElement("ul");	
				}				
			
				this.databaseByCategoryArray[index] = langeweb.database[i];
				
				listElement = document.createElement("li");
				
				imageDivElement = document.createElement("div");
				dojo.addClass(imageDivElement, "image");
				anchorImageElement = document.createElement("a");
				if (langeweb.database[i].ml == false){
					anchorImageElement.href = langeweb.database[i].purl;
				} else {
					anchorImageElement.href = "javascript:void(openApplication('" + langeweb.database[i].purl + "',0,0,'" + langeweb.database[i].appurl + "'))";
				}
				imageElement = document.createElement("img");
				imageElement.src = langeweb.database[i].iurl;
				imageElement.border = 0;
				anchorImageElement.appendChild(imageElement);
				imageDivElement.appendChild(anchorImageElement);
				listElement.appendChild(imageDivElement);
				
				anchorTextElement = document.createElement("a");
				dojo.addClass(anchorTextElement, "arrow");
				if (langeweb.database[i].ml == false){
					anchorTextElement.href = langeweb.database[i].purl;
				} else {
					anchorTextElement.href = "javascript:void(openApplication('" + langeweb.database[i].purl + "',0,0,'" + langeweb.database[i].appurl + "'))";
					
				}
				textElement = document.createTextNode(langeweb.database[i].lt);
				anchorTextElement.appendChild(textElement);
				listElement.appendChild(anchorTextElement);
				
				clearDivElement = document.createElement("div");
				dojo.addClass(clearDivElement, "clear");
				listElement.appendChild(clearDivElement);
				
				
				//dojo.connect(anchorTextElement, "onclick", this, "onSearchLinkClicked");
				uListElement.appendChild(listElement);
				
				index++;	
				indexTopKeywords++;		
				
				if ((indexTopKeywords == 4)){			
					tmpResultListTopKeywords.appendChild(uListElement);
				}		
			}				
	}

	if ((indexTopKeywords > 0)){
		tmpResultListTopKeywords.appendChild(uListElement)
	}
}

/**
 * Once a link within the list search result links is clicked, this function
 * is called just before the actual url is opened. It tracks the keywords,
 * that stand in the search field and that have lead to this searchresult link
 * being displayed.
 */
langeweb.langewebSearch.prototype.onSearchLinkClicked = function(evt){
	var trackKeywords = this.searchField.inputField.value;
	//evt.target.href += "?q=" + trackKeywords;
	//trackKeywords = trackKeywords.split(" ").join(",");
	// var websiteLanguage defined in concrete page
	
	// no tracking for lange
	//dojo.xhrPost({url: this.top5TrackingUrl, content: {lang: websiteLanguage, keywords: trackKeywords}, sync: true});
}

/**
 * This class represents the search Field with its behaviour
 * @param {Object} i_inputField The html dome node for the search field
 * @param {langewebResultField} i_resultField The instance reference of langewebResultField
 */
langeweb.langewebSearchField = function(i_inputField, i_resultField, i_topKeyword, i_search) {
	
	this.inputField = i_inputField;
	this.focusClearMute = false;
	this.timeoutID = null;
	this.resultField = i_resultField;
	this.topKeyword = i_topKeyword;
	this.searchMain = i_search;
	this.completionText = "";
	
	this.inputField.value = this.startText;
	this.currentInput = this.startText;
	
	dojo.connect(this.inputField, "onkeyup", this, "onChangeInInput");
	dojo.connect(this.inputField, "onfocus", this, "onClick");
	//this.inputField.focus();
}

// Constant: text, that is in the search field initially.
langeweb.langewebSearchField.prototype.startText = searchText; // var searchText defined in conrete page

langeweb.langewebSearchField.prototype.onClick = function(evt) {
	
	//if(!this.focusClearMute) {
	if(this.inputField.value == this.startText){
		this.inputField.value = "";
		this.currentInput = "";
		this.focusClearMute = true;
		this.currentInput = this.topKeyword;
	}
	// remove start text always
	/*
	if(this.inputField.value == this.startText){
		this.inputField.value = "";
	}
	*/
	
	var refi = this;
	this.timeoutID = window.setTimeout(function() {refi.onInputChangeDelay();}, 100);
}

/**
 * Event handler. Fires, whenever the user is typing in the
 * search field. Calls onInputChangeDelay after a small delay.
 */
langeweb.langewebSearchField.prototype.onChangeInInput = function(evt) {
	
	if(this.inputField.value.length == 0) {
		this.currentInput = this.topKeyword;
		this.resultField.setKeyword(this.topKeyword);
		document.getElementById('resultListLayerTopKeywords').style.display = 'block';
		document.getElementById('resultListLayer').style.display = 'none';
	} else {		
		if(this.inputField.value != this.currentInput && (evt.keyCode == 13 || evt.keyCode == 32)) {
		
			this.inputField.value = this.currentInput + this.completionText + (evt.keyCode==32? " ":"");
			if(this.timeoutID != null) window.clearTimeout(this.timeoutID);
			var refi = this;
			this.timeoutID = window.setTimeout(function() {refi.onInputChangeDelay();}, 100);
			
		}else if(this.inputField.value != this.currentInput && evt.keyCode != 13) {
			this.currentInput = this.inputField.value;
			if(this.timeoutID != null) window.clearTimeout(this.timeoutID);
			var refi = this;
			this.timeoutID = window.setTimeout(function() {refi.onInputChangeDelay();}, 100);
			
		}else if(evt.keyCode == 8 && this.completionText.length > 0) {
			
			this.inputField.value = this.inputField.value.substring( 0, this.inputField.value.length - 1 );
			
			this.currentInput = this.inputField.value;
			if(this.timeoutID != null) window.clearTimeout(this.timeoutID);
			var refi = this;
			this.timeoutID = window.setTimeout(function() {refi.onInputChangeDelay();}, 100);
			
		}else if(evt.keyCode == 13 && this.inputField.value.length > 0) {
			
			if(this.resultField.filteredResults.length > 0) {
				this.searchMain.onSearchLinkClicked({target:this.resultField.filteredResults[0].firstChild});
				self.location.href = this.resultField.filteredResults[0].firstChild.href;
			}else {
				self.location.href = langeweb.pageCreatorUrl + "?q=" + this.inputField.value;
			}
			return;
		} 
	}
	
}

/**
 * Handles a change in the input field after a small delay
 */
langeweb.langewebSearchField.prototype.onInputChangeDelay = function() {

	if (this.currentInput == this.topKeyword) {
		this.resultField.setKeyword(this.topKeyword);
		//var tmpCurrentKeywords = this.resultField.currentKeyword.split(" ");
		//this.setCompletion(this.resultField.currentBestCompletion.substr(tmpCurrentKeywords[tmpCurrentKeywords.length-1].length));
	} else {
		if (this.resultField != undefined) {
			
			this.resultField.setKeyword(this.inputField.value);
			//var tmpCurrentKeywords = this.resultField.currentKeyword.split(" ");
			//this.setCompletion(this.resultField.currentBestCompletion.substr(tmpCurrentKeywords[tmpCurrentKeywords.length-1].length));
		}
	}	
}

/**
 * Upon the characters, that the user has currently typed in the searchfield,
 * the autocompletion for keywords is triggered here. This is done simply by
 * writing the rest of the characters for completed keyword and text-selecting
 * those, so that they appear marked.
 */
langeweb.langewebSearchField.prototype.setCompletion = function(i_completion) {
	
	//if(i_completion != "") {
		this.inputField.value += i_completion;
		this.completionText = i_completion;
		
		if( this.inputField.createTextRange ) {
			
			hRange = this.inputField.createTextRange();
			if(i_completion.toString().length > 0) {
				hRange.findText( i_completion.toString() );
				hRange.select();
			}else {
				hRange.collapse( false );
				hRange.select();
			}
		}else {
			
			this.inputField.setSelectionRange( (this.inputField.value.length - i_completion.length), this.inputField.value.length );
		}
	//}
}

/**
 * This class represents the resulting List of
 * pages, that are shown, depending on the users search.
 * @param {Object} i_resultList A dojo DOM query result, that represents the list elements for the result links
 * @param {Array} i_database An array, that contains the keyword->page mapping. The setup of this array is
 * like this:
 * var database = new Array();
 * database[0] = {label:<label-string>,url:<url-string>,hidden:true|false,keywords:<array-of-keywords>};
 */
langeweb.langewebResultField = function(i_categoryQueryArray, i_categoryList, i_resultListTopKeywords, i_categoryNames, i_database, i_databaseCategories, i_top5TrackingUrl, i_topKeyword) {
	
	this.database = i_database;
	this.databaseCategories = i_databaseCategories;
	this.categoryNames = i_categoryNames;
	this.categoryQueryArray = i_categoryQueryArray;
	this.categoryList = i_categoryList;
	this.resultListTopKeywords = i_resultListTopKeywords;
	this.top5TrackingUrl = i_top5TrackingUrl;
	this.topKeyword = i_topKeyword;
	this.searchField; // Will be set after constructor call from langewebSearch
	this.filteredResults = new Array();
	this.currentKeyword = "";
	this.currentBestCompletion = "";
	this.databaseCategoriesIndex = 0;
	this.keywordInList = false;
}

/**
 * Sets one or multiple new keyword(s). Sets a links, that are mapped to this keywords to being displayed.
 * @param {String} the new keyword or keywords. Separated with spaces.
 */
langeweb.langewebResultField.prototype.setKeyword = function(i_keyword) {
	
	this.currentKeyword = i_keyword.toLowerCase();
	
	for (var i = 0; i < this.categoryNames.length; i++) {
		dojo.forEach(this.categoryQueryArray[i], this.hideElementInList, this);
	}
	
	this.resultListTopKeywords.removeClass("selected");
	this.categoryList.removeClass("visible");
	this.categoryList.removeClass("hidden");
	this.keywordInList = false;
	this.currentBestCompletion = "";
	
	
	
	if (this.currentKeyword == this.topKeyword) {
		this.databaseCategoriesIndex = 0;
		this.filteredResults[0] = dojo.filter(this.resultListTopKeywords, this.isRelevant, this);
		dojo.forEach(this.resultListTopKeywords, this.showKeywordsInList, this);
		dojo.forEach(this.categoryList, this.hideCategoryInList, this);
		document.getElementById('resultListLayerTopKeywords').style.display = 'block';
		document.getElementById('resultListLayer').style.display = 'none';	
	} else { 		
		for (var i = 0; i < this.categoryNames.length; i++) {
			this.databaseCategoriesIndex = i;
			this.filteredResults[i] = dojo.filter(this.categoryQueryArray[i], this.isRelevant, this);
			dojo.forEach(this.filteredResults[i], this.showElementInList, this);
			if (this.filteredResults[i].length != 0){
				this.keywordInList = true;
			}
		}
		dojo.forEach(this.categoryList, this.showCategoryInList, this);
		document.getElementById('resultListLayerTopKeywords').style.display = 'none';
		if (this.keywordInList == true){
			document.getElementById('resultListLayer').style.display = 'block';
		} else {
			document.getElementById('resultListLayer').style.display = 'none';
		}
		
		
	}
	
	
}

/**
 * dojo.filter compatible function to tell if a certain link matches the
 * currently given keywords.
 * @param {Object} elmt the current element in the list of the to be filtered elements.
 * @param {int} index the index of the current element within the list
 * @param {Array} the array itself
 */
langeweb.langewebResultField.prototype.isRelevant = function(elmt, index, array) {
	
	var itemsKeywords;
	var currentKeywords = this.currentKeyword.split(" ");
	var tmpKeyword;
	var result = true;
	var tmpResult = false;
	var tmpCurrentBestCompletion = "";
	
	if (this.currentKeyword == this.topKeyword) {
		itemsKeywords = this.database[index].keywords;
	} else {	
		itemsKeywords = this.databaseCategories[this.databaseCategoriesIndex][index].keywords;
	}	
	
	/*
	$('#debug').append('Comparing "'+this.currentKeyword+'" to ');
	for (var i = itemsKeywords.length-1; i >= 0; i--) {
		$('#debug').append(' "'+itemsKeywords[i]+'"');
	}
	
	var table = '<table width="500" border="1"><tr><td>tmpKeyword</td><td>currentKeyword</td><td>length</td><td>equals</td><td>result</td></tr>';
	*/
	for (var i = itemsKeywords.length-1; i >= 0; i--) {
		tmpKeyword = itemsKeywords[i].toLowerCase();
		
		for (var j = currentKeywords.length-1; j >= 0; j--) {
			if(currentKeywords[j].length < 1) continue;
			                           
			if (tmpKeyword.length >= currentKeywords[j].length &&
			tmpKeyword.substr(0, currentKeywords[j].length) == currentKeywords[j].toLowerCase()) {
				tmpResult = true;
				if(j == currentKeywords.length-1 && (tmpCurrentBestCompletion == "" || tmpCurrentBestCompletion.length > tmpKeyword.length)) {
					tmpCurrentBestCompletion = tmpKeyword;
				}
			}
			
			//table += '<tr><td>'+ tmpKeyword + '</td><td>' + currentKeywords[j] + '</td><td>' + (tmpKeyword.length >= currentKeywords[j].length) + '</td><td>' + tmpKeyword.substr(0, currentKeywords[j].length) +': '+ (tmpKeyword.substr(0, currentKeywords[j].length) == currentKeywords[j].toLowerCase())+ '</td><td>' +tmpResult+'</tr>\n';
			
		}
		//result = result && tmpResult; 
		result = tmpResult;
		tmpResult = false;
		if(result) {
			break;
		}
	}
	
	if ((this.currentBestCompletion == "" || this.currentBestCompletion.length > tmpCurrentBestCompletion.length) && tmpCurrentBestCompletion.length > 0) {
		this.currentBestCompletion = tmpCurrentBestCompletion;
	}
	//$('#debug').append(table+'</table>Result = '+result+'<br /><br />');
	return result;
}

/**
 * dojo.forEach compatible function, that set certain links to the style class "selected".
 */
langeweb.langewebResultField.prototype.showElementInList = function(elmt, indx, arr) {
	if (indx < 5){
		var id = elmt.getAttribute('id');
		var index = id.substr(9); // remove 'searchLI-'
		var imgElement = dojo.byId('searchIMG-'+index);
		if(!imgElement.src){
			imgElement.src = langeweb.database[parseInt(index)].iurl;
		}
		if(!elmt.hideLink) dojo.addClass(elmt, "selected");
	}	
}

langeweb.langewebResultField.prototype.showKeywordsInList = function(elmt, indx, arr) {
	
	if(!elmt.hideLink) dojo.addClass(elmt, "selected");
}

langeweb.langewebResultField.prototype.hideElementInList = function(elmt, indx, arr) {

		if(!elmt.hideLink) dojo.removeClass(elmt, "selected");
}

/**
 * function, that shows or hide category headlines.
 */
langeweb.langewebResultField.prototype.showCategoryInList = function(elmt, indx, arr) {
	
	dojo.addClass(elmt, "visible");
}

langeweb.langewebResultField.prototype.hideCategoryInList = function(elmt, indx, arr) {
	
	dojo.addClass(elmt, "hidden");
}

// Start everything
dojo.addOnLoad(function(){ langeweb.langeSearch = new langeweb.langewebSearch(langeweb.database, langeweb.categoryNames, langeweb.top5, langeweb.buildPromoKeywordList); });