///////////////////////////////////////////////////////////////////////
//
// Script		:	incNavigation_1.js
// Developer	:	Clive Howard
// Created		:	July 2004
// Updated		:	July 2004
//
// Methods		:	
//
// Overview		:	Code for dynamic drop down navigation
//					For use with originX.WebEditor templates
//					Requires incMenus.js include file
// 
// Copyright (c) 2004 originX
// http://www.originx.com
//
///////////////////////////////////////////////////////////////////////

// --- START BROWSER DETECTION CODE

var NON_BROWSER = false
var IE4_BROWSER = false
var NN_BROWSER = false
var NN6_BROWSER = false
var IE5_BROWSER = false

// Detect non-compatible browsers
if ((navigator.userAgent.indexOf("3.") != -1) || (navigator.userAgent.indexOf("2.") != -1))
{
	NON_BROWSER = true;
}

// Detect IE4 so that the correct DOM reference is used later
// document.all for IE4 but document.getElementById for 5+ and NN6
if (navigator.userAgent.indexOf("MSIE 4") != -1)
{
	IE4_BROWSER = true;
}

// Is the browser netscape 4 (changed from document.layers)
if ((navigator.appName.indexOf("Netscape") != -1) && (parseInt(navigator.appVersion.substring(0,1))==4))
{ 
	NN_BROWSER = true;
} 

if ((navigator.appName.indexOf("Netscape") != -1) && ((navigator.userAgent.indexOf("6.") != -1) || (navigator.userAgent.indexOf("7.") != -1)))
{ 
	NN6_BROWSER = true;
} 

if ((navigator.appVersion.indexOf("MSIE 5.") != -1) || (navigator.appVersion.indexOf("MSIE 6.") != -1))
{ 
	IE5_BROWSER = true;
} 

// --- END BROWSER DETECTION CODE

// GLOBAL VARIABLES THAT CAN BE RESET IN THE PAGE
var g_intMenuWidth = 200; // width of drop down menu
var g_intMenuHeight = 20; // Height of each menu item (inc. level 1)
var g_intMenuTop = 81; // Top of navigation structure (level 1)
var g_intLeftOffset = 0; // Offset from left of screen
var g_intMaxWidth = 750; // Maximum width allowed (reset to width of browser below)
var g_intNavWidth = "600"; // Maximum width of navigation bar
var g_intContentWidth = 750; // Width of the content area which maybe wider than navigation (g_intNavWidth)
var g_booCenter = true; // Is navigation centered in page


var g_intCurrMenuID;
var g_intCurrItemID;

var g_objTimeout;

var g_strRootSectionPCode; // Root section homepage code
var g_strCurrSectionPCode; // Current section homepage code
var g_strCurrPageCode; // Current page code


function BuildNavigation(intVersionID)
{	
	// Build and output menu items
	var strNavL1 = "";

	strNavL1 = strNavL1 + "<table width=\"" + g_intNavWidth + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";	
	strNavL1 = strNavL1 + "<tr>";
	
	var arrTopLevel = eval("arrTopLevel"+intVersionID);
	
	// Loop the top levels
	for(i = 0; i < arrTopLevel.length; i++)
	{
		strClass = "mNavL1";
		intItemID = i;
		
		// If section is there a redirect or use the assigned page
		if (arrTopLevel[i][0] == "s")
		{
			if (arrTopLevel[i][6] != "")
				strUrl = arrTopLevel[i][6];
			else
				strUrl = "Page.asp?" + arrTopLevel[i][2];
		}
		else
			strUrl = "Page.asp?" + arrTopLevel[i][2];
		
		if (g_strRootSectionPCode == arrTopLevel[i][2])
		{
			strClass = "mNavL1x";
			intItemID = null;
		}
	
		strNavL1 = strNavL1 + "<td id=\"nav_l1_" + i + "\" align=\"left\" valign=\"middle\" nowrap class=\"" + strClass + "\"";
		
		// onMouseOver
		strNavL1 = strNavL1 + " onMouseOver=\"ShowMenu(" + arrTopLevel[i][4] + ", " + intItemID + ");\"";
		
		// onMouseOut
		strNavL1 = strNavL1 + " onMouseOut=\"HideMenu(false);\"";		
		
		// onClick
		strNavL1 = strNavL1 + " onClick=\"LoadPage('" + strUrl + "');\">";	
		
		// Item name
		strNavL1 = strNavL1 + "  " 
		strNavL1 = strNavL1 + arrTopLevel[i][1];
		strNavL1 = strNavL1 + "  ";
		strNavL1 = strNavL1 + "</td>";
		
		// Divider
		strNavL1 = strNavL1 + "<td width=\"1\" class=\"mNavL1div\">";
		strNavL1 = strNavL1 + "<img src=../index_files//%22images/spacer.gif/%22 width=\"1\" height=\"1\" border=\"0\">";
		strNavL1 = strNavL1 + "</td>";
	}
	
	strNavL1 = strNavL1 + "<td width=\"100%\" height=\"" + g_intMenuHeight + "\" class=\"mNavL1\">";
	strNavL1 = strNavL1 + "</td>";
	strNavL1 = strNavL1 + "</tr>";
	strNavL1 = strNavL1 + "</table>";
	
	// Write to browser
	document.write(strNavL1);
	
	// Now create the drop down menus
	if ((IE5_BROWSER) || (NN6_BROWSER))
		BuildMenus(intVersionID);
}


