/**
 * calendar.js
 *
 * Creates a set of mini calendars from which a user can click
 * a date which then links to an anchor on the same pages where
 * the events are listed.
 *
 * @author     Unknown
 * @copyright  2008 by Thanksgiving Point, All Rights Reserved
 * @version    2.0
 */

function findAnchor(anchorName) {
	if (anchorExists(anchorName)) {
		self.location = "calendar.html#" + anchorName;
	}
	else {
		anchorName = anchorName.substr(0, 3);
		self.location = "calendar.html#" + anchorName;
	}
}

function anchorExists(anchorName) {
	for (var i = 0; i < window.document.anchors.length; i++) {
		if (window.document.anchors[i].name == anchorName) {
			return true;
		}
	}
	return false;
}

function leapYear(year) {
  if (year % 4 == 0) {// basic rule
    return true; // is leap year
  }
  /* else */ // else not needed when statement is "return"
  return false; // is not leap year
}

function getDays(month, year) {
  // create array to hold number of days in each month
  var ar = new Array(12);
  ar[0] = 31; // January
  ar[1] = (leapYear(year)) ? 29 : 28; // February
  ar[2] = 31; // March
  ar[3] = 30; // April
  ar[4] = 31; // May
  ar[5] = 30; // June
  ar[6] = 31; // July
  ar[7] = 31; // August
  ar[8] = 30; // September
  ar[9] = 31; // October
  ar[10] = 30; // November
  ar[11] = 31; // December

  // return number of days in the specified month (parameter)
  return ar[month];
}

function getMonthName(month) {
  // create array to hold name of each month
  var ar = new Array(12);
  ar[0] = "January";
  ar[1] = "February";
  ar[2] = "March";
  ar[3] = "April";
  ar[4] = "May";
  ar[5] = "June";
  ar[6] = "July";
  ar[7] = "August";
  ar[8] = "September";
  ar[9] = "October";
  ar[10] = "November";
  ar[11] = "December";

  // return name of specified month (parameter)
  return ar[month];
}

function getMiniCals() {
  var now = new Date();
  var nextMonth = now.getMonth() + 1;
  var year = now.getFullYear();
  if (nextMonth > 11) {
  	nextMonth = nextMonth % 12;
	year = year + 1;
  }
  var next = new Date(year, nextMonth);
  nextMonth = nextMonth + 1;
  if (nextMonth > 11) {
  	nextMonth = nextMonth % 12;
	year = year + 1;
  }
  var nextNext = new Date(year, nextMonth);
  

// ---- FIRST MONTH ----------------  
  var year = now.getFullYear();
  var month = now.getMonth();
  var monthName = getMonthName(month);
  var date = now.getDate();
  now = null;

// create instance of first day of month, and extract the day on which it occurs
  var firstDayInstance = new Date(year, month, 1);
  var firstDay = firstDayInstance.getDay();
  firstDayInstance = null;

// number of days in current month
  var days = getDays(month, year);

// call function to draw calendar
  document.write('<table cellpadding="5" cellspacing="0" border="0"><tr><td valign="top">'); 
  drawCal(firstDay + 1, days, date, monthName, year, 1);
  document.write('</td>'); 


// ---- SECOND MONTH ----------------  
  year = next.getFullYear();
  month = next.getMonth();
  monthName = getMonthName(month);
  date = next.getDate();
  next = null;

// create instance of first day of month, and extract the day on which it occurs
  firstDayInstance = new Date(year, month, 1);
  firstDay = firstDayInstance.getDay();
  firstDayInstance = null;

// number of days in current month
  days = getDays(month, year);

// call function to draw calendar
  document.write('<td valign="top">'); 
  drawCal(firstDay + 1, days, date, monthName, year);
  document.write('</td>'); 
  
// ---- THIRD MONTH ----------------  
  year = nextNext.getFullYear();
  month = nextNext.getMonth();
  monthName = getMonthName(month);
  date = nextNext.getDate();
  next = null;

// create instance of first day of month, and extract the day on which it occurs
  firstDayInstance = new Date(year, month, 1);
  firstDay = firstDayInstance.getDay();
  firstDayInstance = null;

// number of days in current month
  days = getDays(month, year);

// call function to draw calendar
  document.write('<td valign="top">'); 
  drawCal(firstDay + 1, days, date, monthName, year);
  document.write('</td></tr></table>'); 
  document.write('<span class="bodyNote"><em>(Closed Sundays)</em></span><br>'); 

}

function drawCal(firstDay, lastDate, date, monthName, year, highlightToday) {
  var monthAnchor = monthName.substr(0, 3);
  monthAnchor = monthAnchor.toLowerCase();
  
// create basic table structure
  var text = ""; // initialize accumulative variable to empty string
  text += '<table border="0" cellspacing="1" cellpadding="3">'; // table settings
  text += '<tr><td colspan="7" class="monthHeader" align="center">'; // create table header cell
  text += monthName + ' ' + year; 
  text += '</td></tr>'; // close header cell


  text += '<tr align="center" valign="center">';
  text += '<td class="monthDayLables">S</td>'; 
  text += '<td class="monthDayLables">M</td>'; 
  text += '<td class="monthDayLables">T</td>'; 
  text += '<td class="monthDayLables">W</td>'; 
  text += '<td class="monthDayLables">T</td>'; 
  text += '<td class="monthDayLables">F</td>'; 
  text += '<td class="monthDayLables">S</td>'; 
  text += '</tr>';
	
// declaration and initialization of two variables to help with tables
  var dayOfMonth = 1;
  var curCell = 1;
	
  for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
    text += '<tr align="right" valign="top">';
    for (var col = 1; col <= 7; ++col) {
      if ((curCell < firstDay) || (dayOfMonth > lastDate)) {
        text += '<td bgcolor="#FFFFFF"> </td>';
        curCell++
      } else {
          if ((dayOfMonth == date) && (highlightToday)){ // current cell represents today's date
            text += '<td class="todayDay"><a href="javascript:findAnchor(\'' + monthAnchor + dayOfMonth + '\');" class="todayDay">' + dayOfMonth + '</a></td>';
          } else {
              if (col == 1){ // sunday
			    text += '<td class="sundayDay">' + dayOfMonth + '</td>';
			  } else {
	            text += '<td class="regularDay"><a href="javascript:findAnchor(\'' + monthAnchor + dayOfMonth + '\');" class="regularDay">' + dayOfMonth + '</a></td>';
			  }
          }
          dayOfMonth++;
      }
    }
    text += '</tr>';
  }
	
// close all basic table tags
  text += '</table>';

// print accumulative HTML string
  document.write(text);
}