// JavaScript Document

// original code source: http://jszen.blogspot.com/2007/03/how-to-build-simple-calendar-with.html

// these are labels for the days of the week
cal_days_labels = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];

// these are human-readable month name labels, in order
cal_months_labels = ['January', 'February', 'March', 'April',
                     'May', 'June', 'July', 'August', 'September',
                     'October', 'November', 'December'];

// these are the days of the week for each month, in order
cal_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// this is the current date
cal_current_date = new Date(); 



function Calendar(month, year) {
  this.month = (isNaN(month) || month == null) ? cal_current_date.getMonth() : month;
  this.year  = (isNaN(year) || year == null) ? cal_current_date.getFullYear() : year;
  this.html = '';
}

Calendar.prototype.generateHTML = function(){

  // get first day of month
  var firstDay = new Date(this.year, this.month, 1);
  var startingDay = firstDay.getDay();
  
  // find number of days in month
  var monthLength = cal_days_in_month[this.month];
  
  // compensate for leap year
  if (this.month == 1) { // February only!
    if((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0){
      monthLength = 29;
    }
  }
  
  //calculate previous and next links
  if (this.month == 0) {
	var lastLink = '<a href="javascript:writeCalendar(11,' + (this.year-1) + ');">&laquo;</a>'; 
	var nextLink = '<a href="javascript:writeCalendar(' + (this.month+1) + ',' + (this.year) + ');">&raquo;</a>';	  
  }
  else if (this.month == 11) {
	var lastLink = '<a href="javascript:writeCalendar(' + (this.month-1) + ',' + (this.year) + ');">&laquo;</a>'; 
	var nextLink = '<a href="javascript:writeCalendar(0,' + (this.year+1) + ');">&raquo;</a>';  
  }
  else {
	var lastLink = '<a href="javascript:writeCalendar(' + (this.month-1) + ',' + (this.year) + ');">&laquo;</a>'; 
	var nextLink = '<a href="javascript:writeCalendar(' + (this.month+1) + ',' + (this.year) + ');">&raquo;</a>';
  }
  
  // do the header
  var monthName = cal_months_labels[this.month];
  var html = '<table id="calendar_nav"><tr><td colspan="5" style="text-align:left;"><p>' + monthName + "&nbsp;" + this.year + '</p></td>';
  html += '<td class="nav_cell"><p>' + lastLink + '</p></td>';
  html += '<td class="nav_cell"><p>' + nextLink + '</p></td>';
  html += '</tr><tr>';
  for(var i = 0; i <= 6; i++ ){
    html += '<td>';
    html += cal_days_labels[i];
    html += '</td>';
  }
  html += '</tr></table><table id="calendar_table" cellpadding="0" cellspacing="0"><tr>';

  // fill in the days
  var day = 1;
  // this loop is for is weeks (rows)
  for (var i = 0; i < 9; i++) {
    // this loop is for weekdays (cells)
    for (var j = 0; j <= 6; j++) { 
      html += '<td>';
      if (day <= monthLength && (i > 0 || j >= startingDay)) {
        html += writeDay(day, this.month, this.year);
        day++;
      }
	  else {
	  	html += '&nbsp;';
	  }
      html += '</td>';
    }
    // stop making rows if we've run out of days
    if (day > monthLength) {
      break;
    } else {
      html += '</tr><tr>';
    }
  }
  html += '</tr></table>';

  this.html = html;
}

Calendar.prototype.getHTML = function() {
  return this.html;
}

//write calendar to page
function writeCalendar(numMonth, numYear) {
	if (numMonth!="" && numYear!="") {
		var cal = new Calendar(numMonth, numYear);
	}
	else {
		var cal = new Calendar();	
	}
	cal.generateHTML();
	theDiv = document.getElementById("calendar");
	theDiv.innerHTML = cal.getHTML();
}

function writeDay(numDay, numMonth, numYear) {
	var theDate = (numMonth+1) + '/' + (numDay) + '/' + numYear;
	
	var i = -1;
	if (swEventDates.indexOf) {
		if (swEventDates.indexOf(theDate)!=-1) {
			var i = swEventDates.indexOf(theDate);
		}
	}
	else {
		for (z=0;z<swEventDates.length;z++) {
			
			if 	(swEventDates[z] == theDate) {
			var i = z;
			break;
			}
		}
	}
	
	if (i!=-1) {
		
		return '<a href="' + swEventFiles[i] + '">' + numDay + '</a>';
		}
	else {
		return numDay;
		}
	}

function swapImage(imgName, sFilename, mouseEvent) {
	thisImgSrc = document.images[imgName].src
	document.images[imgName].src = "images/" + sFilename + mouseEvent + ".jpg";
}

function formatDate(whichDate){
	showDate = new String()
	thisDate = new Date(whichDate)
	thisMonth = thisDate.getMonth()
	thisMonth += 1
	thisDay = thisDate.getDate()
	thisYear = thisDate.getYear()

	switch(thisMonth){
		case 1:
			showDate = "January ";
			break;
		case 2:
			showDate = "February ";
			break;
		case 3:
			showDate = "March ";
			break;
		case 4:
			showDate = "April ";
			break;
		case 5:
			showDate = "May ";
			break;
		case 6:
			showDate = "June ";
			break;
		case 7:
			showDate = "July ";
			break;
		case 8:
			showDate = "August ";
			break;
		case 9:
			showDate = "September ";
			break;
		case 10:
			showDate = "October ";
			break;
		case 11:
			showDate = "November ";
			break;
		case 12:
			showDate = "December ";
			break;
		}
		
		
		showDate = showDate + thisDay + ", " + thisYear
		
		return showDate
		
	}