/* *****************************************************************************
 * MenuScripts.js
 * Client-side JavaScript for DHTML Menu System - Script File
 *
 * Description: This file contains the core functions for the DHTML menus.  It
 *   is not intended to be editable for routine updates.  See MenuContent.js
 *   for regular site updates.
 *
 * Menu Type: Single-Level
 *
 * Author: Stephen Kratowicz, OakTree.com
 *
 * ****************************************************************************/

/* *****************************************************************************
 * Browser Detect
 * ****************************************************************************/

var blnDOM = (document.getElementById) ? true : false;
var blnIE = (document.all) ? true : false;
var blnNS6 = (navigator.vendor == 'Netscape6' || navigator.product == 'Gecko')
	? true : false;
if (window.parent)
	var blnNS4 = (parent.document.layers) ? true : false;

/* *****************************************************************************
 * Global Variables
 * ****************************************************************************/

// Document load boolean
var blnLoaded = false;

// Top-level Container
var objTopContainer = null;

// Top-level Menu Container Object Array
var aryTopMenuContainers = new Array();

// Currently Selected Menu Objects
var objTopMenuOn = null;
var objTopItemOn = null;

// Item Count at top level
var intMenuCount;

// Timer ID
var intTimerID = 0;

/* *****************************************************************************
 * Event Handlers
 * ****************************************************************************/

// Call to onload and onresize initialization
window.onload = function()
{ 
	var imgTemp;
	if (window.pageName && (imgTemp = MM_findObj(pageName))) {
		imgTemp.src = '/spanish/images/sidenav/' + pageName + '-open.gif';
	}
	setTimeout('init()', 10)
};

// Call to NS4 resize bug fix
if (blnNS4) window.onresize = reDraw;

/* *****************************************************************************
 * MenuContainer Class - Outer Container for Individual Menu Items
 * ****************************************************************************/
function MenuContainer (
	intID,
	intLeft,
	intTop,
	intWidth,
	intHeight,
	intItemCount,
	strSeparatorColor)
{
	this.ID = intID;
	this.Left = (blnNS4) ? TopMenu.NS4AdjustmentLeft + intLeft : intLeft;
	this.Top = (blnNS4) ? TopMenu.NS4AdjustmentTop + intTop : intTop;
	this.ImageHolder = null;
	this.ImageOff = null;
	this.ImageOn = null;
	
	this.Layer = createLayer(
		objTopContainer,
		'container_' + intID,
		-800,
		intTop,
		intWidth,
		intHeight,
		0,
		0,
		true);

	setBackgroundColor(this.Layer, strSeparatorColor);
	this.Layer.onmouseover = stopTimer;
	this.Layer.onmouseout = MenuOut;
}

/* *****************************************************************************
 * MenuItem Class - Individual Items in a Menu
 * ****************************************************************************/

function MenuItem (intParentID, intID, blnTopLevel, blnFirstItem)
{
	// Get the Template Menu Object
	var objMenu = blnTopLevel ? TopMenu : LowerMenu;

	// Set High-level Properties
	this.ID = 'Item' + intParentID + '_' + intID;
	this.IsTop = blnTopLevel;
	this.IsFirst = blnFirstItem;

	// Set Properties from Template Object
	for (var strKey in objMenu)
		this[strKey] = objMenu[strKey];
	
	// Define additional Properties
	this.Container = null;
	this.ChildContainer = null;
	this.Link = null;
	this.URL = '';
	this.Text = '';
	this.StringOff = '';
	this.StringOn = '';
	this.Left = 0;
	this.Top = 0;
	this.Width = 0;
	this.Height = 0;
}

