/*
file: main.js

Modified 2004may24: added viewNextOrPreviousMsg() so user may skip
through scrolling messages, rather than wait for each message to
scroll by.

Modified 2002feb20: Changed scrollerPeriod to 6000 from 5000 milliseconds.

2001jan5 phj: Original.  Main variables and functions used by HTML home page.
*/

// Constants
var LEFT = 0; // index into arrays associated with left scrolling messages.
var RIGHT = 1; // index into arrays associated with right scrolling messages.

// Variables
var resizeCalls = 0;

/* 2010apr13, phj: Adding a right message scroller to the existing message
 * scroller, so we'll maintain the left messages in scrollMsg[0] array and
 * maintain the right messages in scrollMsg[1] array.  Similarly for
 * scrollerState, scrollMsgPeriod.  Wish IE supported const. */

var scrollMsg = new Array(); //holds messages that are displayed in the
			       // "scrolling area".
scrollMsg[RIGHT] = new Array();
scrollMsg[LEFT] = new Array();

var scrollerDiv = new Array();
scrollerDiv[RIGHT] = 'textScrollerRight';
scrollerDiv[LEFT] = 'textScroller';

var scrollerState = new Array();
scrollerState[RIGHT] = 0; // range is [ 0, numMessages - 1 ].
scrollerState[LEFT] = 0; // range is [ 0, numMessages - 1 ].

// 2008jan17, phj: Added scrollMsgPeriod, so each message has its own
// display period.
var scrollMsgPeriod = new Array(); //time, in milliseconds, a message is
                                   // displayed.
scrollMsgPeriod[RIGHT] = new Array();
scrollMsgPeriod[LEFT] = new Array();

/*
scrollMsg[ 0 ] = '<A HREF="School/craftfairflyer.pdf"> <IMG SRC="./Images/Scroll/scroll000147.png" BORDER=0 ALT="Holiday Craft Fair - Nov. 19, 10 AM-3 PM."> </A>';

scrollMsg[ 0 ] = '<A HREF="MusicAndLiturgy/masses.html"> <IMG SRC="./Images/Scroll/scroll000149.png" BORDER=0 ALT="Please view our Christmas Mass schedule."> </A>';

scrollMsg[ 0 ] = '<A HREF="School/openHouse2006.pdf"> <IMG SRC="./Images/Scroll/scroll000150.png" BORDER=0 ALT="Open House: Pre-School Jan. 11, 2006, K-8 Feb. 1, 2006."> </A>';

scrollMsg[ 2 ] = '<A HREF="School/SAPA/sapa.html#CFC"> <IMG SRC="./Images/Scroll/scroll000148.png" BORDER=0 ALT="Support St. Andrew Apostle School via the CFC."> </A>';

scrollMsg[ 0 ] = '<A HREF="School/schoolBoard.html"> <IMG SRC="./Images/Scroll/scroll000139.png" BORDER=0 ALT="Golf and support St. Andrew Apostle School."> </A>';

scrollMsg[ 0 ] = '<A HREF="School/SAPA/sapa.html"> <IMG SRC="./Images/Scroll/scroll000158.png" BORDER=0 ALT="St. Andrew Apostle Craft Fair, Nov. 15, 10:00 AM-3:00 PM."> </A>';
scrollMsgPeriod[ 0 ] = 6000;

scrollMsg[ 0 ] = '<IMG SRC="./Images/Scroll/scroll000160.png" BORDER=0 ALT="St. Andrew Apostle School Open House, Wed. Feb. 11, 9:00 AM - 11:00 AM.">';
scrollMsgPeriod[ 0 ] = 6000;

scrollMsg[ 0 ] = '<p><span class="emphasis">Saturday, Dec. 19, 2009:</span> Due to inclement weather, <span class="sortofred emphasis">First Reconciliation</span> will <span class="emphasis">NOT</span> be held today. </p>';
scrollMsgPeriod[ 0 ] = 8000;

scrollMsg[ 0 ] = '<div class="center"><span class="emphasis sortofred">St. Andrew Apostle School</span><br/><br/><span class="emphasis">Open House</span><br/><br/>Visit Pre-K - Grade 8 Classes<br/><br/>Wednesday, February 10<br/>9:00 AM till 11:00 AM</div>';
scrollMsgPeriod[ 0 ] = 6000;

scrollMsg[ 0 ] = '<IMG SRC="./Images/Scroll/scroll000161.png" BORDER=0 ALT="St. Andrew Apostle School Open House, Wed. Mar. 3, 9:00 AM - 11:00 AM.">';
scrollMsgPeriod[ 0 ] = 6000;
*/

