// Highlights the buttons for calendar next and previous link
function VirtualButton(Cell, ButtonDown) 
{
	if (ButtonDown) 
	{
		Cell.style.borderLeft = 'buttonshadow 1px solid';
		Cell.style.borderTop = 'buttonshadow 1px solid';
		Cell.style.borderBottom = 'buttonhighlight 1px solid';
		Cell.style.borderRight = 'buttonhighlight 1px solid';
	}
	else 
	{
		Cell.style.borderLeft = '1px solid buttonface';
		Cell.style.borderTop = '1px solid buttonface';
		Cell.style.borderBottom = '1px solid buttonface';
		Cell.style.borderRight = '1px solid buttonface';
	}
}

// these are labels for the days of the week
cal_days_labels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

// 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;
	}
  }
  
	// do the header
	var monthName = cal_months_labels[this.month];
	var html = '';
	html += '<table width="100%" cellpadding="1" cellspacing="0" class="calendar-table">';
	html += '<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 class="calendar-day">';
			
			if (day <= monthLength && (i > 0 || j >= startingDay)) 
			{
				html += day;
				day++;
			}
			
			html += '</td>';
		}
		
		// stop making rows if we've run out of days
		if (day > monthLength) 
		{
			break;
		} 
		else 
		{
			html += '</tr><tr>';
		}
	}
  
  html += '</tr>';
  html += '</table>';

  this.html = html;
}

Calendar.prototype.getHTML = function() {
  return this.html;
}

 var monthLength;

//SWU CALENDAR
function cal_navigate(month, year , day_today)
{
	month = parseInt(month);
	year = parseInt(year);

	month--;

	// get first day of month
	firstDay = new Date(year, month, 1);
	startingDay = firstDay.getDay();
	
	// find number of days in month
	 monthLength = cal_days_in_month[month];
  
	// compensate for leap year
	if (month == 1) 
	{ 
		// February only!
		if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		{
			monthLength = 29;
		}
	}
  	
	// do the header
	var monthName = cal_months_labels[month];
	var html = '';
	html += '<table width="100%" cellpadding="1" cellspacing="0" class="calendar-table">';
	html += '<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++) 
		{ 
			if (day <= monthLength && (i > 0 || j >= startingDay)) 
			{
				var calendar_day_style = day == day_today ? "calendar_current_day" : "calendar-day";
				var dat_style = day == day_today ? day : "<a>"+ day +"</a>";
				
				if(day == day_today) {  var day_clicked = "schoolcalendar"+day;}
				
				html += '<td class="' + calendar_day_style + '" align="center" id="schoolcalendar'+ day +'" onclick="javascript: getCalendar_events('+ year +', '+ month +', '+ day +', \'schoolcalendar'+ day +'\');">';
				html += dat_style;
				day++;
			}
			else
			{
				html += '<td align="center" class="calendar_day_blank">';
				html += '&nbsp;';
			}
			
			html += '</td>\n';
		}
		
		// stop making rows if we've run out of days
		if (day > monthLength) 
		{
			break;
		} 
		else 
		{
			html += '</tr><tr>';
		}
	}
  
	html += '</tr>';
	html += '</table>';
	
	$('cal_container').innerHTML = html;

	
	// Use for the next month and next year
	if(month == 11) 
	{ 
		next_month	= 1; 
		next_year = (year + 1); 
	} 
	else 
	{ 
		next_month = (month + 2); 
		next_year = year; 
	}
	
	// Use for the previous month and previous year
	if(month == 0) 
	{ 
		prev_month = 12; 
		prev_year = (year-1); 
	} 
	else 
	{ 
		prev_month = month; 
		prev_year = year; 
	}
	
	var next_month_string = cal_months_labels[next_month-1];
	var prev_month_string = cal_months_labels[prev_month-1];

	$('cal_previous_link').title = prev_month_string;
	$('cal_next_link').title = next_month_string;
	$('cal_month_select').value = (month+1);
	$('cal_year_display').innerHTML = year;
	
	//previous link (onclick)
	$('cal_previous_link').onclick = function() {
		cal_navigate(prev_month,prev_year,day_today);	
	}
	
	//next link (onclick)
	$('cal_next_link').onclick = function() {
		cal_navigate(next_month,next_year,day_today);	
	}
	
	getCalendar_events(year, month, day_today, day_clicked);
}



function getCalendar_events(year, month, day_today, day_clicked)
{
	for(var i = 1; i <= monthLength; i++)
	{
		$('schoolcalendar'+i).className = 'calendar-day';	
	}
	
	document.getElementById(day_clicked).className = 'calendar_current_day';
	
	$('cal_month_select').onchange = function () {
		cal_navigate(document.getElementById('cal_month_select').value,year,day_today);
	}
	
	// Use for the next month and next year
	if(month == 11) 
	{ 
		next_month	= 1; 
		next_year = (year + 1); 
	} 
	else 
	{ 
		next_month = (month + 2); 
		next_year = year; 
	}
	
	// Use for the previous month and previous year
	if(month == 0) 
	{ 
		prev_month = 12; 
		prev_year = (year-1); 
	} 
	else 
	{ 
		prev_month = month; 
		prev_year = year; 
	}
	
	
	//previous link (onclick)
	$('cal_previous_link').onclick = function() {
		cal_navigate(prev_month,prev_year,day_today);	
	}
	
	//next link (onclick)
	$('cal_next_link').onclick = function() {
		cal_navigate(next_month,next_year,day_today);	
	}
	
	var arrayUpdate = new Array();
	
	curr_pagelocation = location.href;
	if(curr_pagelocation.search("/gallery2/") != -1)
	{	var url = "../swu_lefmenu_calendar.php"; }
	else if(curr_pagelocation.search("/gallery2/") == -1)
	{	var url = "./swu_lefmenu_calendar.php"; }
	
	
	var calendar_pars = "year=" + year + "&month=" + (month+1) + "&day=" + day_today; 
	var myAjax = new Ajax.Request( url, { 
									method: 'get',
									parameters: calendar_pars,
									onComplete: function(transport) { 
										if (200 == transport.status)
										{
											var newData = transport.responseText;
											
											if(newData.indexOf('|' != -1))
											{
												arrayUpdate = newData.split('|');
												
												if(arrayUpdate[0] == 0)
												{
													document.getElementById('calendar_rowid').style.display = "none";
													$('calendar_events_container').innerHTML = '&nbsp;';
												}
												else
												{
													$('calendar_rowid').style.display = "";
													$('calendar_events_container').innerHTML = arrayUpdate[1];
												}
											}
										}
									}
								 	} );	
}
