var MENU_MANAGER_C_CLASS_ACTIVE = "active";

var	menu_manager = {
	register_clickout: false,
	onclick:function(id, css, register_clickout, need_delay)
	{
        if (need_delay)
        {
            setTimeout(this._onclick.bind(this, id, css, register_clickout, false), 1);
            return (true);
        }
        if(this._onclick(id, css, register_clickout)) {
            return (true);
        }
        else
        {
            return (false);
        }    
	},
    _onclick:function(id, css, register_clickout)
    {
		if (register_clickout)
		{
			this.register_clickout = true;
		}
		if (!css)
		{
			css = MENU_MANAGER_C_CLASS_ACTIVE;
		}
		if (!this.elem_exists(id))
		{
			return (false);
		}
		if (this.is_opened(id, css))
		{
			this.close(id, css);
			return (false);
		}
		this.open(id, css);
		return (true);        
    },
	is_opened:function(id, css)
	{
		var	elem;

		if (!css)
		{
			css = MENU_MANAGER_C_CLASS_ACTIVE;
		}
		if ((elem = $(id)))
		{
			return (elem.hasClassName(css));
		}
	},
	open:function(id, css)
	{
		var	elem;

		if (!css)
		{
			css = MENU_MANAGER_C_CLASS_ACTIVE;
		}
		if ((elem = $(id)))
		{
			elem.addClassName(css);
			if (this.register_clickout)
			{
				popupmanager.register(id, function() {
            	this.close(id, css);
        		}.bind(this));
			}
			return (true);
		}
		return (false);
	},
	close:function(id, css)
	{
		var	elem;

		if (!css)
		{
			css = MENU_MANAGER_C_CLASS_ACTIVE;
		}
		if ((elem = $(id)))
		{
			elem.removeClassName(css);
			popupmanager.id = null;
			this.register_clickout = false;
			return (true);
		}
		return (false);		
	},
	elem_exists:function(id)
	{
		return ($(id));
	}
};