scrollMsg[LEFT][0] = '<div id="textScroller" class="center" style="font-size:12px;"><br/><br/><span class="sortofred">St. Andrew Apostle School</span><br/><br/><br/><span style="font-size:14px;" class="emphasis">New Principal Hired</span><br/><br/><span class="subNav"><a href="School/2010/principalHired.html">Mr. Gary Michael Rand II</a></span><br/><br/><a href="School/2010/principalHired.html" class="scrollingMessage">read more ...</span></a></div>';
scrollMsgPeriod[LEFT][0] = 6000;

scrollMsg[LEFT][1] = '<div id="textScroller" class="center" style="font-size:12px;"><br/><br/>Johns Hopkins University<br/>designates our school a 2009<br/><br/><span style="font-size:14px;" class="emphasis">Center for Talented Youth</span><br/><span style="font-size:14px;" class="emphasis sortofred">“Top School”</span><br/><br/>in Maryland<br/><a href="School/awards.html" class="scrollingMessage">read more ...</a></div>';
scrollMsgPeriod[LEFT][1] = 6000;

scrollMsg[LEFT][2] = '<div id="textScroller" class="center" style="background-image: url(/Images/Scroll/BlueRibbon2007-trimmed-scaled-opacity.jpg); background-position: center; background-repeat: no-repeat"><span style="font-size:14px;" class="emphasis"><span class="sortofred"><br/><br/>St. Andrew Apostle School</span><br/><br/>2007 Dept. of Education<br/><br/>Blue Ribbon School</span></div>';
scrollMsgPeriod[LEFT][2] = 6000;

/* Should replace ./Images/ with /Images/ */
scrollMsg[LEFT][3] = '<div id="textScroller" class="center"><br/><br/><br/><br/><span class="emphasis"><span class="indexstandardblue">Our community prays<br/>for peace</span><br/>based on justice.<br/>God bless us all.</span></div>';
/*
scrollMsg[RIGHT][3] = '<IMG SRC="./Images/Scroll/scroll000100a.gif" ALT="Pray for peace based on justice.">';
*/
scrollMsgPeriod[LEFT][3] = 6000;


scrollMsg[RIGHT][0] = '<div id="textScrollerRight" style="font-size:12px;"><span class="sortofred">St. Andrew Apostle School</span><p class="book">Volunteers must complete a<br/><span class="indexstandardblue">criminal background check</span><br/>and be <span class="indexstandardblue">fingerprinted.</span></p><a class="scrollingMessage" href="General/FingerPrint/fingerprint.html">read more ...</a></div>';
/*
scrollMsg[RIGHT][0] = '<div id="textScrollerRight"><a href="General/FingerPrint/fingerprint.html"> <IMG SRC="./Images/Scroll/scroll000140.png" BORDER=0 ALT="Volunteers must complete a criminal background check and be fingerprinted."> </A></div>';
*/
/*
scrollMsg[RIGHT][0] = '<A HREF="General/FingerPrint/fingerprint.html"> <IMG SRC="./Images/Scroll/scroll000140.png" BORDER=0 ALT="Volunteers must complete a criminal background check and be fingerprinted."> </A>';
*/
scrollMsgPeriod[RIGHT][0] = 6000;

