var menu_Hang_Time = 1500;    //  this value determines for how many milliseconds a submenu is shown once the mouse leaves it
                              //  1000 milliseconds = 1 second
/* SET THE WIDTH OF THE PAGE HERE */
var constraint_pageWidth = 1050;
var constraint_leftAdWidth = 125;
var constraint_rightAdWidth = 125;
var constraint_bodyWidth = constraint_pageWidth - constraint_leftAdWidth - constraint_rightAdWidth;

/* SET THE AD DIV CONTENTS HERE */
var ads = new Array(1);
ads[0] = '<iframe src ="http://www.dyingtolivewell.com/dyingToLiveWell_ad_3.html" width="125px" height="200px" frameborder="0" scrolling="no">' +
           '<p>Your browser does not support iframes.</p>' +
         '</iframe>';

/* MENU OBJECT */
var menuItems = new Array(1);
menuItems[0] = new Object();
menuItems[0].name = 'Home';
menuItems[0].link = 'index.html';
menuItems[0].pageTitle = 'Renate Weiler';
menuItems[0].menuMode = 'both';
menuItems[0].newFooterLine = 'false';
menuItems[0].styleOverride = '';
menuItems[0].children = null;

menuItems[1] = new Object();
menuItems[1].name = 'Growing-Up';
menuItems[1].link = 'growingUp.html';
menuItems[1].pageTitle = 'Growing-Up';
menuItems[1].menuMode = 'both';
menuItems[1].newFooterLine = 'false';
menuItems[1].styleOverride = '';
menuItems[1].children = new Array(1);
menuItems[1].children = null;

menuItems[2] = new Object();
menuItems[2].name = 'Marriage';
menuItems[2].link = 'marriage.html';
menuItems[2].pageTitle = 'Marriage';
menuItems[2].menuMode = 'both';
menuItems[2].newFooterLine = 'false';
menuItems[2].styleOverride = '';
menuItems[2].children = new Array(1);
menuItems[2].children = null;

menuItems[3] = new Object();
menuItems[3].name = 'Motherhood';
menuItems[3].link = 'motherhood.html';
menuItems[3].pageTitle = 'Motherhood';
menuItems[3].menuMode = 'both';
menuItems[3].newFooterLine = 'false';
menuItems[3].styleOverride = '';
menuItems[3].children = new Array(1);
menuItems[3].children = null;

menuItems[4] = new Object();
menuItems[4].name = 'The Author';
menuItems[4].link = 'theAuthor.html';
menuItems[4].pageTitle = 'The Author';
menuItems[4].menuMode = 'both';
menuItems[4].newFooterLine = 'false';
menuItems[4].styleOverride = '';
menuItems[4].children = new Array(1);
menuItems[4].children[0] = new Object;
menuItems[4].children[0].name = 'Dying To Live Well';
menuItems[4].children[0].link = 'author_dyingToLiveWell.html';
menuItems[4].children[0].pageTitle = 'Dying To Live Well<br /><small><i>loving the journey</i></small>';
menuItems[4].children[0].styleOverride = '';
menuItems[4].children[1] = new Object;
menuItems[4].children[1].name = 'Get A Grip (edition 2)';
menuItems[4].children[1].link = 'author_getAGrip_aJourneyTowardsInnerStrength.html';
menuItems[4].children[1].pageTitle = 'Get A Grip<br /><small><i>a journey towards inner strength</i></small>';
menuItems[4].children[1].styleOverride = '';
menuItems[4].children[2] = new Object;
menuItems[4].children[2].name = 'Get A Grip';
menuItems[4].children[2].link = 'author_getAGrip_onYourAuthenticSelf.html';
menuItems[4].children[2].pageTitle = 'Get A Grip<br /><small><i>on your authentic self</i></small>';
menuItems[4].children[2].styleOverride = '';

menuItems[5] = new Object();
menuItems[5].name = 'Contact Us';
menuItems[5].link = 'contact.html';
menuItems[5].pageTitle = 'Contact Us';
menuItems[5].menuMode = 'bottom';
menuItems[5].newFooterLine = 'true';
menuItems[5].styleOverride = '';
menuItems[5].children = null;

