var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = ((userAgent.indexOf('opera') != -1) || (typeof(window.opera) != 'undefined'));
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie7    = ((is_ie) && (userAgent.indexOf('msie 7.') != -1));
var is_mac    = (userAgent.indexOf('mac') != -1);

function Popup_Handler()
{
	this.menus          = new Array();
	this.activemenu     = null;
};

Popup_Handler.prototype.register = function(controlkey)
{
	this.menus[controlkey] = new Popup_Menu(controlkey);
};
var menu = new Popup_Handler();

function Popup_Menu(controlkey)
{
	this.controlkey       = controlkey;
	this.menuname         = this.controlkey.split('.')[0] + '_menu';
	this.controlobj       = document.getElementById(this.controlkey);
	this.controlobj.state = false;

	if (!this.controlobj.firstChild || (this.controlobj.firstChild.tagName != 'TEXTAREA' && this.controlobj.firstChild.tagName != 'INPUT'))
	{
		this.controlobj.unselectable = true;
		this.controlobj.onmouseover  = Popup_Events.prototype.controlobj_onmouseover;
		this.controlobj.onmouseout   = Popup_Events.prototype.controlobj_onmouseout;
	}
	if (document.getElementById(this.menuname))
	{
		this.menuobj = document.getElementById(this.menuname);

		if (this.menuobj && !this.menuobj.initialized)
		{
			this.menuobj.initialized    = true;
			this.menuobj.style.position = 'absolute';
			this.menuobj.style.zIndex   = 50;
			this.menuobj.onmouseover    = Popup_Events.prototype.controlmenu_onmouseover;
			this.menuobj.onmouseout     = Popup_Events.prototype.controlmenu_onmouseout;

			if (is_ie && !is_mac)
			{
				this.menuobj.style.filter += "progid:DXImageTransform.Microsoft.alpha(enabled=1,opacity=100)";
				this.menuobj.style.filter += "progid:DXImageTransform.Microsoft.shadow(direction=135,color=#8E8E8E,strength=3)";
			}
		}
	}
};

Popup_Menu.prototype.show = function(obj)
{
	if (!this.menuobj || menu.activemenu == this.controlkey)
	{
		return false;
	}
	if (menu.activemenu != null && menu.activemenu != this.controlkey)
	{
		menu.menus[menu.activemenu].hide();
	}
	menu.activemenu            = this.controlkey;
	this.menuobj.style.display = '';
	this.set_menu_position(obj);
};

Popup_Menu.prototype.set_menu_position = function(obj)
{
	this.pos    = this.fetch_offset(obj);
	this.leftpx = this.pos['left'];
	this.toppx  = this.pos['top'] + obj.offsetHeight;

	if ((this.leftpx + this.menuobj.offsetWidth) >= document.body.clientWidth && (this.leftpx + obj.offsetWidth - this.menuobj.offsetWidth) > 0)
	{
		this.leftpx    = this.leftpx + obj.offsetWidth - this.menuobj.offsetWidth;
		this.direction = 'right';
	}
	else
	{
		this.direction = 'left'
	}
	this.menuobj.style.left = this.leftpx + 'px';
	this.menuobj.style.top  = this.toppx + 'px';
};

Popup_Menu.prototype.hide = function(e)
{
	if (e && e.button && e.button != 1)
	{
		return true;
	}
	this.menuobj.style.display = 'none';
	menu.activemenu = null;
};

Popup_Menu.prototype.fetch_offset = function(obj)
{
	if (obj.getBoundingClientRect)
	{
		var rect       = obj.getBoundingClientRect();
		var scrollTop  = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
		var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);

		if (document.documentElement.dir == 'rtl')
		{
			scrollLeft = scrollLeft + document.documentElement.clientWidth - document.documentElement.scrollWidth;
		}
		return { 'left' : rect.left + scrollLeft, 'top' : rect.top + scrollTop };
	}
	var left_offset = obj.offsetLeft;
	var top_offset  = obj.offsetTop;

	while ((obj = obj.offsetParent) != null)
	{
		left_offset += obj.offsetLeft;
		top_offset  += obj.offsetTop;
	}
	return { 'left' : left_offset, 'top' : top_offset };
};

function Popup_Events() {};

Popup_Events.prototype.controlobj_onmouseover = function(e)
{
	menu.menus[this.id].show(this, true);
};

Popup_Events.prototype.controlobj_onmouseout = function(e)
{
	h = this.id;
	g = setTimeout("menu.menus[h].hide();", 50);
};

Popup_Events.prototype.controlmenu_onmouseover = function(e)
{
	clearTimeout(g);

	var temp_id = this.id.replace(/_menu/, "");
	menu.menus[temp_id].show(document.getElementById(temp_id), true);
};

Popup_Events.prototype.controlmenu_onmouseout = function(e)
{
	var temp_id = this.id.replace(/_menu/, "");
	menu.menus[temp_id].hide();
};