/* ---------------------------------------------------------------------

A text message, such as the following, works, but is very difficult to
format correctly.

scrollMsg[ 5 ] = '<P CLASS="sortofred">Christmas Mass Schedule</SPAN></P><P>Christmas Eve, Friday, December 24</P><P>Children&quot;s Masses at 3:30 PM and 5:30 PM</P><P>"Vigil Mass" at 8:00 PM</P><P>"Midnight Mass" at 10:00 PM</P><P>Christmas Day, Saturday, December 25</P><P>10:00 AM Mass</P>';

scrollMsg[ 0 ] = '<A HREF="javascript:myOpenWindow( \'General/christmasConcert-2004.html\', 400, 300 )"> <IMG SRC="./Images/Scroll/scroll000141.png" BORDER=0 ALT="Annual Christmas Concert."> </A>';

scrollMsg[ 0 ] = '<IMG SRC="/Images/Scroll/scroll000136.gif">';

scrollMsg[ 1 ] = '<A HREF="General/goodGolfStart.html"> <IMG SRC="./Images/Scroll/scroll000139.png" BORDER=0 ALT="Golf and support St. Andrew Apostle School."> </A>';

scrollMsg[ 2 ] = '<IMG SRC="./Images/Scroll/scroll000138.gif" BORDER=0 ALT="All the Summer Reading Information you could possibly want.">';

scrollMsg[ 1 ] = '<A HREF="javascript:myOpenWindow( \'General/christmas.html\', 480, 350 )"> <IMG SRC="./Images/Scroll/scroll000113.gif" BORDER=0 ALT="Christmas and New Year Mass schedule> </A>';

scrollMsg[ 1 ] = '<IMG SRC="./Images/Scroll/scroll000124.gif" BORDER=0 ALT="Wednesday, Aug. 27, Kindergarten Orientation at 7:00 PM, New Parents Orientation at 8:00 PM.">';

scrollMsg[ 1 ] = '<A HREF="School/openhouse.html"> <IMG SRC="./Images/Scroll/scroll000128.gif" BORDER=0 ALT="Wednesday, Dec. 3, Kindergarten and Pre-School Open House, 9:00-10:30, 1:00-2:00.">';

scrollMsg[ 2 ] = '<A HREF="javascript:myOpenWindow( \'School/emergencyCard.html\', 480, 350 )"> <IMG SRC="./Images/Scroll/scroll000118.gif" BORDER="0" ALT="Emergency Dismissal Card."> </A>';
------------------------------------------------------------------------- */


var numMessages = new Array(); //number of messages to be scrolled.
numMessages[RIGHT] = scrollMsg[RIGHT].length; //# messages to be scrolled.
numMessages[LEFT] = scrollMsg[LEFT].length; //# messages to be scrolled.

var timeOutID = new Array(); //ID returned by a call to setTimeout(); used
                             // in call to clearTimeout().

var currentMenuChoice = 0; // points to current menu item chosen.
var MenuChoiceMax = 7; // # of main menu items.
var currentSubMenuChoice = 0; // values may be 0, 1, 2, 3, ...

// 2008mar08, phj: Assigned "more correct" initial value to zeroBasedWidth;
// previously was 600.
var zeroBasedWidth = 750; // estimated position of right most pixel in
	 // right most image on the home page.  That image is church3.jpg.

var leftMargin = 0; // initially there is no left margin.  if window
		     // width increases, leftMargin becomes positive.  is
		     // changed in calcLeftMargin() and referenced in
		     // checkit().

// Top, right, bottom, left.  Values along Y axis increase as you move down
// the screen; values along X axis increase as you move to the right.  See also
// checkIt().
var activeScreenAreaLimits = new Array( 120, 680, 395, 0 );