MenuItem.prototype.CreateLayer = function()
{
	if ((blnDOM || blnIE) && !this.IsFirst) {
		if (this.IsHorizontal)
			this.Width += this.SeparatorWidth;
		else
			this.Height += this.SeparatorWidth;
	}
	this.Layer = createLayer(
		this.Container.Layer,
		'item_' + this.ID,
		this.Left,
		this.Top,
		this.Width,
		this.Height,
		true);

	if (blnDOM || blnIE) {
		this.Layer.style.cursor = (blnNS6) ? 'pointer' : 'hand';
		if (!this.IsFirst){
			if (this.IsHorizontal)
				this.Layer.style.borderLeft = this.SeparatorWidth + 'px solid ' + this.SeparatorColor;
			else
				this.Layer.style.borderTop = this.SeparatorWidth + 'px solid ' + this.SeparatorColor;
		}
	}
	setBackgroundColor (this.Layer, this.BgColor)

	this.Layer.Item = this;
	this.Layer.onmouseover = itemOn;
	this.Layer.onclick = itemClick;
}

MenuItem.prototype.Fill = function()
{
	if (blnDOM){
		var objAnchor = document.createElement("a");
		with(objAnchor) {
			href = this.URL;
			with(style) {
				font = this.FontWeight + ' ' + this.FontSize + 'px ' + this.FontFamily;
				color = this.FontColor;
				textDecoration = this.TextDecoration;
				textAlign = left;
			}
			innerHTML = this.Text;
		}
		this.Layer.appendChild(objAnchor);
		this.Link = objAnchor;
	}	else {
		this.MakeContentString();
		insertContent(this.Layer, this.StringOff);
	}
	
	with ((blnNS4) ? this.Layer : this.Layer.style) {
		color = this.FontColor;
		paddingTop = this.PaddingTop;
		paddingBottom = 30;
	}
}

/* *****************************************************************************
 * Menu Content String Functions
 * ****************************************************************************/
MenuItem.prototype.MakeContentString = function ()
{
	var strTemp;
	var strText = this.Text;
	if (this.FontWeight == 'bold') strText.bold();

	strTemp = '<table cellpadding="' + this.PaddingTop
		+ '" cellspacing="0" border="0" width="100%"><tr><td align="center"'
		+ '><a id="Link' + this.ID
		+ '" style="text-decoration: ' + this.TextDecoration
		+ ';" href="' + this.URL
		+ '"><FONT FACE="' + this.FontFamily
		+ '" POINT-SIZE="' + this.NS4FontSize
		+ '" COLOR="' + this.FontColor
		+ '">' + strText + '</FONT></a></td></tr></table>';
	this.StringOff = strTemp;
	
	strTemp = '<table cellpadding="' + this.PaddingTop
		+ '" cellspacing="0" border="0" width="100%"><tr><td align="center"'
		+ '><a id="Link' + this.ID
		+ '" style="text-decoration: ' + this.TextDecoration
		+ ';" href="' + this.URL
		+ '"><FONT FACE="' + this.FontFamily
		+ '" POINT-SIZE="' + this.NS4FontSize
		+ '" COLOR="' + this.FontColorOn
		+ '">' + strText + '</FONT></a></td></tr></table>';
	this.StringOn = strTemp;
}

Object.prototype.debug = function()
{
	var strTemp = '';
	for (var strKey in this) {
			strTemp += (strKey + ' : ' + this[strKey] + '<br>');
	}
	var objLayer = document.createElement("div");
	
		with(objLayer)
		{
			id = 'test';
			with(style)
			{
				position = 'absolute';
				visibility = 'visible';
				left = '100px';
				top = '100px';
				width = '800px';
			}
		}
	document.body.appendChild(objLayer);
	objLayer.innerHTML = strTemp;
	
}


/* *****************************************************************************
 * Initialization Function
 * ****************************************************************************/
function init()
{
	// Find Number of Top-level Menus in the Menu Arrays
	for (var i=1; i<100; i++)
		if (validateObject('aryMenu' + i)) intMenuCount = i;

	// Set the Top-level Container
	objTopContainer = (blnNS4) ?
		document.layers[TopMenu.ContainerName] : document.getElementById(TopMenu.ContainerName);

	// Build the Menus
	setTimeout('buildMenus(1)', 10);
}


