// Menu v2.03a
Menu = {
	items: new Object(),
	isGecko: (navigator.userAgent.toLowerCase().indexOf('gecko') != -1),
	mozVersion: getBrowserVersion('Mozilla'),
	msieVersion: getBrowserVersion('MSIE'),

	create: function(id)
		{
		this.items[id] = new menu(null, id);
		return this.items[id];
		},

	write: function(idMenu)
		{
		if(idMenu)
			{
			if(this.items[idMenu])
				this.items[idMenu].write();
			}
		else
			for(var id in this.items)
				this.items[id].write();
		},

	init: function(idMenu)
		{
		if(idMenu)
			{
			if(this.items[idMenu])
				this.items[idMenu].init();
			}
		else
			for(var id in this.items)
				this.items[id].init();
		},

	show: function(idMenu)
		{
		if(idMenu && this.items[idMenu])
			this.items[idMenu].show();
		},

	hide: function(idMenu)
		{
		if(idMenu && this.items[idMenu])
			this.items[idMenu].hide();
		},

	select: function(selection)
		{
		var sep = selection.indexOf('|');
		if(sep == -1)
			sep = selection.length;
		var menuID = selection.substring(0, sep);
		selection = selection.substring(sep + 1);

		if(this.items[menuID])
			{
			this.items[menuID].reset();
			this.items[menuID].select(selection);
			}
		},

	getItemById: function(id)
		{
		var sep = id.indexOf('|');
		if(sep == -1)
			sep = id.length;
		var menuID = id.substring(0, sep);
		var submenuID = id.substring(sep + 1);

		if(this.items[menuID])
			return this.items[menuID].getItemById(submenuID);

		return null;
		},

	findFrame: function(name, frameref, level, levelfound)
		{
		if(!level)
			level = 0;
		if(!levelfound)
			levelfound = -1;
		var f = null;
		var framefound = null;

		for(var i = 0; i < frameref.frames.length; i++)
			{
			if((frameref.frames[i].name == name) && ((levelfound == -1) || (level < levelfound)))
				{
				levelfound = level;
				return frameref.frames[i];
				}
			else
				{
				f = this.findFrame(name, frameref.frames[i], level + 1, levelfound);
				if(f)
					framefound = f;
				}
			}
		return framefound;
		},

	goTo: function(href, target)
		{
		var f = null;
		if(target == '')
		    f = window;
		else if(target == '_top')
		    f = top;
		else if(target == '_blank')
			f = false;
		else
		    f = this.findFrame(target, top);

		if(f)
			f.location.href = href;
		else
			{
		    window.open(href, '', 'width=' + screen.availWidth + ',height' + screen.availHeight + ',screenX=0,screenY=0,left=0,top=0,toolbar=yes,menubar=yes,resizable=yes,scrollbars=yes,location=yes,status=yes');
		    return true;
		    }
		}

}


