/* START: user settings */
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

/* END:   user settings */





/*  following SHOULDN'T be touched  */
var hide_is_a_go  = null;         //  stores the currently visible subMenu object...used to hide it after small delay
var hide_index = 0;
var visibleWindowWidth;
var visibleWindowHeight;

window.onload = init;
window.onscroll = pageScroll;

/*
 *  init()
 *    Initializes processes common to all pages
 *  
 *  return: null
 */
function init()
{
  addImgEnlargePanel();
  var bod = document.getElementsByTagName( 'body' )[0];
  
  if ( bod != null )
  {
    var output = bod.innerHTML;
    output += '<br /><div style="font-size: 12px; text-align: center;">' + getLastModifiedDate() + '</div>';
    bod.innerHTML = output;
  };
};

/*
 *  pageScroll()
 *    
 *  
 *  return: null
 */
function pageScroll()
{
  var viewPanel = document.getElementById( 'viewEnlargePanel' );
  if ( viewPanel != null )
  {
    if ( viewPanel.style.visibility == 'visible' )
    {
      positionViewEnlargePanel();
    };
  };
};

/*
 *  getLastModifiedDate()
 *    creates a String of last modified date in easy-to-read format
 *  
 *  return: a String of last modified date in easy-to-read format
 */
function getLastModifiedDate()
{
  var startString = 'Last Modified: ';
  return startString + getHumanDate( new Date( document.lastModified ) );
};


/*
 *  humanDate( input )
 *    converts date object into a nice readable format
 *  
 *  input:  a Date object
 *  return: a String of input date in readable format (ie: "Wednesday July 23rd, 2008")
 */
function getHumanDate( input )
{
  var output;
  try
  {
    var day = input.getDay();
    var month = input.getMonth();
    var date = input.getDate();
    var year;
    try
    {
      year = input.getFullYear();
    }
    catch ( e1 )
    {
      year = input.getYear();
    };
    output = getDayOfWeek( day ) + ' ' + getMonth( month )  + ' ' + getDate( date ) + ', ' + year;
  }
  catch ( e )
  {
    output = '';
  };
  return output;
};

/*
 *  getDate( d )
 *    creates a String of input date
 *  
 *  return: a String of input date
 */
function getDate( d )
{
  var st = '<sup>st</sup>';
  var nd = '<sup>nd</sup>';
  var rd = '<sup>rd</sup>';
  var th = '<sup>th</sup>';
  switch( d )
  {
    case 1:
      return d + st;
      break;
    case 21:
      return d + st;
      break;
    case 31:
      return d + st;
      break;
    case 2:
      return d + nd;
      break;
    case 22:
      return d + nd;
      break;
    case 3:
      return d + rd;
      break;
    case 23:
      return d + rd;
      break;
    default:
      return d + th;
  };
};

/*
 *  getDayofWeek( d )
 *    creates a String of input date
 *  
 *  return: a String of input date
 */
function getDayOfWeek( d )
{
  switch( d )
  {
    case 0:
      return 'Sunday';
      break;
    case 1:
      return 'Monday';
      break;
    case 2:
      return 'Tuesday';
      break;
    case 3:
      return 'Wednesday';
      break;
    case 4:
      return 'Thursday';
      break;
    case 5:
      return 'Friday';
      break;
    case 6:
      return 'Saturday';
      break;
    default:
      return '';
  };
};

/*
 *  getMonth( m )
 *    creates a String of input month
 *  
 *  return: a String of input month
 */
function getMonth( m )
{
  switch( m )
  {
    case 0:
      return 'January';
      break;
    case 1:
      return 'February';
      break;
    case 2:
      return 'March';
      break;
    case 3:
      return 'April';
      break;
    case 4:
      return 'May';
      break;
    case 5:
      return 'June';
      break;
    case 6:
      return 'July';
      break;
    case 7:
      return 'August';
      break;
    case 8:
      return 'September';
      break;
    case 9:
      return 'October';
      break;
    case 10:
      return 'November';
      break;
    case 11:
      return 'December';
      break;
    default:
      return '';
  };
};

/*
 *  GetAbsPosition( object )
 *    Returns an object containing the (x, y) position of the input object along with the objects width/height
 *  
 *  object: the object whose location to be determined
 *  return: an object with 4 properties (x, y, w [width], h [height])
 */
function GetAbsPosition( thing )
{
  var position = new Object;
  position.x = 0;
  position.y = 0;
  position.w = 0;
  position.h = 0;
  
  position.w = thing.offsetWidth;
  position.h = thing.offsetHeight;
  
  if( thing )
  {
    position.x = thing.offsetLeft;
    position.y = thing.offsetTop;
    
    if( thing.offsetParent )
    {
      var parentpos = GetAbsPosition( thing.offsetParent );
      position.x += parentpos.x;
      position.y += parentpos.y;
    };
  };
//  position.w = object.offsetWidth;
//  position.h = object.offsetHeight;
  return position;
};

/*****************************
 *****************************
 **  START: main menu code  **
 *****************************
 *****************************/
/*
 *  newHideIndex()
 *    sets the hide_index variable to a unique value
 *  
 *  return: null*/
function newHideIndex()
{
  if ( hide_index >= 100 )
  {
    hide_index = 0;
  }
  else
  {
    hide_index++;
  };
};

/*
 *  showMenu( index )
 *    If the selected main menu items has a submenu associated with it, it is shown...all other sub menus are hidden
 *  
 *  index:  the index of the element in the menu structure to display
 *  return: null
 */