function init()
{       
// Stuff that is done when the page loads or reloads.  See <BODY> element.

// 2008mar08, phj: No longer need to call repositionLeft(), as style sheets now
// center the page content.  Note there were two references to repositionLeft
// in this function.  Made window.onresize call calcLeftMargin and assigned an
// initial value to leftMargin by calling calcLeftMargin().

// Remove enclosing frame, if such exists.
     if (top.location != location)
        top.location.href = document.location.href;

    if (document.images)
    {
// Show the "off" version of each menu item.
        for( myMenu = 0; myMenu < MenuChoiceMax; myMenu++ )
            show( "layer" + myMenu + "off" );

    } //if (document.images)

    if ( nscp )
    {
	/* 2010apr13, phj: Got this "Warning: Use of captureEvents() is
	   deprecated. To upgrade your code, use the DOM 2 addEventListener()
	   method. For more help
	   http://developer.mozilla.org/en/docs/DOM:element.addEventListener" */

      document.captureEvents(Event.MOUSEMOVE);
    }            

    document.onmousemove = checkIt; //call checkIt() when mouse moves.
    //2008mar08    window.onresize = repositionLeft;
    //2008mar08, phj: Added next two statements.
    window.onresize = calcLeftMargin;
    calcLeftMargin();

    JSscroller2(LEFT); // start the JS scrollers.
    JSscroller2(RIGHT);

    if ( numMessages[LEFT] == 1 ) // no need for "next message" &
    {			    // "previous message" arrows.
      hide( "nextMsg" );
      // 2010apr14, phj: Currently only using control arrow in nextMsg, as
      // two arrows clutters things.
      //      hide( "prevMsg" );
    }

    if ( numMessages[RIGHT] == 1 ) // no need for "next message" &
    {			    // "previous message" arrows under right text area.
      hide( "nextMsgRight" );
      // 2010apr14, phj: Currently only using control arrow in nextMsgRight, as
      // two arrows clutters things.
      //      hide( "prevMsgRight" );
    }

    checkMenu(); //monitor the current choice and menus.
//phj 2002mar18    showtime(); // so time is displayed.

    return true;

}//end of init()



function menuRoll( which, status )
{

// menuRoll() is called when the cursor is placed over a menu item, and that
// menu item corresponds to which, i.e., if the cursor is on the 0th menu
// item, which is 0, if the cursor is on the 1st menu item, which
// is 1, if the cursor is on the 2nd menu item, which is 2, etc.

// Hides the "off" menu item that is associated with which, makes visible the
// "on" menu that is associated with which, and makes visible the submenu that
// is associated with which.

// Note this function does essentially nothing when status is false.

// Sample calls are: menuRoll( 0, true ) or menuRoll( 2, false ).

       if ( status )
       {
// Hide the off version, show the on version, and show the submenu.
	  hide( "layer" + which + "off" );
	  show( "layer" + which + "on" );
//phj 2002mar18	  hide("message" ); //added 2001dec02 to hide the message so a
	                    // submenu will be visible.
	  show("submenu" + which + "0" );

// Added 2001nov12.  When a menu item is chosen, hide the current
// level 2 submenu.

	  myWaste = l2smenuRoll( currentSubMenuChoice, false )

// Added 2002mar19.  When a menu item is chosen, show the "faded"
// timelypic, which automatically hides the timelypicture.
	  show( "fadedtimelypicture" );

	  currentMenuChoice = which; // keep track of the choice.
        }// if ( status )

	return true;

}//end of menuRoll().



function l2smenuRoll( subChoice, status )
{

// l2smenuRoll() is called, generally with onmouseover(), when the cursor
// moves onto a submenu item.  The number of the submenu item corresponds to
// subChoice, i.e., if the cursor is on the 0th submenu item, subChoice is 0,
// if the cursor is on the 1st submenu item, subChoice is 1, if the cursor
// is on the 2nd submenu item, subChoice is 2, etc.

// Iff status, hides the level 2 submenu associated with currentSubMenuChoice,
// sets currentSubMenuChoice to subChoice, and makes visible the level 2
// submenu items that are associated with subChoice.

// If not status and if subChoice is greater than -1, hides the level 2
// submenu associated with currentMenuChoice and subChoice (if any).

// If not status and subChoice is -1, hides the level 2 submenu associated
// with currentMenuChoice and currentSubMenuChoice.

// Sample call: l2smenuRoll( 0, true )

// alert( subChoice + "  " + status );

  if ( status )
  {

// Use "currentSubMenuChoice != subChoice" to check whether the
// previous and current submenu choices are the same, so the same menu
// isn't flashed on and off.

    if ( currentSubMenuChoice != subChoice )
      if (objExists("l2smenu" + currentMenuChoice + currentSubMenuChoice))
        hide( "l2smenu" + currentMenuChoice + currentSubMenuChoice );

    currentSubMenuChoice = subChoice; // keep track of the choice.

//    alert("l2smenu" + currentMenuChoice + currentSubMenuChoice );
// Defensive programming: show it only if it exists.
    if (objExists("l2smenu" + currentMenuChoice + currentSubMenuChoice))
      show( "l2smenu" + currentMenuChoice + currentSubMenuChoice );
  }

  else // hide something.
  {
    if ( subChoice == -1 ) // hide the current level 2 submenu.
    {
      if (objExists("l2smenu" + currentMenuChoice + currentSubMenuChoice))
        hide( "l2smenu" + currentMenuChoice + currentSubMenuChoice );
    }
    else // hide the specified level 2 submenu.
    {
      if (objExists("l2smenu" + currentMenuChoice + subChoice))
        hide( "l2smenu" + currentMenuChoice + subChoice );
    }
  }

  return true;

}//end of l2smenuRoll().