/* *****************************************************************************
 * BuildMenus - Called from Init function to build Menu Objects and Layers
 * ****************************************************************************/
function buildMenus(i)
{
	// Local Vars
	var strItemOff;
	var strItemOn;
	var intItemCount;

	// Find the current Menu Array
	var aryTop = eval('aryMenu' + i); 
	
	// Top Menu Positioning
	var intTopMenuLeft = aryTop[0][0];
	var intTopMenuTop = aryTop[0][1];
	
	// Menu Sizing
	var intTopItemWidth = (aryTop[0][2]) ? aryTop[0][2] : TopMenu.DefaultWidth;
	var intTopItemHeight = (aryTop[0][3]) ? aryTop[0][3] : TopMenu.DefaultHeight;

	// Set aryTopItems to be Lower-dimension of Content Array
	var aryTopItems = aryTop[1];
	
	// Get the width and height of the Top-level Container
	var intItemsTotalWidth = 0;
	var intItemsTotalHeight = 0;
	if (TopMenu.IsHorizontal) {
		for (var j=0; j<aryTopItems.length; j++)
			intItemsTotalWidth += (aryTopItems[j][2]) ? aryTopItems[j][2] : intTopItemWidth; 
		intItemsTotalWidth += (aryTopItems.length -1) * TopMenu.SeparatorWidth;
		intItemsTotalHeight = intTopItemHeight;
	} else {
		for (var j=0; j<aryTopItems.length; j++)
			intItemsTotalHeight += (aryTopItems[j][2]) ? aryTopItems[j][2] : intTopItemHeight;
		intItemsTotalHeight += (aryTopItems.length -1) * TopMenu.SeparatorWidth;
		intItemsTotalWidth = intTopItemWidth;
	}
	intItemsTotalWidth += 2 * TopMenu.BorderWidth;
	intItemsTotalHeight += 2 * TopMenu.BorderWidth;

	// Create Top-level Container Menus
	aryTopMenuContainers[i] = new MenuContainer(
		i,
		intTopMenuLeft,
		intTopMenuTop,
		intItemsTotalWidth,
		intItemsTotalHeight,
		aryTopItems.length,
		TopMenu.SeparatorColor
	);

	// Set Additional Top-level Container Properties
	with (aryTopMenuContainers[i]) {
		ImageHolder = (blnNS4) ? 
			objTopContainer.document.images[aryTop[0][4]] : document.images[aryTop[0][4]] ;
		ImageOff = aryTop[0][5];
		ImageOn = aryTop[0][6];
	}
	
	// Set the starting Current Top Item X and Y Position to 0
	var intTopItemCurrentX = 0;
	var intTopItemCurrentY = 0;
	
	// Iterate through items in Top-level Menu
	for (var j=0; j<aryTopItems.length; j++) {
		
		// Instantiate the MenuItem Class (Top-level)
		var objTopMenuItem = new MenuItem(i, j, true, (j==0));
		
		// Set Properties to Top-level MenuItem Object
		with (objTopMenuItem) {
			Left = intTopItemCurrentX;
			Top = intTopItemCurrentY;
			Width = (TopMenu.IsHorizontal && aryTopItems[j][2]) ?
				aryTopItems[j][2] : intTopItemWidth;
			Height = (!TopMenu.IsHorizontal && aryTopItems[j][2]) ?
				aryTopItems[j][2] : intTopItemHeight;
			Text = aryTopItems[j][0];
			URL = aryTopItems[j][1];
			Container = aryTopMenuContainers[i];
		}

		// Create the Top-level MenuItem's Layer
		objTopMenuItem.CreateLayer();
	
		// Insert Content into Item
		objTopMenuItem.Fill();
		
		// Increase Current Top Item X or Y Position
		if (TopMenu.IsHorizontal) {
			intTopItemCurrentX += (aryTopItems[j][2]) ? aryTopItems[j][2] : intTopItemWidth;
			if (blnNS4 || j>0)
				intTopItemCurrentX += TopMenu.SeparatorWidth;
		} else {
			intTopItemCurrentY += (aryTopItems[j][2]) ? aryTopItems[j][2] : intTopItemHeight;
			if (blnNS4 || j>0)
				intTopItemCurrentY += TopMenu.SeparatorWidth;
		}

	}//End j Loop
	
	// After Items are created, hide layer and move onto screen
	showOrHide (aryTopMenuContainers[i].Layer, 'hidden');
	positionLayer(aryTopMenuContainers[i].Layer, aryTopMenuContainers[i].Left, aryTopMenuContainers[i].Top);

	// Iterate i
	i++;

	// Repeat as directed
	if (i <= intMenuCount)
		setTimeout('buildMenus(' + i + ');', 5);
	// If finished, set blnLoaded true and display
	else {
		blnLoaded = true;
	}
}