function showMenu( index )
{
  hide_is_a_go  = null;
  newHideIndex();
  var menu = document.getElementById( 'menu' );
  if ( menu != null )
  {
    var menuItems = menu.getElementsByTagName( 'li' );
    for ( var n=1; n<=menuItems.length; n++ )
    {
      var subList = document.getElementById( 'subMenu' + n );
      if ( subList != null )
      {
        if ( n == index )
        {
          subList.style.visibility = 'visible';
          var outPosition = GetAbsPosition( menuItems[n-1].firstChild );
          subList.style.top = ( outPosition.y + outPosition.h ) + 'px';
          subList.style.left = outPosition.x + 'px';
        }
        else
        {
          subList.style.visibility = 'hidden';
        };
      };
    };
  };
};

/*
 *  holdMenu()
 *    If a sub menu was previously told to disapear, that is cancelled now
 *  
 *  return: null
 */
function holdMenu()
{
  hide_is_a_go = null;
  newHideIndex();
};

/*
 *  hideMenu( 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 hideMenu( index )
{
  var menu = document.getElementById( 'menu' );
  if ( menu != null )
  {
    var subList = document.getElementById( 'subMenu' + index );
    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 ( hide_is_a_go != null )
  {
    if ( index == hide_index )
    {
      hide_is_a_go.style.visibility = 'hidden';
      hide_is_a_go = null;
      newHideIndex();
    };
  };
};
/***************************
 ***************************
 **  END: main menu code  **
 ***************************
 ***************************/
/***********************************
 ***********************************
 **  START: image enlarging code  **
 ***********************************
 ***********************************/
/*
 *  addImgEnlargePanel()
 *    
 *  
 *  return null
 */
function addImgEnlargePanel()
{
  try
  {
    var headerText = '<div id="viewEnlargePanel" class="viewEnlargePanel" onClick="closeImgPanel();"><img class="viewEnlargeImg" id="viewEnlargePanelImg" src="" title="click to shrink" /></div><div id="regularPanel">';
    var footerText = '</div>';
    var bod = document.getElementsByTagName( 'body' )[0];
    if ( bod != null )
    {
      var bodyText = headerText + bod.innerHTML + footerText;
      bod.innerHTML = bodyText;
    };
  }
  catch (err)
  {
  };
};

/*
 *  positionViewEnlargePanel()
 *    ensures that, if the Enlarged Image Panel is visible, it is always in the users screen
 *  
 *  return: null
 */
function positionViewEnlargePanel()
{
  var viewPanel = document.getElementById( 'viewEnlargePanel' );
  if ( viewPanel != null )
  {
    if ( viewPanel.style.visibility == 'visible' )
    {
      visibleWindowWidth = window.innerWidth;
      visibleWindowHeight = window.innerHeight;
      if ( visibleWindowWidth == undefined )
      {
        visibleWindowWidth = document.body.offsetWidth;
        visibleWindowHeight = document.body.offsetHeight;
      };
      
      viewPanel.style.top = document.body.scrollTop;
      viewPanel.style.left = document.body.scrollLeft;
    };
  };
};

/*
 *  enlargeImage( img )
 *    The source of the input img object is used.
 *    Another img tag is used to occupy the entire screen
 *  
 *  img:    image object that is to be enlarged
 *  return: null
 */
function enlargeImage( img )
{
  showMenu( -1 );     //  ensures that no sub menus are visible
  var viewPanel = document.getElementById( 'viewEnlargePanel' );
  var viewPanelImg = document.getElementById( 'viewEnlargePanelImg' );
  var regularPanel = document.getElementById( 'regularPanel' );
  if ( ( regularPanel != null ) && ( viewPanel != null ) && ( viewPanelImg != null ) && ( img != null ) )
  {
    try
    {
      regularPanel.style.visibility = 'hidden';
      viewPanel.style.visibility = 'visible';
      
      positionViewEnlargePanel();
      
      viewPanelImg.src = img.src;
      
      var iW = viewPanelImg.offsetWidth;
      var iH = viewPanelImg.offsetHeight;
      var pW = visibleWindowWidth;
      var pH = visibleWindowHeight;
      
      if ( ( pH / pW ) > ( iH / iW ) )
      {
        viewPanelImg.style.height = '';
        viewPanelImg.style.width = '100%';
      }
      else
      {
        viewPanelImg.style.height = '100%';
        viewPanelImg.style.width = '';
      }
    }
    catch (err)
    {
      viewPanel.style.visibility = 'hidden';
      regularPanel.style.visibility = 'visible';
      viewPanel.width = 0;
      viewPanel.height = 0;
    };
  };
};

/*
 *  closeImgPanel()
 *    If the Enlarged Image Panel is open...this closes it and ensures that the regular view panel is visible
 *  
 *  return: null
 */
function closeImgPanel()
{
  var viewPanel = document.getElementById( 'viewEnlargePanel' );
  var viewPanelImg = document.getElementById( 'viewEnlargePanelImg' );
  var regularPanel = document.getElementById( 'regularPanel' );
  if ( ( regularPanel != null ) && ( viewPanel != null ) )
  {
    if ( viewPanelImg != null )
    {
      viewPanelImg.src = '';
    };
    viewPanel.width = 0;
    viewPanel.height = 0;
    regularPanel.style.visibility = 'visible';
    viewPanel.style.visibility = 'hidden';
  };
};
/*********************************
 *********************************
 **  END: image enlarging code  **
 *********************************
 *********************************/