var __sto = setTimeout;
window.setTimeout = function(callback, timeout, param)
{
    var args = Array.prototype.slice.call(arguments, 2);
    var _cb = function()
    {
        callback.apply(null,args);
    }
    
    return __sto(_cb,timeout);
}

function get_px(obj)
{
	_x = 0;
	for(_obj=obj; _obj != undefined; _obj=_obj.offsetParent)
		_x += _obj.offsetLeft;
	return _x;
}

function get_py(obj)
{
	_y = 0;
	for(_obj=obj; _obj != undefined; _obj=_obj.offsetParent)
		_y += _obj.offsetTop;
	return _y;
}

function $(_id)
{
	return document.getElementById(_id);
}

function __do_hide(_div)
{
	_div.style.visibility = "hidden";
}

function __hide_menu()
{
	this.timer = window.setTimeout(__do_hide, 100, this);
}

function __show_menu()
{
	window.clearTimeout(this.timer);
	this.timer = null;
	if (this.style.visibility == 'visible')
		return;
	else
		this.style.visibility = 'visible';
}


function __expend_div(_div, _w, _h)
{
	if (parseInt(_div.style.width)==_w && 
		parseInt(_div.style.height)==_h)
		return;
	
	if (parseInt(_div.style.width) < _w)
		_div.style.width = (parseInt(_div.style.width)+1)+"px";
	if (parseInt(_div.style.height) < _h)
		_div.style.height = (parseInt(_div.style.height)+1)+"px";

	window.setTimeout(__expend_div, 10, _div, _w, _h);
}

function __init_menu(obj, _div)
{
	if (_div.style.visibility == "visible")
		return;
		
	_div.style.width = _div.scrollWidth + "px";
	_div.style.height = _div.scrollHeight + "px";
	
	_w = parseInt(_div.style.width);
	_h = parseInt(_div.style.height);

	if (_w+get_px(obj) > document.body.clientWidth)
		_div.style.left = (document.body.clientWidth - _w)+"px";
	else
		_div.style.left = get_px(obj)+"px";
	_div.style.top = (get_py(obj)+obj.scrollHeight+5)+"px";
	_div.style.overflow = 'hidden';
	_div.style.visibility = "visible";
	_div.onmouseover = __show_menu;
	_div.onmouseout = __hide_menu;

	_div.style.height = 0 + "px";
	_div.timer = null;

	__expend_div(_div, _w, _h);
}

/*
	menu_arr : [text, event, submenu]
*/

function simple_menu(obj, menu_arr)
{
	if (menu_arr.length == 0)
		return;

	if (obj.menu == undefined) {
		_div = document.createElement("div");
		_div.align = "left";
		_div.style.background = '#AAAAAA';
		_div.style.position = 'absolute';

		_html = "";
		for (i=0; i<menu_arr.length; i++) {
			if (menu_arr[i][1] == null)
				_html += menu_arr[i][0] + "<br>";
			else
				_html += "<a style='cursor:pointer' onclick=\""+ menu_arr[i][1] +"\">" + menu_arr[i][0] + "</a><br>";
		}
		_div.innerHTML += _html;
		_div.style.visibility = "hidden";

		document.body.appendChild(_div);
		_div.style.width = (_div.scrollWidth + 10) + "px";
		_div.style.height = _div.scrollHeight + "px";
		obj.menu = _div;
	}

	__init_menu(obj, obj.menu);
}

function cool_menu(obj, div_id)
{
	obj.menu = $(div_id);
	__init_menu(obj, $(div_id));
}

function hidden_menu(obj)
{
	obj.menu.timer = window.setTimeout(__do_hide, 100, obj.menu);
}