// JavaScript Document

function Calendar(name, calendarContainer) {
	this.containerElement = calendarContainer;	// the ID of of div on the page that is to contain this calendar
	this.name = name;	// as in what we called the variable that this calendar is stored in, so we can make links to it.
	
	
	this.allowPastMonths = true;
	this.datesAsLinks = true;
	this.dateLink = "";
	this.dateLinkFunction = null;
	this.onDates={};
	this.permenantOnDates={};	// dates that can not be toggled
	
	this.date = new Date();
	
	this.date.setDate(1);

}

new Calendar();	// to force object prototype creation


/* ==============================================
                 Class Attributes
		       (basically constants)
=================================================*/





/* ==============================================
               Instance Methods
=================================================*/


/* ==== On Dates ===== */
Calendar.prototype.onDate = function(dateString) {	// date is alwasy yyyymmdd
	if (this.onDates[dateString]) {
		return true;
	} else {
		return false;	
	}
}

Calendar.prototype.setOnDates = function(datesArray) {
	this.onDates={};
	for (var i=0; i<datesArray.length; i++) {
		x = datesArray[i]
		this.addOnDate(x);
	}
	this.draw()
}

Calendar.prototype.addOnDate = function(dateString) {
	this.onDates[dateString] = true;
}
Calendar.prototype.removeOnDate = function(dateString) {
	delete this.onDates[dateString];
	this.draw()
}
Calendar.prototype.toggleOnDate = function(dateString) {
	if (this.onDate(dateString)) {
		this.removeOnDate(dateString);
		this.draw()
		return false;
	} else {
		this.addOnDate(dateString);
		this.draw()
		return true;
	}
	
}


/* ==== Permenant On Dates ===== */
Calendar.prototype.permenantOnDate = function(dateString) {	// date is alwasy yyyymmdd

	if (this.permenantOnDates[dateString]) {
		return true;
	} else {
		return false;	
	}
}

Calendar.prototype.setPermenantOnDates = function(datesArray) {
	this.permenantOnDates={};
	for (var i=0; i<datesArray.length; i++) {
		x = datesArray[i]
		this.addPermenantOnDate(x);
	}
	this.draw()
}

Calendar.prototype.addPermenantOnDate = function(dateString) {
	this.permenantOnDates[dateString] = true;
	//this.draw()
}




Calendar.prototype.draw = function() {
	
	var now = new Date();

	var prevLink = (this.date > now  || this.allowPastMonths)
		?
			"<a class='prevMonth' href='javascript:" + this.name + ".prevMonth()'>&lt;</a>"
		:
			"";
	
	var cal = 
	"<div class='calendarContainer'>" + 						// the cendar needs a container to keep the transp background
																// and the links working in IE6
		"<div class='calendar'>" + 
		"	<div class='heading'>" + 
	
				prevLink +
		"		<a class='nextMonth' href='javascript:" + this.name + ".nextMonth()'>&gt</a>" +
		" 		<h1>" + this.monthName() + " " + this.year() + "</h1>" + 
		"	</div>" + 
		"	<div class='dates'>" +
				this.makeDatesTable() +
		"	</div>" + 
		"</div>" + 
		"</div>" + 
		""
	;
	
	document.getElementById(this.containerElement).innerHTML = cal;
	
}

Calendar.prototype.makeDatesTable = function() {
	
	var columnIndexes = [1,2,3,4,5,6,0];
	
	//alert(this.monthStartDay());
	var blankCellsAtStart = this.monthStartDay() -1;	// 0-6
	if (blankCellsAtStart < 0) { blankCellsAtStart=6};
	
	//alert("blanks: " + blankCellsAtStart + "  startDay: " + this.monthStartDay());
	
	var day = 1;	
	var started = false;
	

	var tableRows="";
	var count=0;
	
	var headerRow = "<tr>";
	for (var i=0; i<columnIndexes.length; i++) {	// each column of days

		headerRow += "<th>" + Date.dayName(columnIndexes[i]).charAt(0) + "</th>";	
	}
	tableRows += headerRow + "</tr>";


	while (day <= this.daysInMonth()) {
		var thisRow = "<tr>";
	
		for (var i=0; i<columnIndexes.length; i++) {	// each column of days
			
			
			if (( count < blankCellsAtStart) || (day > this.daysInMonth()) ) {
				thisRow += "<td></td>";	
			} else {

				var dateString = this.year() + (this.month()+1).pad(2) + day.pad(2);
				
				var link = 	this.dateLinkFunction	// were we given the name of a function to call?
							?
								"javascript:" + this.dateLinkFunction + "(" + dateString + ")"
							:
								this.dateLink;		// maybe we have a complete link to use instead.

				
				link = link.replace("DATESTRING", dateString);
				
				var linkClass =	this.permenantOnDate(dateString)
								?
									"datePermenantOn"
									
								:	this.onDate(dateString)
									?
										"dateOn"
									:
										"dateOff";
				
				
				if (this.datesAsLinks && !this.permenantOnDate(dateString)) {
					thisRow += "<td class='" + linkClass +"'><a href='" + link + "'>" +day +"</a></td>";
				} else {
					thisRow += "<td class='" + linkClass +"'>" + day + "</td>";
				}
				day++;
			}
			
			count++;
		}

		tableRows += thisRow + "</tr>";
	}

	return (
		"<table class='datesTable' cellpadding='0' cellspacing='0' border='0'>" +
			tableRows +
		"</table>"
	);
	
}

Calendar.prototype.year = function() {
	return this.date.getFullYear();
}

Calendar.prototype.month = function() {
	return this.date.getMonth();
}
Calendar.prototype.daysInMonth = function() {

	return this.date.daysInMonth();
}

Calendar.prototype.monthName= function() {
	return Date.monthName(this.month());
}

Calendar.prototype.monthStartDay= function() {
	return this.date.monthStartDay();
}



Calendar.prototype.nextMonth = function() {
	// increment to the next month
	var month = this.month() +1;
	var year = this.year();
	if (month > 11) {
		month=0;
		year+=1;
	}
	
	this.date.setFullYear(year, month);
	this.draw()
}

Calendar.prototype.prevMonth = function() {
	// increment to the next month
	var month = this.month() -1;
	var year = this.year();
	if (month <0) {
		month=11;
		year-=1;
	}
	
	this.date.setFullYear(year, month);
	this.draw()
}


/* ==============================================
                 Class Methods
=================================================*/