function BuildMenus(intVersionID)
{	
	var arrTopLevel = eval("arrTopLevel"+intVersionID);

	// Build drop down menus
	var intTotalWidth = 0;
	var strMenus = "";
	
	var intBrowserWidth = document.body.clientWidth;
	
	if (g_intMaxWidth == 0)
		g_intMaxWidth = intBrowserWidth;
	
	// Calculate the browser offset if menus are centralised
	if (g_booCenter)
		g_intLeftOffset = ((intBrowserWidth - g_intContentWidth)/2)+g_intLeftOffset;

	// Loop the top levels
	for(i = 0; i < arrTopLevel.length; i++)
	{	
		// Top navigation can include pages/sections
		intTop = g_intMenuTop + g_intMenuHeight +1;
		intLeft = g_intLeftOffset + intTotalWidth;

		intLevel1Width = document.getElementById("nav_l1_"+i).offsetWidth;

		// Will this exceed the available space?
		if ((intLeft + g_intMenuWidth) > g_intMaxWidth)
		{
			intLeft = (intLeft + intLevel1Width +1)-g_intMenuWidth;
		}
		
		// Get the total width of level 1 items by adding width of each (+1 is the divider)
		intTotalWidth = intTotalWidth + intLevel1Width +1;
	
		// If it's a section then continue to loop any children
		if (arrTopLevel[i][0] == "s")
		{
			intMenuID = arrTopLevel[i][4];
			intItemID = i;
			
			if (g_strRootSectionPCode == arrTopLevel[i][2])
				intItemID = null;
				
			var arrLevel = eval("arrLevel"+intVersionID);
			
			if (arrLevel[intMenuID])
			{
				if (arrLevel[intMenuID].length > 0)
				{							
					strMenus = strMenus + "<div id=\"menu" + intMenuID + "\" ";
					strMenus = strMenus + "style=\"position: absolute; visibility: hidden; display: none; ";
					strMenus = strMenus + "width: " + g_intMenuWidth + "px; ";
					strMenus = strMenus + "left: " + intLeft + "px; ";
					strMenus = strMenus + "top: " + intTop + "px;\"";
					strMenus = strMenus + ">";
					strMenus = strMenus + "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";

					for(j = 1; j < arrLevel[intMenuID].length; j++)
					{				
						strClass = "mNavL2";
						booCurr = false;
						
						// Is this page/section the current one?
						if ((g_strCurrSectionPCode == arrLevel[intMenuID][j][2]) || (g_strCurrPageCode == arrLevel[intMenuID][j][2]))
						{
							strClass = "mNavL2x";
							booCurr = true;
						}		
						
						// If section is there a redirect or use the assigned page
						if (arrLevel[intMenuID][j][0] == "s")
						{
							if (arrLevel[intMenuID][j][5] != "")
								strUrl = arrLevel[intMenuID][j][5];
							else
								strUrl = "Page.asp?" + arrLevel[intMenuID][j][2];
						}
						else
							strUrl = "Page.asp?" + arrLevel[intMenuID][j][2];	
						
						strMenus = strMenus + "<tr class=\"" + strClass + "\" ";
						
						strMenus = strMenus + " onMouseOver=\"ShowMenu(" + intMenuID + ", " + intItemID + ");";
						if (booCurr == false)
							strMenus = strMenus + "this.className='mNavL2x';";
						strMenus = strMenus + "\"";
						
						strMenus = strMenus + " onMouseOut=\"HideMenu(false);";
						if (booCurr == false)
							strMenus = strMenus + "this.className='mNavL2';";
						strMenus = strMenus + "\">";
						
						strMenus = strMenus + "<td><img src=../index_files//%22images/spacer.gif/%22 width=\"5\" height=\"1\" border=\"0\"></td>";							
						
						strMenus = strMenus + "<td height=\"" + g_intMenuHeight + "\" align=\"left\" valign=\"middle\" width=\"100%\"";
						strMenus = strMenus + " class=\"" + strClass + "\"";
					
						if (booCurr == false)
							strMenus = strMenus + " onMouseOver=\"this.className='mNavL2x';\"";

						if (booCurr == false)
							strMenus = strMenus + " onMouseOut=\"this.className='mNavL2';\"";						
						
						strMenus = strMenus + " onClick=\"LoadPage('" + strUrl + "');\">" + arrLevel[intMenuID][j][1] + "</td>";
						
						strMenus = strMenus + "<td><img src=../index_files//%22images/spacer.gif/%22 width=\"5\" height=\"1\" border=\"0\"></td>";	
						
						strMenus = strMenus + "</tr><tr><td colspan=\"3\" class=\"mNavL2div\">";
						strMenus = strMenus + "<img src=../index_files//%22images/spacer.gif/%22 width=\"1\" height=\"1\" border=\"0\">";
						strMenus = strMenus + "</td></tr>";				
					}
				
					strMenus = strMenus + "</table>";
					strMenus = strMenus + "</div>";
				}
			}
		}
	}	

	// Write menus to the browser
	document.write(strMenus);
}