menuItems[6] = new Object();
menuItems[6].name = 'Links';
menuItems[6].link = 'links.html';
menuItems[6].pageTitle = 'Links';
menuItems[6].menuMode = 'bottom';
menuItems[6].newFooterLine = 'false';
menuItems[6].styleOverride = '';
menuItems[6].children = null;

menuItems[7] = new Object();
menuItems[7].name = 'Site Map';
menuItems[7].link = 'siteMap.html';
menuItems[7].pageTitle = 'Site Map';
menuItems[7].menuMode = 'bottom';
menuItems[7].newFooterLine = 'false';
menuItems[7].styleOverride = '';
menuItems[7].children = null;

/* ****************************************
 ******************************************
 **  FOLLOWING SHOULDN'T BE MESSED-WITH  **
 ******************************************
 **************************************** */
var hide_is_a_go  = null;         //  stores the currently visible subMenu object...used to hide it after small delay
var hide_index = 0;
var current_ad_index = -1;

window.onresize = pageScroll;
window.onscroll = pageScroll;

/*
 *  initPage( pageIndex )
 *    initializes the header, footer and sider bars
 *  
 *  pageIndex: the page # that we're loading (starting @ 1)
 */
function initPage( pageIndex )
{
	var body = document.getElementsByTagName( 'body' );
	if ( body.length > 0 )
	{
		surroundWithMenu( body[0], pageIndex );
	};
	positionAdDivs();
	body[0].style.visibility = 'visible';
};

/*
 *  pageScroll()
 *    when the user scrolls the page, this function ensures that everything looks right
 *  
 *  return: null
 */
function pageScroll()
{
	positionAdDivs();
};

/*
 *  RandomAd()
 *    retrieves an ad at random from list of ads
 *  
 *  return: (string) representing a random ad from the array of ads
 */
function RandomAd()
{
	var output = '&nbsp;';
	var new_ad_index = 0;
	if ( ads.length > 0 )
	{
		if ( ads.length > 1 )
		{
			if ( current_ad_index >= 0 )
			{
				new_ad_index = randomNum( 0, ads.length - 2 );
				if ( new_ad_index >= current_ad_index )
				{
					new_ad_index++;
				};
			}
			else
			{
				new_ad_index = new_ad_index = randomNum( 0, ads.length - 1 );
			};
		}
		else
		{
			new_ad_index = 0;
		};
		current_ad_index = new_ad_index;
		output = ads[current_ad_index];
	};
	return output;
};

/*
 *  positionAdDivs()
 *    vertically aligns the left and right ad div, if they exist
 *  
 *  return:  nothing (null)
 */
function positionAdDivs()
{
	var sider_miniDiv = null;
	var sider_adDiv = null;
	var footer_miniDiv = document.getElementById( 'footer_miniDiv' );

	for ( var n=0; n<=1; n++ )
	{
		if ( n == 0 )
		{
			sider_miniDiv = document.getElementById( 'siderLeft_miniDiv' );
			sider_adDiv = document.getElementById( 'siderLeft_adDiv' );
		}
		else
		{
			sider_miniDiv = document.getElementById( 'siderRight_miniDiv' );
			sider_adDiv = document.getElementById( 'siderRight_adDiv' );
		};

		if ( ( sider_miniDiv != null ) && ( sider_adDiv != null ) )
		{
			var page = getPageSize();
			var siderAd_height = GetAbsPosition( sider_adDiv ).height;
			var sider_availableSpace = 0;
			if ( GetAbsPosition( sider_miniDiv ).top >= page.top )
			{
				sider_availableSpace = page.bottom - GetAbsPosition( sider_miniDiv ).top;
			}
			else
			{
				sider_availableSpace = page.height;
			};

			var siderAd_newTop = 0;
			if ( ( sider_availableSpace > siderAd_height ) )
			{
				if ( GetAbsPosition( sider_miniDiv ).top >= page.top )
				{
					siderAd_newTop = parseInt( '' + ( ( sider_availableSpace - siderAd_height ) / 2 ) );
				}
				else
				{
					siderAd_newTop = parseInt( '' + ( ( page.top - GetAbsPosition( sider_miniDiv ).top ) + ( ( sider_availableSpace - siderAd_height ) / 2 ) ) );
				};
				if ( siderAd_newTop < 0 )
				{
					siderAd_newTop = 0;
				};
			};

			if ( footer_miniDiv != null )
			{
				if ( ( GetAbsPosition( sider_miniDiv ).top + siderAd_newTop + siderAd_height ) > GetAbsPosition( footer_miniDiv ).top )
				{
					siderAd_newTop = GetAbsPosition( footer_miniDiv ).top - GetAbsPosition( sider_miniDiv ).top - siderAd_height;
				};
			};
			sider_adDiv.style.top = siderAd_newTop + 'px';
		};
	};
};

