function Search(ids) {
	this.ids = ids;
	this.forms = [];
	this.init();
	this.searching = false;
	this.scroll_size = 5;
	this.lastSearch =  null;
}
Search.prototype.search = function(form, loader) {
	if (loader) {
		loader.style.display = 'inline';
	}
	if (this.searching) { 
		return false;
	}
	var q = null;
	this.lastSearch = null;
	q = form.q.value;
	if (form.need.value !== "__empty__") {
		q = form.need.value;
		this.lastSearch = form.need.options[form.need.selectedIndex].text;
	} else {
	}
	if (q !== null) {
		// clear form
		form.need.selectedIndex = 0;
		form.q.value = "";
		this.searchRequest(q);
	}
	return false;
};
Search.prototype.searchRequest = function(q,pos) {
	if (!pos) {
		pos = 1;
	}
	json("search", {q:q}, bind(this.showResults, this, pos));
	// disable search button(s)
	this.setSearching(true);
	return false;
};
Search.prototype.showResults = function(pos, result) {
	this.setSearching(false);
	this.result = result;
	this.drawResults(pos);
	if ($('frontpage-loader-input')) {
		$('frontpage-loader-input').style.display = 'none';
	}
	if ($('frontpage-loader-select')) {
		$('frontpage-loader-select').style.display = 'none';
	}
};
Search.prototype.drawResults = function(pos) {
	var headline = H1({'id':"mainheader",'class':"fullheader"},"Søgeresultat");
	var descr;
	var results = null;
	var scr = null;
	if (this.result && this.result.resultList && this.result.resultList.length) { 
		tstat("Søgning","Søgeresultat","Velkommen,Søgning,Søgeresultat"
			,{'prop1':""+this.result.startWord.toString().toLowerCase()
			,'eVar1':""+this.result.startWord.toString().toLowerCase()
			,'events':"event1"
			,'prop2':this.result.resultList.length > 0 ? this.result.resultList.length : 'zero'});
		descr = P(null,"Søgningen på \"",EM(null,this.lastSearch ? this.lastSearch : this.result.endWord),"\" har følgende resultater:");
		var begin = 0;
		var end = this.result.resultList.length - 1;
		begin = (pos - 1) * this.scroll_size;
		end = begin + this.scroll_size;
		results = UL(null,map(this.showResult, this.result.resultList.slice(begin,end)));
		scr = this.scroll(pos);
	} else {
		tstat("Søgning","Søgeresultat","Velkommen,Søgning,Søgeresultat"
			,{'prop1':""+this.result.startWord.toString().toLowerCase()
			,'eVar1':""+this.result.startWord.toString().toLowerCase()
			,'events':"event1"
			,'prop2':'zero'});
		descr = P(null,"Din søgning gav desværre ingen resultater");
		this.result = null;
	}
	var back = DIV(
		{'id':"search_back"},
		A({
			'href'		:"page?action=frontpage",
			'onclick'	:"return thepage.frontpage();"
			},
			IMG({'src':"resources/images/arrow_left_icon.gif"}),
			"Tilbage til søgning"
		)
	);
	
	var dictionaryDefinition = '';
	//if (this.result.dictionaryList[this.result.startWord]) log("word: "+(this.result.dictionaryList[this.result.startWord]).word);
	if (pos == 1 && this.result && this.result.dictionaryList && this.result.dictionaryList.length > 0) {
		dictionaryDefinition = map(bind(this.showDictDef, this, this.result.startWord), this.result.dictionaryList);
	}
	var div = DIV({id:"search_results"}, headline, DIV({id:"page_search"}, descr, dictionaryDefinition, results, back));
	if (scr) {
		appendChildNodes(div,scr);
	}
	replaceChildNodes("mainpage", div);
	appendChildNodes(getElement("mainpage"),footer());
	thepage.refresh();
	toolbox();
	thepage.updateLocation("Søgeresultat", "search");
	return false;
};
Search.prototype.showDictDef = function(word, item) {
	if (word.toLowerCase() == item.word.toLowerCase()) {
		return P({'class':"dictionary-box"},H2({},item.word), P({'class':"dictionary-definition"},item.definition));
	}
	return null;
}
Search.prototype.showResult = function(item) {
	var a = A({href:thepage.getLink(item.pageId)},item.title);
	setNodeAttribute(a,"class","search");
	var h2 = H2(null,a);
	var p = P(item.description);
	setNodeAttribute(p,"class","search");
	appendChildNodes(h2,p);
	return LI(null,h2);
};
Search.prototype.setSearching = function(disable) {
	var inputs = getElementsByTagAndClassName("input",null,"form_quicksearch");
	var select = getElementsByTagAndClassName("select",null,"form_quicksearch");
	if (disable) {
		setNodeAttribute(inputs, "disabled", "disabled");
		setNodeAttribute(select, "disabled", "disabled");
	} else {
		removeNodeAttribute(inputs, "disabled");
		removeNodeAttribute(select, "disabled");
	}
};
Search.prototype.init = function() {
	if ($("form_frontsearch")) {
		this.forms = setNodeAttribute("form_frontsearch", "onsubmit", "return search.search(this);");
	}
	this.forms = setNodeAttribute(this.ids, "onsubmit", "return search.search(this);");
};
Search.prototype.li_img_href = function(alt,src) {
	var li = LI();
	var a = A({href:"#"},null);
	appendChildNodes(a,IMG({alt:alt,src:"resources/images/"+src},null));
	appendChildNodes(li,a);
	return li;
};
Search.prototype.scroll = function(pos) {
	thepage.updateLocation("Søgeresultat", "search", "q=" + this.result.endWord + "&pos=" + (pos == -1 ? 1 : pos));
	var div = DIV();
	if (this.result) {
		setNodeAttribute(div,"id","scroll");
		var prev = null;
		if( pos > 1) {
			prev = pos - 1;
		}
		
		if(prev) {
			var prev_img = IMG({border:"0",alt:"forbigangen",src:"resources/images/arrow_left_icon.gif",'style':"margin-bottom: 0px"},null);
			var prev_a = A({href:"#",onclick:"return search.drawResults(" + prev + ");"},null);
			appendChildNodes(prev_a,prev_img);
			appendChildNodes(div,prev_a);
		}
		prev_img = null;
		appendChildNodes(div,SPAN(null,"side:"));
		var index = 1;
		for (var i = 0; i < this.result.resultList.length; i=i+this.scroll_size) {
			appendChildNodes(div,IMG({src:"resources/images/link_bullet.gif"},null));
			if ((pos == 1 && index == 1)|| pos == index) {
				appendChildNodes(div,SPAN({'class':"selected_page"},index++));
			} else {
				appendChildNodes(div,A({href:"#",onclick:"return search.drawResults(" + index + ");"},index++));
			}
		}
		
		var next = null;
		if (pos == 1 && this.result.resultList.length > this.scroll_size) {
			next = 2; 
		}
		if( pos != 1 && pos < (this.result.resultList.length / this.scroll_size)) {
			next = pos + 1;
		}
		if(next) {
			var next_img = IMG({border:"0",alt:"næste",src:"resources/images/arrow_right_icon.gif",'style':"margin-bottom: 0px"},null);
				var next_a = A({href:"#",onclick:"return search.drawResults(" + next + ");"},null);
				appendChildNodes(next_a,next_img);
				appendChildNodes(div,next_a);
		}
	}
	return div;
};
addLoadEvent(function () {
	search = new Search(["form_quicksearch"]);
	connect("search_input", "onkeyup", bind(dict.search, dict));
	
});
