// ===================================================
// Attach event handlers
// ===================================================
if (window.addEventListener)
	window.addEventListener("load", BCMenu_CreateAll, true);
else if (window.attachEvent)
	window.attachEvent("onload", BCMenu_CreateAll);

// ===================================================
// BCMenu class
// ===================================================
function BCMenu(parentElement) {
	this.element = null;
	this.parentElement = parentElement;
	this.iframeElement = null;

	this.orientation = null;
	this.border = 0;
	this.cellSpacing = 0;
	this.cellPadding = 0;

	this.cssClass = null;
	this.itemCssClass = null;
	this.itemIconCssClass = null;
	this.itemTextCssClass = null;
	this.itemArrowCssClass = null;
	this.selectedItemCssClass = null;
	this.selectedItemIconCssClass = null;
	this.selectedItemTextCssClass = null;
	this.selectedItemArrowCssClass = null;
	this.separatorCssClass = null;

	this.items = new Array();

	this.trackSelectedItem = true;
	this.itemOffsetLeft = 0;
	this.itemOffsetTop = 0;
	this.showItemIcons = true;
	this.showSubMenuArrows = true;
	this.subMenuArrow = null;
	this.spacerImage = null;

	this.timeoutId = null;

	this.highlightItem = BCMenu_HighlightItem;
	this.getItemIcon = BCMenu_GetItemIcon;
	this.getItemArrow = BCMenu_GetItemArrow;
	this.getItemPosition = BCMenu_GetItemPosition;
	this.getItemSize = BCMenu_GetItemSize;
	this.getSize = BCMenu_GetSize;
	this.getAbsolutePositionOfElement = BCMenu_GetAbsolutePositionOfElement;
	this.getSeparatorDefaultText = BCMenu_GetSeparatorDefaultText;
	this.getDepth = BCMenu_GetDepth;
	this.getParentMenu = BCMenu_GetParentMenu;
	this.getRootMenu = BCMenu_GetRootMenu;
	this.isCreated = BCMenu_IsCreated;
	this.isVertical = BCMenu_IsVertical;
	this.isSubMenu = BCMenu_IsSubMenu;
	this.hasItems = BCMenu_HasItems;
	this.create = BCMenu_Create;

	BCMenu_EnsureMenuArray();

	this.index = BCMenu_MenuArray.length;
	BCMenu_MenuArray[this.index] = this;

	return this;
}

// ===================================================
// BCMenu_EnsureMenuArray method
// ===================================================
function BCMenu_EnsureMenuArray() {
	if (typeof BCMenu_MenuArray == "undefined")
		BCMenu_MenuArray = new Array();
	else if (!BCMenu_MenuArray || BCMenu_MenuArray.constructor != Array)
		BCMenu_MenuArray = new Array();
}

// ===================================================
// BCMenu_OnItemClick method
// ===================================================
function BCMenu_OnItemClick(menuIndex, itemIndex) {
	BCMenu_EnsureMenuArray();

	var menu = BCMenu_MenuArray[menuIndex];
	if (!menu) return;

	var item = menu.items[itemIndex];
	if (!item) return;

	var rootMenu = menu.getRootMenu();
	if (rootMenu.timeoutId) {
		window.clearTimeout(rootMenu.timeoutId);
		rootMenu.timeoutId = null;
	}

	if (item.constructor != BCMenuItem) return;

	if (item.action && typeof item.action == "string") {
		var action = item.action;
		var jsprefix = "javascript:";

		if (action.length >= jsprefix.length && action.substr(0, jsprefix.length).toLowerCase() == jsprefix.toLowerCase())
			action = action.substr(jsprefix.length);
		else
			action = "document.location.href = \"" + action.replace(/\"/gi, "\\\"") + "\";";

		BCMenu_HideAll(rootMenu.index);
		menu.highlightItem(item, false);

		window.setTimeout("eval(\"" + action.replace(/\"/gi, "\\\"") + "\")", 0);
	}
	else {
		if (!item.subMenu) {
			BCMenu_HideAll(rootMenu.index);
			menu.highlightItem(item, false);
		}
	}
}

