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.html" width="125px" height="380px" frameborder="0" scrolling="no">' +
           '<p>Your browser does not support iframes.</p>' +
         '</iframe>';
/*
ads[0] = '<table width="100px" cellpadding="0px" cellspacing="0px" border="0px">' +
           '<tr>' +
             '<td align="center">' +
               '<span style="color: #F28242; font-size: 20px; font-weight: bold;">Order Now</span>' +
               '<br />' +
               '<br />' +
               '<span style="color: #0098D9; font-size: 14px; font-weight: normal;">Canadian <i>Orders</i>:</span>' +
               '<br />' +
               '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">' +
                 '<input type="hidden" name="cmd" value="_s-xclick">' +
                 '<input type="hidden" name="hosted_button_id" value="3409395">' +
                 '<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_paynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">' +
                 '<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">' +
               '</form>' +
               '<br />' +
               '<span style="color: #0098D9; font-size: 14px; font-weight: normal;">US <i>Orders</i>:</span>' +
               '<br />' +
               '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">' +
                 '<input type="hidden" name="cmd" value="_s-xclick">' +
                 '<input type="hidden" name="hosted_button_id" value="3409800">' +
                 '<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_paynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">' +
                 '<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">' +
               '</form>' +
               '<br />' +
               '<span style="color: #0098D9; font-size: 14px; font-weight: normal;">International/Bulk <i>Orders</i>:</span>' +
               '<br />' +
               '<a style="color: #0098D9; font-size: 20px; font-weight: bold;" href="mailto:billing@dyingtolivewell.com?Subject=International%20Shipping%20inquiry&Body=I%20would%20like%20to%20know%20how%20much%20would%20shipping%20be%20for%20me?">Inquire</a>' +
             '</td>' +
           '</tr>' +
         '</table>';
*/

/* MENU OBJECT */
var menuItems = new Array(1);
menuItems[0] = new Object();
menuItems[0].name = 'The Book';
menuItems[0].link = 'index.html';
menuItems[0].menuMode = 'both';
menuItems[0].newFooterLine = 'false';
menuItems[0].styleOverride = '';
menuItems[0].children = null;
menuItems[1] = new Object();
menuItems[1].name = 'Blog';
menuItems[1].link = 'blog.html';
menuItems[1].menuMode = 'both';
menuItems[1].newFooterLine = 'false';
menuItems[1].styleOverride = '';
menuItems[1].children = null;
menuItems[2] = new Object();
menuItems[2].name = 'The Authors';
menuItems[2].link = 'authors.html';
menuItems[2].menuMode = 'both';
menuItems[2].newFooterLine = 'false';
menuItems[2].styleOverride = '';
menuItems[2].children = new Array(1);
menuItems[2].children[0] = new Object;
menuItems[2].children[0].name = 'Renate Weiler';
menuItems[2].children[0].link = 'authors_renateWeiler.html';
menuItems[2].children[0].styleOverride = '';
menuItems[2].children[1] = new Object;
menuItems[2].children[1].name = 'Bren Comacchio';
menuItems[2].children[1].link = 'authors_brenComacchio.html';
menuItems[2].children[1].styleOverride = '';
menuItems[2].children[2] = new Object;
menuItems[2].children[2].name = 'Kim Redman';
menuItems[2].children[2].link = 'authors_kimRedman.html';
menuItems[2].children[2].styleOverride = '';
menuItems[2].children[3] = new Object;
menuItems[2].children[3].name = 'Deborah Johnson';
menuItems[2].children[3].link = 'authors_deborahJohnson.html';
menuItems[2].children[3].styleOverride = '';
menuItems[3] = new Object();
menuItems[3].name = 'Testimonials';
menuItems[3].link = 'testimonials.html';
menuItems[3].menuMode = 'both';
menuItems[3].newFooterLine = 'false';
menuItems[3].styleOverride = '';
menuItems[3].children = null;
menuItems[4] = new Object();
menuItems[4].name = 'Media';
menuItems[4].link = 'media.html';
menuItems[4].menuMode = 'both';
menuItems[4].newFooterLine = 'false';
menuItems[4].styleOverride = '';
menuItems[4].children = null;
menuItems[5] = new Object();
menuItems[5].name = 'Contact Us';
menuItems[5].link = 'contact.html';
menuItems[5].menuMode = 'both';
menuItems[5].newFooterLine = 'true';
menuItems[5].styleOverride = '';
menuItems[5].children = null;
menuItems[6] = new Object();
menuItems[6].name = 'Site Map';
menuItems[6].link = 'siteMap.html';
menuItems[6].menuMode = 'bottom';
menuItems[6].newFooterLine = 'false';
menuItems[6].styleOverride = '';
menuItems[6].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;

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 )
  {
    var menuColCount = 1;
    if ( menuItems != null )
    {
      menuColCount = menuItems.length;
    };
    var output = '';
    var outChildrenDivs = '';
    var header = '<map name="header_memberCollageMap">' +
                   '<area shape="rect" coords="0,0,132,199" href="index.html" alt="Dying to Live Well" title="Dying to Live Well" />' +
                   '<area shape="rect" coords="133,5,254,199" href="authors_renateWeiler.html" alt="Renate Weiler" title="Renate Weiler" />' +
                   '<area shape="rect" coords="255,40,404,199" href="authors_brenComacchio.html" alt="Bren Comacchio" title="Bren Comacchio" />' +
                   '<area shape="rect" coords="410,40,546,199" href="authors_kimRedman.html" alt="Kim Redman" title="Kim Redman" />' +
                   '<area shape="rect" coords="547,10,683,199" href="authors_deborahJohnson.html" alt="Deborah Johnson" title="Deborah Johnson" />' +
                 '</map>' +
                 '<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">' +
                             '<img src="images/dying_to_live_well_author_collage.jpg" height="200px" usemap="#header_memberCollageMap" />' +
                           '</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: 20px;">';
                   
    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>';
    output = header + 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';
        };
      };
    };
  };
};