var NumCompetitions = 0;
var StartTime = new Date();
var EndTime = new Date();
var Today = new Date();
var MsPerSecond = 1000; // Milliseconds per second
var MsPerMinute = 60 * MsPerSecond; // Milliseconds per minute
var MsPerHour = 60 * MsPerMinute; // Milliseconds per hour
var MsPerDay = 24 * MsPerHour; // Milliseconds per day
// Month names are zero based
var MonthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 

// Assume competition starts at 9AM and ends at 5PM
DefaultStartHour = 8;
DefaultStartHour = 16;
DefaultEndHour = 17;

// Set the message strings (long and short) to one of:
// - n days!
// - 1 day!
// - this weekend!
// - all finished!
// countdownMsg will be displayed with the details of the competition.
// countdownHead may be displayed as a headline for the entire list 
// of competitions. Only one head will be displayed - the head 
// associated with the next competition.
function
SetCountdownMsg()
{
	// Is this our first competition or our next?
	var next;
	if(this.n == 0)
	{
		next = "first";
	}
	else
	{
		next = "next";
	}

	// Case 1: Competition has not started yet
	if (this.untilStartMs > MsPerMinute)
	{
		var remainingDays = Math.round(this.untilStartMs / MsPerDay);
		if(remainingDays > 1)
		{
			this.countdownMsg = this.name + " begins in <b>" + remainingDays + "</b>&nbsp;days!";
			this.countdownHead = remainingDays + " days until our " + next + " competition!";
			return;
		}

		if(remainingDays == 1)
		{
			this.countdownMsg = this.name + " starts in <b>1</b>&nbsp;day!";
			this.countdownHead = "1 day until our " + next + " competition!";
			return;
		}

		var remainingHours = Math.round(this.untilStartMs / MsPerHour);
		if(remainingHours > 1)
		{
			this.countdownMsg = this.name + " starts in <b>" + remainingHours + "</b>&nbsp;hours!";
			this.countdownHead = remainingHours + " hours until our " + next + " competition!";
			return;
		}

		var remainingMinutes = Math.round(this.untilStartMs / MsPerMinute);
		if(remainingMinutes > 1)
		{
			this.countdownMsg = this.name + " starts in <b>" + remainingMinutes + "</b>&nbsp;minutes!";
			this.countdownHead = remainingMinutes + " minutes until our " + next + " competition!";
			return;
		}

		if(remainingMinutes == 1)
		{
			this.countdownMsg = this.name + " starts in <b>1</b>&nbsp;minute!";
			this.countdownHead = "1 minute until our " + next + " competition!";
			return;
		}

		this.countdownMsg = this.name + " is about to start!";
		this.countdownHead = "Our " + next + " competition is about to start!";
		return;
	}

	// Case 2: Competition is over
	if (this.untilEndMs < 0)
	{
		if(this.place.length > 0)
		{
			this.countdownMsg = this.place;
		}
		else
		{
			this.countdownMsg = this.name + " is over!";
		}
		this.countdownHead = "It has been a great season!";
		return;
	}

	// Case 3: Competition has started but not finished
	this.countdownMsg = this.name + " is happening right now!";
	this.countdownHead = "We are competing this weekend!";
}


function
GetDateRangeString(start, end)
{
	// start date
	var smNum = start.getMonth();
	var sm = MonthNames[smNum];
	var sd = start.getDate();
	var sy = start.getFullYear();

	// end date
	var emNum = end.getMonth();
	var em = MonthNames[emNum];
	var ed = end.getDate();
	var ey = end.getFullYear();

	// Case 1: different years
	if(sy != ey)
	{
		var s = sm + " " + sd + ", " + sy + " - " + em + " " + ed + ", " + ey;
		return(s);
	}

	// Case 2: different months
	if(smNum != emNum)
	{
		var s = sm + " " + sd + " - " + em + " " + ed + ", " + ey;
		return(s);
	}

	// Case 3: different dates
	if(sd != ed)
	{
		var s = sm + " " + sd + " - " + ed + ", " + ey;
		return(s);
	}

	// Case 4: one day competition
	var s = sm + " " + sd + ", " + sy;
	return(s);
}


// All HTML creation happens here
function
WriteHTML()
{
	// event name
	var s = "<p class=eventName>" + this.name + "</p>";
	document.write(s);

	// dates
	s = "<p class=eventDate>" + 
		GetDateRangeString(this.startTime, this.endTime) + "</p>";
	document.write(s);

	// event location
	var s = "<p class=eventLocation>" + this.location + "</p>";
	document.write(s);

	// countdown
	s = "<p class=eventCountdown>" + this.countdownMsg + "</p>";
	document.write(s);

	// links
	s = "<p class=eventLink>";

	// add links if we are given any
	// first time we do not need a divider bar
	dividerBar = "";
	for(i = 0; i < 3; i++)
	{
		if(this.url[i].length > 0)
		{
			if(this.label[i].length > 0)
			{
				s += dividerBar + "<a href='" + this.url[i] + "'>" + this.label[i] + "</a>";
			}
			else
			{
				s += dividerBar + "<a href='" + this.url[i] + "'>" + "link" + "</a>";
			}
		}
		else
		{
			if(this.label[i].length > 0)
			{
				s += dividerBar + this.label[i] ;
			}
		}

		// next time we may need a divider bar
		dividerBar = " | ";
	}

	s += "</p>";
	document.write(s);

}

// All HTML creation happens here
function
WriteHeadHTML()
{
	var s = "<h2>" + this.countdownHead + "</h2>";
	document.write(s);
}


// constructor
function
Competition(	name, location,
		startYear, startMonth, startDay,
		endYear, endMonth, endDay,
		labelA, urlA, 
		labelB, urlB, 
		labelC, urlC,
		place)
{
	// methods
	this.WriteHTML = WriteHTML;
	this.WriteHeadHTML = WriteHeadHTML;
	this.SetCountdownMsg = SetCountdownMsg;

 	// initialize properties
	this.n          = NumCompetitions++;
	this.name       = name;
	this.location   = location;

	this.startYear  = startYear;
	this.startMonth = startMonth;
	this.startDay   = startDay;
	this.startTime = new Date(startYear, startMonth - 1, startDay,
		DefaultStartHour, 0, 0, 0);
	this.untilStartMs = this.startTime - Today;

	this.endYear    = endYear;
	this.endMonth   = endMonth;
	this.endDay     = endDay;
	this.endTime = new Date(endYear, endMonth - 1, endDay,
		DefaultEndHour, 0, 0, 0);
	this.untilEndMs = this.endTime - Today;

	this.label	= new Array (labelA, labelB, labelC);;
	this.url	= new Array (urlA, urlB, urlC);;

	this.place	= place;

	this.countdownMsg = "How long?";
	this.countdownHead = "How short?";
	
	this.SetCountdownMsg();
}