// ===================================================
// BCMenu_OnItemMouseOver method
// ===================================================
function BCMenu_OnItemMouseOver(menuIndex, itemIndex) {
	BCMenu_EnsureMenuArray();

	var menu = BCMenu_MenuArray[menuIndex];
	if (!menu) return;

	var item = menu.items[itemIndex];
	if (!item) return;

	var rootMenu = menu.getRootMenu();
	if (rootMenu.timeoutId) {
		window.clearTimeout(rootMenu.timeoutId);
		rootMenu.timeoutId = null;
	}

	BCMenu_HideAfter(menuIndex, itemIndex);

	if (item.constructor == BCMenuSeparator) return;

	if (item.subMenu && item.subMenu.hasItems()) {
		if (!item.subMenu.isCreated())
			item.subMenu.create();

		var position = menu.getItemPosition(item);
		var size = menu.getItemSize(item);

		var x = position.left;
		var y = position.top;

		x += menu.isVertical() ? size.width : 0;
		y += menu.isVertical() ? 0 : size.height;

		x += menu.itemOffsetLeft;
		y += menu.itemOffsetTop;

		// ----------------------------------------------------------- //
		// EOates (06-SEP-2005): Fix to keep menus inside the window
		// boundaries
		var viewPort = BCMenu_GetViewPortSize();
		var subMenuSize = item.subMenu.getSize();
		var xScroll = document.body.scrollLeft ? document.body.scrollLeft : 0;
		var yScroll = document.body.scrollTop ? document.body.scrollTop : 0;
		var rtl = menu.rtl ? true : false; // right-to-left?
		var btt = menu.btt ? true : false; // bottom-to-top?
		
		if (menu.isVertical()) {
			// Get X coordinate for vertical menus
			if (rtl) {
				x = position.left - subMenuSize.width - menu.itemOffsetLeft;
				if (x < xScroll) {
					rtl = false;
					x = position.left + size.width + menu.itemOffsetLeft;
				}
			}
			else {
				x = position.left + size.width + menu.itemOffsetLeft;
				if ((x + subMenuSize.width) - xScroll > viewPort.width) {
					rtl = true;
					x = position.left - subMenuSize.width - menu.itemOffsetLeft;
				}
			}

			// Get Y coordinate for vertical menus
			if (btt) {
				y = (position.top + size.height) - subMenuSize.height - menu.itemOffsetTop;
				if (y < yScroll) {
					btt = false;
					y = position.top + menu.itemOffsetTop;
				}
			}
			else {
				y = position.top + menu.itemOffsetTop;
				if ((y + subMenuSize.height) - yScroll > viewPort.height) {
					btt = true;
					y = (position.top + size.height) - subMenuSize.height - menu.itemOffsetTop;
				}
			}
		}
		else {
			// Get X coordinate for horizontal menus
			if (rtl) {
				x = (position.left + size.width) - subMenuSize.width - menu.itemOffsetLeft;
				if (x < xScroll) {
					rtl = false;
					x = position.left + menu.itemOffsetLeft;
				}
			}
			else {
				x = position.left + menu.itemOffsetLeft;
				if ((x + subMenuSize.width) - xScroll > viewPort.width) {
					rtl = true;
					x = (position.left + size.width) - subMenuSize.width - menu.itemOffsetLeft;
				}
			}

			// Get Y coordinate for horizontal menus
			if (btt) {
				y = position.top - subMenuSize.height - menu.itemOffsetTop;
				if (y < yScroll) {
					btt = false;
					y = position.top + size.height + menu.itemOffsetTop;
				}
			}
			else {
				y = position.top + size.height + menu.itemOffsetTop;
				if ((y + subMenuSize.height) - yScroll > viewPort.height) {
					btt = true;
					y = position.top - subMenuSize.height - menu.itemOffsetTop;
				}
			}
		}
		
		if (x < xScroll) x = xScroll;
		if (y < yScroll) y = yScroll;

		item.subMenu.rtl = rtl;
		item.subMenu.btt = btt;
		// ----------------- END CHANGES 06-SEP-2005 ----------------- //

		// ----------------------------------------------------------- //
		// EOates (09-SEP-2005): Added "IFrame trick" to keep menus
		// above or "on top" of other form elements in Internet Explorer
		if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
			if (item.subMenu.iframeElement == null) {
				item.subMenu.iframeElement = document.createElement("iframe");
				item.subMenu.iframeElement.src = "";
				item.subMenu.iframeElement.scrolling = "no";
				item.subMenu.iframeElement.frameBorder = "no";
				item.subMenu.iframeElement.style.top = "0px";
				item.subMenu.iframeElement.style.left = "0px";
				item.subMenu.iframeElement.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
				item.subMenu.iframeElement.style.display = "none";
				item.subMenu.iframeElement.style.position = "absolute";

				document.body.appendChild(item.subMenu.iframeElement);
			}
		}

		if (item.subMenu.iframeElement != null) {
			item.subMenu.iframeElement.style.left = x + "px";
			item.subMenu.iframeElement.style.top = y + "px";
			item.subMenu.iframeElement.style.width = subMenuSize.width + "px";
			item.subMenu.iframeElement.style.height = subMenuSize.height + "px";
			item.subMenu.iframeElement.style.zIndex = item.subMenu.element.style.zIndex - 1;
			item.subMenu.iframeElement.style.display = "block";
		}
		// ----------------- END CHANGES 09-SEP-2005 ----------------- //

		item.subMenu.element.style.left = x + "px";
		item.subMenu.element.style.top = y + "px";
		item.subMenu.element.style.visibility = "visible";
	}

	menu.highlightItem(item, true);
}