function menu(parent, id)
	{
	this.parentItem = parent;
	this.id = id;
	this.left = '';
	this.top = '';
	this.width = 0;
	this.height = 0;
	this.position = 'relative';
	this.baseClass = '';
	this.overClass = '';
	this.selClass = '';
	this.baseStyle = '';
	this.overStyle = '';
	this.selStyle = '';
	this.itemTemplate = '';
	this.baseImg = '';
	this.overImg = '';
	this.selImg = '';
	this.imgAttribute = '';
	this.orientation = 'V';
	this.hidden = false;
	this.items = new Object();
	this.offsetWidth = 0;
	this.offsetHeight = 0;

	this.write = function()
		{
		document.writeln('<div id="menu_' + this.id + '" style="visibility: hidden; z-index: 1000;">')
		for(var i in this.items)
			this.items[i].write();
		document.writeln('</div>')
		};

	this.init = function()
		{
		var oMenu = document.getElementById('menu_' + this.id);

		var l = 0;
		var t = 0;
		for(var i in this.items)
			{
			var oItem = this.items[i].htmlElement = document.getElementById('menuitem_' + this.items[i].id);

			oItem.onmouseover = new Function('Menu.getItemById(\'' + this.items[i].id + '\').mouseover();');
			oItem.onmouseout = new Function('Menu.getItemById(\'' + this.items[i].id + '\').mouseout();');

			oItem.style.position = 'relative';      // setted to relative to fix MSIE 5.0 bug

			if(this.orientation == 'H')
				{
				mL = parseInt(oItem.style.marginLeft);
				if(!isNaN(mL))
					l += mL;
				}
			oItem.style.left = l + 'px';
			if(this.orientation == 'H')
				{
				oItem.style.width = oItem.offsetWidth + 'px';
				l += oItem.offsetWidth;
				mR = parseInt(oItem.style.marginRight);
				if(!isNaN(mR))
					l += mR;
				if((this.height > 0) && isNaN(parseInt(oItem.style.height)))
					oItem.style.height = this.height + 'px';
				}

			if(this.orientation == 'V')
				{
				mT = parseInt(oItem.style.marginTop);
				if(!isNaN(mT))
					t += mT;
				}
			oItem.style.top = t + 'px';
			if(this.orientation == 'V')
				{
				oItem.style.height = oItem.offsetHeight + 'px';
				t += oItem.offsetHeight;
				mB = parseInt(oItem.style.marginBottom);
				if(!isNaN(mB))
					t += mB;
				if((this.width > 0) && isNaN(parseInt(oItem.style.width)))
					oItem.style.width = this.width + 'px';
				}

			oItem.style.position = 'absolute';       // then we can set it to absolute

			this.items[i].cssText = oItem.style.cssText;
			if(this.items[i].submenu)
				{
				if(this.items[i].submenu.left === '')
					{
					this.items[i].submenu.left = oItem.offsetLeft;
					if(this.orientation == 'V')
						this.items[i].submenu.left += oItem.offsetWidth;
					}
				if(this.items[i].submenu.top === '')
					{
					this.items[i].submenu.top = oItem.offsetTop;
					if(this.orientation == 'H')
						this.items[i].submenu.top += oItem.offsetHeight;
					}
				this.items[i].submenu.init();
				}

            offsetBottom = oItem.offsetTop + oItem.offsetHeight;
            if(offsetBottom > this.offsetHeight)
                this.offsetHeight = offsetBottom;

            offsetRight = oItem.offsetLeft + oItem.offsetWidth;
            if(offsetRight > this.offsetWidth)
                this.offsetWidth = offsetRight;

			}

		oMenu.style.position = this.position;
		oMenu.style.left = this.left + 'px';
		oMenu.style.top = this.top + 'px';
		if(this.parentItem == null)
			{
			oMenu.style.width = this.offsetWidth + 'px';
			oMenu.style.height = this.offsetHeight + 'px';
			}
		
		if((this.parentItem == null) && !this.hidden)
			this.show();
		};

	this.show = function()
		{
		document.getElementById('menu_' + this.id).style.visibility = 'visible';
		};

	this.hide = function()
		{
		document.getElementById('menu_' + this.id).style.visibility = 'hidden'; 
		};

	this.restoreAll = function()
		{
		for(var i in this.items)
			{
			if(this.items[i].status != 'normal')
				this.items[i].restore();
			}
		};

	this.addItem = function(itemID, text, url, target, attributes)
		{
		this.items[itemID] = new menuItem(this, itemID, text, url, target, attributes);
		return this.items[itemID];
		};

	this.reset = function()
		{
		for(var i in this.items)
			if(this.items[i].selected)
				{
				if(this.items[i].submenu)
					this.items[i].submenu.reset();
				this.items[i].reset();
				this.items[i].selected = false;
				break;
				}
		};

	this.select = function(selection)
		{
		if(selection != '')
			{
			var sep = selection.indexOf('|');
			if(sep == -1)
				sep = selection.length;
			var menuID = selection.substring(0, sep);
			selection = selection.substring(sep + 1);

			if(this.items[menuID])
				this.items[menuID].select(selection);
			}
		};

	this.getItemById = function(id)
		{
		var sep = id.indexOf('|');
		if(sep == -1)
			sep = id.length;
		var itemID = id.substring(0, sep);
		var submenuID = id.substring(sep + 1);

		if(this.items[itemID])
			{
			if(submenuID == '')
				return this.items[itemID];
			else if(this.items[itemID].submenu)
				return this.items[itemID].submenu.getItemById(submenuID);
			}
		return null;
		};

	return this;
	}




