// All the JavaScript code for the fly-out side menu is
// included in this neat little package.

// Global variables.  Mucho importante.
var aMenu;
var aMenuImages;

var bMenuArrayInit = false;
var bMenuImagesInit = false;
var bMainMenuDrawn = false;

var iCaptureLayerWidth = 400;

var iLayoutWidth = 669;		// Width of the content.
var iMainMenuTop = 183;		// Never changes.
var iMainMenuLeft = 50;		// Left edge of the main menu,
							// measured from the edge of the content
var iSubMenuLeft = 148;		// Left edge of the sub menu,
							// measured from the edge of the content
							
var iOffsetLeft;			// The amount the menus need to be offset 
							// in order to be in the correct position
							
var sOpenSubId = '';		// The id of the open sub menu (if any)
var sMenuImageNameRoot = 'mnuImg';


// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//
// Functions to control the main menu images
// 
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

// Function to initialise the menu images.
// Arguments:
//		None.
//
// Returns:
//		None.
function initMenuImages()
{
	var iLoop;
	var oImg;
	
	if (!bMenuArrayInit)
		initMenuArray();
		
	aMenuImages = new Array();
	
	for (iLoop = 0; iLoop < aMenu.length; iLoop++)
	{
		oImg = new Image;
		oImg.src =  aMenu[iLoop].imageOut;
		aMenuImages[aMenuImages.length] = oImg;
		
		oImg = new Image;
		oImg.src =  aMenu[iLoop].imageOver;
		aMenuImages[aMenuImages.length] = oImg;
	}
	
	bMenuImagesInit = true;
}

// Function to swap the menu images.
// Arguments:
//		sImgName:			the name of the image
//		iMenuArrayIndex:	the menu item's index
//							in the menu array
//		bOver:				mouseOver (true) or mouseOut?
//
// Returns:
//		None.
function swapMenuImage(sImgName, iMenuArrayIndex, bOver)
{
	var oImg;
	
	// Dumbass NN4 won't recognise images written on the fly,
	// so can't swap them.  
	if ((document.layers) || (!document.images))
		return false;

	if ((iMenuArrayIndex < 0) || (iMenuArrayIndex > aMenu.length))
		return false;
		
	oImg = document.images[sImgName];
	oImg.src = bOver ? aMenu[iMenuArrayIndex].imageOver : aMenu[iMenuArrayIndex].imageOut;
}	



// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//
// Functions to construct the menu in code, and render it
// dynamically.
// 
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

// Constructor function for the main menu items.
// Arguments:
//		sImgOut:		the path/name of the mouseOut image
//		sImgOver:		the path/name of the mouseOver image
//		iImageWidth:	width of the image
//		iImageHeight:	height of the image
//		sAltText:		alt text for the image
//		sHref:			the link for the menu item
//
// Returns:
//		A reference to the new menu item.
function menuItem(sImgOut, sImgOver, iImgWidth, iImgHeight, sAltText, sHref)
{
	this.imageOut = sImgOut;
	this.imageOver = sImgOver;
	this.imageWidth = iImgWidth;
	this.imageHeight = iImgHeight;
	this.altText = sAltText;
	this.href = sHref;
	this.subMenu = null;
	
	return this;
}

// Constructor function for the sub menu items.
// Arguments:
//		sMenuText:		the menu text to display
//		sHref:			the link for the menu item
//		sStyle:			class name for the link or 'NULL'
//
// Returns:
//		A reference to the new sub menu item.
function subMenuItem(sMenuText, sHref, sStyle)
{
	this.menuText = sMenuText;
	this.href = sHref;
	this.style = sStyle;
}

