﻿
var showDelay = 100;
var hideDelay = 500;
var showTimer;
var hideTimer;
var currentMenuId;
var allTopEntries = new Object();
var allSubContent = new Object();


function ShowSubMenu(no) {
	//currentMenuId = "SM" + no;
	clearTimeout(hideTimer);
	clearTimeout(showTimer);

	for (prop in allTopEntries) {
		if (allTopEntries[prop].isActive)
			$("#" + prop + "_link").css("background-position", "right -85px");
		else
			$("#" + prop + "_link").css("background-position", "right -1px");
	}

	showTimer = setTimeout("ShowSubMenuDelayed('" + no + "')", showDelay);

	allTopEntries["SM" + no] = new Object();
	if ($("#SM" + no + "_link").hasClass("active"))
		allTopEntries["SM" + no].isActive = true;
	else
		allTopEntries["SM" + no].isActive = false;

	if (allTopEntries["SM" + no].isActive)
		$("#SM" + no + "_link").css("background-position", "right -57px");
	else
		$("#SM" + no + "_link").css("background-position", "right -29px");
}

function ShowSubMenuDelayed(no) {
	currentMenuId = "SM" + no;
	if (BuildMenu($("#TM" + no))) {
		HideShowNotOverlayableElements("hide");
		$("#cSubmenu").css("display", "block");
	}
	else
		$("#cSubmenu").css("display", "none");
}

function HideShowNotOverlayableElements(showHide) {
	if ($.browser.msie && parseInt($.browser.version, 10) < 7)
		$("select").css("visibility", (showHide == "show" ? "visible" : "hidden"));
}

function StopHideTimer() {
	clearTimeout(hideTimer);
	for (prop in allTopEntries) {
		if (allTopEntries[prop].isActive)
			$("#" + prop + "_link").css("background-position", "right -85px");
		else
			$("#" + prop + "_link").css("background-position", "right -1px");
	}
	if (allTopEntries[currentMenuId].isActive)
		$("#" + currentMenuId + "_link").css("background-position", "right -57px");
	else
		$("#" + currentMenuId + "_link").css("background-position", "right -29px");
}


function HideSubMenu() {
	clearTimeout(showTimer);
	hideTimer = setTimeout("HideSubMenuDelayed()", hideDelay);
}


function HideSubMenuDelayed() {
	for (prop in allTopEntries) {
		if (allTopEntries[prop].isActive)
			$("#" + prop + "_link").css("background-position", "right -85px");
		else
			$("#" + prop + "_link").css("background-position", "right -1px");
	}

	HideShowNotOverlayableElements("show");

	$("#cSubmenu").css("display", "none");
}


function BuildMenu(elm) {
	if (allSubContent[currentMenuId]) {
		SetMenuWidthAndHeight(allSubContent[currentMenuId].noOfColumns, allSubContent[currentMenuId].noOfRows, elm);
		$("#cSubmenuContent").html(allSubContent[currentMenuId].html);
		return true;
	}
	else
	{
		var mc = "<div>";
		var children;
		try {
			children = document.getElementById(currentMenuId).childNodes;
		}
		catch (e) {
			return false;
		}
		var noOfColumns = 1;
		var noOfRows = 0;
		var noOfRowsMax = -1;

		for (var i = 0; i < children.length; i++) {
			var newColumn = children[i].className;
			if (newColumn == "newColumn") {
				mc += "</div><div>";
				noOfColumns++;
				if (noOfRows > noOfRowsMax)
					noOfRowsMax = noOfRows;
				noOfRows = 0;
			}
			var title = children[i].firstChild.innerHTML;
			var noTop = children[i].firstChild.className;
			var cClass = "";
			if (title.toLowerCase().indexOf("<br>") != -1) {
				cClass = noTop=="" ? "topTwoLines" : "twoLines";
				noOfRows = noOfRows + 2;
			}
			else {
				cClass = noTop=="" ? "top" : "";
				noOfRows++;
			}
			var href = children[i].firstChild["href"];
			var target = children[i].firstChild["target"];
			if (target != "") target = ' target="' + target + '"';
			if (href != "" && href != null)
				mc += '<a href="' + href + '"' + target + ' class="' + cClass + '">' + title + '</a>';
			else
				mc += '<span class="' + cClass + '">' + title + '</span>';

			// Test for sub entries
			if (children[i].childNodes[1]) {
				var subEntries = children[i].childNodes[1].childNodes;
				for (var j = 0; j < subEntries.length; j++) {
					var title = subEntries[j].firstChild.innerHTML;
					var cClass = "";
					if (title.toLowerCase().indexOf("<br>") != -1) {
						cClass = ' class="twoLines"';
						noOfRows = noOfRows + 2;
					}
					else {
						noOfRows++;
					}
					var href = subEntries[j].firstChild["href"];
					var target = subEntries[j].firstChild["target"];
					if (target != "") target = ' target="' + target + '"';
					mc += '<a href="' + href + '"' + target + cClass + '>' + title + '</a>';
				}
			}
		}
		mc += "</div";

		if (noOfRows > noOfRowsMax)
			noOfRowsMax = noOfRows;

		SetMenuWidthAndHeight(noOfColumns, noOfRowsMax, elm);

		$("#cSubmenuContent").html(mc);
		allSubContent[currentMenuId] = new Object();
		allSubContent[currentMenuId].html = mc;
		allSubContent[currentMenuId].noOfColumns = noOfColumns;
		allSubContent[currentMenuId].noOfRows = noOfRowsMax;

		return true;
	}
}

function SetMenuWidthAndHeight(columns, rows, topMenuElement) {
	var columnWidth = 228;
	var rowHeight = 24;
	var posX = $(topMenuElement).position().left + 11;

	var width = 8 + ((columnWidth + 8) * columns);
	var height = 16 + (rowHeight * rows);

	$("#cSubmenuContainer").css("width", width + "px");
	$("#cSubmenu_ds4").css("width", (width + 2) + "px");
	$("#cSubmenu_ds3").css("width", (width + 4) + "px");
	$("#cSubmenu_ds2").css("width", (width + 6) + "px");
	$("#cSubmenu_ds1").css("width", (width + 8) + "px");
	$("#cSubmenu").css("width", (width + 10) + "px");

	$("#cSubmenuContainer").css("height", height + "px");
	$("#cSubmenuContent").css("height", (height - 16) + "px");
	$("#cSubmenu_ds4").css("height", (height) + "px");
	$("#cSubmenu_ds3").css("height", (height + 2) + "px");
	$("#cSubmenu_ds2").css("height", (height + 4) + "px");
	$("#cSubmenu_ds1").css("height", (height + 6) + "px");
	$("#cSubmenu").css("height", (height + 8) + "px");

	if (columns < 4) {
		if ((posX + width) > 952)
			$("#cSubmenu").css("left", (posX - ((posX + width) - 952) + 11) + "px");
		else
			$("#cSubmenu").css("left", posX + "px");
	}
	else
		$("#cSubmenu").css("left", "11px");
}