/*
 *  surroundWithMenu( inside, pageIndex )
 *    dynamically puts a header, footer, etc... around the the page
 *  
 *  inside:    the object to surround (typically the page body)
 *  pageIndex: the page index that is currently selected (different style applied to it's menu item)
 *  return:    nothing (null)
 */
function surroundWithMenu( inside, pageIndex )
{
	if ( inside != null )
	{
		tempPageIndex = '' + pageIndex;
		pageIndex = '';
		childPageIndex = '';
		spacerFound = false;
		for ( n=0; n<tempPageIndex.length; n++ )
		{
			if ( tempPageIndex.charAt( n ) == '.' )
			{
				spacerFound = true;
			}
			else
			{
				if ( spacerFound )
				{
					childPageIndex = childPageIndex + tempPageIndex.charAt(n);
				}
				else
				{
					pageIndex = pageIndex + tempPageIndex.charAt(n);
				};
			};
		};
		
		var menuColCount = 1;
		if ( menuItems != null )
		{
			menuColCount = menuItems.length;
		};
		var output = '';
		var outChildrenDivs = '';
		var header = '<table width="' + constraint_pageWidth + 'px" border="0px" align="center" class="mainTable" cellpadding="0px" cellspacing="0px">' +
						'<tr>' +
							'<td colspan="3">' +
								'<table align="center" cellpadding="0px" cellspacing="0px">' +
									'<tr>' +
										'<td colspan="' + menuColCount + '" align="center">' +
											'<a href="index.html" title="www.RenateWeiler.com">' +
												'<img src="images/renateCollage.jpg" width="900px" height="200px" border="0px" title="www.RenateWeiler.com" alt="www.RenateWeiler.com" />' +
											'</a>' +
										'</td>' +
									'</tr>';

		if ( menuItems != null )
		{
			header += 				'<tr id="header_menu">';
			for ( var n=0; n<menuItems.length; n++ )
			{
				if ( menuItems[n] != null )
				{
					if ( menuItems[n].menuMode.trim().toLowerCase() != 'bottom' )
					{
						var styleOverride = '';
						if ( menuItems[n].styleOverride != '' )
						{
							styleOverride += ' style="' + menuItems[n].styleOverride + '"';
						};

						if ( menuItems[n].children != null )
						{
							/* BUILD EXTRA DIV */
							outChildrenDivs += '<div id="menuChild' + n + '" onmouseover="holdMenu();" onmouseout="hideSubMenu( ' + n + ' );" style="position: absolute; left: 0; top: 0; visibility: hidden; background-color: #FFFFFF;">';
							outChildrenDivs +=   '<table align="center" class="header_menuItem_child_table">';
							for ( var k=0; k<menuItems[n].children.length; k++ )
							{
								var child_styleOverride = '';
								if ( menuItems[n].children[k].styleOverride != '' )
								{
									child_styleOverride += ' style="' + menuItems[n].children[k].styleOverride + '"';
								};
								outChildrenDivs += 		'<tr>' +
															'<td align="left"' + child_styleOverride + '>' +
																'<a href="' + menuItems[n].children[k].link + '" class="header_menuItem_child">' + menuItems[n].children[k].name + '</a>' +
															'</td>' +
														'</tr>';
							};
							outChildrenDivs += 		'</table>';
							outChildrenDivs +=	'</div>';
						};
						var menuItemClassName = 'header_menuItem';
						if ( pageIndex == ( n + 1 ) )
						{
							menuItemClassName = 'header_menuItem_selected';
						};
						header += 	'<td onmouseover="showSubMenu( ' + n + ' );" onmouseout="hideSubMenu( ' + n + ' );"' + styleOverride + '>' +
										'<a href="' + menuItems[n].link + '" class="' + menuItemClassName + '">' + menuItems[n].name + '</a>' +
									'</td>';
					};
				};
			};
			header += 			'</tr>';
		};
		header += 			'</table>' +
						'</td>' +
					'</tr>' +
					'<tr>' +
						'<td colspan="3">' +
							'<hr color="#000000" />' +
						'</td>' +
					'</tr>' +
					'<tr>' +
						'<td width="' + constraint_leftAdWidth + 'px" class="sider_ad">' +
							'<div id="siderLeft_miniDiv" style="font-size: 1px;">&nbsp;</div>' +
							'<div id="siderLeft_adDiv" width="' + ( constraint_leftAdWidth - 20 ) + 'px">' +
								RandomAd() +
							'</div>' +
						'</td>' +
						'<td width="' + constraint_bodyWidth + 'px" align="center" style="vertical-align: top; padding-top: 15px; padding-bottom: 15px; padding-left: 50px; padding-right: 50px;">';

		var footer = 	'</td>' +
						'<td width="' + constraint_rightAdWidth + 'px" class="sider_ad">' +
							'<div id="siderRight_miniDiv" style="font-size: 1px;">&nbsp;</div>' +
							'<div id="siderRight_adDiv" width="' + ( constraint_rightAdWidth - 20 ) + 'px">' +
								RandomAd() +
							'</div>' +
						'</td>' +
					'</tr>' +
					'<tr>' +
						'<td colspan="3">' +
							'<div id="footer_miniDiv" style="font-size: 1px;">&nbsp;</div>';
		if ( menuItems != null )
		{
			footer += 			'<table align="center" cellpadding="0px" cellspacing="0px">' +
									'<tr class="footer_menu">';
			for ( var n=0; n<menuItems.length; n++ )
			{
				if ( menuItems[n] != null )
				{
					if ( menuItems[n].menuMode.trim().toLowerCase() != 'top' )
					{
						var styleOverride = '';
						if ( menuItems[n].styleOverride != '' )
						{
							styleOverride = ' style="' + menuItems[n].styleOverride + '"';
						}
						var menuItemClassName = 'footer_menuItem';
						if ( pageIndex == ( n + 1 ) )
						{
							menuItemClassName = 'footer_menuItem_selected';
						};
						if ( menuItems[n].newFooterLine.trim().toLowerCase() == 'true' )
						{
							footer += 	'</tr>' +
									'</table>' +
									'<table align="center" cellpadding="0px" cellspacing="0px">' +
										'<tr class="footer_menu">';
						};
						footer += 			'<td' + styleOverride + '>' +
												'<a href="' + menuItems[n].link + '" class="' + menuItemClassName + '">' + menuItems[n].name + '</a>' +
											'</td>';
					};
				};
			};
			footer += 					'</tr>' +
									'</table>';
		};
		footer += 				'</td>' +
							'</tr>' +
							'<tr>' +
								'<td colspan="3" align="center" class="copyRight">' +
									'copyright &copy; 2009' +
									'<br />' +
									'<br />' +
									'<b>Page Last Updated:</b> ' + GetLastModifiedDate() +
								'</td>' +
							'</tr>' +
						'</table>';
		var pageTitle = "<br /><br /><br /><br />";
		if ( pageIndex > 0 )
		{
			thisPageTitle = menuItems[pageIndex-1].pageTitle;
			if ( childPageIndex != '' )
			{
				if ( ( childPageIndex > 0 ) && ( menuItems[pageIndex-1].children.length >= childPageIndex ) )
				{
					thisPageTitle = menuItems[pageIndex-1].children[childPageIndex-1].pageTitle;
				};
			};
			pageTitle = '<div class="pageTitle">' +
							thisPageTitle +
						'</div>' +
						'<br />' +
						'<br />';
		};
		output = header + pageTitle + inside.innerHTML + footer + outChildrenDivs;
		inside.innerHTML = output;
	};
};

