var htflmActiveMenu = null;

function htflmDefaultMenuList(descriptor) {
	var json = new Array();
	for(var i = 0; i < descriptor.menu.length; i++) {
		with(descriptor.menu[i]) {
			if (popup == 1) {
				json.push([ "A", { 'href': href, 'onclick': function() { openPopup(href, '_blank', 800, 689, ',scrollbars=0'); return false; } }, title ]);
			} else {
				json.push([ "A", { 'href': href }, title ]);
			}
		}
	}

	return json;
}

function htflmDefaultView(descriptor) {
	var json = htflmDefaultMenuList(descriptor);
	json.unshift("DIV", { 'style': 'position: absolute;' });
	return json;
}

function HTFloatMenu(element, descriptor) {
	if(typeof element == 'string') element = document.getElementById(element);
	if(typeof descriptor.hideTime == 'undefined' || new Number(descriptor.hideTime) < 0) descriptor.hideTime = 300;
	if(typeof descriptor.view == 'undefined') descriptor.view = htflmDefaultView;

	var hideSpin = 0;
	var _this = this;
	this.layer = null;

	function pan_over() {
		if(_this.layer != null) {
			hideSpin ++;
		}
	}

	function over() {
		if(_this.layer != null) {
			hideSpin ++;
		} else {
			if(htflmActiveMenu != null) {
				htflmActiveMenu.hide();
				htflmActiveMenu = null;
			}
			try {
				var ljs = descriptor.view(descriptor);
				_this.layer = jsonML(ljs);
				document.body.appendChild(_this.layer);

				var pos = GetBounds(element);

				with(_this.layer.style) {
					left = pos.x + 'px';
					top = (pos.y + pos.h) + 'px';
				}

				CaptureEvent(_this.layer, "mouseover", pan_over, true);
				CaptureEvent(_this.layer, "mouseout", out, true);

				htflmActiveMenu = _this;
			} catch(e) {
				console.error("Error while hover: " + e);
				console.dir(e);
			}
		}
	}

	function out() {
		var _spin = ++ hideSpin;

		window.setTimeout(function() {
			if(_spin == hideSpin) {
				_this.hide();
			}
		}, descriptor.hideTime);
	}

	CaptureEvent(element, "mouseover", over, true);
	CaptureEvent(element, "mouseout", out, true);
}

HTFloatMenu.prototype.hide = function() {
	if(this.layer != null) {
		this.layer.parentNode.removeChild(this.layer);
		this.layer = null;
	}
}

/**
 * Automatikus lebegõ menük kezelése.
 */
CaptureEvent(window, "load", function() {
	var lnks = document.getElementsByTagName("A");
	for(var i = 0; i < lnks.length; i++) {
		var lnk = lnks[i];
		var descr = lnk.getAttribute("ht:floatmenu");
		if(descr) {
			try {
				console.dir(new HTFloatMenu(lnk, eval(descr)));
			} catch(e) {
				console.warn("Error while init: " + e);
				console.dir(e);
			}
		}
	}
}, true);

