var yesterday=new Date();
var thirteendaystime=new Date();
thirteendaystime.setDate(yesterday.getDate()+13); // on the home groups page only show meetings for the next couple of weeks
yesterday.setDate(yesterday.getDate()-1); // don't display items that are now in the past

walkingcount = 0; //only display 1 walking group item on the diarydates page

var items = new Array();
var regulars = new Array();

function printNthOfMonth(d)
{
	date = d.getDate();
	document.write(date);
	document.write("<sup>");
	switch (date)
	{
		case 1:
		case 21:
		case 31:
			document.write("st");
			break;
		case 2:
		case 22:
			document.write("nd");
			break;
		case 3:
		case 23:
			document.write("rd");
			break;
		default:
			document.write("th");
	}
	document.write("</sup> ");
	document.write(monthName[d.getMonth()].substring(0,3));
}

function printNthOfMonthWithYear(dAsString) //differs from function above which takes a Date type as input parameter.
{
	d = new Date();
	d.setTime(Date.parse(dAsString));
	date = d.getDate();
	document.write(date);
	document.write("<sup>");
	switch (date)
	{
		case 1:
		case 21:
		case 31:
			document.write("st");
			break;
		case 2:
		case 22:
			document.write("nd");
			break;
		case 3:
		case 23:
			document.write("rd");
			break;
		default:
			document.write("th");
	}
	document.write("</sup> ");
	document.write(monthName[d.getMonth()].substring(0,3)+" ");
	document.write(1900+d.getYear());
}

function hhmm(d)
{
	hours = d.getHours();
	minutes = d.getMinutes();
	if (hours != 0)
	{
		ampm = " am";
		if (hours > 12)
		{
			hours -= 12;
			ampm = " pm";
		}
		ret = hours + ":";
		if (minutes < 10)
		{
			ret += "0";
		}
		ret += minutes;
		ret += ampm;
		return ret;
	}
	return "";
}

function printItems(filter, i)
{
	if (typeof i == "undefined")
		i = 0;
	var d = new Date();
	var count = new Array();

	count["walk"]=0;
	// setting an element of the count colection to 0 means that at the bottom of the diary, the first occurance of the 'event' will
	// be printed in bulletlist form. Not setting it means that it is undefined, e.g. if we're not filtering on homegroups leave it
	// undefined, so that no homegroup events will be listed in the bulletpoints below the diary
	if (filter.substring(0,10) == "homegroups")
	{
		count[filter]=0;
	}
	
	do
	{
		d.setTime (Date.parse(items[i++]));
		if (filter.substring(0,10) == "homegroups" && d >= thirteendaystime) break;
		page = items[i++];
		text = items[i++];

		if (	d > yesterday 
				&& (	filter == "diary" 
						&& (	page != "override" 
							&& page != "suppress" 
							&& (	page != "walk" 
								&& page.substring(0,10) != "homegroups" 
								|| count[page] == 0
								)
							) 
						|| filter == page
						&& ( filter.substring(0,10) != "homegroups"
							|| count[page] == 0
							)
					)
			)
		{
			document.write("<ul><li><font color=red>");
			if (page == "walk" && filter == "diary") //show the icon
			{
				document.write("<img style=\"width: 75px; height: 66px; float: left;\" src=\"../Activities/th_WalkingGroup.jpg\">");
			}
			if (count[page] >= 0 && filter.substring(0,10) != "homegroups") count[page]++; //only print the first walk in the bulletlist
			printNthOfMonth(d);
			document.write(" ("+dayName[d.getDay()]+") ");
			document.write (hhmm(d)+" - </font>");
			if (page == "walk" && filter == "diary") //show the icon
			{
				document.write("Walking Group - "); // on the diary page, make sure that it's clear this is a walk
			}
//			pos = text.indexOf(':');
//			if (pos > 0 && pos <= 20 && (text.length - pos) > 2)
//			//if (page == "diary")
//			{
//				document.write (text.substring (pos+2));
//			}
//			else
//			{
				document.write (text);
//			}
			document.write("</ul></li>");
			if (page == "walk" && filter == "diary") //make sure we don't allow the next bulletpoint to be next to the floating walking icon
			{
				document.write("<br>");
			}
		}
	} while (i < items.length);
}