// Function to initialise the menu array.
// Arguments:
//		None.
//
// Returns:
//		None.
function initMenuArray()
{
	aMenu = new Array();

	aMenu[0] = new menuItem('images/mnu/review_off.gif', 'images/mnu/review_on.gif', 103, 24, 'Full Financial Review', 'contractor_financial_review.html');
	
	aMenu[1] = new menuItem('images/mnu/mortgage_off.gif', 'images/mnu/mortgage_on.gif', 103, 24, 'Mortgages Section', 'contractormortgages.html');
	aMenu[1].subMenu = new Array();
	aMenu[1].subMenu[0] = new subMenuItem('Contractor Mortgages', 'contractormortgages.html', 'subMenu');
	aMenu[1].subMenu[1] = new subMenuItem('Mortgage Calculator', 'mortgagecalculator.html', 'subMenu');
	aMenu[1].subMenu[2] = new subMenuItem('Online Mortgage Finder', 'contractormortgage.html', 'subMenu');
	aMenu[1].subMenu[3] = new subMenuItem('Remortgage Finder', 'remortgage.html', 'subMenu');

	aMenu[2] = new menuItem('images/mnu/phi_off.gif', 'images/mnu/phi_on.gif', 103, 24, 'PHI', 'phi.html');
	aMenu[2].subMenu = new Array();
	aMenu[2].subMenu[0] = new subMenuItem('Contractor Income Protection', 'phi.html', 'subMenu');
	aMenu[2].subMenu[1] = new subMenuItem('Online Income Protection Finder', 'contractorincomeprotection.html', 'subMenu');
	
	aMenu[3] = new menuItem('images/mnu/pensions_off.gif', 'images/mnu/pensions_on.gif', 103, 24, 'Pensions', 'pensions.html');
	aMenu[3].subMenu = new Array();
	aMenu[3].subMenu[0] = new subMenuItem('Contractor Pensions', 'pensions.html', 'subMenu');
	aMenu[3].subMenu[1] = new subMenuItem('Pensions Simplification', 'pensionsimplification.html', 'subMenu');
	aMenu[3].subMenu[2] = new subMenuItem('Online Pension Finder', 'contractorpension.html', 'subMenu');
	
	aMenu[4] = new menuItem('images/mnu/criticalillness_off.gif', 'images/mnu/criticalillness_on.gif', 103, 24, 'Critical Illness', 'critical_illness.html');
	aMenu[4].subMenu = new Array();
	aMenu[4].subMenu[0] = new subMenuItem('Critical Illness Cover', 'critical_illness.html', 'subMenu');
	aMenu[4].subMenu[1] = new subMenuItem('Critical Illness Finder', 'critical_illness_finder.html', 'subMenu');
	
	aMenu[5] = new menuItem('images/mnu/lifecover_off.gif', 'images/mnu/lifecover_on.gif', 103, 24, 'Life Cover', 'lifecover.html');
	aMenu[5].subMenu = new Array();
	aMenu[5].subMenu[0] = new subMenuItem('Contractor Life Cover', 'lifecover.html', 'subMenu');
	aMenu[5].subMenu[1] = new subMenuItem('Online Life Cover Finder', 'online_lifecover.html', 'subMenu');
	
	aMenu[6] = new menuItem('images/mnu/med_off.gif', 'images/mnu/med_on.gif', 103, 24, 'ISAs', 'contractorhealthinsurance.html');
	
	aMenu[7] = new menuItem('images/mnu/isa_off.gif', 'images/mnu/isa_on.gif', 103, 24, 'ISAs', 'isa.html');
	
	aMenu[8] = new menuItem('images/mnu/will_off.gif', 'images/mnu/will_on.gif', 103, 24, 'ISAs', 'contractorwillservice.html');

	

	aMenu[9] = new menuItem('images/mnu/banking_off.gif', 'images/mnu/banking_on.gif', 103, 24, 'Banking', 'banking.html');
	
	aMenu[10] = new menuItem('images/mnu/sitemap_off.gif', 'images/mnu/sitemap_on.gif', 103, 24, 'Site Map', 'itcontractormortgage.html');
	


	
	
	
	
	
		
	
	
	
	
		
	
	
	
	bMenuArrayInit = true;
}

// Function to render the main menu.
// Arguments:
//		None.
//
// Returns:
//		None.
function renderMainMenu()
{
	var iLoop;
	var sHTML = '';
	var sImageName;
	
	if (!bMenuArrayInit)
		initMenuArray();
	
	if (document.layers)
		sHTML += '<layer id="layMainMenu" position="absolute" left="3" top="' + iMainMenuTop + '" width="103" visibility="show" z-index="10">\n';
	else
		sHTML += '<div id="layMainMenu" style="position: absolute; left: 3px; top: ' + iMainMenuTop + 'px; width: 103px; visibility: visible; z-index: 10">\n';
		
	sHTML += '	<table width="103" cellspacing="0" cellpadding="0" border="0">\n';
	
	for (iLoop = 0; iLoop < aMenu.length; iLoop++)
	{
		sImageName = sMenuImageNameRoot + iLoop.toString();
	
		sHTML += '		<tr align="left" valign="top">\n';
		sHTML += '			<td bgcolor="#999999"><a href="' + aMenu[iLoop].href + '" onMouseOver="swapMenuImage(\'' + sImageName + '\', ' + iLoop + ', true);';
		
		if (aMenu[iLoop].subMenu != null)
			sHTML += ' openSub(\'laySub' + iLoop + '\');"';
		else
			sHTML += ' closeSub();"';
		
		sHTML += ' onMouseOut="swapMenuImage(\'' + sImageName + '\', ' + iLoop + ', false);"><img src="' + aMenu[iLoop].imageOut + '" width="' + aMenu[iLoop].imageWidth + '" height="' + aMenu[iLoop].imageHeight + '" name="' + sImageName + '" border="0" alt="' + aMenu[iLoop].altText + '" /></a></td>\n';
		sHTML += '		</tr>\n';
		
		sHTML += '		<tr align="left" valign="top">\n';
		sHTML += '			<td><img src="images/dot.gif" width="1" height="1" /></td>\n';
		sHTML += '		</tr>\n';
	}

	sHTML += '	</table>\n';
	
	if (document.layers)
		sHTML += '</layer>\n';
	else
		sHTML += '</div>\n';

	document.write(sHTML);
	bMainMenuDrawn = true;
}

