/******************************************************************************************
*
* General Functions for Congregation Machzikei Hadas web site
*
*******************************************************************************************/

//******************************************************************************************
//	Global Variables
//******************************************************************************************

var curText = 1;					// Text rendering: 1=light on dark; 0=dark on light.
var dateModified = "July 29, 2010";		// Last modified date. 

/*****************************************************************************
*
*       createCookie - Creates a cookie
*
*****************************************************************************/

function createCookie (

	name,								// Name of cookie.
   value,                     // Value for cookie.
   days)                      // Number of days until cookie expiration.

{
	// Local variables.
   
   var date,						// A date.
       expires;					// Expiration date.

	if (days)
   {
      date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires=" + date.toGMTString();
	}
	else
      expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}


/*****************************************************************************
*
*       deleteCookie - Deletes a cookie
*
*****************************************************************************/

function deleteCookie (

	name)								// Name of cookie to be deleted.

{
   createCookie(name, "", -1);
}


/*****************************************************************************
*
*       findPosX - Return "x" position of an element
*
*****************************************************************************/

function findPosX (

	obj)								// Object or element in question.

{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}


/*****************************************************************************
*
*       findPosY - Return "y" position of an element
*
*****************************************************************************/

function findPosY (

	obj)								// Object or element in question.