// ===================================================
// BCMenu_OnItemMouseOut method
// ===================================================
function BCMenu_OnItemMouseOut(menuIndex, itemIndex) {
	BCMenu_EnsureMenuArray();

	var menu = BCMenu_MenuArray[menuIndex];
	if (!menu) return;

	var item = menu.items[itemIndex];
	if (!item) return;

	var rootMenu = menu.getRootMenu();
	if (!rootMenu.timeoutId) {
		rootMenu.timeoutId = window.setTimeout("BCMenu_HideAll(" + rootMenu.index + ");", 500);
	}

	if (item.constructor == BCMenuSeparator) return;

	if (rootMenu.trackSelectedItem) {
		if (!item.subMenu) {
			menu.highlightItem(item, false);
		}
	}
	else {
		menu.highlightItem(item, false);
	}
}

// ===================================================
// BCMenu_HideAll method
// ===================================================
function BCMenu_HideAll(menuIndex) {
	BCMenu_EnsureMenuArray();

	if (typeof menuIndex == "undefined") {
		for (var i = 0; i < BCMenu_MenuArray.length; i++) {
			if (!BCMenu_MenuArray[i].isSubMenu())
				BCMenu_HideAll(i);
		}
		return;
	}

	var menu = BCMenu_MenuArray[menuIndex];

	if (!menu) return;
	if (!menu.isCreated()) return;
	if (menu.isSubMenu()) menu.element.style.visibility = "hidden";

	if (menu.iframeElement) menu.iframeElement.style.display = "none";

	if (menu.hasItems()) {
		for (var i = 0; i < menu.items.length; i++) {
			var item = menu.items[i];

			if (item && item.constructor == BCMenuItem) {
				menu.highlightItem(item, false);

				if (item.subMenu && item.subMenu.isCreated())
					BCMenu_HideAll(item.subMenu.index);
			}
		}
	}
}