/* *************************
 ***************************
 ** MENU SHOWING & HIDING **
 ***************************
 ************************* */
/*
 *  newHideIndex()
 *    sets the hide_index variable to a unique value
 *  
 *  return: null*/
function newHideIndex()
{
	if ( hide_index >= 1000 )
	{
		hide_index = 0;
	}
	else
	{
		hide_index++;
	};
};

/*
 *  holdMenu()
 *    If a sub menu was previously told to disapear, that is cancelled now
 *  
 *  return: null
 */
function holdMenu()
{
	hide_is_a_go = null;
	newHideIndex();
};

/*
 *  hideSubMenu( index )
 *    If the user's mouse leaves the current main or sub menu, the sub menu (if it exists) is told to disapear in 2000 milliseconds
 *  
 *  index:  the index of the element in the menu structure to display
 *  return: null
 */
function hideSubMenu( menuIndex )
{
	var subList = document.getElementById( 'menuChild' + menuIndex );
	if ( subList != null )
	{
		hide_is_a_go = subList;
		newHideIndex();
		setTimeout( 'menuDisapear(' + hide_index + ') ', menu_Hang_Time );
	};
};

/*
 *  menuDisapear()
 *    This function is called when the users mouse leaves either the current main/sub menu.
 *    If the hide_is_a_go variable is not null, then set the visibility of that object to "hidden"
 *  
 *  index:  the index to be compared to hide_index
 *  return: null
 */
