//Created 21st February 2008.
//Used for rollovers and drop down menus.

var arrInImages = new Array();
var arrMenus = new Array();
var arrMenuNames = new Array();
var arrOutImages = new Array();
var blnInitialised = false;
var numTimerId = null;

var numTimerButtonId = null;
var numTimerMenuId = null;

var numOffsetLayout;
var numScreenHeight;
var numScreenWidth;
var strAltTemp = new String("");
var strButtonPrevious = new String("");
var strMenuActive = new String("");


function checkAdjacentSubMenuItem(strPrevious, strPresent)
{
	var blnResult = false;
	
	//Get the main menu of the previous menu option.
	var imgPrevious = document.images[strButtonPrevious];
	var objPreviousSubMenu = imgPrevious.parentNode.parentNode;
	var strSubMenu = objPreviousSubMenu.id;
	var blnSubMenu = isMenuItem(strSubMenu, "sub");
	
	if (blnSubMenu)
	{
		var strPreviousMainOption = getMenuParent(strSubMenu);
		var imgPreviousMainOption = document.images[strPreviousMainOption];
		var objPreviousMainMenu = imgPreviousMainOption.parentNode.parentNode;
		var strPreviousMainMenu = objPreviousMainMenu.id;
		
		//Get the main menu of the present menu option.
		var imgPresent = document.images[strPresent];
		var objPresentMainMenu = imgPresent.parentNode.parentNode;
		var strPresentMainMenu = objPresentMainMenu.id;
		
		var blnResult = strPreviousMainMenu == strPresentMainMenu;
	}
	
	//Turn off the sub-menu.
	if (blnResult) 
	{
		lightButton(strPreviousMainOption, false);
		showDropDown(strSubMenu, false);
	}
}

function DropDownMenu(numLeft, numTop, numWidth, numHeight)
//Constructor for the class DropDownMenu.
{
	this.left = numLeft;
	this.top = numTop;
	this.width = numWidth;
	this.height = numHeight;
}
				
function getMenuBackgroundName(strMenu)
{
	var arrTest = new Array("main", "sub");
	var strName = new String();

	if (strMenu != null)
	{
		for (numElement in arrTest)
		{
			var strTest = arrTest[numElement];
			blnFound = strMenu.indexOf(strTest) > -1;
			
			if (blnFound)
			{
				var arrSubString = strMenu.split(strTest);
				var strName = "back" + arrSubString[1];
				break;
			}
		}
	}
	
	return strName;
}

function getMenuOffset()
{
	//Calculate the offset for the menus.
	numOffsetLayout = (numScreenWidth - 1001);
	if (numOffsetLayout < 0) numOffsetLayout = 0;
	numOffsetLayout = numOffsetLayout / 2;
	numOffsetLayout = Math.floor(numOffsetLayout);
}

function getMenuParent(strMenu)
//Need to find out the parent menu or button of the specified menu.
{
	var blnFound = false;
	var strParent = new String("");
	var strValue = new String();

	for (strName in arrMenuNames)
	{
		strValue = arrMenuNames[strName];
		blnFound = strMenu == strValue;
		if (blnFound) break;
	}

	if (blnFound) strParent = strName;
	return strParent;
}

function getParentMenuName(objMenu)
//Need to find out the parent menu of the specified menu.
{
	var blnFound = false;
	var strId = objMenu.id;
	var strValue = new String();

	for (strName in arrMenuNames)
	{
		strValue = arrMenuNames[strName];
		blnFound = strId == strValue;
		if (blnFound) break;
	}
	
	return strName;
}

function getRolloverImageName(strImage)
//Get the file name of the rollover image.
{
	posPeriod = strImage.lastIndexOf(".");
	strName = strImage.substring(0, posPeriod);
	strExtension = strImage.substr(posPeriod, 4);

	return strName + "-over" + strExtension;
}

function getScreenAreaDimensions()
{
	var objDocument = document.documentElement;
	
	if (objDocument.clientWidth != null)
	{
		numScreenHeight = objDocument.clientHeight;
		numScreenWidth = objDocument.clientWidth;		
	}
}

function getStyleSheetObject(strTitle)
//Style sheet collection can only accept numeric indices so test on title attribute.
{
	for (numElement in document.styleSheets)
	{
		var objSheet = document.styleSheets[numElement];
		var blnFound = objSheet.title == strTitle;
		if (blnFound) break;
	}

	return objSheet;	
}