// ===================================================
// BCMenu_HideAfter method
// ===================================================
function BCMenu_HideAfter(menuIndex, itemIndex) {
	BCMenu_EnsureMenuArray();

	var menu = BCMenu_MenuArray[menuIndex];

	if (!menu) return;
	if (!menu.isCreated()) return;
	if (!menu.hasItems()) return;

	var item = menu.items[itemIndex];
	if (item) {
		var rootMenu = BCMenu_MenuArray[item.menuIndex].getRootMenu();
		for (var i = 0; i < BCMenu_MenuArray.length; i++) {
			if (i != rootMenu.index && !BCMenu_MenuArray[i].isSubMenu())
				BCMenu_HideAll(i);
		}
	}

	for (var i = 0; i < menu.items.length; i++) {
		if (i != itemIndex) {
			var item = menu.items[i];

			if (item && item.constructor == BCMenuItem) {
				menu.highlightItem(item, false);

				if (item.subMenu && item.subMenu.isCreated()) {
					BCMenu_HideAll(item.subMenu.index);
				}
			}
		}
	}
}

// ===================================================
// BCMenu_HighlightItem method
// ===================================================
function BCMenu_HighlightItem(item, highlight) {
	if (!item) return;
	if (item.constructor != BCMenuItem) return;

	var cssClass = null;
	var iconCssClass = null;
	var textCssClass = null;
	var arrowCssClass = null;
	var state = 0;

	if (highlight) {
		cssClass = item.selectedCssClass ? item.selectedCssClass : this.selectedItemCssClass;
		iconCssClass = item.selectedIconCssClass ? item.selectedIconCssClass : this.selectedItemIconCssClass;
		textCssClass = item.selectedTextCssClass ? item.selectedTextCssClass : this.selectedItemTextCssClass;
		arrowCssClass = item.selectedArrowCssClass ? item.selectedArrowCssClass : this.selectedItemArrowCssClass;
		state = 1;
	}
	else {
		cssClass = item.cssClass ? item.cssClass : this.itemCssClass;
		iconCssClass = item.iconCssClass ? item.iconCssClass : this.itemIconCssClass;
		textCssClass = item.textCssClass ? item.textCssClass : this.itemTextCssClass;
		arrowCssClass = item.arrowCssClass ? item.arrowCssClass : this.itemArrowCssClass;
		state = 0;
	}

	if (this.isVertical())
		item.rowElement.className = cssClass;
	else
		item.cellElement.className = cssClass;


	if (item.iconCellElement)
		item.iconCellElement.className = iconCssClass;

	item.textCellElement.className = textCssClass;

	if (item.arrowCellElement)
		item.arrowCellElement.className = arrowCssClass;

	if (item.iconImageElement) {
		var icon = this.getItemIcon(item, false);
		if (icon && icon.selectedImageUrl)
			item.iconImageElement.src = highlight ? icon.selectedImageUrl : icon.imageUrl;
	}

	if (item.arrowImageElement) {
		var arrow = this.getItemArrow(item, false);
		if (arrow && arrow.selectedImageUrl)
			item.arrowImageElement.src = highlight ? arrow.selectedImageUrl : arrow.imageUrl;
	}
}

// ===================================================
// BCMenu_GetItemIcon method
// ===================================================
function BCMenu_GetItemIcon(item, useSpacerIfNone) {
	var icon = null;

	if (item.icon)
		icon = item.icon;

	if (!icon && useSpacerIfNone)
		icon = this.spacerImage;

	return icon;
}

// ===================================================
// BCMenu_GetItemArrow method
// ===================================================
function BCMenu_GetItemArrow(item, useSpacerIfNone) {
	var arrow = null;

	if (item.subMenu && item.subMenu.hasItems()) {
		if (item.arrow)
			arrow = item.arrow;
		else if (this.subMenuArrow)
			arrow = this.subMenuArrow;
	}

	if (!arrow && useSpacerIfNone)
		arrow = this.spacerImage;

	return arrow;
}

