// Edeptive Events Calendar Gadget - display functions
// Copyright (c) 2007 Edeptive
// www.edeptive.com
// [requires event_handlers_<environment>.js for the environment you want to run in (e.g. vista)]

// constants
var kRefreshSchool = 1, kRefreshHourly = 2, kRefreshDaily = 3;
// globals class
function CalendarSettings() {
	this.fShowYearControls = false; // show year arrows (true or false)
	this.fShowMonthControls = true; // show month arrows (true or false)
	this.fShowYear = false; // show year in heading (true or false)
	this.sButtonPrevMonth = '<img src="/assets/images/calendar/prev.gif" width="17" height="17" border="0" alt="Previous Month" />';
	this.sButtonNextMonth = '<img src="/assets/images/calendar/next.gif" width="17" height="17" border="0" alt="Next Month" />';
	this.sButtonPrevYear = '<img src="/assets/images/calendar/prev_year.gif" width="17" height="17" border="0" alt="Previous Year" />';
	this.sButtonNextYear = '<img src="/assets/images/calendar/next_year.gif" width="17" height="17" border="0" alt="Next Year" />';
	this.aMonthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	this.aDayNames = new Array("S", "M", "T", "W", "T", "F", "S");
	this.aEventsDates = new Array(); // to contain arrays of CalendarEvent objects by date
	this.aEvents = new Array(); // to contain CalendarEvent objects by id
	this.iRefreshPattern = kRefreshSchool;
	this.oRefreshTimeout = null; // id of refresh timeout, so it can be cancelled
}
var gCalendarSettings = new CalendarSettings();

// CalendarEvent class, will be instantiated for each event loaded from the webservice
function CalendarEvent(sID, dateStart, dateEnd, sTitle, sSummary, sDescription) {
	this.sID = sID;
	this.dateStart = dateStart;
	this.dateEnd = dateEnd;
	this.sTitle = sTitle;
	this.sSummary = sSummary;
	this.sDescription = sDescription;
} // end CalendarEvent class

// returns a string of HTML defining the calendar
// iYear is full year (e.g. 2007), iMonth is 0 based (e.g. January = 0)
function buildCalendar(iYear, iMonth) {
	var strHtml = "";
	var dateCurrent = null, dateToday = new Date();
	var sDayClass = "", sDayHtml = "";
	var aEvents = null;
	
	// work out first day to display (because most months don't start on a sunday)
	dateCurrent = new Date(iYear, iMonth, 1);
	dateCurrent.setDate(dateCurrent.getDate() - dateCurrent.getDay()); // move back to sunday
	// controls
	strHtml += buildCalendarControls(iYear, iMonth);
	// calendar table
	strHtml += "<table id='calTable' border='0' cellspacing='0' cellpadding='0'>";
	// days of week
	strHtml += "<tr>";
	for(var i = 0; i < gCalendarSettings.aDayNames.length; i++) {
		strHtml += "<th>" + gCalendarSettings.aDayNames[i] + "</th>";
	}
	strHtml += "</tr>";
	// weeks
	for(var iWeek = 1; iWeek < 7; iWeek++) {
		strHtml += "<tr>";
		for(var iDayOfWeek = 0; iDayOfWeek < 7; iDayOfWeek++) {
			sDayHtml = dateCurrent.getDate();
			aEvents = getEvents(dateCurrent);
			sDateCurrent = dateCurrent.getFullYear() + (dateCurrent.getMonth() < 9 ? "0" : "") + (dateCurrent.getMonth() + 1) + (dateCurrent.getDate() < 10 ? "0" : "") + dateCurrent.getDate();
			if(dateCurrent.getMonth() != iMonth) {
				sDayClass = "dayOtherMonth";
			} else if(aEvents == null) {
				sDayClass = "day";
			} else {
				sDayClass = "dayEvent";
				sDayHtml = "<a href='#' onmouseover='onEventRollOn(\"" + sDateCurrent + "\")' onmouseout='onEventRollOff(\"" + sDateCurrent + "\");' onclick='onEventClick(\"" + sDateCurrent + "\");return false;'>" + sDayHtml + "</a>";
			}
			if((dateCurrent.getMonth() == dateToday.getMonth()) && (dateCurrent.getDate() == dateToday.getDate()) && (dateCurrent.getFullYear() == dateToday.getFullYear())) {
				sDayClass += " dayToday";
			}
			strHtml += "<td class='" + sDayClass + "'>" + sDayHtml + "</td>";
			dateCurrent.setDate(dateCurrent.getDate() + 1); // move to next day (automatically updates month)
		}
		strHtml += "</tr>";
	}
	strHtml += "</table>";
	return strHtml;
} // end buildCalendar

// returns a string of HTML defining the calendar controls
// iYear is full year (e.g. 2007), iMonth is 0 based (e.g. January = 0)
function buildCalendarControls(iYear, iMonth) {
	var strHtml = "";
	
	strHtml += "<table id='calControls' cellpadding='0' cellspacing='0'>";
	strHtml += "<tr>";
	if(gCalendarSettings.fShowYearControls) {
		strHtml += "<td class='prevYear'><a href='#' onclick='goMonth(" + (iYear - 1) + "," + iMonth + "); return false;'>" + gCalendarSettings.sButtonPrevYear + "</a></td>";
	}
	if(gCalendarSettings.fShowMonthControls) {
		strHtml += "<td class='prevMonth'><a href='#' onclick='goMonth(" + iYear + "," + (iMonth - 1) + "); return false;'>" + gCalendarSettings.sButtonPrevMonth + "</a></td>";
	}
	strHtml += "<td class='monthName'>" + gCalendarSettings.aMonthNames[iMonth] + " " + iYear + "</td>";
	if(gCalendarSettings.fShowYear) {
		strHtml += "<td class='year'>" + iYear + "</td>";
	}
	if(gCalendarSettings.fShowMonthControls) {
		strHtml += "<td class='nextMonth'><a href='#' onclick='goMonth(" + iYear + "," + (iMonth + 1) + "); return false;'>" + gCalendarSettings.sButtonNextMonth + "</a></td>";
	}
	if(gCalendarSettings.fShowYearControls) {
		strHtml += "<td class='nextYear'><a href='#' onclick='goMonth(" + (iYear + 1) + "," + iMonth + "); return false;'>" + gCalendarSettings.sButtonNextYear + "</a></td>";
	}
	strHtml += "</tr>";
	strHtml += "</table>";
	return strHtml;
} // end buildCalendarControls

// returns a CalendarEvent if there is an event on dateTest, null otherwise
function getEvents(dateTest) {
	var oEvent = null;
	
	oEvent = gCalendarSettings.aEventsDates[dateTest.getFullYear() + (dateTest.getMonth() < 9 ? "0" : "") + (dateTest.getMonth() + 1) + (dateTest.getDate() < 10 ? "0" : "") + dateTest.getDate()];
	// actually an array, cheat for now
	if(oEvent != null) oEvent = oEvent[0];
	return oEvent;
} // end getEvents

// displays the given month (0 based) of the given year (adjusts year if month<0 or month>11)
function goMonth(iYear, iMonth) {
	if(iMonth < 0) {
		iYear--;
		iMonth = 11;
	} else if(iMonth > 11) {
		iYear++;
		iMonth = 0;
	}
	getEventXml(iYear, iMonth + 1);
} // end goMonth

// View the calendar in different modes this week, next month, year, next year etc.
function cal_jumpMenu(selObj){
  objValue = selObj.options[selObj.selectedIndex].value;
  if (objValue != '0') {
  	eval(objValue+"()");
  }
}