/* *****************************************************************************
 * Show/Hide and Timer Functions
 * ****************************************************************************/

// MenuOver() function - Sets visibility on for a Menu layer and registers it
// as the currently selected menu.
function MenuOver (intMenuID)
{
	if (!blnLoaded) return;
	HideMenu();
	objTopMenuOn = aryTopMenuContainers[intMenuID];
	showOrHide (objTopMenuOn.Layer, 'visible');
	objTopMenuOn.ImageHolder.src = objTopMenuOn.ImageOn;
}

// MenuOut() function - begins timer to hide menu
function MenuOut () {
	intTimerID = setTimeout('HideMenu();', 500);
}

// HideMenu() function - Sets visibility off for the currently selected menu.
function HideMenu ()
{
	if (objTopMenuOn != null) {
		showOrHide(objTopMenuOn.Layer, 'hidden');
		objMenuTopOn = null;
		objTopMenuOn.ImageHolder.src = objTopMenuOn.ImageOff;
	}
	if (objTopItemOn != null) {
		setBackgroundColor(objTopItemOn.Layer, objTopItemOn.BgColor);
		if (objTopItemOn.Link)
			objTopItemOn.Link.style.color = objTopItemOn.FontColor;
		else if (blnNS4)
			insertContent(objTopItemOn.Layer, objTopItemOn.StringOff);
			
		objTopItemOn = null;
	}
	stopTimer();
}

// stopTimer() function - stops timer for menu hide
function stopTimer () {
	if (intTimerID) { clearTimeout(intTimerID); clearTimeout(intTimerID - 1); }
}

// Called by Mouseover of a Menu Item
function itemOn()
{
	var objItem;
	if (blnDOM && this.tagName != 'DIV')
		return;
	objItem = this.Item;
	setBackgroundColor(this, objItem.BgColorOn);
	
	// If it is a Top-level Menu Item
	if (objItem.IsTop) {
		
		// If a Top Menu Item is On
		if (objTopItemOn != null)
			setItemState(objTopItemOn, false);
		
		// Set new Top Menu Item On
		objTopItemOn = objItem;
		setItemState(objTopItemOn, true);
	}
}

function setItemState(objItem, blnOn)
{
	var strItemString = (blnOn) ? objItem.StringOn : objItem.StringOff;
	var strBgColor = (blnOn) ? objItem.BgColorOn : objItem.BgColor;
	
	if (objItem.Link)
		objItem.Link.style.color = (blnOn) ? objItem.FontColorOn : objItem.FontColor;

	else if (blnNS4)
		insertContent(objItem.Layer, strItemString);

	setBackgroundColor(objItem.Layer, strBgColor);
}

function itemClick()
{
	if (blnDOM && this.tagName != 'DIV')
		return;
	location = this.Item.URL;
	return false;
}
 