function hasSubMenu(strButton)
//Returns true if the menu option has a sub menu.
{
	var blnFound = false;
	var blnOption = false;
	var strTest = strButton.substr(0, 3);
	var strTest = strTest.toLowerCase();
	blnOption = strTest == "opt";
	
	if (blnOption)
	{
		for (strName in arrMenuNames)
		{
			blnFound = strButton == strName;
			if (blnFound) break;
		}
	}
	
	return blnFound
}

function hideParents(strButton)
//Hides all the parent menus and buttons.
{
	var blnButton = isMenuItem(strButton, "btn");
	
	while (!blnButton)
	{
		var img = document.images[strButton];
		var objParentMenu = img.parentNode.parentNode;
		var strParent = objParentMenu.id;
		showDropDown(strParent, false);
		strParent = getMenuParent(strParent);
		lightButton(strParent, false);
		
		blnButton = isMenuItem(strParent, "btn");
		strButton = strParent;
	}
}

function initialiseMenus()
//Initialises the left position for each of the menu items in the header menu bar.
{
	arrMenus["main-about-tl"] = new DropDownMenu(805, 121, 120, 110);
	arrMenus["main-banking"] = new DropDownMenu(204, 119, 111, 67);
	arrMenus["main-companies"] = new DropDownMenu(83, 122, 175, 82);
	arrMenus["main-credit-repair"] = new DropDownMenu(445, 116, 127, 55);
	arrMenus["main-miscellaneous"] = new DropDownMenu(686, 118, 102, 39);
	arrMenus["main-trade-marks"] = new DropDownMenu(324, 117, 119, 110);
	arrMenus["main-trusts"] = new DropDownMenu(564, 117, 129, 96);
	arrMenus["sub-banking-offshore"] = new DropDownMenu(314, 133, 142, 68);
	arrMenus["sub-banking-uk"] = new DropDownMenu(314, 119, 142, 68);
	arrMenus["sub-internet"] = new DropDownMenu(787, 132, 153, 96);
	arrMenus["sub-offshore"] = new DropDownMenu(257, 136, 121, 236);
	arrMenus["sub-uk-company"] = new DropDownMenu(257, 122, 127, 68);
}

function initialiseMenuNames()
//Need to initialse the menu names indexed by the name of the image.
{
	arrMenuNames["btnAboutTurnerLittle"] = "main-about-tl";
	arrMenuNames["btnBanking"] = "main-banking";
	arrMenuNames["btnCompany"] = "main-companies";
	arrMenuNames["btnCreditRepair"] = "main-credit-repair";
	arrMenuNames["btnOtherServices"] = "main-miscellaneous";
	arrMenuNames["btnTradeMark"] = "main-trade-marks";
	arrMenuNames["btnTrusts"] = "main-trusts";
	arrMenuNames["optInternetServices"] = "sub-internet";
	arrMenuNames["optOffshoreBanking"] = "sub-banking-offshore";
	arrMenuNames["optOffshoreCompany"] = "sub-offshore";
	arrMenuNames["optUKBanking"] = "sub-banking-uk";
	arrMenuNames["optUKCompany"] = "sub-uk-company";
}

function IsButtonSameType(strButtonNew, strButtonPrevious)
//Returns true if if moving from a menu button to another mennu button.
//Also returns true if moving from a menu option to another menu option.
{
	var blnSame = false;

	var strTestNew = strButtonNew.substr(0, 3);
	var strTestPrevious = strButtonPrevious.substr(0, 3);
	
	strTestNew = strTestNew.toLowerCase();
	strTestPrevious = strTestPrevious.toLowerCase();
	blnSame = strTestPrevious == strTestNew;	

	return  blnSame;
}

function isChildMenuItem(strButtonParent, strButtonChild)
//Returns true if the second image is a sub menu item called by the first menu item.
{
	var blnChild = false;
	var strSubMenuId = arrMenuNames[strButtonParent];

	if (strSubMenuId != null)
	{
		var objChildMenu = document.images[strButtonChild];
		var objMenu = objChildMenu.parentNode.parentNode;
		blnChild = objMenu.id == strSubMenuId;
	}

	return blnChild;
}

function isChildMenuVisible(img)
//Returns true if the present menu or button has a child menu.
{
	var blnVisible = false;
	strImageName = img.name
	var strMenu = arrMenuNames[strImageName];
	obj = document.getElementById(strMenu);

	blnChildMenu = obj != null;
	if (blnChildMenu) blnVisible =  obj.style.display == "block";
	
	return blnVisible;
}