function menuItem(parent, id, text, url, target, attributes)
	{
	this.parent = parent;
	this.id = this.parent.id + '|' + id;
	this.type = 'MENU_ITEM';
	this.text = text;
	this.url = url? url: '';
	this.target = target? target: '';
	this.attributes = attributes? ' ' + attributes: '';
	this.submenu = null;
	this.htmlElement = null;
	this.cssText = '';
	this.itemTemplate = '';
	this.baseClass = '';
	this.overClass = '';
	this.selClass = '';
	this.baseStyle = '';
	this.overStyle = '';
	this.selStyle = '';
	this.baseImg = '';
	this.overImg = '';
	this.selImg = '';
	this.baseImage = null;
	this.overImage = null;
	this.selImage = null;
	this.imgAttribute = '';
	this.selected = false;
	this.staus = 'normal';
	this.timer = null;

	this.write = function()
		{
		var attr = '';
		if(this.target != '')
			attr += ' target="' + this.target + '"';

		var classname = getMergedProperty(this, 'baseClass');
		attr += ' class="' + classname + '"';

		var style = getMergedProperty(this, 'baseStyle');
		attr += ' style="' + style + '"';

		var itemTemplate = getMergedProperty(this, 'itemTemplate');
		if(itemTemplate == '')
			itemTemplate = 'ITEM_TEXT';

		var img = '';
		var img_attr = getMergedProperty(this, 'imgAttribute');

		var base_img = getMergedProperty(this, 'baseImg');
		if(base_img != '')
			{
			this.baseImage = new Image();
			this.baseImage.src = base_img;
			var fixIE = '';
			if((this.url != '') && (Menu.msieVersion != -1) && (Menu.msieVersion < 5.5))
				fixIE = ' onClick="Menu.goTo(this.parentNode.href, this.parentNode.target);" onMouseOver="this.style.cursor=\'hand\';" onMouseOut="this.style.cursor=\'auto\';"';

			img = '<img id="menuitemimg_' + this.id + '" src="' + base_img + '" ' + img_attr + fixIE + '>';
			}

		var over_img = getMergedProperty(this, 'overImg');
		if(over_img != '')
			{
			this.overImage = new Image();
			this.overImage.src = over_img;
			}

		var sel_img = getMergedProperty(this, 'selImg');
		if(sel_img != '')
			{
			this.selImage = new Image();
			this.selImage.src = sel_img;
			}


		re_text = /ITEM_TEXT/g;
		re_img = /ITEM_IMG/g;

		itemTemplate = itemTemplate.replace(re_text, this.text);
		itemTemplate = itemTemplate.replace(re_img, img);

		aHref = '';
		if(this.url != '')
			aHref = ' href="' + this.url + '"';

		document.write('<a id="menuitem_' + this.id + '"' + aHref + attr + this.attributes + '>' + itemTemplate + '</a>');

		if(this.submenu)
			this.submenu.write();
		};

	this.addSubmenu = function()
		{
		this.submenu = new menu(this, this.id);
		return this.submenu;
		};

	this.setTimer = function()
		{
		this.clearTimer();
		this.timer = window.setTimeout('Menu.getItemById(\'' + this.id + '\').restore();', 40);
		if(this.parent && this.parent.parentItem)
			this.parent.parentItem.setTimer();
		};

	this.clearTimer = function()
		{
		if(this.parent && this.parent.parentItem)
			this.parent.parentItem.clearTimer();
		if(this.timer != null)
			window.clearTimeout(this.timer);
		};

	this.restore = function()
		{
		if(!this.selected)
			this.reset();

		if(this.submenu)
			{
			this.submenu.hide();
			if((Menu.mozVersion != -1) && (Menu.mozVersion < 1.3))
				this.submenu.restoreAll();
			}
		};

	this.reset = function()
		{
		var classname = getMergedProperty(this, 'baseClass');
		var style = getMergedProperty(this, 'baseStyle');

		this.status = 'normal';
		this.htmlElement.className = classname;
		this.htmlElement.style.cssText = this.cssText;

		if((this.baseImage != null) && document.getElementById('menuitemimg_' + this.id))
			document.getElementById('menuitemimg_' + this.id).src = this.baseImage.src;
		};

	this.mouseover = function()
		{
		this.clearTimer();
		if((Menu.mozVersion != -1) && (Menu.mozVersion < 1.3) && this.parent)
			this.parent.restoreAll();

		if(!this.selected)
			{
			var baseclass = getMergedProperty(this, 'baseClass');
			var overclass = getMergedProperty(this, 'overClass');
			var classname = baseclass + ' ' + overclass;

			var basestyle = getMergedProperty(this, 'baseStyle');
			var overstyle = getMergedProperty(this, 'overStyle');
			var style = basestyle + '; ' + overstyle;
			style = this.cssText + '; ' + style;

			this.status = 'hover';
			if(Menu.isGecko)
				{
				this.htmlElement.setAttribute('class', classname, 0);
				this.htmlElement.setAttribute('style', style, 0);
				}
			else
				{
				this.htmlElement.className = classname;
				this.htmlElement.style.cssText = style;
				}

			if((this.overImage != null) && document.getElementById('menuitemimg_' + this.id))
				document.getElementById('menuitemimg_' + this.id).src = this.overImage.src;
			}

		if(this.submenu)
			this.submenu.show();
		return true;
		};

	this.mouseout = function()
		{
		this.setTimer();
		return true;
		};

	this.select = function(selection)
		{
		this.selected = true;

		var baseclass = getMergedProperty(this, 'baseClass');
		var selclass = getMergedProperty(this, 'selClass');
		var classname = baseclass + ' ' + selclass;

		var basestyle = getMergedProperty(this, 'baseStyle');
		var selstyle = getMergedProperty(this, 'selStyle');
		var style = basestyle + '; ' + selstyle;
		style = this.cssText + '; ' + style;

		this.status = 'selected';
		if(Menu.isGecko)
			{
			this.htmlElement.setAttribute('class', classname, 0);
			this.htmlElement.setAttribute('style', style, 0);
			}
		else
			{
			this.htmlElement.className = classname;
			this.htmlElement.style.cssText = style;
			}

		if((this.selImage != null) && document.getElementById('menuitemimg_' + this.id))
			document.getElementById('menuitemimg_' + this.id).src = this.selImage.src;

		if(this.submenu)
			this.submenu.select(selection);
		};
	}


function getMergedProperty(obj, propertyName)
	{
	var mp = eval('obj.parent.' + propertyName);
	p = eval('obj.' + propertyName);
	if(p != '')
		mp = p;
	return mp;
	}

function getBrowserVersion(browserName)
	{
	result = -1;
	var ua = navigator.userAgent;
	switch(browserName)
		{
		case 'Mozilla':
			ua = ua.toLowerCase();
			idf = 'rv:';
			break;
		case 'MSIE':
			idf = 'IE ';
			break;
		}
	var i = ua.indexOf(idf);
	if(i != -1)
		{
		i += idf.length;
		result = parseFloat(ua.substring(i));
		}

	return result;
	}
