// JavaScript Document

var Menu = new Array();
var showErrors = false;
var backgroundImages = new Array();
var currentBackgroundImageNumber = 0;
var backgroundImage_opacityValue = 100;
var backgroundImage_opacityChangeRate = 5;
var backgroundImage_switchDelay = 15000;
var backgroundImage_transissionDalay = 100;
var mainTableElement;
var interval;
var topMenuElement;
var subMenuElement;
var blurbDivElement;
var backGroundImageCell;
var backGroundImageElement;
var viewportWidth;
var viewportHeight;
var topMenuHover;
var subMenuHover;
var topMenuChoice;
var subMenuChoice;
var useFullPageMenuLoad = false;
var imageCounter = 0;
var getNextImageTimeout;

// Called from the index.php page in the body tag - called every time the page is (re)loaded
function startUp(divOrStack, t, s, lastBackgroundImageNumber)
{
	try
	{		
		currentBackgroundImageNumber = lastBackgroundImageNumber;
	
		var mainPage_status = document.getElementById("mainPage_status");
		if (showErrors && mainPage_status)
		{
			mainPage_status.style.display = "block";
			mainPage_status.style.visibility = "visible";
		}
	
		if (divOrStack == "div")
		{
			useFullPageMenuLoad = false;
		}
		else if (divOrStack == "stack")
		{
			useFullPageMenuLoad = true;
		}
		
		// set some variables to various elements on the main page
		mainTableElement = document.getElementById("mainTableElement");
		topMenuElement = document.getElementById("topMenuElement");
		subMenuElement = document.getElementById("subMenuElement");
		blurbDivElement = document.getElementById("blurbDivElement");
		backGroundImageCell = document.getElementById("backGroundImageCell");
		backGroundImageElement = document.getElementById("backGroundImageElement");
		
		// set first image
		SwitchImage();		
		
		// start loading all the other picture only after the background picture needed has downloaded
		backGroundImageElement.onload = loadImages;
				
		// set menu choice and submenu choice to passed arguments 
		topMenuChoice = t;
		topMenuHover = t;
		subMenuChoice = s;
		subMenuHover = s;
				
		
		SetBackgroundImageRotator();	
		ExecuteBlurbFunction();
		
	} catch(ex) {
		mainPage_DisplayStatus("Exception (startUp): " + ex.message);
	}
}



// display the contents of the blurb (transparent white block for text)
function ExecuteBlurbFunction()
{
	try
	{
		var blurbDivElement_textArea = document.getElementById("blurbDivElement_textArea");
		var ChildDiv;
		
		// Get the child DIV within the blurb area. This will be loaded in from the 'Blurbs' folder as
		// one of the file (i.e. '22.php'). If the first element is not the DIV (as it wont be for FireFox), then
		// use the second element
		ChildDiv = blurbDivElement_textArea.childNodes[0];
		if (ChildDiv.tagName != "DIV")
			ChildDiv = blurbDivElement_textArea.childNodes[1];
		
		var OmitHeading = ChildDiv.OmitHeading;	
		var menu_DisplayName = ChildDiv.getAttribute("menu_DisplayName");
		var Name = menu_DisplayName.replace(/ /g, "_");
		var CallFunction = ChildDiv.getAttribute("callFunction");
		
		// If the child DIV element does NOT have a 'OmitHeading' attribute, add the menu_DisplayName attribute
		// of the child DIV to the central blurb area
		if (!OmitHeading)
			blurbDivElement_textArea.innerHTML = "<b>" + menu_DisplayName + "</b><br><br>" + blurbDivElement_textArea.innerHTML;
		else
			blurbDivElement_textArea.innerHTML = "<br><br>" + blurbDivElement_textArea.innerHTML;
					
		// If the child DIV has a 'callFunction' attribute, then call the function with a underscored version of the
		// child DIV's menu_DisplayName attribute
		if (CallFunction)
		{
			eval(CallFunction + "('" + Name + "')");
		}
		
		// Resize the blurb area to fit the contents of the text inside
		var blurbDivElement_textArea_bounds = getBounds(blurbDivElement_textArea);
		blurbDivElement.style.height = blurbDivElement_textArea_bounds.height + "px";
		document.getElementById("blurbDivElement_background").style.height = blurbDivElement_textArea_bounds.height + "px";
	}
	catch (ex)
	{
		mainPage_DisplayStatus("Exception (ExecuteBlurbFunction): " + ex.message);
	}
}

