if (document.layers && window.innerWidth) {
		window.onresize = NS4resize;
		window.NS4sIW = window.innerWidth;
		window.NS4sIH = window.innerHeight;
	}

// declare and test browser variables.
IE4 = document.all;
NS4 = document.layers;
ID = document.getElementById;
DHTML = (IE4 || NS4 || ID);

var subMenuLeft = new Array();
var largestItemWrite = new Array();
var largestItemTemp = new Array();

// use as follow: menuItems[item_number][property]
var menuItemsProperties = IK_Array[0];
var menuItems = IK_Array;

function findDOM(objectID,withStyle)
{
	if (withStyle == 1)
	{
		if (ID) 
			return (document.getElementById(objectID).style);
		else if (IE4)
			return (document.all[objectID].style); 
		else if (NS4)
			return (document.layers[objectID]);
	}
	else
	{
		if (ID)
			return(document.getElementById(objectID)); 
		else if (IE4)
			return (document.all[objectID]);
		else if (NS4)
			return (document.layers[objectID]);
	}
}

function arrayInit()
{
  for (i = 0; i < largestItemWrite.length; i++)
	largestItemWrite[i] = netscapeMenuSize;
}

function addSpace(str)
{
	// For the span to strech, we need to have an uninterupted string. This function replaces empty spaces with "&nbsp;"
	return str.replace(/ /g, "&nbsp;");
}

function checkSpace(menuItemId)
{
	var firstSpace = menuItemId.indexOf(" ");
	
	if (firstSpace != -1)
		return menuItemId.substring(0, firstSpace);
		
	return menuItemId;
}

function createMouseOut(counter)
{
	var linkChild = eval("IK_Array[" + counter + "][2]");
	
	if (linkChild == 1)
		currentMouseEvent = " onMouseOut='FW_startTimeout();' ";
	else
		currentMouseEvent = " onMouseOver='hideActiveMenus();'"
			
	return currentMouseEvent;
}
	
function displayMenu()
{
	var str = "<LAYER id='netscapeFix'  top=10 left=200><SPAN style='font-family:arial,Helvetica,sans serif; font-size:16px; color:#FFFFFF'></SPAN></A> </LAYER>";
	for (i = 0; i < menuItems.length; i++)
		if (ID || IE4)
		{
			// loop through arrays and build a single string containing all the divs.
			topnavOn = "topnavOn";
			topnav = "topnav";
			navigation = "navigation";
			
			str += "<div id='" + checkSpace(menuItems[i][0]) + "' style='position:absolute; font-weight: bold; font-family:" + menuFontFamily;
			
			if (IE4) 
				str += "; top:" + menuTop + "; left:" + menuLeft;
				
			// add 2 to the top position of the menu in netscape 6 (probably only in this project).
			if (ID && (!IE4 && ID)) 
				str += "; top:" + (menuTop  + 2) + "; left:" + menuLeft;
				
			if (IE4) 
				str += ";width: 20";

			// Find the folder name in the "selectedItem" and search for it in the current link's url string. highlight link if found (not -1)
			var selectedFolderName = IK_selectedItem.split("/");
			var currentFolderName = menuItems[i][1].indexOf(selectedFolderName[0]);
			var currentMenuName = menuItems[i][1].indexOf(selectedFolderName[1]);

			//menuItems[i][0] = name
			//menuItems[i][1] = path
			//menuItems[i][2] = set to 1 if has a submenu
			
			var selectedItem =  (IK_appPath + "/" + IK_selectedItem).toLowerCase();
			
			var menuEntry
			
			if (selectedItem == menuItems[i][1].toLowerCase())
				str +=  ">\n" + "<a href=" + menuItems[i][1] + " " + createMouseOut(i) + " " + getItemChild(checkSpace(menuItems[i][0]),i) + " style='text-decoration:none'>\n<span id=" + [i] + " style='font-family:" + menuFontFamily + "; font-size:" + menuFontSize + "px; color:" + menuFontOverColor + ";font-weight: bold;'>\n" + addSpace(menuItems[i][0]) + "</span></a> </div>\n";
			else
				str +=  "'>\n" + "<a href=" + menuItems[i][1] + " " + createMouseOut(i) + " " + getItemChild(checkSpace(menuItems[i][0]),i) + " style='text-decoration:none'>\n<span id=" + [i] + " style='font-family:" + menuFontFamily + "; font-size:" + menuFontSize + "px; color:" + menuFontColor + "' onMouseOut='this.style.color=&quot;" + menuFontColor + "&quot;' onMouseOver='this.style.color =&quot;" + menuFontOverColor + "&quot;' >" + addSpace(menuItems[i][0]) + "</span>\n</a></div>\n";
				
			if (NS4)
			{
				str += "<layer id='" + checkSpace(menuItems[i][0]) + "' top=" + menuTop + " left=" + menuLeft;
				
				// if else in order to highlight the current page's button.
				if (IK_appPath + IK_selectedItem == menuItems[i][1]) 
					str +=  ">\n" + "<a href=" + menuItems[i][1] + " " + createMouseOut(i) + " " + getItemChild(checkSpace(checkSpace(menuItems[i][0])),i) + " style='text-decoration:none'>\n<span style='font-family:" + menuFontFamily + "; font-size:" + menuFontSize + "px; color:" + menuFontOverColor + ";font-weight: bold;'>\n" + addSpace(menuItems[i][0]) + "</span></a> </layer>\n";
				else
					str +=  ">\n" + "<a href=" + menuItems[i][1] + " " + createMouseOut(i) + " " + getItemChild(checkSpace(checkSpace(menuItems[i][0])),i) + " style='text-decoration:none'>\n<span style='font-family:" + menuFontFamily + "; font-size:" + menuFontSize + "px; color:" + menuFontColor + ";font-weight: bold;'>\n" + addSpace(menuItems[i][0]) + "</span></a> </layer>\n";
				
			}
		}
	
	document.open();
	document.writeln(str);
	document.close();
	//alignMenu(); 
	// netscape
	setTimeout("alignMenu()",10);
}

