﻿
var popupmenu = {};
popupmenu.display = function(bar, display) {
	if (display) {
		var p = $('popupmenu_' + window.tmp_popupmenu_current);
		var menu = $('popupmenu_' + bar.current);
		menu.style.display = 'block';
		
		if (p) {
			var mp, mp2 = menu;
			while(mp2) {
				if (mp2.id && /popupmenu_\d+/.test(mp2.id) && mp2.previousSibling == p.previousSibling) {
					mp = 1;
					break;
				}
				mp2 = mp2.parentNode;
			}
			if (!mp) p.style.display = 'none';
		}
		window.tmp_popupmenu_current = bar.current;
		
		if (!menu.parentNode.onmouseout) {
			menu.parentNode.onmouseout = function() {
				window.tmp_popuumenu_handler = setTimeout(popupmenu.display, 200, bar);
			};
			menu.onmouseover = 
			menu.parentNode.onmouseover = function() {
				clearTimeout(window.tmp_popuumenu_handler);
			}
		}
	} else {
		$('popupmenu_' + bar.current).style.display = 'none';
	}
};
popupmenu.createArgs = [null];
popupmenu.count = 0;
popupmenu.create = function(text) {
	if (arguments.length < 2) return text;
	popupmenu.createArgs.push(arguments);
	
	var cur = ++popupmenu.count;
	if (typeof(text) == 'string') {
		return '<div current="' + cur + '" onmouseover="popupmenu.mouseover.apply(this)"><a href="' + (arguments.length > 1 && arguments[1][0] ? arguments[1][0] : 'javascript:void(0);') + '" class="{0}"' + (arguments.length > 1 && arguments[1][0] ? ' target=_blank' : '') + '>' + text + '</a></div>';
	} else {
		alert(text);
		text.current = cur;
		text.onmouseover = function() {
			mouseover.apply(this);
		};
	}
}

popupmenu.mouseover = function() {
	if (!this.current) this.current = this.attributes['current'].value;
	if (!$('popupmenu_' + this.current)) {
		var div = document.createElement('div');
		div.id = 'popupmenu_' + this.current;
		div.style.position = 'absolute';
		div.style.display = 'none';
		var html = [], ul = '<table class="popupmenu" border="0" cellpadding="0" cellspacing="0" style="width:{0}px;">', size = [];
		var args = Array.prototype.slice.call(popupmenu.createArgs[this.current],1);
		var rows = 12;
		var cols = Math.ceil(args.length / rows);
		for(var a = 0; a < rows; a++) {
			html.push('<tr>');
			for(var b = a; b < a + rows * cols ; b+=rows)
				if (b < args.length && args[b][1]) {
					var isempty = !args[b][0] || args[b][0] == '' && args[b][0] == '#';
					var t = '<a href="' + (isempty ? 'javascript:void(0);' : args[b][0]) + '"' + (isempty ? '' : ' target="_blank"') + '>' + args[b][1] + '</a>';
					if (typeof args[b][2] == 'object' && args[b][2].push && args[b][2].length > 0) {
						t = popupmenu.create.apply(this, args[b].slice(1));
					}
					html.push('<td' + (a == 0 ? ' style="wdith:{0}px;"' : '') + '>' + t + '</td>');
					var idx = (b - a) / rows;
					if (isNaN(size[idx])) size[idx] = 14;
					size[idx] = Math.max(size[idx], args[b][1].getLength());
				} else {
					html.push('<td onmouseover="$(\'popupmenu_' + this.current + '\').parentNode.onmouseout()"> </td>');
				}
			html.push('</tr>');
		}
		html.push('</table>');
		var sumw = 0;
		if (html.length > 1) {
			for(var a = 1; html[a] != '</tr>'; a++) {
				var width = 60 + size[a - 1] * 6;
				sumw += width;
				html[a] = html[a].format(width);
			}
		}
		div.innerHTML = ul.format(sumw) + html.join('');
		html = null; delete html;
		
		var ns = this.parentNode.nextSibling;
		if (ns && ns.parentNode == this.parentNode)
			this.insertBefore(div, this.parentNode.nextSibling);
		else this.parentNode.appendChild(div);
	}
	popupmenu.display(this, 1);

	var div = $('popupmenu_' + this.current), r = Math.abs(document.body.offsetWidth + document.body.scrollLeft - 1000 + 22) / 2;
	getOffset(div);
	var ws = (div.offsetLeft2 + div.offsetWidth) - (document.body.offsetWidth + document.body.scrollLeft - r);
	div.style.right = ws > -1 ? r : '';
}