// load images in the background by calling a timeout (essentially threading)
function loadImages()
{
	try
	{
		backGroundImageElement.onload = null;
		
		if (imageCounter < imageArray.length)
		{
			getNextImageTimeout = setTimeout("loadNextImage()", 100, "");
		}
	}
	catch (ex)
	{
		mainPage_DisplayStatus("Error (loadImages): " + ex.message);
	}
}

// load the next image
function loadNextImage()
{
	try
	{		
		clearTimeout(getNextImageTimeout);
		clearTimeout(getNextImageTimeout);
	
		var x = imageArray[imageCounter];		
		var image = new Image();
		image.onload = loadImages;
		image.src = x;
		imageCounter++;
	}
	catch (ex)
	{
		mainPage_DisplayStatus("Error (loadNextImage): " + ex.message);
	}
}

// switch background image
function SwitchImage()
{
	try
	{
		if ((currentBackgroundImageNumber + 1) < (imageArray.length))
			currentBackgroundImageNumber ++;
		else
			currentBackgroundImageNumber = 0;
				
		var imageFilename = imageArray[currentBackgroundImageNumber];		
		backGroundImageElement.src = imageFilename;
		backGroundImageElement.imageName = imageFilename.substr(17, imageFilename.length - 21);		
		var backgroundImage_description = document.getElementById("backgroundImage_description");
		backgroundImage_description.innerHTML = backGroundImageElement.imageName;
	}
	catch (ex)
	{
		alert(ex.message);
		mainPage_DisplayStatus("Exception (SwitchImage): " + ex.message);
	}
}

function floatingLabel_setCurrentlyOver(currentlyOver)
{
	try
	{
		var floatingLabel = document.getElementById("floatingLabel");
		floatingLabel.setAttribute("currentlyOver", currentlyOver);
	}
	catch (ex)
	{
		mainPage_DisplayStatus("Exception (floatingLabel_setCurrentlyOver): " + ex.message);
	}
}

function floatingLabel_show(theEvent, innerHTML)
{
	try
	{
		var floatingLabel = document.getElementById("floatingLabel");
		floatingLabel.style.left = (document.documentElement.scrollLeft + theEvent.clientX + 5) + "px";
		floatingLabel.style.top = (document.documentElement.scrollTop + theEvent.clientY + 5) + "px";
		floatingLabel.innerHTML = innerHTML;
		floatingLabel.style.display = "block";
		floatingLabel.style.visibility = "visible";
	}
	catch (ex)
	{
		mainPage_DisplayStatus("Error (floatingLabel_show): " + ex.message);
	}
}

function floatingLabel_hide()
{
	try
	{
		var floatingLabel = document.getElementById("floatingLabel");
		floatingLabel.style.visibility = "hidden";
		floatingLabel.style.display = "none";
	}
	catch (ex)
	{
		mainPage_DisplayStatus("Error (floatingLabel_hide): " + ex.message);
	}
}

// Temporary function - used by developer for debugging
function mainPage_DisplayStatus(txt)
{
	var mainPage_status = document.getElementById("mainPage_status");
	if (showErrors && mainPage_status)
	{
		mainPage_status.innerHTML += txt + "<br>";
		mainPage_status.scrollTop = mainPage_status.scrollHeight;
		mainPage_status.style.visibility = "visible";
	}
}

