/******************************************************************************************
*
* 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 = "March 7, 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
*
*****************************************************************************/

function getClientHeight ()

{
   var x;
   if (document.documentElement && document.documentElement.clientHeight)
   	// Explorer 6 Strict Mode.
	   x = document.documentElement.clientHeight;
   else if (document.body)
      // Other Explorers.
	   x = document.body.clientHeight;
   else
      // Everything else.
	   x = self.innerHeight;
	return x;
}


/*****************************************************************************
*
*       getClientWidth - Return width of client window
*
*****************************************************************************/

function getClientWidth ()

{
   var x;
   if (document.documentElement && document.documentElement.clientWidth)
	   // Explorer 6 Strict Mode.
   	x = document.documentElement.clientWidth;
   else if (document.body)
      // Other Explorers.
	   x = document.body.clientWidth;
   else
      // Everything else.
	   x = self.innerWidth;
	return x;
}


/*****************************************************************************
*
*       makeBottom - Outputs 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 - Creates 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 - Returns 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;
}


/*****************************************************************************
*
*       setPointer - Sets mouse pointer for div's with links
*
* 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";
		}
   }
}


/*****************************************************************************
*
*       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.
   
   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.
   // <body> element too??
   
   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;
}
