﻿/*****************
* TODO
* ----
* add earliest navigable date
* add latest navigable date
******************/

//constants
CAL_DAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
CAL_MONTHS = ["January", "February", "March", "April", "May", "June", "July", 
			  "August", "September", "October", "November", "December"];

cal_current_date = new Date();	//today
//if no date passed, default to today
function Calendar(year, month) {
	this.month = (isNaN(month) || month == null) ? cal_current_date.getMonth() : month;
	this.year  = (isNaN(year) || year == null) ? cal_current_date.getFullYear() : year;
	this.html = "";
	this.addNavigation = false;
	this.minimumDate = null;
}

//utility functions
function GetDaysInMonth(year, month) {
	var dd = new Date(year, month + 1, 0);
	return dd.getDate();
}

function GetCalendarRows(monthLength, startingDay) {
	return(Math.ceil(monthLength + startingDay) / 7);
}

Calendar.prototype.NextMonth = function(){
	this.month = (this.month + 1) % 12;
	if (this.month == 0){
		this.year++;
	}
}

Calendar.prototype.PreviousMonth = function () {
	this.month = (this.month + 11) % 12;
	if (this.month == 11) {
		this.year--;
	}
}

Calendar.prototype.Month = function () {
	return this.month;
}

Calendar.prototype.Year = function () {
	return this.year;
}

Calendar.prototype.MinimumDate = function () {
	return this.minimumDate;
}

//do it.
Calendar.prototype.getHtml = function () {
	return this.html;
}

Calendar.prototype.generateHtml = function () {
	//member variables
	var firstDay = new Date(this.year, this.month, 1);
	var startingDay = firstDay.getDay();
	var monthLength = GetDaysInMonth(this.year, this.month);
	var monthName = CAL_MONTHS[this.month];

	//top row with month/year
	var html = "<table class='calendar'>"
		+ "<tr class='month'>";
	html += "<th><a href='#' class='prev'>&lt;</a></th>"
		+ "<th colspan='5'>" + monthName + " " + this.year + "</th>"
		+ "<th><a href='#' class='next'>&gt;</a></th>";
	html += "</tr>";
	//row of day names
	html += "<tr class='days'>";
	for (var i = 0; i <= 6; i++) {
		html += '<th>' + CAL_DAYS[i].substr(0, 1) + "</th>";
	}
	html += "</tr>";
	//fill in the dates
	html += "<tr>";
	var day = 1;
	//loop the weeks (rows)
	for (var i = 0; i < 9; i++) {
		//loop the days (cells)
		for (var j = 0; j <= 6; j++) {
			html += "<td>";
			if (day <= monthLength && (i > 0 || j >= startingDay)) {
				html += day++ + "<br>";
			}
			html += "</td>";
		}
		if (day > monthLength) {
			break;
		} else {
			html += "</tr><tr>";
		}
	}

	html += "</tr>"
	html += "</table>"	//display complete

	this.html = html;
}

//Appointment Object
function Appointment(title, details, location, start, end, className) {
	this.title = title;
	this.details = details;
	this.location = location;
	this.start = start;
	this.end = end;
	this.className = className;
}
Appointment.prototype.Title = function(){
	return this.title;
}
Appointment.prototype.Details = function(){
	return this.details;
}
Appointment.prototype.Location = function(){
	return this.location;
}
Appointment.prototype.Start = function(){
	return this.start;
}
Appointment.prototype.End = function(){
	return this.end;
}
Appointment.prototype.Class = function(){
	return this.className;
}