function isMenuItem(strName, strImageType)
{
	var strTest = strName.substr(0, 3);
	var blnResult = strTest == strImageType;
	
	return blnResult;
}

function isMenuItemFirst(strMenu, strMenuItem)
//Returns true if it is the top menu item.
{
	var objMenu = document.getElementById(strMenu);
	var numNode1 = objMenu.childNodes.length;
	
	for (var numLoop1 = 0; numLoop1 < numNode1; numLoop1++)
	{
		var objLink = objMenu.childNodes[numLoop1];
		
		if (objLink.nodeName == "A")
		{
			numNode2 = objLink.childNodes.length;
			
			for (var numLoop2 = 0; numLoop2 < numNode2; numLoop2++)
			{
				objLinkImage = objLink.childNodes[numLoop2];
				if (objLinkImage.nodeName == "IMG") break;
			}
			
			break;
		}
	}

	var strLinkName = objLinkImage.name;

	return strLinkName == strButtonPrevious;
}

function isMenuItemLast(strMenu, strMenuItem)
//Returns true if it is the top menu item.
{
	var objMenu = document.getElementById(strMenu);
	var numNode1 = objMenu.childNodes.length - 1;
	
	for (var numLoop1 = numNode1; numLoop1 >= 0; numLoop1--)
	{
		var objLink = objMenu.childNodes[numLoop1];
		
		if (objLink.nodeName == "A")
		{
			numNode2 = objLink.childNodes.length;
			
			for (var numLoop2 = 0; numLoop2 < numNode2; numLoop2++)
			{
				objLinkImage = objLink.childNodes[numLoop2];
				if (objLinkImage.nodeName == "IMG") break;
			}
			
			break;
		}
	}
	
	var strLinkName = objLinkImage.name;

	return strLinkName == strButtonPrevious;
}

function light(img, blnHover, blnDropDown)
//Controls the rollover for the button.
{
	if (!blnInitialised) return;
	var strImage = img.name;

	//Need to test for the visible alt attribute for blanking in Internet Explorer.	
	var blnAlt = blnBrowserExplorer && numBrowserVersion >= 4;

	var blnButton = isMenuItem(strImage, "btn");
	var blnSameType = IsButtonSameType(strImage, strButtonPrevious)

	//In case the Button In images have not been preloaded.

	//Need to know the name of any drop-down menus where appropriate.
	strMenu = null;
	if (blnDropDown) strMenu = arrMenuNames[strImage];
	
	if (blnHover)
	{
		if (strButtonPrevious != "")
		{
			var blnButtonPrevious = isMenuItem(strButtonPrevious, "btn");
			var strMenuPrevious = arrMenuNames[strButtonPrevious];
			
			if (blnSameType)
			{
				var blnHasSubMenu = hasSubMenu(strButtonPrevious);
				var blnChildItem = isChildMenuItem(strButtonPrevious, strImage);
				
				//This code is executed unless the user moves onto the first option of a sub-menu.
				if (!(blnHasSubMenu && blnChildItem))
				{
					//Need to make sure that the user is not returning to the drop down menu
					//from an option on a sub-menu.
					if (!blnButton) checkAdjacentSubMenuItem(strButtonPrevious, strImage);
					
					//Turn off the previous button to prevent the linger effect.
					lightButton(strButtonPrevious, false);
					if (strMenuPrevious != null) showDropDown(strMenuPrevious, false);
					strButtonPrevious = "";
				}
			}
			else if (!blnButtonPrevious && blnButton)
			{
				//Special case of leaving the first menu option for the menu button above it.
				lightButton(strButtonPrevious, false);
				hideParents(strButtonPrevious);
				if (strMenuPrevious != null)  showDropDown(strMenuPrevious, false);
			}

			//Moving from a menu button to a drop down menu.
			window.clearTimeout(numTimerId);				
		} //strButtonPrevious != null
			
		lightButton(strImage, true);

		if (blnAlt)
		{
			strAltTemp = img.alt;
			img.alt = "";
		}
		
		if (blnDropDown)
		{
			//Show the drop down menu.
			showDropDown(strMenu, blnHover);
			strMenuActive = strMenu;
		}
	} //blnHover = true
	else
	{
		//Set the timers to remove the menu items.
		if (blnAlt) img.alt = strAltTemp;
		setMenuTimers(strImage, blnDropDown, strMenu);
	}
}

