(function() {

var hwCache = {};
var hwReq = null;
function showHeadword(anchor, id) {
	var b = getBox();
	if (hwCache[id]) {
		b.style.display = "block";
		displayResult(hwCache[id], anchor);
		centerOverAnchor(anchor);
		return;
	}

	loading.src = "/img/loading.gif";
	loading.style.display = "block";
	b.style.display = "block";
	centerOverAnchor(anchor);

	hwReq = $A.ajax({
		url: "/dict/cenyani/"+id+"?ajax",
		method: "get",
		oncomplete: function(req) {
			hwReq = null;
			var json, error = req.status != 200;

			try {
				json  = JSON.parse(req.responseText);
				error = error || !json.result[0];
			} catch(e) { error = true; }

			if (!error) {
				loading.style.display = "none";
				hwCache[id] = json.result[0];
				displayResult(json.result[0], anchor);
				centerOverAnchor(anchor);
			} else {
				loading.src = "/img/loading_error.png";
			}
		}
	});
}

function displayResult(hw, anchor) {
	var fragment = document.createDocumentFragment();

	var hwname = $A.create("p", {className: "hwname"});
	hwname.appendChild($A.text(hw.name));
	if (hw.obsolete)
		hwname.appendChild($A.create("sup", {className: "obsolete"}, "\u2020"));
	fragment.appendChild(hwname);

	var hasAuxinfo = false;
	for (var i = 0; i < hw.entries.length; i++) {
		var entry = hw.entries[i];
		var elem  = $A.create("div", {className: "entry"});
		elem.innerHTML = entry.definition;

		var pos = $A.create("span", {className: "pos"});
		pos.appendChild($A.text(entry.part_of_speech.abbr+(entry.wordclass.abbr && ", "+entry.wordclass.abbr || "")));
		elem.insertBefore(pos, elem.firstChild);

		fragment.appendChild(elem);
		hasAuxinfo = hasAuxinfo || !!entry.auxinfo;
	}
	if (hasAuxinfo) {
		var elem = $A.create("p", {className: "auxinfo"});
		elem.appendChild($A.text(ST.auxinfo));
		fragment.appendChild(elem);
	}

	content.appendChild(fragment);
}

function centerOverAnchor(anchor) {
	var pos = $A.getElemPos(anchor);

	box.style.left = Math.ensureInRange(0, $A.getWindowWidth()-box.offsetWidth,
		pos[0]+(anchor.offsetWidth - box.offsetWidth)/2)+"px";

	var anchorY = pos[1] - box.offsetHeight - 1;
	if (anchorY < $A.getYScroll())
		anchorY = pos[1] + anchor.offsetHeight + 1;
	box.style.top = anchorY+"px";
}

function hideHeadword() {
	if (box) {
		box.style.display = "none";
		content.innerHTML = "";
	}
}

var box = null, loading = null,
	content = null;
function getBox() {
	if (!box) {
		box = $A.create("div", {id: "cendfn"});

		loading = $A.create("img", {className: "loading", src: "/img/loading.gif"});
		box.appendChild(loading);

		content = $A.create("div", {className: "content"});
		box.appendChild(content);

		$A.body.appendChild(box);
	}
	return box;
}

$A.onload(function() {
	if ($A.getCurrentMedia() !== "screen") return;

	var anchors = document.getElementsByTagName("a");
	var len = anchors.length;
	for (var i = 0; i < len; i++) {
		var a = anchors[i];
		if (/\bcendict\b/.test(a.className))
			initAnchor(a);
	}

	var timeoutId = null;
	function initAnchor(a) {
		var id = a.href.match(/d(?:ict)?\/cen(?:yani)?\/(\d+)/)[1];
		a.onmouseover = function() {
			window.clearTimeout(timeoutId);
			if (hwReq) hwReq.abort();
			timeoutId = window.setTimeout(function() {
				showHeadword(a, id);
			}, 500);
		};
		a.onmouseout = function() {
			window.clearTimeout(timeoutId);
			if (hwReq) hwReq.abort();
			hideHeadword();
		};
	}
});

})();

if (!window.JSON)
	JSON = {parse: function(t) { return eval("("+t+")"); }};