// (re)create the sub menu
function setSubMenu()
{
	try
	{
		var aCell;
		var innerHTML;
		var subMenuArray = menu.SubMenu[topMenuHover].SubMenu;
		
		// delete all previous cells
		while (subMenuElement.cells.length > 0)
			subMenuElement.deleteCell(0);
		
		// add new menu choices
		aCell = subMenuElement.insertCell(0);
		aCell.style.width = "50%";
		aCell.innerHTML = "&nbsp;";
		for (var index = 0; index < subMenuArray.length; index++)
		{		
			aCell = subMenuElement.insertCell(subMenuElement.cells.length);
			aCell.style.paddingLeft = "10px";
			aCell.style.paddingRight = "10px";
			aCell.style.width = "0px";
			aCell.style.textAlign = "center";
			innerHTML = "";
			innerHTML += "<a href='index.php?topMenuChoice=" + topMenuHover + "&subMenuChoice=" + index + "&lbin=" + index + "' ";
			innerHTML += " index='" + index + "' ";
			innerHTML += " id='subMenu_option_" + index + "' ";
			innerHTML += " class='darkGreenText' ";
			innerHTML += " onMouseOver='subMenu_mouseOver(this);' ";		
			innerHTML += " onMouseOut='subMenu_mouseOut(this);' ";		
			innerHTML += " style='white-space:nowrap;text-align:center;cursor:pointer;text-decoration:none;"
			if (index + 1 < subMenuArray.length)
			{
				aCell.style.borderRight = "1px solid #ffffff";
			}
			innerHTML += "'>" + subMenuArray[index] + "</a>";
			aCell.innerHTML = innerHTML;
		}
		aCell = subMenuElement.insertCell(subMenuElement.cells.length);
		aCell.style.width = "50%";
		aCell.innerHTML = "&nbsp;";
	}
	catch (ex)
	{
		mainPage_DisplayStatus("Exception (setSubMenu): " + ex.message);
	}
}

function topMenu_mouseOver(theElement)
{
	try
	{
		topMenuHover = Number(theElement.getAttribute("index"));
  		
		// show sub menu
		setSubMenu();		
		
		if (topMenuHover == topMenuChoice)
		{
			// if the mouse hovers over the top menu that is alrready the chosen top ment, then
			// show the highlighted sub menu that is choosen
			highlightMenuItem(null, document.getElementById("subMenu_option_" + subMenuChoice));
		}
		else
		{
			// highlight the button
			theElement.className = "whiteText";
		}
	} catch(ex) {
		mainPage_DisplayStatus("Exception (topMenu_mouseOver): " + ex.message);
	}
}

function topMenu_mouseOut(theElement)
{
	try
	{
		if (topMenuHover != topMenuChoice)
			// un-highlight the button
			theElement.className = "lightGreenText";
		
	} catch(ex) {
		mainPage_DisplayStatus("Exception (topMenu_mouseOut): " + ex.message);
	}
}

function subMenu_mouseOver(theElement)
{
	try
	{
		var aSubMenuHover = Number(theElement.getAttribute("index"));
  		
		if (aSubMenuHover != subMenuChoice || topMenuHover != topMenuChoice)
		{
			// highlight the button
			theElement.className = "whiteText";
		}
	} catch(ex) {
		mainPage_DisplayStatus("Exception (subMenu_mouseOver): " + ex.message);
	}
}

function subMenu_mouseOut(theElement)
{
	try
	{
		var aSubMenuHover = Number(theElement.getAttribute("index"));
		
		if (aSubMenuHover != subMenuChoice || topMenuHover != topMenuChoice)
			// un-highlight the button
			theElement.className = "darkGreenText";
		
	} catch(ex) {
		mainPage_DisplayStatus("Exception (subMenu_mouseOut): " + ex.message);
	}
}

// highlights the menu choices chosen by the user (highlights in white)
function highlightMenuItem(oldElement, newElement, oldClassName)
{
	// remove highlight
	if (oldElement)
	{
		oldElement.className = oldClassName;
	}
	// highlight new choice
	if (newElement)
	{
		newElement.className = "whiteText";
	}
}

// set up the background image flicker interval
function SetBackgroundImageRotator()
{
	clearInterval(interval);
	interval = setInterval("SwitchImage()", backgroundImage_switchDelay, "");
}

// get the bounds of an element (x,y, width, height)
function getBounds(anElement)
{
    var bounds = new Object();
    
	try
    {
        bounds.width = anElement.offsetWidth;
        bounds.height = anElement.offsetHeight;

        var count = 0;
        var x = 0;
        var y = 0;
		
        while(true) 
        {
            x += anElement.offsetLeft;
            y += anElement.offsetTop;
            if(!anElement.offsetParent)
			{
                break;
			}
            anElement = anElement.offsetParent;     
        }

        bounds.x = x;
        bounds.y = y;
    }   
	catch (ex)
	{
		mainPage_DisplayStatus("Exception (getBounds): " + ex.message);
	}
    return bounds;
}
