function lightButton(strImage, blnHover)
//Lights or unlights the respective button.
{
	var img = document.images[strImage];

	if (blnHover)
	{
		img.src = arrInImages[strImage].src;
	}
	else
	{
		img.src = arrOutImages[strImage].src;
	}
}

function lightTimeout(strImage, blnDropdown, strMenu)
//The action carried out when the timers reach the alloted interval.
{
	lightButton(strImage, false);
	if (blnDropdown) showDropDown(strMenu, false);
	numTimerId = null;
	hideParents(strImage);
}

function loadImages()
//Preloads the images for the rollover buttons.
{
	var images = document.images

	if (images && !blnInitialised)
	{
		var numImage = images.length;

		for (var numLoop = 0; numLoop < numImage; numLoop++)
		{		
			//See if the image is a hyperlink.
			var img = document.images[numLoop];
			var strImagePath = img.src;
			var strName = img.name;

			//See if the image is a button or menu option.
			var blnRollover = (strName.indexOf("btn") == 0) || (strName.indexOf("opt") == 0)

			if (blnRollover)
			{
				arrInImages[strName] = new Image;
				arrOutImages[strName] = new Image;
				
				arrOutImages[strName].src = strImagePath;
				arrInImages[strName].src = getRolloverImageName(strImagePath);
			}
		}
	}	
}

function setMenuStyleRules()
//For IE versions 5.5 and 6, the rules for the background are rewritten for the image filter.
{
	//Need to get the style sheet object.
	var objSheet = getStyleSheetObject("Menu Background");
	
	//Replace all occurences of background-image with the filter declaration.
	var regex1 = /background\-image: url\(/g;
	var strCSS = objSheet.cssText;
	var strCSS = strCSS.toLowerCase();
	var strReplace = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=";
	var strNew = strCSS.replace(regex1, strReplace);
	objSheet.cssText = strNew
}

function setMenuTimers(strImage, blnDropDown, strMenu)
//Sets the timers for the menus and the buttons to be turned off.
{
	var numInterval = 200;
	strButtonPrevious = strImage;
	strDropDown = (blnDropDown) ? "true" : "false";
	var strFunction = "lightTimeout('" + strImage + "', " + strDropDown + ", '" + strMenu + "')";
	numTimerId = window.setTimeout(strFunction, numInterval);
}

function showDropDown(strMenu, blnHover)
{
	var strBackground = getMenuBackgroundName(strMenu);
	var objMenuBackground = document.getElementById(strBackground);
	var objMenuFrame = document.getElementById(strMenu);
	var objMenu2 = arrMenus[strMenu];
	var strVisibility = (blnHover) ? "block" : "none";

	getScreenAreaDimensions();
	getMenuOffset();
	showMenuDiv(objMenuFrame, objMenu2, strVisibility);
	showMenuDiv(objMenuBackground, objMenu2, strVisibility);
	//checkMenuParent(objMenuFrame, blnHover);
}

function showDropDownMenu(strMenu, blnHover)
//Do Nothing.
{
	if (blnHover)
	{
		//Do not want to be cancelling the timer if it is the last image in the menu.
		if (strButtonPrevious != null)
		{
			var blnMenuTop = isMenuItemFirst(strMenu, strButtonPrevious);
			var blnMenuBottom = isMenuItemLast(strMenu, strButtonPrevious);
		}

		if (!(blnMenuTop || blnMenuBottom))
		{
			window.clearTimeout(numTimerId);
			numTimerId = null;
		}
	}
	else
	{
		var blnPreviousButton = isMenuItem(strButtonPrevious, "btn");

		if (blnPreviousButton)
			setMenuTimers(strButtonPrevious, true, strMenu);
	}
}

function showMenuDiv(objMenu1, objMenu2, strVisibility)
//The actual process of making the div tag for the menu visible, or invisible, as the case may be.
{
	if (objMenu1 != null)
	{
		with (objMenu1.style)
		{
			//Need to calculate the offset for the menu based on the screen size.
			numLeft = objMenu2.left;
			numLeft += numOffsetLayout;

			left = numLeft + "px";
			top = objMenu2.top + "px";
			width = objMenu2.width + "px";
			height = objMenu2.height + "px";
			display = strVisibility;
		}
	}
}


