// rjbg
// 07-06-02 0.8
// 08-06-02 0.9
// 15-06-02 1.0
// 08-07-02 1.0.1 preachers loaded
// 28-07-02 1.1   now has landed. hides today if after 13:00.
// 28-07-02 1.1.1 new format for id. date-DD-MM-YYYY . Old method still works, but this is tidier
// 03-01-03 1.2   new markup scheme.
// 05-08-06 1.2.1 altered for jws

//get date etc
today = new Date();
month = today.getMonth()+1;
date = today.getDate();
year = today.getFullYear();

function showPreacher(){

//ensure browser supports the W3C DOM/
if (document.getElementsByTagName){

//select elements
fg = document.getElementById("upcoming");
d = fg.getElementsByTagName("h4");
l = fg.getElementsByTagName("dl");

// count number shown
shown = 0;

//loop through each <h4> in preacher box
for (i=0;i<d.length;i++){

//hide all the <h4>s
d.item(i).style.display = "none";

//and all their <dl>s
l.item(i).style.display = "none";

//rip out the <h4>'s id and split it up into array
id = d.item(i).getAttribute("id");
idar = id.split(":");
idar = idar[1].split("-");

//get next year and next month
nyr = year+1;
nmth = month+1;

//display again if the month and year is correct,  and the date is greater than to today's.
if (idar[1]==month && idar[2]==year && idar[0]>=date){
d.item(i).style.display = "block";
l.item(i).style.display = "block";
shown++;
}

//display again if the month is next month and the year is correct and date is the same as today's or less.
else if (idar[1]==nmth && idar[2]==year && idar[0]<=date){
d.item(i).style.display = "block";
l.item(i).style.display = "block";
shown++;
}

//display again if the month is December and the year is next year and the month of the <li> is january.
else if (month==12 && idar[2]==nyr && idar[1]==1){
d.item(i).style.display = "block";
l.item(i).style.display = "block";
shown++;
}
}


}

// ensures at least four items are displayed
if (shown<4){
for (i=0;i<=4;i++){
if (i<d.length){
d.item(i).style.display = "block";
l.item(i).style.display = "block";

}
}


}

}
// below should be reviewed, as it is non DOM, but other methods are not well supported, esp by ie5/mac and ie5.1/mac



if (document.getElementById && document.getElementsByTagName && document.createElement) {
	window.onload = initToggleCategories;
}

function initToggleCategories() {
	var events = document.getElementById('events').getElementsByTagName("div");
	
	for (var j = 0; j < events.length; j++){
	events.item(j).style.display = "none";
	}
	
	var as = document.getElementById('activities').getElementsByTagName('a');
	for (var i = 0; i < as.length; i++) {
		as[i].onclick = function() {
			toggle(this);
			return false;
		}
	}
	
	// Activate the first tab by default
	toggle(as[0]);
	showPreacher();
}

function toggle(l) {
// Try to get a reference to an element with the id 'current' (a previously active tab header)
var oldCurrent = document.getElementById('current');
// If this element exists, remove its ID attribute
if (oldCurrent){
oldCurrent.removeAttribute('id');
var old_current_name = oldCurrent.getElementsByTagName("a").item(0).getAttribute('href').match(/#(\w.+)/)[1];
// Create helper array for sorting elements on alphabetical order 
var old_current_block = document.getElementById(old_current_name);

old_current_block.style.display = 'none';
}

// Add the ID attribute with value 'current' to the newly active tab header (LI element)
l.parentNode.setAttribute('id', 'current');
var current_name = l.getAttribute('href').match(/#(\w.+)/)[1];
// Create helper array for sorting elements on alphabetical order 
var current_block = document.getElementById(current_name);
if (current_block) {
current_block.style.display = 'block';
}
}
			
