//
// script.js
// JavaScript routines for the Highworth Methodist Church web site
//



// Constants (simulated)
var TOP_OFFSET		=	4;
var LEFT_OFFSET		=	0;
var DISAPPEAR_DELAY	=	250;



// Date arrays
var month		=	new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

// Variables

// Current menu object


// HTML
var dropDownHTML	=	'';

// Internal
var amountMenus		=	0;
var activeDropDown	=	0;
var hidingTimeoutId;
var menuObj;

// Compatibility checking
var ie4			=	document.all;
var ns6			=	document.getElementById && !document.all;

// Get an object's offset (top or left)
function getOffset(obj, type) {
	// Get the initial offset
	var offset	=	(type == 'left') ? obj.offsetLeft : obj.offsetTop;
	// Get the parent element

	var parent	=	obj.offsetParent;
	// Calculate total offset

	while (parent != null) {
		// Change the offset
		offset	+=	(type == 'left') ? parent.offsetLeft : parent.offsetTop;
		// Get the new parent
		parent	=	parent.offsetParent;
	}
	// Return offset
	return offset;
}



// Create a drop down menu
function createDropDown() {
	// If browser compatible...
	if (ie4 || ns6) {
		// Increase the amount of menus
		amountMenus++;
		// Start the HTML
		dropDownHTML	=	'<DIV CLASS="dropDown" ID="dropDown' + amountMenus.toString() + '" onMouseOver="clearHideDropDown();" onMouseOut="offDropDown();">';
	}
}



// Finish a drop down menu
function endDropDown() {
	// If browser compatible...
	if (ie4 || ns6) {
		// End the HTML
		dropDownHTML	+=	'</DIV>';
		// Write the HTML
		document.write(dropDownHTML);
		// Clear the HTML
		dropDownHTML	=	'';
	}
}



// Add an item to the menu
function addLink(name, link) {
	// If browser compatible...
	if (ie4 || ns6) {

		// Create the HTML for this item
		dropDownHTML	+=	'<A HREF="' + link + '">' + name + '</A><BR>';
	}
}



// Show a drop down menu
function showDropDown(number, obj) {
	// If browser compatible

	if (ie4 || ns6) {
		// Hide the current drop down menu
		hideDropDown();
		clearHideDropDown();
		// Get the drop down menu object

		menuObj			=	document.getElementById ? document.getElementById('dropDown' + number.toString()) : document.all.dropDown1;
		// Change the position of the menu object
		menuObj.style.left	=	getOffset(obj, 'left') + LEFT_OFFSET + 'px';
		menuObj.style.top	=	getOffset(obj, 'top') + obj.offsetHeight + TOP_OFFSET + 'px';
		// Change the visibility of the menu object
		menuObj.style.visibility	=	'visible';
	}
}



// Mouse off a drop down menu
function offDropDown() {
	// Set the timeout
	if (ie4 || ns6) {
		hidingTimeoutId		=	setTimeout("hideDropDown();", DISAPPEAR_DELAY);
	}
}



// Hide a drop down menu
function hideDropDown() {
	if (ie4 || ns6) {
		if (typeof menuObj != 'undefined') {
			menuObj.style.visibility	=	'hidden';
		}
	}
}



// Stop hiding a drop down menu
function clearHideDropDown() {
	if (ie4 || ns6) {
		if (typeof hidingTimeoutId != 'undefined') {
			clearTimeout(hidingTimeoutId);
		}
	}
}



// Draw the events box
function drawEventsBox() {
	// Get the current date
	var today	=	new Date();
	var nextSunday	=	new Date();
	// Get days to Sunday
	var daysToNextSunday	=	(7 - today.getUTCDay()) % 7;
	// Set the date of the next Sunday object
	nextSunday.setUTCDate(today.getUTCDate() + daysToNextSunday);
	// Get the month and the day
	var nextSundayDay	=	nextSunday.getUTCDate();
	var nextSundayMonth	=	month[nextSunday.getUTCMonth()];

	// Get the suffix ("st", "nd", "rd", "th")
	var nextSundayDate;
	if (nextSundayDay <= 10 || nextSundayDay >= 20) {
		if ((nextSundayDay % 10) == 1)
			nextSundayDate	=	nextSundayDay + 'st ' + nextSundayMonth;
		else if ((nextSundayDay % 10) == 2)
			nextSundayDate	=	nextSundayDay + 'nd ' + nextSundayMonth;
		else if ((nextSundayDay % 10) == 3)
			nextSundayDate	=	nextSundayDay + 'rd ' + nextSundayMonth;
		else
			nextSundayDate	=	nextSundayDay + 'th ' + nextSundayMonth;
	} else {
		nextSundayDate		=	nextSundayDay + 'th ' + nextSundayMonth;
	}
	// Check for Christmas
	var isChristmasNotOnSunday	=	false;
	var isChristmasOnSunday		=	false;
	if (today.getUTCMonth() == 11) {
		if (today.getUTCDate() > 18 && today.getUTCDate() <= 25)
			isChristmasNotOnSunday	=	true;
		if (nextSundayDay == 25) {
			isChristmasOnSunday 	=	true;
			isChristmasNotOnSunday	=	false;
		}
	}
	// Write the box
	// The header

	document.write('<DIV CLASS="events">');
	document.write('<P ALIGN="CENTER">THIS WEEK</P>');
	document.write('<HR>');
	// If Christmas on Sunday

	if (isChristmasOnSunday) {
		document.write('<P><B>Christmas Day, 10:00am</B><BR>Morning Service</P>');
	// It isn't Christmas on this coming Sunday

	} else {
		// If it's Christmas this week
		if (isChristmasNotOnSunday && nextSundayDay > 25) {
			document.write('<P><B>Christmas Day, 10:00am</B><BR>Morning Service</P>');
			document.write('<HR>');
		}
		// If the first Sunday of the month then we have a Holy Communion service
		if (nextSundayDay <= 7) {
			document.write('<P><B>Sunday ' + nextSundayDate + ', 8:30am</B><BR>Holy Communion Service</P>');
			document.write('<HR>');
		}
		// Morning service
		document.write('<P><B>Sunday ' + nextSundayDate + ', 10:30am</B><BR>Morning Service</P>');
		document.write('<HR>');
		// Evening service
		document.write('<P><B>Sunday ' + nextSundayDate + ', 6:00pm</B><BR>Evening Service</P>');	
		// If it's Christmas this week
		if (isChristmasNotOnSunday && nextSundayDay < 25) {
			document.write('<HR><P><B>Christmas Day, 10:00am</B><BR>Morning Service</P>');
		}
	}
	// Close
	document.write('<HR><P ALIGN="CENTER"><I>Please contact the minister in case of change to these times</I></P>');
	document.write('</DIV>');
}



// Create drop down menus
createDropDown();
	addLink('About Us', 'index.html');
	addLink('History', 'history.html');
	addLink('Directions', 'directions.html');
	addLink('Contacts', 'contacts.html');
endDropDown();
createDropDown();
	addLink('Sunday services', 'services.html');
	addLink('Midweek groups', 'midweek.html');
	addLink('Youth', 'youth.html');
endDropDown();

// Hide menus on click
document.onclick	=	hideDropDown;




//
// End of script
//