//  SET ARRAYS
var day_of_week = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var DAYS_OF_WEEK = 7;		// "constant" for number of days in a week
var DAYS_OF_MONTH = 31;		// "constant" for number of days in a month

//  DECLARE AND INITIALIZE VARIABLES
var nowDate = new Date();
var now_year = nowDate.getFullYear();// Returns year
var now_month = nowDate.getMonth(); // Returns month (0-11)
var now_date = nowDate.getDate();	// Returns daate of month (1-31)
var now_day = nowDate.getDay();    	// Returns day of week (0-6) - not Rainbow "day"!

function calendarCell(cal_date,high) {
	if (high)
		return '<TD class="selected"><A HREF="'+location.pathname+'?day='+cal_date+'&monthOffset='+monthOffset+'&tz='+tz+'&view='+view+'">'+cal_date+'</A>';
	else
		return '<TD class="unselected"><A HREF="'+location.pathname+'?day='+cal_date+'&monthOffset='+monthOffset+'&tz='+tz+'&view='+view+'">'+cal_date+'</A></TD>';
}

function daysInFebruary(year) {
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function getCalendar()
{
    var schDate = new Date();
	var sch_month = now_month;
	var sch_year = now_year;
	var sch_date = day ? day : now_date;
	sch_month += monthOffset;
	while (sch_month < 0) {
		--sch_year;
		sch_month += 12;
	}
	while (sch_month > 11) {
		++sch_year;
		sch_month -= 12;
	}

	schDate.setDate(1);	
	schDate.setFullYear(sch_year);
	schDate.setMonth(sch_month);
	schDate.setDate(sch_date);
	var sch_day = schDate.getDay();	// Day of week (0-6)

	schDate.setDate(1);    			// Start the calendar day at '1'
	var itr_day = schDate.getDay();	// Day of week (0-6)
	var TR_start = '<TR class="numbers">';
	var TR_end = '</TR>';
	var TD_start = '<TD><CENTER>';
	var TD_end = '</CENTER></TD>';

	var retval = '<div class="calendarNavigation">';
	retval += '<a href="/schedule/"><img border="0" src="http://media.ifc.com/img/schedule/today.gif" alt="Today" /></a><br/>';
	retval += '<a id="prev" href="javascript:adjustMonth(-1);"><img border="0" src="http://media.ifc.com/img/schedule/iconPrev.gif" alt="Previous" /></a>';
	retval += '<span class="month">' + month_of_year[sch_month].substring(0,3) + '</span>';
	retval += '<a id="next" href="javascript:adjustMonth(1);"><img border="0" src="http://media.ifc.com/img/schedule/iconNext.gif" alt="Next" /></a><br/>';
	retval += '<span class="day">' + day + '</span><br/>';
	retval += '<span class="year">' + sch_year + '</span>';
	retval += '</div>';
	retval += '<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=2>' + TR_start;
	
	// Black last month's dates at start of first week
	for (var i = 0; i < itr_day;++i) 
		retval += TD_start + ' ' + TD_end;

	schDate = null;

	DAYS_OF_MONTH = (sch_month == 3 || sch_month == 5 || sch_month == 8 || sch_month == 10) ? 30 : 31;
	if (sch_month == 1)
		DAYS_OF_MONTH = daysInFebruary(sch_year);

	var itr_end = itr_day + DAYS_OF_MONTH > 35 ? 42 - (itr_day + DAYS_OF_MONTH) : 35 - (itr_day + DAYS_OF_MONTH) ;

	// LOOPS FOR EACH DAY IN CALENDAR
	for (var i = 0; i < DAYS_OF_MONTH; ++i) {
		var itr_date = i+1;
		if (itr_day == 0)
				retval += TR_start;
		if (itr_date == sch_date)
			retval += calendarCell(itr_date,true);
		else
			retval += calendarCell(itr_date,false);

		if (++itr_day == DAYS_OF_WEEK) {
			retval += TR_end;
			itr_day = 0;
		}
	}
	
	// LOOPS TO CLOSE OUT THE CALENDAR WITH EMPTY SQUARES
	for (var x = 0; x < itr_end; ++x) {
		retval += TD_start + ' ' + TD_end;
	}
	
	retval += '</TD></TR></TABLE>';

	retval += '<BR/>'
		/*+'  monthOffset = '+monthOffset
		+', day = '+(day ? day : '&lt; Not set &gt;')
		+', view = "'+view+'"'
		+', tz = "'+tz+'"'
		+', bc = "'+bc+'"'
		+'<BR/><BR/>'
		*/;

	return retval;
}

function renderCalendar() {

	//document.getElementById('ifcCalendar').innerHTML = 'Loading...';
	document.getElementById('cal').innerHTML = getCalendar();

}

function renderCalendarForTz() {
	if (tz == 'PT') {
		var option = document.getElementById('west').checked = true;
	}
	else {
		var option = document.getElementById('east').checked = true;
	}
	renderCalendar();
}

function setDate(cal_date) {
	day = cal_date;
	renderCalendar();
}

function adjustMonth(offset) {
	if (offset > 0)
		day = 1;
	else {
		var sch_month = now_month + monthOffset + offset;
		var sch_year = now_year;
		while (sch_month > 11) {
			++sch_year;
			sch_month -= 12;
		}
		while (sch_month < 0) {
			--sch_year;
			sch_month += 12;
		}
		DAYS_OF_MONTH = (sch_month == 3 || sch_month == 5 || sch_month == 8 || sch_month == 10) ? 30 : 31;
		if (sch_month == 1)
			DAYS_OF_MONTH = daysInFebruary(sch_year);
		day = DAYS_OF_MONTH;
	}	
	monthOffset += offset;
	renderCalendar();
}

function getElementsByTagName(element, tagname)
{
	return ie ? element.getElementsByTagName('tns:'+tagname) : element.getElementsByTagNameNS('http://www.example.org/Schedule', tagname);
}