/* *****************************************************************************
 * DHTML Utility Functions
 * ****************************************************************************/

// showOrHide() - sets visibility on or off for a layer object
function showOrHide (obj,vis)
{
	if (blnIE || blnDOM) obj.style.visibility = vis;
	else if (blnNS4) obj.visibility = ((vis == "visible") ? "show" : "hide");
}

// setBackgroundColor - set the background color of a layer
function setBackgroundColor (objLayer, strColor)
{
	if (blnNS4) objLayer.bgColor = strColor;
	else objLayer.style.backgroundColor = strColor;
}

// getWidth() - returns the width of the window
function getWidth()
{
	if (blnNS4 || blnNS6) return window.innerWidth;
	else return document.body.clientWidth;
}

// positionLayer - move layer to a given X, Y position
function positionLayer (objLayer, xPos, yPos) {
	if (blnDOM||blnIE) {
		objLayer.style.left = xPos + "px";
		objLayer.style.top = yPos + "px";
	}
	else
		objLayer.moveTo(xPos, yPos);
}

// insertContent() - puts content into an existing HTML DIV/Layer
function insertContent(objLayer, strContent)
{
	if (blnIE || blnDOM) objLayer.innerHTML = strContent;
	else if (blnNS4)
	{
		objLayer.document.write(strContent);
		objLayer.document.close();
	}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

/* *****************************************************************************
 * createLayer - The primary Layer Creation function
 * ****************************************************************************/

function createLayer (
	objContainer,
	strId,
	intLeft,
	intTop,
	intWidth,
	intHeight,
	blnVis)

{
	var objLayer = null;

	var strVis;
	if (blnNS4)
		strVis = (blnVis) ? 'inherit' : 'hide';
	else
		strVis = (blnVis) ? 'inherit' : 'hidden';

	var strClip = '0px '
		+ intWidth + 'px '
		+ intHeight + 'px '
		+ '0px';

	if (blnDOM)
	{
		objLayer = document.createElement("div");
		with(objLayer)
		{
			id = strId;
			with(style)
			{
				position = 'absolute';
				visibility = strVis;
				left = intLeft + 'px';
				top = intTop + 'px';
				width = intWidth + 'px';
				if (intHeight != 'auto' && intWidth != 'auto')
					clip = 'rect(' + strClip + ')';
			}
		}
		objContainer.appendChild(objLayer);
	}
	
	else if (blnIE)
	{
		var strHTML = '<div id="' + strID
			+ '" style="position:absolute; visibility:' + strVis + ';"></div>';
		objContainer.insertAdjacentHTML('BeforeEnd', strHTML);
		objLayer = document.all[strId];
		with(objLayer.style)
		{
			left = intLeft + 'px';
			top = intTop + 'px';
			width = intWidth + 'px';
			if (intHeight != 'auto' && intWidth != 'auto')
				clip = 'rect(' + strClip + ')';
		}
	}
	
	else if (blnNS4)
	{
		objLayer = new Layer (intWidth, objContainer);
		with(objLayer)
		{
			name = strId;
			visibility = strVis;
			top = intTop;
			left = intLeft;
			width = intWidth;
			if (intWidth != 'auto')
				clip.width = intWidth;
			if (intHeight != 'auto')
				clip.height = intHeight;
		}
	}
	
	return objLayer;
}

/* *****************************************************************************
 * Core JavaScript Utilities
 * ****************************************************************************/

// validateObject - checks to ensure that a variable name represents an object
function validateObject(strName)
{
	return (typeof eval("window." + strName) == "object");
}

/* *****************************************************************************
 * Browser-specific bug fixes
 * ****************************************************************************/

// Netscape 4 Resize Fix
if (blnNS4)
{      
	origWidth = innerWidth;
	origHeight = innerHeight;   	
}

function reDraw()
{
	if (innerWidth != origWidth || innerHeight != origHeight) location.reload();
}