function checkIt( evt )
{

// checkIt() is called whenever the mouse moves, as specified in
// init().  Ensures that currentMenuChoice has an undefined value
// whenever the cursor is outside the active screen area, i.e., the
// screen area that holds the menu/submenus.  Note that the
// (spatially) upper y bound corresponds to the top of the upper menu
// images, while the (spatially) lower y bound corresponds to the
// bottom of the lower menu images.

  x1 = (nscp) ? evt.pageX : event.clientX;
  y1 = (nscp) ? evt.pageY : event.clientY + document.body.scrollTop;

/*
leftMargin is defined in repositionLeft(), which see.  Essentially
defines a white space left margin when the browser window is wider
than the width of the page.

2004dec06, phj: changed "x1 > (400 ..." to "x1 > (600 ..." so the
menus may extend farther to the right.

2006nov30, phj: changed y1 > 332 to y1 > 350 so addition School submenu
items could be displayed and clicked.
*/

// 2008mar08, phj: Try removing leftMargin from x1 < test.  That works.
// 2008mar08, phj: Better: remove 11 from x1 < test.  That works.
//  if ((( x1 > (600 + leftMargin) ) || ( x1 < (11 + leftMargin) )) ||
// 2008mar08, phj: Use activeScreenAreaLimits values vice hard coded constants.
//  if ((( x1 > (600 + leftMargin) ) || ( x1 < leftMargin )) ||
  if ((( x1 > ( activeScreenAreaLimits[ 1 ] + leftMargin) ) ||
       ( x1 < leftMargin + activeScreenAreaLimits[ 3 ] )) ||
      (( y1 > activeScreenAreaLimits[ 2 ] ) ||
       ( y1 < activeScreenAreaLimits[ 0 ] )))
  {

//Added 2001nov12 so level 2 submenus disappear when mouse moves "out
// of bounds".

    myWaste = l2smenuRoll( currentSubMenuChoice, false );

// Added 2002mar19.  When cursor moves "out of bounds", hide the faded
// timely picture div, which automatically shows the timelypicture div.
    hide( "fadedtimelypicture" );

    currentMenuChoice = -1;
    currentSubMenuChoice = -1;
  }

  return true;

}//end of checkIt().



function checkMenu()
{

// Called every 100 milliseconds, this function hides every submenu
// that is not associated with the currentMenuChoice, hides every "on"
// menu item that is not associated with the currentMenuChoice, and makes
// visible every "off" menu item that is not associated with the
// currentMenuChoice.

  for( myMenu = 0; myMenu < MenuChoiceMax; myMenu++ )
  {
     if( myMenu != currentMenuChoice )
     {
       hide( "submenu" + myMenu + "0" );
       hide( "layer" + myMenu + "on" );  //hide the "on" version of the menu.
       show( "layer" + myMenu + "off" ); //show the "off" version of the menu.
     }
  }

// Added 2001dec02, so the message will be visible when NO menu item
// is chosen.
//phj 2002mar18   if ( currentMenuChoice == -1 )
//phj 2002mar18     show( "message" );

  setTimeout( "checkMenu()", 100 ); //do this again in 100 milliseconds.

  return true;

}//end of checkMenu().



