//
// Variables
//                
var oCurrentMenu = null;     // pointer to the current menu object
var oCurrentParent = null;   // pointer to the current menu's parent object
var iMenuAddTopOffset = 0;   // additional pixel offset to add to menu top offset
var iMenuAddLeftOffset = 0;  // additional pixel offset to add to menu left offset
var bAlignCenter = true;

// Footer array to create footer menu
var aFooterLinks = [
                     ['index.html','Home'],
                     ['MissionStatement.htm','Mission Statement'],
                     ['OurBeliefs.htm','Our Beliefs'],
                     ['OurPastor.htm','Our Pastor'],
                     ['History.htm','History'],
                     ['Directions.htm','Directions'],
                     ['ContactUs.htm','Contact Us'],
                     ['events.htm','Events'],
                     ['sermons.htm','Sermons']
                   ];

var ie,ns4,ns6;

if (document.all)
  ie = true;
else if (document.layers)
  ns4 = true;
else if(document.getElementById)
  ns6 = true;
  
/////////////////////////////////////////////////////////////
// Functions
//

function WriteMenus()
{
  document.write('<TD width="25px" align="right">|</TD>');
  document.write('<TD width="130px" onmouseover="ChangeClass(this, \'MenuHeaderHover\');" onmouseout="ChangeClass(this, \'MenuHeader\');" onclick="top.location.href=\'index.html\'" class="MenuHeader">Home</TD>');
  document.write('<TD>|</TD>');
  document.write('<TD width="130px" id="AboutUsHeader" onmouseover="OnMenuHover(this, \'AboutUsMenuDiv\', \'\', true)" onmouseout="SetCloseMenu()" class="MenuHeader">About PBC</TD>');
  document.write('<TD>|</TD>');
  document.write('<TD width="130px" onmouseover="ChangeClass(this, \'MenuHeaderHover\');" onmouseout="ChangeClass(this, \'MenuHeader\');" onclick="top.location.href=\'events.htm\'" class="MenuHeader">Events</TD>');
  document.write('<TD>|</TD>');
  document.write('<TD width="130px" onmouseover="ChangeClass(this, \'MenuHeaderHover\');" onmouseout="ChangeClass(this, \'MenuHeader\');" onclick="top.location.href=\'sermons.htm\'" class="MenuHeader">Sermons</TD>');
  document.write('<TD>|</TD>');
  document.write('<TD width="130px" onmouseover="ChangeClass(this, \'MenuHeaderHover\');" onmouseout="ChangeClass(this, \'MenuHeader\');" onclick="top.location.href=\'ContactUs.htm\'" class="MenuHeader">Contact Us</TD>');
  document.write('<TD width="25px" align="left">|</TD>');
}

//
// Initialize Menus
//
function InitializeMenu()
{
  CreateMenu('AboutUsMenu', new Array('Mission Statement', 'MissionStatement.htm',
                                      'Our Beliefs', 'OurBeliefs.htm', 
                                      'Our Pastor', 'OurPastor.htm', 
                                      'History', 'History.htm',
                                      'Directions', 'Directions.htm'));
}
  
//
// Gets an element using the given id
//
function GetElement(id)
{
  el = document.all ? document.all[id] : document.getElementById(id);
  return el;
}

//
// Changes the CSS class used by the given element
//
function ChangeClass(oElement, sNewClass)
{
	if ( (oElement == null) || (sNewClass == null) )
    return;

	oElement.className = sNewClass;  
}

//
// Finds the pixel offset from the left of the page to the given object
//
function FindLeftOffset(oObject)
{
  var iLeftOffset = 0;
  var oCurrentObject = oObject;
  
  while( oCurrentObject != null )
  {
    iLeftOffset += oCurrentObject.offsetLeft;
    oCurrentObject = oCurrentObject.offsetParent;
  }

  return iLeftOffset;
}

//
// Finds the pixel offset from the top of the page to the given object
//
function FindTopOffset(oObject)
{
  var iTopOffset = 0;
  var oCurrentObject = oObject;
  
  if( oObject == null )
    return;
  
  while( oCurrentObject != null )
  {
    iTopOffset += oCurrentObject.offsetTop;
    oCurrentObject = oCurrentObject.offsetParent;
  }
  
  iTopOffset += oObject.offsetHeight;
  
  return iTopOffset;
}

