function Dictionary(result_wrapper, query_field) {
	this.results_box = result_wrapper;
	this.query_field = query_field;
	this.last = null;
	this.page_pos = getElementPosition('page');	
	this.isIE = navigator.userAgent.toLowerCase().indexOf("msie") != -1;
}
Dictionary.prototype.search = function(e) {
	if ($(this.query_field).value.length > 0) {
		var q = $(this.query_field).value;
		json(
			"dict_lookup",
			{'query':q},
			bind("showResults", this)
		);
		this.showText('Søger...');
	}
	return false;
};
Dictionary.prototype.showResults = function(dict) {
	var results = dict.suggestions;
	this.show();
	if (isArrayLike(results)) {
		var list = UL(null, map(partial(LI, null), map(partial(A, {'href':"#"}), results)));
		for (var i = 0; i < list.childNodes.length; i++) {
			setNodeAttribute(list.childNodes[i].firstChild, "onclick", "return dict.load(this);");
		}
		replaceChildNodes($(this.results_box), list);
		this.positionResults();
	} else {
		this.showText("Ingen opslag fundet");
	}
};
Dictionary.prototype.showResult = function(dict) {
	var result = dict.result;
	this.show();
	if (!result) {
		this.showText("Der blev ikke fundet noget");
	} else {
		this.showText(result);
		$(this.query_field).focus();
	}
};
Dictionary.prototype.showText = function(text) {
	replaceChildNodes($(this.results_box), UL(null, LI({'style':"display: none;"}), LI(null, text)));
	this.positionResults();
};
Dictionary.prototype.submit = function() {
	var ul = getElementsByTagAndClassName("ul", null, this.results_box)[0];
	if (ul.firstChild.textContent) {
		$(this.query_field).value = ul.firstChild.textContent;
	}
	this.load($(this.query_field));
	return false;
};
Dictionary.prototype.positionResults = function() {
	if (!this.positioned) {
		this.positioned = true;
		var dict_pos_rel = getElementPosition($(this.query_field), 'page');
		var dict_pos = getElementPosition($(this.query_field));
		var result = new MochiKit.Style.Coordinates(dict_pos.x, this.page_pos.y + dict_pos_rel.y + 14);
		//var result = new MochiKit.Style.Coordinates(dict_pos.x, dict_pos_rel.y + 14);
		if (this.isIE) {
			// fix pos for ie (dunno why though)
			result.x -= 2;
			result.y -= 6;
		}
		setElementPosition($(this.results_box), result);
	}
	// check if hook or hook parent is missing
	if (!this.hook || !this.hook.source.parentNode) {
		if (this.hook) disconnect(this.hook);
		this.hook = connect("search", "onmouseleave", bind(this.hide, this));
	}
}
Dictionary.prototype.load = function(elem) {
	if (elem.id != this.query_field && !elem.childNodes[0].nodeValue) {
		return false;
	}
	var q = "";
	if (elem.id == this.query_field) {
		q = $(this.query_field).value;
	} else {
		q = elem.childNodes[0].nodeValue;
	}
	this.showText('Søger...');
	$(this.query_field).value = q;
//	stat("Søgning :: Ordbog :: "+q);
	//json("dict_search", {'query':q}, bind("showResult", this));
	json("search", {q:q}, bind(search.showResults, search, 1));
	return false;
};
Dictionary.prototype.hide = function() {
	$(this.results_box).style.display = 'none';
	$('need').style.display= 'block';
};
Dictionary.prototype.show = function() {
	$(this.results_box).style.display = 'block';
	$('need').style.display= 'none';
};
addLoadEvent(function () {
	dict = new Dictionary("dict_results","search_input");
});