// ===================================================
// BCMenu_GetItemPosition method
// ===================================================
function BCMenu_GetItemPosition(item) {
	var position = {left: 0, top: 0};

	if (!item) return position;
	if (item.constructor != BCMenuItem && item.constructor != BCMenuSeparator) return position;

	var element = this.isVertical() ? item.rowElement : item.cellElement;
	if (element)
		position = this.getAbsolutePositionOfElement(element);

	return position;
}

// ===================================================
// BCMenu_GetItemSize method
// ===================================================
function BCMenu_GetItemSize(item) {
	var size = {width: 0, height: 0};

	if (!item) return size;
	if (item.constructor != BCMenuItem && item.constructor != BCMenuSeparator) return size;

	var element = this.isVertical() ? item.rowElement : item.cellElement;
	if (element) {
		size.width = element.offsetWidth;
		size.height = element.offsetHeight;
	}

	return size;
}

// ===================================================
// BCMenu_GetSize
// ===================================================
function BCMenu_GetSize() {
	var size = {width: 0, height: 0};
	
	if (this.isCreated()) {
		// Width
		if (this.element.clip && this.element.clip.width)
			size.width = this.element.clip.width;
		else if (this.element.style && this.element.style.pixelWidth)
			size.width = this.element.style.pixelWidth;
		else if (this.element.offsetWidth)
			size.width = this.element.offsetWidth;
		
		// Height
		if (this.element.clip && this.element.clip.height)
			size.height = this.element.clip.height;
		else if (this.element.style && this.element.style.pixelHeight)
			size.height = this.element.style.pixelHeight;
		else if (this.element.offsetHeight)
			size.height = this.element.offsetHeight;
	}

	return size;
}

// ===================================================
// BCMenu_GetViewPortSize
// ===================================================
function BCMenu_GetViewPortSize() {
	var size = {width: 0, height: 0};

	if (window.innerWidth) {
		size.width = window.innerWidth;
		size.height = window.innerHeight;
	}
	else if (document.documentElement.clientWidth) {
		size.width = document.documentElement.clientWidth;
		size.height = document.documentElement.clientHeight;
	}
	else {
		size.width = document.body.clientWidth;
		size.height = document.body.clientHeight;
	}

	return size;
}

// ===================================================
// BCMenu_GetAbsolutePositionOfElement method
// ===================================================
function BCMenu_GetAbsolutePositionOfElement(element) {
	var position = {left: 0, top: 0};

	var e = element;
	if (e) {
		if (e.offsetParent) {
			while (e) {
				position.left += e.offsetLeft ? e.offsetLeft : 0;
				position.top += e.offsetTop ? e.offsetTop : 0;

				e = e.offsetParent;
			}
		}
		else {
			position.left = e.x ? e.x : 0;
			position.top = e.y ? e.y : 0;
		}
	}

	return position;
}

// ===================================================
// BCMenu_GetSeparatorDefaultText method
// ===================================================
function BCMenu_GetSeparatorDefaultText() {
	return this.isVertical() ? "<hr size=\"1\" noshade=\"noshade\" />" : "|";
}

// ===================================================
// BCMenu_GetDepth method
// ===================================================
function BCMenu_GetDepth() {
	var depth = 0;

	BCMenu_EnsureMenuArray();

	if (this.isSubMenu()) {
		var menu = this;

		while (menu.isSubMenu()) {
			menu = BCMenu_MenuArray[menu.parentElement.menuIndex];
			depth++;
		}
	}

	return depth;
}

// ===================================================
// BCMenu_GetParentMenu method
// ===================================================
function BCMenu_GetParentMenu() {
	var parentMenu = null;

	BCMenu_EnsureMenuArray();

	if (this.isSubMenu()) {
		parentMenu = BCMenu_MenuArray[this.parentElement.menuIndex];
	}

	return parentMenu;
}

// ===================================================
// BCMenu_GetRootMenu method
// ===================================================
function BCMenu_GetRootMenu() {
	var rootMenu = this;

	while (rootMenu.isSubMenu()) {
		rootMenu = rootMenu.getParentMenu();
	}

	return rootMenu;
}