{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


/*****************************************************************************
*
*       getClientHeight - Return height of client window 
*
* Note: In Firefox, this can sometimes return the doc height which MAY be
* SMALLER than the client height. But this may be OK anyway.
*
*****************************************************************************/

function getClientHeight ()

{
	return getMinNonZero (
		typeof(window.innerHeight) == 'number' ? window.innerHeight : 0,
		document.documentElement && typeof(document.documentElement.clientHeight) == 'number' ? document.documentElement.clientHeight : 0,
		document.body && typeof(document.body.clientHeight) == 'number' ? document.body.clientHeight : 0);
}


/*****************************************************************************
*
*       getClientWidth - Return width of client window
*
*****************************************************************************/

function getClientWidth ()

{
	return getMinNonZero (
		typeof(window.innerWidth) == 'number' ? window.innerWidth : 0,
		document.documentElement && typeof(document.documentElement.clientWidth) == 'number' ? document.documentElement.clientWidth : 0,
		document.body && typeof(document.body.clientWidth) == 'number' ? document.body.clientWidth : 0);
}


/*****************************************************************************
*
*       getMinNonZero - Return a non-zero minimum
*
* This is a utility function that compares 3 values and returns the smallest
* value (of the 3) that is greater than 0.
*
*****************************************************************************/

function getMinNonZero (

   x1, x2, x3)                // The values to compare.

{
   var x = (x1 > 0  ?  x1 : 0);
   if (x2 > 0  &&  (x1 <= 0 || x2 < x))  x = x2;  
   if (x3 > 0  &&  (x1 <= 0 || x3 < x))  x = x3;  
	return x;
}


/*****************************************************************************
*
*       getScrollX - Return amount client window scrolled horizontally
*
*****************************************************************************/

function getScrollX ()

{
	return getMinNonZero (
		typeof(window.pageYOffset) == 'number' ? window.pageXOffset : 0,
		document.documentElement && typeof(document.documentElement.scrollLeft) == 'number' ? document.documentElement.scrollLeft : 0,
		document.body && typeof(document.body.scrollLeft) == 'number' ? document.body.scrollLeft : 0);
}


/*****************************************************************************
*
*       getScrollY - Return amount client window scrolled vertically
*
*****************************************************************************/

function getScrollY ()

{
	return getMinNonZero (
		typeof(window.pageYOffset) == 'number' ? window.pageYOffset : 0,
		document.documentElement && typeof(document.documentElement.scrollTop) == 'number' ? document.documentElement.scrollTop : 0,
		document.body && typeof(document.body.scrollTop) == 'number' ? document.body.scrollTop : 0);
}


/*****************************************************************************
*
*       hideDefinition - Hide a definition window
*
* This function gets rid of a definition (pop up window) of a word contained in
* a <span> element.
*
*****************************************************************************/

function hideDefinition ()

{
   // Local variables.
   
   var  box;                  // Window containing definition.

   // Obtain definition. Hide by making it invisible and off the view port.

   box = document.getElementById("definitionBox");
   box.innerHTML = " ";
   box.style.visibility = "hidden";
   box.style.top ="-20px";
   box.style.left ="-20px";
   box.style.bottom = "auto";
   box.style.right = "auto";
   box.style.zIndex = "-10";
   box.style.minWidth = "0px"; 
}


/*****************************************************************************
*
*       makeBottom - Output copyright, update information and site map
*
*****************************************************************************/

function makeBottom ()

{
   var  text;
   
   text =
      '<div style="font-style:normal;font-size:120%;"><a href="#" onclick="scroll(0,0);">Back to top</a></div>' +
      '<br />' +
      '<p><hr size="1" width="175" />' +
      'Copyright © 2007 Congregation Machzikei Hadas<br />' +
      'Last modified on ' + dateModified +
      '<hr size="1" width="175" />' +
      '<br />' +
      '<a href="index.htm">Home</a>&nbsp;| ' +
      '<a href="contact us.htm">Contact&nbsp;Us</a>&nbsp;| ' +
      '<a href="schedule.htm">Schedule&nbsp;of&nbsp;Services</a>&nbsp;| ' +
      '<a href="sisterhood.htm">Sisterhood</a>&nbsp;| ' +
      '<a href="iton.htm">Shabbat&nbsp;Iton</a><br />' +
      '<a href="facilities.htm">Facilities&nbsp;and&nbsp;Professional&nbsp;Services</a>&nbsp;| ' +
      '<a href="history.htm">History</a>&nbsp;| ' +
      '<a href="arms.htm">Coat&nbsp;of&nbsp;Arms</a>&nbsp;| ' +
      '<a href="mantles.htm">Torah&nbsp;Mantles</a><br />' +
      '<a href="bulka.htm">Rabbi&nbsp;Bulka</a>&nbsp;| ' +
      '<a href="clergy.htm">Our&nbsp;Other&nbsp;Clergy</a>&nbsp;| ' +
      '<a href="board.htm">Board&nbsp;of&nbsp;Directors</a><br />' +
      '<a href="events.htm">Events</a>&nbsp;| ' +
      '<a href="education.htm">Ongoing&nbsp;Classes&nbsp;and&nbsp;Education</a>&nbsp;| ' +
      '<a href="other.htm">Other&nbsp;Activities</a><br />' +
      '<a href="info.htm">Community&nbsp;Information</a>&nbsp;| ' +
      '<a href="visitor.htm">Visitor&nbsp;Information</a>&nbsp;| ' +
      '<a href="hospital.htm">Hospital&nbsp;Visitation</a><br />' +
      '<a href="q&a.htm">Question&nbsp;of&nbsp;the&nbsp;Week</a>&nbsp;| ' +
      '<a href="membership.htm">Membership</a>' +
      '<br />&nbsp;</p>';
   document.write(text);      
}


/*****************************************************************************
*
*       makeMenu - Create standard menu for use in ALL pages
*
*****************************************************************************/

function makeMenu ()

{
	// Build the menus.
   
	var menu1 = new MMenu ("menu1", null, 0, 0, "xMenu");       // Main menu.
	var menu2 = new MMenu ("menu2", menu1, 130, -10, "yMenu");  // "About" sub-menu.
	var menu3 = new MMenu ("menu3", menu1, 130, -10, "yMenu");  // "People" sub-menu.
	var menu4 = new MMenu ("menu4", menu1, 130, -10, "yMenu");  // "Events" sub-menu.
   var menu5 = new MMenu ("menu5", menu1, 130, -10, "yMenu");  // "Information" sub-menu.
   
// Add the menu items.

	menu1.addItem(null, "index.htm", "images/menu/home1.jpg", "images/menu/home0.jpg", 140, 40, "Home");
	menu1.addItem(null, "contact us.htm", "images/menu/contact us1.jpg", "images/menu/contact us0.jpg", 140, 40, "Contact Machzikei Hadas");
	menu1.addItem(menu2, "", "images/menu/about1.jpg", "images/menu/about0.jpg", 140, 40, "About Machzikei Hadas");
	menu1.addItem(null, "schedule.htm", "images/menu/schedule1.jpg", "images/menu/schedule0.jpg", 140, 40, "Schedule of Services");
	menu1.addItem(menu3, "", "images/menu/people1.jpg", "images/menu/people0.jpg", 140, 40, "Our People");
	menu1.addItem(menu4, "", "images/menu/activities1.jpg", "images/menu/activities0.jpg", 140, 40, "Shul Activities");
	menu1.addItem(null, "sisterhood.htm", "images/menu/sisterhood1.jpg", "images/menu/sisterhood0.jpg", 140, 40, "Sisterhood");
	menu1.addItem(null, "iton.htm", "images/menu/iton1.jpg", "images/menu/iton0.jpg", 140, 40, "Shabbat Iton");
	menu1.addItem(menu5, "", "images/menu/information1.jpg", "images/menu/information0.jpg", 140, 40, "Information");
	menu1.create("absolute");
	menu2.addItem(null, "facilities.htm", "images/menu/facilities1.jpg", "images/menu/facilities0.jpg", 140, 40, "Facilities and Professional Services");
	menu2.addItem(null, "history.htm", "images/menu/history1.jpg", "images/menu/history0.jpg", 140, 40, "History");
	menu2.addItem(null, "arms.htm", "images/menu/arms1.jpg", "images/menu/arms0.jpg", 140, 40, "Coat of Arms");
	menu2.addItem(null, "mantles.htm", "images/menu/mantles1.jpg", "images/menu/mantles0.jpg", 140, 40, "Torah Mantles");
	menu2.create();
	menu3.addItem(null, "bulka.htm", "images/menu/bulka1.jpg", "images/menu/bulka0.jpg", 140, 40, "Rabbi Bulka");
	menu3.addItem(null, "clergy.htm", "images/menu/clergy1.jpg", "images/menu/clergy0.jpg", 140, 40, "Our Other Clergy");
	menu3.addItem(null, "board.htm", "images/menu/board1.jpg", "images/menu/board0.jpg", 140, 40, "Board of Directors");
	menu3.create();
	menu4.addItem(null, "events.htm", "images/menu/events1.jpg", "images/menu/events0.jpg", 140, 40, "Events");
	menu4.addItem(null, "education.htm", "images/menu/education1.jpg", "images/menu/education0.jpg", 140, 40, "Ongoing Classes and Education");
	menu4.addItem(null, "other.htm", "images/menu/other1.jpg", "images/menu/other0.jpg", 140, 40, "Other Activities");
	menu4.create();
	menu5.addItem(null, "info.htm", "images/menu/info1.jpg", "images/menu/info0.jpg", 140, 40, "Community Information");
	menu5.addItem(null, "visitor.htm", "images/menu/visitor1.jpg", "images/menu/visitor0.jpg", 140, 40, "Visitor Information");
	menu5.addItem(null, "hospital.htm", "images/menu/hospital1.jpg", "images/menu/hospital0.jpg", 140, 40, "Hospital Visitation");
	menu5.create();
   menu1.show(0,225);
}


/*****************************************************************************
*
*       readCookie - Return the value of a cookie (or null if cookie doesn't exist)
*
*****************************************************************************/

function readCookie (

	name)								// Name of cookie.

{
	// Local variables.
   
   var nameEQ,						// Cookie name with equals sign.
       ca,							// Array of cookies.
       i, c;						// Work variables.
       
	nameEQ = name + "=";
	ca = document.cookie.split(';');
	for (i = 0;  i < ca.length;  i++)
   {
   	c = ca[i];
		while (c.charAt(0)==' ')  c = c.substring(1, c.length);//there is a better way - see HOTT book
		if (c.indexOf(nameEQ) == 0)
      	return c.substring(nameEQ.length, c.length);
	}
	return null;
}

/*****************************************************************************
*
*       resizeBanner - Fix banner image size and text rendering button position
*
*****************************************************************************/

function resizeBanner ()

{
	var  x;
	x = getClientWidth() > 760 ? getClientWidth() : 760;
   document.images.imgBanner.style.width = x;
   document.images.imgIcon.style.left = x - 35;
}


/*****************************************************************************
*
*       setDefine - Set attributes for span's with defined terms 
*
* This function sets a number of attributes for span elements that contain a 
* word for which a definition is available. The definition pops up when the
* mouse hovers over the word.
*
*****************************************************************************/

function setDefine ()

{
	// Local work variables.
   
   var i, t, x;

   // Lookup the word. Set the span element's attribute only if a definition
   // for that word exists.
   
   t = document.getElementsByTagName("span");
   for (i=0; i < t.length; i++)
   {
      if (t[i].className == "define")
      {
         t[i].style.borderBottom = "dashed 1px";
         t[i].style.cursor = "help";
         t[i].onmouseover = showDefinition;
         t[i].onmouseout = hideDefinition; 
		}
   }
}


/*****************************************************************************
*
*       setPointer - Set mouse pointer for div's with links ??can get rid of using css??
*
* This function set the mouse pointer for div elements that handle the onclick()
* event. Use "pointer" as the cursor value. If it is not valid (for this
* browser) use "hand".
*
*****************************************************************************/

function setPointer ()

{
	// Local work variables.
   
   var i, t;
   
   t = document.getElementsByTagName("div");
   for (i=0; i < t.length; i++)
   {
      if (t[i].onclick)
      {
      	t[i].style.cursor = "pointer";
         if (t[i].style.cursor != "pointer")
         	t[i].style.cursor = "hand";
		}
   }
}


/*****************************************************************************
*
*       showDefinition - Pop up a definition ??to be done
*
* This function pops up a definition of a word contained in a <span> element.
*
*****************************************************************************/

function showDefinition ()

{
   // Local variables.
   
   var  box;                  // Window containing definition.
   var  boxH, boxW;           // Its height and width.
   var  def;                  // Term's definition.
   var  spanX, spanY;         // Position of span element.
   var  spanH, spanW;         // Its height and width.
   var  x;                    // Work variables.

   // Obtain window for definition. Look up the definition for the word in the
   // <span> element and place definition in it.
   // If defeinition doesn't exist, exit.
   
   box = document.getElementById("definitionBox");
   def = lookupDefinition (this.innerHTML);
   if (!def)  return;
   box.innerHTML = def;
   
   // Get some metrics.
   
   boxH = box.offsetHeight;
   boxW = box.offsetWidth;
   spanX = findPosX(this);
   spanY = findPosY(this);
   spanH = this.offsetHeight;
   spanW = this.offsetWidth;
   box.style.minWidth = boxW + "px"; 
   
   // Position definition window under element if there is room. Otherwise
   // position it over the element.

   box.style.bottom = "auto";
   x = spanY + (2*spanH);  
   if (getClientHeight() - (x-getScrollY())  >  boxH)
      // Below element.
      box.style.top = x + "px";
   else
      // Above element.
      box.style.top = (spanY - boxH) + "px";      

   // Center window horizonally under (over) element. Assume there is always
   // room on the left. Move window to the left if not enough room on the right.   
   
   box.style.right = "auto";
   x = spanX + (spanW/2) - (boxW/2);   
   if (getClientWidth() - (x-getScrollX())  >  boxW)
      // Centered.
      box.style.left = x + "px";
   else
      // Left of element.
      box.style.left = (spanX - boxW) + "px";      
   
   // Finally, make window visible.
      
   box.style.zIndex = "10";
   box.style.visibility = "visible";
}

/*****************************************************************************
*
*       showMap - Displays Map of Shul Location
*
*****************************************************************************/

function showMap ()

{
   window.open ("http://ca.maps.yahoo.com/py/maps.py?Pyt=Tmap&addr=2310 Virginia Drive&city=Ottawa&state=ON", "CMHmap", "status=0,toolbar=0,menu=0,resizable=1,scrollbars=1"); 
}

/*****************************************************************************
*
*       startUp - Code executed after page loads
*
*****************************************************************************/

function startUp ()

{
	// Establish text rendering from associated cookie.

	var x = readCookie("curText");
   if (x != null)  curText = parseInt(x);   
   curText = (curText == 1 ? 0 : 1);
   switchText();
   
   // Other start code.
   
   setDefine();
   setPointer();
   
   // Establish width for banner image and text rendering button psition.
   
	resizeBanner();
}


/*****************************************************************************
*
*       switchText - Changes text display from light (dark) to dark (light)
*
* This function changes the appropriate <td> element classes to switch the text
* display from dark-on-light to light-on-dark or vice vesa. It also changes the
* "switch" icon and the "title" image to conform with the text display. This
* information is stored as a permanent cookie to be used for initial text
* display for every page.
*
*****************************************************************************/

function switchText ()

{
	// Local work variables.
   
   var i, t, date;
   
	// Switch the indicator.
   
   curText = (curText == 1 ? 0 : 1);
   
   // Switch the icon image.
   
	t = "images/" + (curText == 1 ? "light.gif" : "dark.gif");
	if (document.images.imgIcon) document.images.imgIcon.src = t;
   
	// Get the name of the "title" image. Switch it. Dark-on-light "title" image
   // file names end with a zero ("0"); light-on-dark names don't.

   t = /^.*images\/(.*)\.jpg/.exec(document.images.imgTitle.src); 
   t = t[1].replace(/0$/, "");
   if (t.length > 1)
   {
	   if (curText == 0) t += "0";
   	document.images.imgTitle.src = "images/" + t + ".jpg";
   }
   
   // Switch all appropriate <td> and <span> elements, by changing their class
   // names, and <hr> elements by changing their colour.
   
   t = document.getElementsByTagName("td");
   for (i=0; i < t.length; i++)
   {
      if (t[i].className=="bodyStart" || t[i].className=="bodyLight" || t[i].className=="bodyDark")
      	t[i].className = (curText==0 ? "bodyLight" : "bodyDark");
      if (t[i].className=="newsStart" || t[i].className=="newsLight" || t[i].className=="newsDark")
      	t[i].className = (curText==0 ? "newsLight" : "newsDark");
   }
   t = document.getElementsByTagName("span");
   for (i=0; i < t.length; i++)
   {
      if (t[i].className=="highlightStart" || t[i].className=="highlightLight" || t[i].className=="highlightDark")
      	t[i].className = (curText==0 ? "highlightLight" : "highlightDark");
   }
   t = document.getElementsByTagName("hr");
   for (i=0; i < t.length; i++)
		t[i].color = (curText==0 ? "#424973" : "white");
   
   // Finally, store text display info as a permanent cookie for all initial 
   // page rendering. 

   date = new Date();
   date.setFullYear(date.getFullYear()+1);
   i = "curText=" + curText + "; expires=" + date.toGMTString() + "; path=/";
   document.cookie = i;
}