function menuDisapear( index )
{
	if ( index == hide_index )
	{
		try
		{
			if ( hide_is_a_go != null )
			{
				hide_is_a_go.style.visibility = 'hidden';
				hide_is_a_go = null;
			};
		}
		catch (e1)
		{
		};
		newHideIndex();
	};
};

/*
 *  showSubMenu( menuIndex )
 *    If the selected main menu item has a submenu associated with it, it is shown...all other sub menus are hidden
 *  
 *  menuIndex:  the index of the element in the menu structure to display
 *  return: null
 */
function showSubMenu( menuIndex )
{
	hide_is_a_go = null;
	var headerMenu = document.getElementById( 'header_menu' );
	if ( headerMenu != null )
	{
		var header_menuItems = headerMenu.getElementsByTagName( 'a' );
		for ( var n=0; n<header_menuItems.length; n++ )
		{
			var temp_subMenuDiv = document.getElementById( 'menuChild' + n );
			if ( temp_subMenuDiv != null )
			{
				if ( n == menuIndex )
				{
					var subMenuDiv_left = GetAbsPosition( header_menuItems[menuIndex] ).left;
					if ( GetAbsPosition( header_menuItems[menuIndex] ).width > GetAbsPosition( temp_subMenuDiv ).width )
					{
						subMenuDiv_left = parseInt( '' + ( GetAbsPosition( header_menuItems[menuIndex] ).left + ( GetAbsPosition( header_menuItems[menuIndex] ).width / 2 ) - ( GetAbsPosition( temp_subMenuDiv ).width / 2 ) ) );
					};
					var subMenuDiv_top = GetAbsPosition( header_menuItems[menuIndex] ).bottom;

					temp_subMenuDiv.style.left = subMenuDiv_left + 'px';
					temp_subMenuDiv.style.top = subMenuDiv_top + 'px';
					temp_subMenuDiv.style.visibility = 'visible';
					hide_is_a_go2 = temp_subMenuDiv;
				}
				else
				{
					temp_subMenuDiv.style.left = '0px';
					temp_subMenuDiv.style.top = '0px';
					temp_subMenuDiv.style.visibility = 'hidden';
				};
			};
		};
	};
};