// ===================================================
// BCMenu_IsCreated
// ===================================================
function BCMenu_IsCreated() {
	return this.element ? true : false;
}

// ===================================================
// BCMenu_IsVertical method
// ===================================================
function BCMenu_IsVertical() {
	var vertical;

	if (!this.isSubMenu()) {
		vertical = false;

		if (this.orientation && typeof this.orientation == "string")
			vertical = (this.orientation.toLowerCase() == "vertical");
	}
	else {
		vertical = true;

		if (this.orientation && typeof this.orientation == "string")
			vertical = (this.orientation.toLowerCase() != "horizontal");
	}

	return vertical;
}

// ===================================================
// BCMenu_IsSubMenu method
// ===================================================
function BCMenu_IsSubMenu() {
	if (this.parentElement && this.parentElement.constructor == BCMenuItem)
		return true;
	else
		return false;
}

// ===================================================
// BCMenu_HasItems method
// ===================================================
function BCMenu_HasItems() {
	if (this.items) {
		for (var i = 0; i < this.items.length; i++) {
			var item = this.items[i];

			if (item && (item.constructor == BCMenuItem || item.constructor == BCMenuSeparator))
				return true;
		}
	}

	return false;
}

// ===================================================
// BCMenu_Create method
// ===================================================
function BCMenu_Create() {
	if (this.isCreated()) return;

	if (this.subMenuArrow && this.subMenuArrow.constructor != BCMenuImage)
		this.subMenuArrow = new BCMenuImage(this.subMenuArrow.toString());

	if (this.spacerImage && this.spacerImage.constructor != BCMenuImage)
		this.spacerImage = new BCMenuImage(this.spacerImage.toString());

	var vertical = this.isVertical();

	var table = document.createElement("table");
	table.className = this.cssClass;
	table.border = this.border;
	table.cellSpacing = this.cellSpacing;
	table.cellPadding = this.cellPadding;

	var tbody = document.createElement("tbody");
	table.appendChild(tbody);

	var tr = null;
	if (!vertical) {
		tr = document.createElement("tr");
		tbody.appendChild(tr);
	}

	if (this.hasItems()) {
		var anyItemHasIcon = false;
		var anyItemHasSubMenu = false;

		for (var i = 0; i < this.items.length; i++) {
			var item = this.items[i];

			if (!item) continue;
			if (item.constructor != BCMenuItem) continue;

			item.init();

			if (item.icon)
				anyItemHasIcon = true;

			if (item.subMenu && item.subMenu.hasItems())
				anyItemHasSubMenu = true;
		}

		for (var i = 0; i < this.items.length; i++) {
			var item = this.items[i];

			if (!item) continue;
			if (item.constructor != BCMenuItem && item.constructor != BCMenuSeparator) continue;

			if (vertical) {
				tr = document.createElement("tr");
				tbody.appendChild(tr);
			}

			var onClick = new Function("BCMenu_OnItemClick(" + this.index + ", " + i + ");");
			var onMouseOver = new Function("BCMenu_OnItemMouseOver(" + this.index + ", " + i + ");");
			var onMouseOut = new Function("BCMenu_OnItemMouseOut(" + this.index + ", " + i + ");");

			if (item.constructor == BCMenuItem) {
				if (vertical) {
					tr.className = item.cssClass ? item.cssClass : this.itemCssClass;
					tr.onclick = onClick;
					tr.onmouseover = onMouseOver;
					tr.onmouseout = onMouseOut;

					var iconCell = null;
					var textCell = null;
					var arrowCell = null;
					var iconImage = null;
					var arrowImage = null;

					if (this.showItemIcons && anyItemHasIcon) {
						iconCell = document.createElement("td");
						iconCell.className = item.iconCssClass ? item.iconCssClass : this.itemIconCssClass;
						iconCell.width = "1";

						var icon = this.getItemIcon(item, true);
						if (icon) {
							iconImage = icon.createImageElement();
							if (iconImage)
								iconCell.appendChild(iconImage);
						}

						tr.appendChild(iconCell);
					}

					textCell = document.createElement("td");
					textCell.className = item.textCssClass ? item.textCssClass : this.itemTextCssClass;
					textCell.innerHTML = item.text;
					tr.appendChild(textCell);

					if (this.showSubMenuArrows && anyItemHasSubMenu) {
						arrowCell = document.createElement("td");
						arrowCell.className = item.arrowCssClass ? item.arrowCssClass : this.itemArrowCssClass;
						arrowCell.width = "1";

						var arrow = this.getItemArrow(item, true);
						if (arrow) {
							arrowImage = arrow.createImageElement();
							if (arrowImage)
								arrowCell.appendChild(arrowImage);
						}

						tr.appendChild(arrowCell);
					}

					item.rowElement = tr;
					item.cellElement = null;
					item.iconCellElement = iconCell;
					item.textCellElement = textCell;
					item.arrowCellElement = arrowCell;
					item.iconImageElement = iconImage;
					item.arrowImageElement = arrowImage;
				}
				else {
					var td = document.createElement("td");
					td.className = item.cssClass ? item.cssClass : this.itemCssClass;
					td.onclick = onClick;
					td.onmouseover = onMouseOver;
					td.onmouseout = onMouseOut;

					var subTable = document.createElement("table");
					var subTableBody = document.createElement("tbody");
					var subTableRow = document.createElement("tr");
					var subTableIconCell = null;
					var subTableTextCell = null;
					var subTableArrowCell = null;
					var subTableIconImage = null;
					var subTableArrowImage = null;

					subTable.border = 0;
					subTable.cellSpacing = 0;
					subTable.cellPadding = 0;
					subTable.width = "100%";

					if (this.showItemIcons) {
						var icon = this.getItemIcon(item, false);
						if (icon) {
							subTableIconImage = icon.createImageElement();
							if (subTableIconImage) {
								subTableIconCell = document.createElement("td");
								subTableIconCell.className = item.iconCssClass ? item.iconCssClass : this.itemIconCssClass;
								subTableIconCell.appendChild(subTableIconImage);
								subTableRow.appendChild(subTableIconCell);
							}
						}
					}

					subTableTextCell = document.createElement("td");
					subTableTextCell.className = item.textCssClass ? item.textCssClass : this.itemTextCssClass;
					subTableTextCell.innerHTML = item.text;
					subTableRow.appendChild(subTableTextCell);

					if (this.showSubMenuArrows && item.subMenu) {
						var arrow = this.getItemArrow(item, false);
						if (arrow) {
							subTableArrowImage = arrow.createImageElement();
							if (subTableArrowImage) {
								subTableArrowCell = document.createElement("td");
								subTableArrowCell.className = item.arrowCssClass ? item.arrowCssClass : this.itemArrowCssClass;
								subTableArrowCell.appendChild(subTableArrowImage);
								subTableRow.appendChild(subTableArrowCell);
							}
						}
					}

					subTableBody.appendChild(subTableRow);
					subTable.appendChild(subTableBody);
					td.appendChild(subTable);
					tr.appendChild(td);

					item.rowElement = tr;
					item.cellElement = td;
					item.iconCellElement = subTableIconCell;
					item.textCellElement = subTableTextCell;
					item.arrowCellElement = subTableArrowCell;
					item.iconImageElement = subTableIconImage;
					item.arrowImageElement = subTableArrowImage;
				}
			}
			else if (item.constructor == BCMenuSeparator) {
				var td = document.createElement("td");
				td.innerHTML = item.text ? item.text : this.getSeparatorDefaultText();

				var e = vertical ? tr : td;
				e.onclick = onClick;
				e.onmouseover = onMouseOver;
				e.onmouseout = onMouseOut;
				e.className = item.cssClass ? item.cssClass : this.separatorCssClass;

				if (vertical) {
					var colSpan = 1;

					colSpan += this.showItemIcons ? 1 : 0;
					colSpan += this.showSubMenuArrows ? 1 : 0;

					if (colSpan > 1)
						td.colSpan = colSpan;
				}

				item.rowElement = tr;
				item.cellElement = td;

				tr.appendChild(td);
			}

			item.itemIndex = i;
			item.menuIndex = this.index;
		}
	}

	this.element = table;

	if (!this.isSubMenu()) {
		if (this.parentElement && typeof this.parentElement == "string")
			this.parentElement = document.getElementById(this.parentElement);

		if (this.parentElement)
			this.parentElement.appendChild(this.element);
		else
			document.body.appendChild(this.element);
	}
	else {
		this.element.style.position = "absolute";
		this.element.style.left = "0px";
		this.element.style.top = "0px";
		this.element.style.visibility = "hidden";
		this.element.style.zIndex = 1000 + this.getDepth() + this.parentElement.itemIndex;

		document.body.appendChild(this.element);
	}
}

