var fullMonthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var abbrevMonthArray = new Array("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
var abbrevDayArray = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var abbrevDaysOfWeekInit = new Array("S", "M", "T", "W", "TR", "F", "S");

function wesCalcDayOfYear() { // Calculates the day of the year between 1 and 365
	var dateVar = new Date();
	var Y = dateVar.getYear(), M = dateVar.getMonth(), D = dateVar.getDate()
	var seasonStartYear = Y;
	if (M < 08) { seasonStartYear--; } // WESJ_REVISIT --> HARDCODED to start in september every season.
	var doy = (Date.UTC(Y,M,D) - Date.UTC(seasonStartYear,08,01)) / 864e5; // WESJ_REVISIT --> Set to first day of season (note months from 0 to 11)
	if (doy < 1 || doy > 365) {
		alert ("Please send Wes email, tell him you saw this message, and let him know that one of his javascript functions isn't working properly.  Y = " + Y + " M = " + M + " D = " + D + " DOY = " + doy);
	}
	
	return doy;
}

function wesMod(X, Y) { // Standard Mod Function
	return X - Math.floor(X/Y)*Y;
}

function wesPlayerNumOfDay() { // Determine the player of the day
	return ( wesMod(wesCalcDayOfYear(), arrayNames.length) );
}

function wesGetPlayerAge() {
	var currDate = new Date();
	var birthDate = arrayDOB[wesPlayerNumOfDay()];
	
	var yearsDiff = currDate.getYear() - birthDate.getYear();
	if (currDate.getMonth() == birthDate.getMonth()) {
		if (currDate.getDate() < birthDate.getDate()) {
			yearsDiff--;
		}
	}
	else if (currDate.getMonth() > birthDate.getMonth()) {
		// Do Nothing, it's all good
	}
	else { // (currDate.getMonth() < birthDate.getMonth())
		yearsDiff--;
	}
	return yearsDiff-1900;
}
													  

function wesGetMonthName() { // What's the current date?
	var dateVariable = new Date();
	return fullMonthArray[dateVariable.getMonth()]
}
















function irwinDaysInMonth(year, month) {
	return 32 - new Date(year, month, 32).getDate();
}

// See schedule.js for irwinGetGameIndex function

function irwinEventChange(newEventIndex) { // Dyanmic HTML to change picture
	var text = "";
	text = '<img src="images/' + schedArrayImage[newEventIndex] + '" width="160" height="82".jpg"><br><b>' + abbrevDayArray[schedArrayEventDate[newEventIndex].getDay()] + ' ' + abbrevMonthArray[schedArrayEventDate[newEventIndex].getMonth()] + ' ' + schedArrayEventDate[newEventIndex].getDate() + ', ' + schedArrayEventDate[newEventIndex].getYear() + '<br>' + schedArrayEventShortDesc[newEventIndex] + '</b>';
	dynCalendar.innerHTML = text;
	window.event.cancelBubble = true; //WESJ_REVISIT --> Became an error after calling the "init" function.
}

function irwinGenerateCalendar() { // Auto Generate the Calendar based on current month, etc
	var currDateTime = new Date();
	var dateVar = new Date(currDateTime.getYear(), currDateTime.getMonth(), 01, 00, 00, 00);
	var currDayOfWeek = dateVar.getDay();
	var lastDayOfMonth = irwinDaysInMonth(dateVar.getYear(), dateVar.getMonth());
	var eventIndex = -1;
	var i=0, j=0, k=1;
	var maxDaysForSixCalendarRows = 35; // This is from 7 days per week times 5 rows (as opposed to needing a sixth).

	// NEW:  The days of the week
	document.write("<tr>");
	for (i=0; i<7; i++) {
		document.write("<td id=\"divIrwinScheduletd0\"><B><div>" + abbrevDaysOfWeekInit[i] + "</div></B></td>");
	}
	document.write("</tr><tr>");

	for (i=0; i<42; i++) {
		// When i travels through the sixth row, but we only need five for this month, do nothing
		if (lastDayOfMonth - 1 + currDayOfWeek < maxDaysForSixCalendarRows & i >= maxDaysForSixCalendarRows) {
			// Do Nothing
		}
		// For blank grids (leading or trailing blanks surrounding the 29-to-31 days of the month
		else if (i < currDayOfWeek | i > (lastDayOfMonth - 1 + currDayOfWeek)) {
			document.write("<td>&nbsp</td>");
		}
		// Default:  All days in the month (that may or may not have an event)
		else {
			eventIndex = irwinGetGameIndex(new Date(dateVar.getYear(), dateVar.getMonth(), k));
			// Nothing
			if (eventIndex == -1) {
				document.write("<td id=\"divIrwinScheduletd1\"><div>" + k + "</div></td>");
			}
			// Feis
			else if (schedArrayEventType[eventIndex] == 1) {
				document.write("<td id=\"divIrwinScheduletd2\"><a onMouseOver=\"irwinEventChange(" + eventIndex + ");\"><B>" + k + "</B></a></td>");
			}
			// Performance
			else if (schedArrayEventType[eventIndex] == 2) {
				document.write("<td id=\"divIrwinScheduletd3\"><a onMouseOver=\"irwinEventChange(" + eventIndex + ");\"><B>" + k + "</B></a></td>");
			}
			// Canceled Class
			else if (schedArrayEventType[eventIndex] == 3) {
				document.write("<td id=\"divIrwinScheduletd4\"><a onMouseOver=\"irwinEventChange(" + eventIndex + ");\"><B>" + k + "</B></a></td>");
			}
			else { alert("Javascript error detected.  Please notify the webmaster of this message:  Error 01"); }
			
			k++;
			
			// Jump to next row at end of 7 days
			if (wesMod(i,7) == 6) {
				document.write("</tr>");
				if (i<maxDaysForSixCalendarRows) { document.write("<tr>"); }
			} // end if wesMod()
		} // end else (default)
	} // end for loop
} // end function

// Dynamic HTML to re-display the Calendar
function irwinReWriteCalendar(selectedDate) {
	var text = "<table cellpadding=\"0\" cellspacing=\"2\" border=\"0\" width=\"164\" id=\"divIrwinSchedule\">";
	var tempText = "";
	var displayDate = new Date(selectedDate);
	var currDateTime = new Date(displayDate.getYear(), displayDate.getMonth(), displayDate.getDate(), 00, 00, 00); // Use input --> Diff than last function
	var dateVar = new Date(currDateTime.getYear(), currDateTime.getMonth(), 01, 00, 00, 00);
	var currDayOfWeek = dateVar.getDay();
	var lastDayOfMonth = irwinDaysInMonth(dateVar.getYear(), dateVar.getMonth());
	var eventIndex = -1;
	var i=0, j=0, k=1;
	var maxDaysForSixCalendarRows = 35; // This is from 7 days per week times 5 rows (as opposed to needing a sixth).

	// NEW:  The days of the week
	text += "<TR>";
	for (i=0; i<7; i++) {
		tempText = "<td id=\"divIrwinScheduletd0\"> <B> <div>" + abbrevDaysOfWeekInit[i] + "</div> </B> </td>";
		text += tempText;
	}
	text += "</TR> <TR>";

	for (i=0; i<42; i++) {
		// When i travels through the sixth row, but we only need five for this month, do nothing
		if (lastDayOfMonth - 1 + currDayOfWeek < maxDaysForSixCalendarRows & i >= maxDaysForSixCalendarRows) {
			// Do Nothing
		}
		// For blank grids (leading or trailing blanks surrounding the 29-to-31 days of the month
		else if (i < currDayOfWeek | i > (lastDayOfMonth - 1 + currDayOfWeek)) {
			text += "<td>&nbsp</td>";
		}
		// Default:  All days in the month (that may or may not have an event)
		else {
			eventIndex = irwinGetGameIndex(new Date(dateVar.getYear(), dateVar.getMonth(), k));
			// Nothing
			if (eventIndex == -1) {
				text += "<td id=\"divIrwinScheduletd1\"><div>" + k + "</div></td>";
			}
			// Feis
			else if (schedArrayEventType[eventIndex] == 1) {
				text += "<td id=\"divIrwinScheduletd2\"> <a onMouseOver=\"irwinEventChange(" + eventIndex + ");\"> <b>" + k + "</b> </a> </td>";
			}
			// Performance
			else if (schedArrayEventType[eventIndex] == 2) {
				text += "<td id=\"divIrwinScheduletd3\"> <a onMouseOver=\"irwinEventChange(" + eventIndex + ");\"> <b>" + k + "</b> </a> </td>";
			}
			// Canceled Class
			else if (schedArrayEventType[eventIndex] == 3) {
				text += "<td id=\"divIrwinScheduletd4\"> <a onMouseOver=\"irwinEventChange(" + eventIndex + ");\"> <b>" + k + "</b> </a> </td>";
			}
			else { alert("Javascript error detected.  Please notify the webmaster of this message:  Error 02"); }
			
			k++;
			
			// Jump to next row at end of 7 days
			if (wesMod(i,7) == 6) {
				text += "</TR>";
				if (i<maxDaysForSixCalendarRows) { text += "<TR>"; }
			} // end if wesMod()
		} // end else (default)
	} // end for loop
	
	text += "</table>";

	calendarDisplay.innerHTML = text;
	window.event.cancelBubble = true; // WESJ_REVISIT --> Not sure what this does
	//alert(text);
}

















function wesGetTargetDate() { //WESJ_REVISIT --> Bad coding, clean up later...
	var currDate = new Date();
	var returnDate = new Date("September 23 22:00 2007"); // WESJ_REVISIT --> Hardcoded = bad
	var i = 0;
	for (i = 0; i < 365; i++) {
		if (!schedArrayWesFix[i] | schedArrayWesFix[i] < currDate) {
			// Do Nothing
		} else {
			return schedArrayWesFix[i];
		}
	}
	return returnDate;
}