function alignMenu()
{
	firstItem = true;
	for (i=0; i<menuItems.length;	i++)
	{
		if (firstItem == 1)
		{
		// Find the first item's left position and width. Then add those values to the "itemsSpacing" variable to 
		// Get the next items position (nextObjectLeft)
		if (IE4 || ID) { var itemWidth = findDOM([checkSpace(menuItems[i][0])]).offsetWidth;} 
		if (NS4) { var itemWidth = findDOM([menuItems[i][0]]).clip.width;}

		if (IE4) { var itemLeft = findDOM([checkSpace(menuItems[i][0])]).style.pixelLeft; }

		// This one is strange. the style sheet is causing troubles if I don't move the first item in N6 elsewhere, before bringing it back to its initial place.
		if (ID) { var itemLeft = findDOM([checkSpace(menuItems[i][0])]).offsetLeft;
					findDOM([checkSpace(menuItems[i][0])]).style.left = 20;
					findDOM([checkSpace(menuItems[i][0])]).style.left = itemLeft;}

		if (NS4) { var itemLeft = findDOM([checkSpace(menuItems[i][0])]).left;}
		subMenuLeft[i] = itemLeft + subMenuOffset;
		var nextObjLeft = itemWidth + itemLeft + itemsSpacing;
		
		firstItem = false;
		}
		else 
		{
		// Now we use the "nextObjLeft" variable as the left position of each new item, before updating the "nextObjLeft" variable.
		if (IE4 || ID) { findDOM([checkSpace(menuItems[i][0])]).style.left = nextObjLeft; }
		if (NS4) { findDOM([checkSpace(menuItems[i][0])]).left = nextObjLeft; }
		
		// this assigns a left value to the current submenu.
		subMenuLeft[i] = nextObjLeft + subMenuOffset;
				
		if (IE4 || ID) { var itemWidth = findDOM([checkSpace(menuItems[i][0])]).offsetWidth; }
		if (NS4) { var itemWidth = findDOM([checkSpace(menuItems[i][0])]).clip.width; }
		nextObjLeft += itemWidth + itemsSpacing;
		}
	}
}


// menuItem is the current link name, and i is the loop integer.
function getItemChild(menuItem, i)
{

	// Get the current link child property in the IK_Array array.
	var linkChild = eval("IK_Array[" + i + "][2]");
	
	if (linkChild == 1)
	{
		largestItemWidth = new Array();
	
		// Get the current link child property in the IK_Array array.

		// Find this submenu longest item and store it for later use.
		largestItemWidth[i] = 0;
		
		var ArrayLength = eval("IK_Array_" + [checkSpace(menuItems[i][0])] + ".length");
		for (j = 0; j < ArrayLength; j++)
		{
		// get current item.
			var currentSubmenuItem = eval("IK_Array_" + [checkSpace(menuItems[i][0])] + "[" + j + "][0]");
			
			if (IE4 || ID) 
			{
				var widthCheckingDiv = "<div id='" + i + j + "' style='position:absolute;left:-400;font-family:" + subMenuFontFamily + ";font-size:" + subMenuFontSize;
				if (IE4) 
					widthCheckingDiv += ";width: 20;";
					
				widthCheckingDiv += "'>" + addSpace(currentSubmenuItem) + "</div>";
				
				document.writeln(widthCheckingDiv);
				
				var widthCheckingDivId = i + "" + j;

				var itemWidth = findDOM(widthCheckingDivId).offsetWidth;  
				
				if (itemWidth > largestItemWidth[i])
				{
					largestItemWidth[i] = itemWidth; 
					largestItemWrite[i] = largestItemWidth[i];
				}
					
			}
		}


		// Construct MouseOver string. Recreate the current array name dynamically, appending "_menu" in order to create a different variable name
		var subMenu = "onMouseOver='window.FW_showMenu(window.IK_Array_" + menuItem + "_menu, subMenuLeft[" + i + "] ," + subMenuTop + ");'";
		
		return subMenu;
	} 
	
	var emptyString = " ";
	return emptyString;
	// Else return a blank string.
}

function NS4resize() {
	if (NS4sIW < window.innerWidth || 
		NS4sIW > window.innerWidth || 
		NS4sIH > window.innerHeight || 
		NS4sIH < window.innerHeight ) 
	{
		window.location.reload();
	}
}