/*

2008mar08, phj: Calculates the left margin, which is used in checkIt()
to determine the active screen area right boundary.  As the left
margin grows/shrinks, so does that right boundary identically.

2002mar09 phj: removed "var leftMargin;" and put it where globals are
defined so it may be used in checkit().
*/

function calcLeftMargin( )
{
  var frameWidth;

  if (self.innerWidth) //Netscape stuff
  {
  	frameWidth = self.innerWidth;
  }
  else if (document.documentElement &&
	   document.documentElement.clientWidth) //IE 6
  {
	frameWidth = document.documentElement.clientWidth;
  }
  else if (document.body) //IE 5
  {
  	frameWidth = document.body.clientWidth;
  }
  else // would like to do something other than returning; but what?
    return;

  leftMargin = ( frameWidth - zeroBasedWidth ) / 2;

  if ( leftMargin > 0 ) // leave it be.
    ;

  else			 // there should be no left margin.
    leftMargin = 0;

  //  alert( "calcLeftMargin: leftMargin is: " + leftMargin +
  //	 ", width is: " + frameWidth + "." );

  return;
} //end of calcLeftMargin().



function JSscroller2( RorL )
{
/*
Writes text into a <div> on the web page.  The text displays a message, and
possibly a background image, or may just display an image.

Value of RorL should be either the constant RIGHT or the constant LEFT.  Other
values will likely lead to pain.

2002oct01, phj: Now call setTimeout() iff there is more than one message.

2008jan17, phj: Call setTimeout() with per message display period.

2010apr13, phj: Change so two separate scrolling areas are handled and use
jQuery.
*/
    var displayPeriod = scrollMsgPeriod[RorL][ scrollerState[RorL]];
    if ( RorL == LEFT )
	$('#textScroller').replaceWith( scrollMsg[RorL][scrollerState[RorL]] );
    else
	$('#textScrollerRight').replaceWith( scrollMsg[RorL][scrollerState[RorL]] );
    /*	writeIt( scrollMsg[RorL][scrollerState[RorL]], scrollerDiv[RorL] ); */

    scrollerState[RorL] ++;
    if ( scrollerState[RorL] >= numMessages[RorL] )
      scrollerState[RorL] = 0;

    if ( numMessages[RorL] > 1 ) // set timer to call this function again.
	// Will passing setTimeout a function with args work?  Not until I read
	// http://www.makemineatriple.com/2007/10/passing-parameters-to-a-function-called-with-settimout/
	timeOutID[RorL] = setTimeout( function(){JSscroller2(RorL)}, displayPeriod );
    //      timeOutID[RorL] = setTimeout( "JSscroller2(RorL)", displayPeriod );

} // end of function JSscroller2().



function viewNextOrPreviousMsg( RorL, direction )
{

/*
Displays the previous or next scrolling message, depending on whether
arg direcition is -1 or 1.  Note if direction is neither -1 or 1, this
function does essentially nothing.  Called from html file when user
clicks the previous or next arrow.

Documentation of clearTimeout() is at
devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/window.html#1201775

2010apr13, phj: Added RorL arg, which chooses the right or left message area.
*/

    if ( numMessages[RorL] > 1 ) // do some work.
    {
	clearTimeout( timeOutID[RorL] ); // cancel pending timeout since user
			           // is choosing message she wants to see.

	if ( direction == -1 ) // view previous message.
	{
	  scrollerState[RorL] -= 2; // Decrement twice since scrollerState is
	      // incremented AFTER current message is displayed in JSscroller().

	  if ( scrollerState[RorL] < 0 ) // boundary condition.
	    scrollerState[RorL] += (numMessages[RorL]);
	  JSscroller2(RorL); //let the existing function do the work.
	}

	else if ( direction == 1 ) // view next.  Since scrollerState already
		// points to the next message, nothing to do but display it...
	{
	  JSscroller2(RorL); // ...which this function does.
	}
    }
} // end of function viewNextOrPreviousMsg().
