var HTMenuHelper = {
	activeMenu: null,

	buildFloatMenuDescriptor: function(d, rootView) {
		var d1 = new Array();
		var ms = new Array();

		if(typeof view == 'undefined') view = HTDefaultFloatMenuView;

		for(var i = 0; i < d.length; i++) {
			var parent_id = Number(d[i].parent_id);

			if(typeof d1[parent_id] == 'undefined') {
				d1[parent_id] = { 'view': rootView, 'menu': [ ] };
			}

			var m = { 'href': d[i].href, 'title': d[i].title };
			if(typeof d[i].target != 'undefined') m.target = d[i].target;
			d1[parent_id].menu.push(m);
			ms[Number(d[i].id)] = { 'orig': d[i], 'menu': m };
		}

		for(i in d1) {
			if(typeof ms[i] == 'object') {
				ms[i].menu.child = d1[i];

				if(typeof ms[i].orig.dir == 'string') d1[i].dir = ms[i].orig.dir;
				if(typeof ms[i].orig.view == 'object') d1[i].view = ms[i].orig.view;
			}
		}

		console.dir(d1);

		return d1;
	}
}

function HTDefaultFloatMenuView(descriptor) {
	this.descriptor = descriptor;
}

HTDefaultFloatMenuView.prototype = {
	element: null,
	childElements: new Array(),

	show: function(x, y) {
		if(this.element == null) {
			document.body.appendChild(this.element = jsonML(this.jsonMenu()));
		}

		with(this.element.style) {
			left = x + 'px';
			top = y + 'px';
		}

		return this.element;
	},

	hide: function() {
		if(this.element == null) return;

		document.body.removeChild(this.element);
		this.element = null;
		this.childElements = new Array();
	},

	isVisible: function() {
		return this.element != null;
	},

	getMenuItem: function(idx) {
		return this.childElements[idx];
	},

	jsonMenuItemList: function() {
		var json = new Array();
		for(var i = 0; i < this.descriptor.menu.length; i++) {
			var m = this.jsonMenuItem(this.descriptor.menu[i]);
			if(typeof m[1] != 'object' || m[1] instanceof Array) {
				m.splice(1, 0, new Object());
			}
			m[1].element = this.childElements;
			m[1].elementId = i;
			if(m != null) json.push(m);
		}

		return json;
	},

	jsonMenu: function() {
		var json = this.jsonMenuItemList();
		json.unshift("div", { 'style': 'position: absolute;' });
		return json;
	},

	jsonMenuItem: function(d) {
		var jd = null;
		if(typeof d.href != 'undefined') {
			if(typeof d.child != 'undefined') {
				jd = [ "A", { 'href': d.href }, [ 'B', d.title ] ];
			} else {
				jd = [ "A", { 'href': d.href }, d.title ];
			}

			if(typeof d.target != 'undefined') jd[1].target = d.target;
		}

		return jd;
	}

/**
 * Opcionális funkció:
 *  attachToMenu: function(menu)
 *
 * Ha ez a funkció létezik, akkor a konstruktor után a létrehozó HTFloatMenu objektum azonnal meghívja, úgy, hogy a 'menu' paraméter mega a létrehozó HTFloatMenu lesz.
 */
};

/**
 * element - DOM object, vagy egy ID a dokumentumból.
 * descriptor - menü leíró
 *  {
 *
 */
function HTFloatMenu(element, descriptor, parent) {
	function _fn(v, def) {
		if(typeof v == 'function') return v;
		if(typeof v == 'object' && v.type == 'ref' && v.value.match(/^[a-zA-Z0-9\._]+$/)) return eval(v.value);
		return def;
	}

	if(typeof descriptor == 'undefined') return;
	if(typeof element == 'string') element = document.getElementById(element);
	if(typeof descriptor.hideTime == 'undefined' || Number(descriptor.hideTime) < 0) descriptor.hideTime = 300;
	this.view = new (_fn(descriptor.view, HTDefaultFloatMenuView))(descriptor);
	if(typeof this.view.attachToMenu == 'function') this.view.attachToMenu(this);

	this.parent = parent;
	var _this = this;

	function pan_over() {
//		console.log('pover: %i', descriptor.id);

		if(!_this.view.isVisible()) {
			console.warn("Over layer hiba.");
		}

		_this.over = true;
		_this.cancelHidePath();
	}

	function over() {
//		console.log('over: %i', descriptor.id);

		if(_this.view.isVisible()) {
			_this.cancelHidePath();
		} else {
			while(HTMenuHelper.activeMenu != _this.parent) {
				HTMenuHelper.activeMenu.hide();
			}
			try {
				var pos = jgtc.getBounds(element);

				var layer;
				if(descriptor.dir == 'h') {
					layer = _this.view.show(pos.x + pos.w, pos.y);
				} else {
					layer = _this.view.show(pos.x, pos.y + pos.h);
				}
				jgtc.captureEvent(layer, "mouseover", pan_over, true);
				jgtc.captureEvent(layer, "mouseout", out, true);

/*
				var ljs = descriptor.view(descriptor);
				_this.layer = jsonML(ljs);
				document.body.appendChild(_this.layer);
*/

				for(var i = 0; i < descriptor.menu.length; i++) {
					if('child' in descriptor.menu[i]) {
						new HTFloatMenu(_this.view.getMenuItem(i), descriptor.menu[i].child, _this);
					}
				}

				HTMenuHelper.activeMenu = _this;
			} catch(e) {
				console.error("Error while hover: %o", e);
				console.dir(e);
			}
		}
	}

	function out() {
		_this.over = false;
		_this.hidePath(descriptor.hideTime);
	}

	jgtc.captureEvent(element, "mouseover", over, true);
	jgtc.captureEvent(element, "mouseout", out, true);

	this.getAnchorElement = function() {
		return element;
	};

/*
	(function() {
		var pos = jgtc.getBounds(element);
		var j1 = [ 'div', { 'style': "border-top: 1px solid red; position: absolute; left: " + pos.x + "px; top: " + pos.y + "px; width: " + pos.w + "px;" } ];
		var j2 = [ 'div', { 'style': "border-left: 1px solid green; position: absolute; left: " + pos.x + "px; top: " + pos.y + "px; height: " + pos.h + "px;" } ];

		document.body.appendChild(jgtc.jsonML().build(j1));
		document.body.appendChild(jgtc.jsonML().build(j2));
	})();
*/
}

HTFloatMenu.prototype = {
	hide: function(t) {
		this.cancelHidePath();

		if(this.view.isVisible()) {
			this.view.hide();

			HTMenuHelper.activeMenu = this.parent;
		}
	},

	hidePath: function(t) {
		this.cancelHidePath();

		var _this = this;

		this.hideTimer = window.setTimeout(function() {
			_this.hideTimer = false;
			if(HTMenuHelper.activeMenu == _this) {
				while(HTMenuHelper.activeMenu != null && !HTMenuHelper.activeMenu.over) {
					HTMenuHelper.activeMenu.hide();
				}
			}
		}, t);
	},

	cancelHidePath: function() {
		if(this.hideTimer !== false) {
			window.clearTimeout(this.hideTimer);
			this.hideTimer = false;
		}
	},

	hideTimer: false,
	layer: null,
	over: false
}

/**
 * Automatikus lebegő menük kezelése.
 */
jgtc.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 {
				new HTFloatMenu(lnk, eval(descr), null);
			} catch(e) {
				console.warn("Error while init: %o", e);
				console.dir(e);
			}
		}
	}
}, true);

