if(Calendar == undefined) var Calendar = {};

Calendar = Object.extend(Calendar, 
{
	months : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
	weekdays : ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
	weekdaysAbbrev : ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
	weekdaysInitial : ["S", "M", "T", "W", "R", "F", "S"], 
	
	allowMonthNavLinks : true,
	prevMonthIcon : "/Themes/pghmom/images/pghmom/calendar_arrow_left.png",
	nextMonthIcon : "/Themes/pghmom/images/pghmom/calendar_arrow_right.png",
	
	table : document.createElement("table"),

	isLeapYear : function(y) {
		// Returns true if the year passed in is a leap year.	
		var rc = false;
			
		if(y % 4 == 0) {
			rc = true;
			if(y % 100 == 0) {
				if(y % 400 != 0) {
					rc = false;
				}
			}
		}	
		
		return rc;
	},
		
	daysInMonth : function(m, y) {
		// Returns the number of days in the month for the year specified.
		if(m == 0 || m == 2 || m == 4 || m == 6 || m == 7 || m == 9 || m == 11) {
			return 31;
		} else if(m == 1) {
			if(this.isLeapYear(y)) {
				return 29;
			} else {
				return 28;
			}
		} else {
			return 30;
		}	
	},
	
	draw : function(containerElement, startingMonth, startingYear) {
		if(startingMonth == undefined || startingYear == undefined) {
			startingMonth = new Date().getMonth();
			startingYear = new Date().getFullYear();
		}
	
		// Draws the calendar into the container element.		
		var i = 0;
		var currentRow = 0;
		var currentClass = "calendar_non_day";		
		
		// Gut the container to empty it out.
		while(containerElement.childNodes[0])
			containerElement.removeChild(containerElement.childNodes[0]);
		
		// Start building a new table for the calendar.
		var table = document.createElement("table");
		table.className = "calendar";
		table.createTHead();		
		
		// Make the header bar with the prev/next links.
		table.tHead.insertRow(0);
		table.tHead.rows[0].className = "calendar_banner";
		currentRow++;
		var cellPrev = table.tHead.rows[0].insertCell(0);
		var cellLabel = table.tHead.rows[0].insertCell(1);
		var cellNext = table.tHead.rows[0].insertCell(2);
		cellLabel.colSpan = 5;
		
		// Figure out the month/year for the previous month.
		var prevMonth = startingMonth - 1;
		var prevYear = startingYear;
		if(prevMonth < 0) {
			prevMonth = 11;
			prevYear--;
		}
		
		// Figure out the month/year for the next month.
		var nextMonth = startingMonth + 1;
		var nextYear = startingYear;
		if(nextMonth > 11) {
			nextMonth = 0;
			nextYear++;
		}		
		
		var prevLink = document.createElement("a");
		prevLink.setAttribute("href", "javascript:Calendar.draw($('" + containerElement.id + "'), " + prevMonth + ", " + prevYear + "); void 0;");
		var prevLinkImg = new Image();
		prevLinkImg.src = this.prevMonthIcon;
		prevLinkImg.alt = "Last month";
		prevLinkImg.style.border = "none";
		prevLink.appendChild(prevLinkImg);
		cellPrev.appendChild(prevLink);
				
		var nextLink = document.createElement("a");		
		nextLink.setAttribute("href", "javascript:Calendar.draw($('" + containerElement.id + "'), " + nextMonth + ", " + nextYear + "); void 0;");		
		var nextLinkImg = new Image();
		nextLinkImg.src = this.nextMonthIcon;
		nextLinkImg.alt = "Next month";
		nextLinkImg.style.border = "none";		
		nextLink.appendChild(nextLinkImg);
		cellNext.appendChild(nextLink);
		
		// Add the banner label (month/year).
		var bannerLabel = this.months[startingMonth].toUpperCase() + " " + startingYear; 
		cellLabel.appendChild(document.createTextNode(bannerLabel));			
		
		// Build the days bar.
		table.tHead.insertRow(1);		
		table.tHead.rows[1].className = "calendar_days_bar";
		for(var i = 0; i < this.weekdaysInitial.length; i++) {
			var thisCell = table.tHead.rows[1].insertCell(i);
			thisCell.innerHTML = this.weekdaysInitial[i];
		}
		currentRow++;
		
		var currentDate = new Date(startingYear, startingMonth, 1); 
		var days = this.daysInMonth(currentDate.getMonth(), currentDate.getFullYear());
		var startDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
		var endDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + days - 1);			
		
		// If the month doesn't start on a Sunday, 
		// march backward to get the last Sunday before the month starts.
		while(startDate.getDay() > 0) {
			//startDate = new Date(startDate.getTime() - 86400000);	
			startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() - 1);
		}
		
		// If the month doesn't end on a Saturday, 
		// march forward to get the first Saturday after the month ends.
		while(endDate.getDay() < 6) {
			//endDate = new Date(endDate.getTime() + 86400000);		
			endDate = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate() + 1);	
		}		
	
		// Fractional day values occur when the month in question spans both EST and DST.
		var totalDays = ((endDate - startDate) / 86400000) + 1;
		switch(currentDate.getMonth()) {
			case 2: 
				totalDays = Math.ceil(totalDays);				
				break;
				
			case 10:
				totalDays = Math.floor(totalDays);
				break;
				
			default:
				break;
		}		
		
		var row;
		var cell;
		var link;
		var actualDate = new Date();
		
		for(i = 0; i < totalDays; i++) {
			//currentDate = new Date(startDate.getTime() + (86400000 * i));	
			currentDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + i);			
			
			// Set the cell class based on the month visible.
			if(currentDate.getMonth() != startingMonth) {
				currentClass = "calendar_non_day";	
			} else {
				currentClass = "calendar_day";
			}
			
			// If the date in the calendar is today, mark it as such.
			if(actualDate.getFullYear() == currentDate.getFullYear() && actualDate.getMonth() == currentDate.getMonth() && actualDate.getDate() == currentDate.getDate()) {
				currentClass = "calendar_today";			
			}
			
			// Beginning of week; append new row.			
			if(currentDate.getDay() == 0) {				
				row = table.insertRow(currentRow);
				currentRow++;
			}
			
			cell = row.insertCell(currentDate.getDay());			
			
			if(currentDate > actualDate - 86400000) {
				link = document.createElement("a");
				link.setAttribute("href", "http://www.post-gazette.com/events/?mode=9&id=2&cm=" + (currentDate.getMonth() + 1).toString() + "&cd=" + currentDate.getDate().toString() + "&cy=" + currentDate.getFullYear().toString());
				link.setAttribute("target", "_blank");
				link.appendChild(document.createTextNode(currentDate.getDate()));
			} else {
				link = document.createTextNode(currentDate.getDate());
			}
			
			//cell.innerHTML = "<a href='http://www.post-gazette.com/events/default.asp?mode=1&cm=" + currentDate.getMonth() + 1.toString() + "&cd=" + currentDate.getDate().toString() + "&cy=" + currentDate.getFullYear().toString() + "'>" + currentDate.getDate() + "</a>";				
			cell.appendChild(link);
			cell.className = currentClass;					
		}	
	
		containerElement.appendChild(table);	
	},
	
	addDays : function(dateVal, daysToAdd) {
		// This is here because daylight saving time messes with the javascript math when adding dates.
		var date2 = dateVal.setDate(dateVal.getDate() + daysToAdd);		
	}
});