//
// Creates the Menu layer
//
function CreateMenu(sMenuId, aMenuItems)
{
  var sDiv;
  var iCount;
  
  if( (sMenuId == null) || (sMenuId.length < 0) || ((iCount = aMenuItems.length) == 0) )
    return;
    
  sDiv = '<DIV id="' + sMenuId + 'Div" class="MenuDiv">\n';
  sDiv += '<TABLE id="' + sMenuId + 'Table" cellpadding=0 cellspacing=0 class="MenuTable">\n<TBODY>\n';
  for(i = 0; i < iCount; i += 2)
  {
    if( i == 0 )  // first item
      sDiv += '<TR><TD id="' + sMenuId + 'I' + i +'" width="100%" onmouseover="OnMenuItemHover(\'' + sMenuId + 'I' + i + '\', \'MenuFirstItemHover\')" onmouseout="OnMenuItemOut(\'' + sMenuId + 'I' + i + '\', \'MenuFirstItem\')" onclick="OnMenuItemClick(\'' + sMenuId + 'I' + i + '\',\'' + aMenuItems[i+1] + '\')" class="MenuFirstItem">' + aMenuItems[i] + '</TD></TR>\n';
    else if( i == (iCount/2) )  // last item
      sDiv += '<TR><TD id="' + sMenuId + 'I' + i +'" width="100%" onmouseover="OnMenuItemHover(\'' + sMenuId + 'I' + i + '\', \'MenuLastItemHover\')" onmouseout="OnMenuItemOut(\'' + sMenuId + 'I' + i + '\', \'MenuLastItem\')" onclick="OnMenuItemClick(\'' + sMenuId + 'I' + i + '\',\'' + aMenuItems[i+1] + '\')" class="MenuLastItem">' + aMenuItems[i] + '</TD></TR>\n';
    else
      sDiv += '<TR><TD id="' + sMenuId + 'I' + i +'" width="100%" onmouseover="OnMenuItemHover(\'' + sMenuId + 'I' + i + '\', \'MenuItemHover\')" onmouseout="OnMenuItemOut(\'' + sMenuId + 'I' + i + '\', \'MenuItem\')" onclick="OnMenuItemClick(\'' + sMenuId + 'I' + i + '\',\'' + aMenuItems[i+1] + '\')" class="MenuItem">' + aMenuItems[i] + '</TD></TR>\n';
  }
  sDiv += '</TBODY>\n</TABLE>\n</DIV>';

  document.write(sDiv);
}

//
//  Displays the menu laye in the correct postion on the page
//
function DisplayMenu(oParent, Id)
{
	var oMenu;
	var iMenuTop;
	var iMenuLeft;

  oMenu = GetElement(Id);  

  bMenuEntered = true;

	if( oCurrentMenu && (oCurrentMenu != oMenu) )
		oCurrentMenu.style.visibility = 'hidden';
		
	if( oCurrentParent )
	{
	  ChangeClass(oCurrentParent, 'MenuHeader');
   	oCurrentParent = null;
  }

	oCurrentMenu = oMenu;
  oCurrentParent = oParent;
  
  ChangeClass(oCurrentParent, 'MenuHeaderHover');
  
  iMenuTop = FindTopOffset(oParent) + iMenuAddTopOffset;
  iMenuLeft = FindLeftOffset(oParent) + iMenuAddLeftOffset;

  if( bAlignCenter )
    iMenuLeft += (oParent.offsetWidth/2) - (oMenu.offsetWidth/2);

  oCurrentMenu.style.top = iMenuTop;
	oCurrentMenu.style.left = iMenuLeft;
	oCurrentMenu.style.visibility = 'visible';
	oCurrentMenu.style.display = 'block';
}

//
// Opens or closes the menu on mouse over for menu headers
//
function OnMenuHover(oParent, Id, NewClass, bShowMenu)
{
  var oElement = GetElement(Id);
  
  if( Id != oElement == null )
  {
    alert('Invalid Menu Id.');
    return;
  }
    
  if( (NewClass != null) && (NewClass != '') )
    ChangeClass(oElement, NewClass);  // change style class
  
  if( bShowMenu )
    DisplayMenu(oParent, Id);
  else
    SetCloseMenu();
}

//
// Closes menu and follows link (if any) when menu item clicked
//
function OnMenuItemClick(Id, Link)
{
  if( Link.length > 0 )
    top.location.href = Link;
  
  bMenuEntered = false;
  CloseMenu();
}

//
// Changes CSS classes on mouse over for menu item
//
function OnMenuItemHover(Id, NewClass)
{
  var oElement = GetElement(Id);
  
  if( oElement == null )
  {
    alert('Invalid Menu Id.');
    return;
  }
    
  bMenuEntered = true;

  if( NewClass != '' )
    ChangeClass(oElement, NewClass);  // change style class
}

//
// Changes CSS classes and sets menu to close on mouse out for menu item
//
function OnMenuItemOut(Id, NewClass)
{
  var oElement = GetElement(Id);

  if( oElement == null )
  {
    alert('Invalid Menu Id.');
    return;
  }

  if( NewClass != '' )
    ChangeClass(oElement, NewClass);  // change style class

	SetCloseMenu();
}

//
// Sets menu to be closed
//
function SetCloseMenu()
{
  bMenuEntered = false;
	setTimeout('CloseMenu()', 500);
}

//
// Closes menu
//
function CloseMenu()
{
	if( !bMenuEntered && oCurrentMenu )
	{
		if (document.layers)
			oCurrentMenu.visibility = 'hidden';
		else
			oCurrentMenu.style.visibility = 'hidden';
			
		oCurrentMenu = null;
  
  	ChangeClass(oCurrentParent, 'MenuHeader');
  	oCurrentParent = null;
	}
}

//////////////////////////////////////////////////////////

function CreateFooterMenu()
{
  var iCount;
  
  if( aFooterLinks == null )
    return;
  
  iCount = aFooterLinks.length;
  
  for(i = 0; i < iCount; i++)
  {
    if( aFooterLinks[i].length == 2 )
    {
      document.write('<A href="'+aFooterLinks[i][0]+'" class="FooterLink">'+aFooterLinks[i][1]+'</A>');
      if( i != (iCount-1) ) // last item
        document.write(' | ');
    }
  }
}