function LoadPage(strUrl)
{
	// Load selected page
	if (strUrl != "")
		document.location.href = strUrl;
}


function ShowMenu(intMenuID, intItemID)
{
	// Show selected menu
	if (NON_BROWSER == false)
	{
		if (g_intContentWidth < document.body.clientWidth)
		{
			// Stop any timeout
			clearTimeout(g_objTimeout);
			
			// Hide any existing menus/revert level 1 items
			if (g_intCurrMenuID >= 0)
			{
				// Change the style of the main level 1 item
				if (g_intCurrItemID != null)
					document.getElementById("nav_l1_"+g_intCurrItemID).className = "mNavL1";		
			
				// Hide the menu
				var strMenuName = "menu"+g_intCurrMenuID;
				if (DoesMenuExist(strMenuName)) HideLayer(strMenuName);
			}
		
			// Change the style of the main level 1 item
			if (intItemID != null)
				document.getElementById("nav_l1_"+intItemID).className = "mNavL1x";
		
			// Show the menu if there is one
			var strMenuName = "menu"+intMenuID;
			if (DoesMenuExist(strMenuName)) ShowLayer(strMenuName);
			
			// Set global variables
			g_intCurrMenuID = intMenuID;
			g_intCurrItemID = intItemID;
		}
	}
}


function ShowLayer(strMenuName)
{
	// Show the layer that has been passed in
	if (NON_BROWSER == false)
	{
		document.getElementById(strMenuName).style.visibility = "visible";
		document.getElementById(strMenuName).style.display = "block";
	}
}


function HideMenu(booHide)
{
	if (NON_BROWSER == false)
	{
		if (booHide == false)
		{
			// Set the timeout for this function call
			g_objTimeout = setTimeout("HideMenu(true)", 1000);		
			// Timeout is now true as one exists
			//gBOOTIMEOUT = true;
		}
		else
		{
			// Change the style of the main level 1 item
			if (g_intCurrItemID != null)
				document.getElementById("nav_l1_"+g_intCurrItemID).className = "mNavL1";		
		
			// Hide the menu
			var strMenuName = "menu"+g_intCurrMenuID;
			if (DoesMenuExist(strMenuName)) HideLayer(strMenuName);
		}
	}
}


function HideLayer(strMenuName)
{
	// Show the layer that has been passed in
	if (NON_BROWSER == false)
	{
		document.getElementById(strMenuName).style.visibility = "hidden";
		document.getElementById(strMenuName).style.display = "none";
	}
}


function DoesMenuExist(strMenuName)
{
	// Check to see if the menu layer exists
	var booExists = false;
	
	if (NN_BROWSER)
	{
		if (document.layers[strMenuName]) booExists = true;
	}
	else
	{
		if (document.getElementById(strMenuName)) booExists = true;
	}
	
	return booExists;
}


function BuildFooterNav(intVersionID)
{	
	// Build and output footer items
	
	var arrTopLevel = eval("arrTopLevel"+intVersionID);
	
	var strFooter = "";
	
	// Loop the top levels
	for(i = 0; i < arrTopLevel.length; i++)
	{	
		strFooter = strFooter + "<a href=\"" + arrTopLevel[i][2] + "\">";	
		strFooter = strFooter + arrTopLevel[i][1];
		strFooter = strFooter + "</a>"
		
		if (i != arrTopLevel.length-1)
			strFooter = strFooter + " | "
	}
	
	// Write to browser
	document.write(strFooter);
}


function ChangeVersion(intVersionID)
{
	document.location.href = "Page.asp?sve=" + intVersionID;
}