// ===================================================
// BCMenu_CreateAll
// ===================================================
function BCMenu_CreateAll() {
	BCMenu_EnsureMenuArray();

	for (var i = 0; i < BCMenu_MenuArray.length; i++) {
		var menu = BCMenu_MenuArray[i];

		if (!menu.isCreated() && !menu.isSubMenu() && menu.hasItems()) {
			menu.create();
		}
	}
}

// ===================================================
// BCMenuItem class
// ===================================================
function BCMenuItem(text, action, icon, arrow) {
	this.rowElement = null;
	this.cellElement = null;
	this.iconCellElement = null;
	this.textCellElement = null;
	this.arrowCellElement = null;
	this.iconImageElement = null;
	this.arrowImageElement = null;

	this.menuIndex = -1;
	this.itemIndex = -1;

	this.text = text;
	this.action = action;
	this.icon = icon;
	this.arrow = arrow;

	this.cssClass = null;
	this.iconCssClass = null;
	this.textCssClass = null;
	this.arrowCssClass = null;
	this.selectedCssClass = null;
	this.selectedIconCssClass = null;
	this.selectedTextCssClass = null;
	this.selectedArrowCssClass = null;

	this.subMenu = null;

	this.init = BCMenuItem_Init;

	return this;
}

// ===================================================
// BCMenuItem_Init method
// ===================================================
function BCMenuItem_Init() {
	if (this.icon && this.icon.constructor != BCMenuImage)
		this.icon = new BCMenuImage(this.icon.toString());

	if (this.arrow && this.arrow.constructor != BCMenuImage)
		this.arrow = new BCMenuImage(this.arrow.toString());

	if (this.subMenu) {
		this.subMenu.parentElement = this;

		if (this.subMenu.hasItems()) {
			for (var i = 0; i < this.subMenu.items.length; i++) {
				var item = this.subMenu.items[i];

				if (item && item.constructor == BCMenuItem)
					item.init();
			}
		}
	}
}

// ===================================================
// BCMenuSeparator class
// ===================================================
function BCMenuSeparator(text) {
	this.rowElement = null;
	this.cellElement = null;

	this.menuIndex = -1;
	this.itemIndex = -1;

	this.text = text;

	this.cssClass = null;

	return this;
}

// ===================================================
// BCMenuImage class
// ===================================================
function BCMenuImage(imageUrl, selectedImageUrl, width, height) {
	this.imageUrl = imageUrl;
	this.selectedImageUrl = selectedImageUrl;
	this.width = width;
	this.height = height;

	this.createImageElement = BCMenuImage_CreateImageElement;

	return this;
}

// ===================================================
// BCMenuImage_CreateImageElement method
// ===================================================
function BCMenuImage_CreateImageElement(selected) {
	var img = document.createElement("img");

	img.src = this.imageUrl;
	img.alt = "";
	img.border = 0;

	if (this.width)
		img.width = this.width;

	if (this.height)
		img.height = this.height;

	return img;
}