// Function to render the sub menus.
// Arguments:
//		None.
//
// Returns:
//		None.
function renderSubMenus()
{
	var iMainLoop, iSubLoop;
	var iSubTop = iMainMenuTop;
	var sHTML;
	
	if (!bMenuArrayInit)
		initMenuArray();
		
	if (!bMainMenuDrawn)
		renderMainMenu();
	
	for (iMainLoop = 0; iMainLoop < aMenu.length; iMainLoop++)
	{
		// Does this menu item have a sub menu
		if (aMenu[iMainLoop].subMenu != null)
		{
			aSubMenu = aMenu[iMainLoop].subMenu;
			
			if (document.layers)
				sHTML = '<layer id="laySub' + iMainLoop + '" position="absolute" left="0" top="' + iSubTop + '" visibility="hide" z-index="10">\n';
			else
				sHTML = '<div id="laySub' + iMainLoop + '" style="position: absolute; left: 0px; top: ' + iSubTop + 'px; visibility: hidden; z-index: 10">\n';
			
			sHTML += '	<table cellspacing="0" cellpadding="1" border="0">\n';
			sHTML += '		<tr align="left" valign="top">\n';
			sHTML += '			<td width="4"><img src="images/dot.gif" width="4" height="1" /></td>\n';
			sHTML += '			<td bgcolor="#999999">\n';
			sHTML += '				<table width="100%" cellspacing="0" cellpadding="3" border="0" bgcolor="#F0F1EB">\n';
			
			// Loop through the sub menu array, creating the layer
			for (iSubLoop = 0; iSubLoop < aSubMenu.length; iSubLoop++)
			{
				sHTML += '					<tr align="left" valign="top">\n';
				sHTML += '						<td><a href="' + aSubMenu[iSubLoop].href + '"';
				if (aSubMenu[iSubLoop].style != 'NULL')
					sHTML += ' class="' + aSubMenu[iSubLoop].style + '"';
					
				sHTML += '>' + aSubMenu[iSubLoop].menuText + '</a></td>\n';
				sHTML += '					</tr>\n';
			}
			
			sHTML += '				</table>\n';
			sHTML += '			</td>\n';
			sHTML += '		</tr>\n';
			sHTML += '	</table>\n';
			
			if (document.layers)
				sHTML += '</layer>\n';
			else
				sHTML += '</div>\n';
			
			document.write(sHTML);
		}
		
		iSubTop += aMenu[iMainLoop].imageHeight + 1;
	}
}

// Function to render the mouse out capture layer.
// Arguments:
//		None.
//
// Returns:
//		None.
function renderMouseCaptureLayer()
{
	var sHTML;
	var iLayerLeft =  iOffsetLeft >= 20 ? iOffsetLeft - 20 : 0;
	
	if (document.layers)
		sHTML = '<layer id="layCaptureMouseOut" left="' + iLayerLeft + '" top="188" visibility="hidden" z-index="2">\n';
	else
		sHTML = '<div id="layCaptureMouseOut" style="position: absolute; left: ' + iLayerLeft + 'px; top: 188px; visibility: hidden; z-index: 2;">\n';

	sHTML += '<table width="100%" cellspacing="0" cellpadding="0" border="0">\n';
	sHTML += '	<tr align="left" valign="top">\n';
	sHTML += '		<td><a href="#" onMouseOver="closeSub();"><img src="images/dot.gif" width="' + iCaptureLayerWidth + '" height="200" border="0" /></a></td>\n';
	sHTML += '	</tr>\n';
	sHTML += '</table>\n';
	
	if (document.layers)
		sHTML += '</layer>\n';
	else
		sHTML += '</div>\n';
		
	document.write(sHTML);
}


// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//
// Utility functions to determine heights, widths and such like
// 
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


// Browser sniff function
// Arguments:
//		None.
//
// Returns:
//		None.
function doBrowserSniff()
{
	if (parseInt(navigator.appVersion) < 4)
		bDHTML = false;
	else
		bDHTML = true;
}

// Function called when the window is resized.
// Arguments:
//		None.
//
// Returns:
//		None.
function windowResize()
{
	if (document.layers)
		window.location.href = window.location.href;
	else
		doPositioning();
}

// Function to position the main menu.
// Arguments:
//		None.
//
// Returns:
//		None.
function doPositioning()
{
	calculateOffsetLeft();
	positionMainMenu();
}


// Function to calculate the position of the content from the
// left edge of the document
// Arguments:
//		None.
//
// Returns:
//		None.
function calculateOffsetLeft()
{
	var iOffset;
	
	// This is a bit of a fudge for NN4 at the moment.  Likely
	// to get worse once we start testing it on a Mac, but can't
	// really be helped.
	if (document.layers)
		iOffset = parseInt(((getDocumentWidth() - iLayoutWidth) / 2) - 9);
	else
		iOffset = parseInt((getDocumentWidth() - iLayoutWidth) / 2);
		
	iOffset < 0 ? iOffsetLeft = 0 : iOffsetLeft = iOffset;
}

// Function to calculate the available width of the window
// (minus chrome and the like).
// Arguments:
//		None.
//
// Returns:
//		The available width in pixels.
function getDocumentWidth()
{
	if (document.layers)
		return parseInt(window.innerWidth);	// NN4 does it the easy way for once...
	else
		return parseInt(document.images['imgMeasureWidth'].width);
}

// Function to position the sub menu.  Called before the
// sub menu is opened.
// Arguments:
//		oLay:	the sub menu layer object
//
// Returns:
//		None.
function positionSubMenu(oLay)
{
	var iLeft = iOffsetLeft + iSubMenuLeft;
	
	if (document.layers)
		oLay.left = iLeft;
	else
		oLay.style.left = iLeft;
}

// Function to position the sub menu.  Called from the
// body onLoad and onResize events.
// Arguments:
//		None.
//
// Returns:
//		None.
function positionMainMenu()
{
	var oLay = getLayerFromId('layMainMenu');
	var iLeft = iOffsetLeft + iMainMenuLeft;
	
	if (document.layers)
		oLay.left = iLeft;
	else
		oLay.style.left = iLeft;
}



// Function to open a sub menu
// Arguments:
//		iIndex:		the index in aMenu
//
// Returns:
//		Nada.
function openSub(sSubId)
{
	var oLay;
	
	// Close any open sub menus
	if (sOpenSubId != '')
	{
		// Already open, depart
		if (sOpenSubId == sSubId)
			return;
		
		closeSub();
	}
	
	if (sSubId != '')
	{
		oLay = getLayerFromId(sSubId);
		positionSubMenu(oLay);
		setLayerVisibility(oLay, true);
		
		oLay = getLayerFromId('layCaptureMouseOut');
		setLayerVisibility(oLay, true);
		
		sOpenSubId = sSubId;
	}
}

// Function to close the sub menu (if any is open)
// Arguments:
//		None.
//
// Returns:
//		Nada.
function closeSub()
{
	var oLay
	
	if (sOpenSubId != '')
	{
		oLay = getLayerFromId(sOpenSubId);
		setLayerVisibility(oLay, false);
		
		oLay = getLayerFromId('layCaptureMouseOut');
		setLayerVisibility(oLay, false);
	
		sOpenSubId = '';
	}
}

// Function to control the visibility of a layer
// Arguments:
//		oLayer:		The layer object.
//		bShow:		Show (true) or hide the layer
//
// Returns:
//		Nada.
function setLayerVisibility(oLayer, bShow)
{
	if (document.layers)
		bShow ? oLayer.visibility = 'show' : oLayer.visibility = 'hide';
	else
		bShow ? oLayer.style.visibility = 'visible' : oLayer.style.visibility = 'hidden';
}

// Function to return a reference to a layer, based
// on the id
// Arguments:
//		The layer id.
//
// Returns:
//		A reference to the layer object.
function getLayerFromId(sId)
{
	var oLayer;
	
	if (document.layers)
		oLayer = document.layers[sId];
	else
	{
		if (document.all)
			oLayer = document.all[sId];
		else
			oLayer = document.getElementById(sId);
	}

		return oLayer;
}