//Calendar script 
// 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];

// these are human-readable month name labels, in order
abr_months_labels = ['Jan', 'Feb', 'Mar', 'Apr',
                     'May', 'Jun', 'Jul', 'Aug', 'Sep',
                     'Oct', 'Nov', 'Dec'];

function popupdate(day){
	var abr = "";
	if(day==1 || day==21 || day==31) abr="st";
	else if(day==2 || day==22) abr="nd";
	else if(day==3 || day==23) abr="rd";
	else abr="th";
	var popupdate = day+abr+" "+cal_months_labels[this.month]+" "+this.year
	return popupdate;
}     

// this is the current date
cal_current_date = new Date(); 
current_day = cal_current_date.getDate();
current_month = cal_current_date.getMonth();
current_year = cal_current_date.getFullYear();

// Special days
var special_month=new Array(); 
var special_day=new Array(); 
var special_note=new Array(); 

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;
  html = '';

	special_month[0] = 1;
	special_day[0] = 18;
	special_note[0] = "The Fish Tanks Forums Birthday.";
	
	special_month[1] = "";
	special_day[1] = 1;
	special_note[1] = "The Picture of the Month competition starts today.";
	if(this.month==1 || this.month==4 || this.month==7 || this.month==10) special_note[1] = "The Picture of the Month and Tank of the Quarter competition starts today.";
	if(this.month==0) special_note[1] += "<br /><br />New Years Day.";
	
	special_month[2] = "";
	special_day[2] = 14;
	special_note[2] = "Entries for the Picture of the Month competition ends today.";
	if(this.month==1 || this.month==4 || this.month==7 || this.month==10) special_note[2] = "Entries for the The Picture of the Month and Tank of the Quarter competition ends today.";
	if(this.month==1) special_note[2] += "<br /><br />Valentines Day";
	
	special_month[3] = "";
	special_day[3] = 15;
	special_note[3] = "Voting for the Picture of the Month competition starts today.";
	if(this.month==1 || this.month==4 || this.month==7 || this.month==10) special_note[3] = "Voting for the Picture of the Month and Tank of the Quarter competition starts today.";
	
	special_month[4] = "";
	special_day[4] = 28;
	special_note[4] = "Voting ends and the Picture of the Month competition winner is announced.";
	if(this.month==1 || this.month==4 || this.month==7 || this.month==10) special_note[4] = "Voting ends and the Picture of the Month and Tank of the Quarter competition winner is announced.";
	
	special_month[5] = 9;
	special_day[5] = 31;
	special_note[5] = "Halloween.";

	special_month[6] = 11;
	special_day[6] = 25;
	special_note[6] = "Christmas Day.";
	
	special_month[7] = 11;
	special_day[7] = 26;
	special_note[7] = "Boxing Day.";
	
	special = special_day.length;

  // 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 = '<span style="font-size: 85%; line-height: normal">';
  html += '<table class="calendar-table">';
  html += '<tr><th colspan="7">';
  html +=  monthName + "&nbsp;" + this.year + "<hr color='#FF7553'>";

  // Start Previous & Next Month
  var pm=this.month-1;
  var py=this.year;
  if(pm==-1) {pm=11; py=this.year-1;}
  var nm=this.month+1;
  var ny=this.year;
  if(nm==12) {nm=0; ny=this.year+1;}
  html += '<table border="0" width="100%" cellspacing="1"><tr>';
  html += "<td align='left' width='33%'><span style='text-decoration: underline'><a href='#' onclick='Calendar("+pm+","+py+");return false;'>"+abr_months_labels[pm]+"</a></span></td>";
  if(this.month!=current_month || this.year!=current_year) html += "<td align='center' width='34%'><span style='text-decoration: underline'><a href='#' onclick='Calendar("+current_month+","+current_year+");return false;'>Today</a></span></td>";
  html += "<td align='right' width='33%'><span style='text-decoration: underline'><a href='#' onclick='Calendar("+nm+","+ny+");return false;'>"+abr_months_labels[nm]+"</a></span></td>";
  html +='</tr></table></span>';
  // End
  
  html += '</th></tr>';
  html += '<tr class="calendar-header">';
  for(var i = 0; i <= 6; i++ ){
    html += '<td width="25" bgcolor="#FF7553" class="calendar-header-day">';
    html += cal_days_labels[i];
    html += '</td>';
  }
  html += '</tr><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 width="20" height="10" class="calendar-day">';
      if (day <= monthLength && (i > 0 || j >= startingDay)) {
      
      	if((day==current_day)&&(this.month==current_month)&&(this.year==current_year)) 
      	{
      		for (var k = 0; k < special; k++) 
      		{
      			var txt = special_note[k];
      			var date= popupdate(day);
      			if((this.month==special_month[k] || special_month[k]=="") && (day==special_day[k])) { html +="<span title='header=["+date+"] body=["+txt+"]' style='color: #FF7553; text-decoration: underline'><a href='#' onclick='return false;'><b>"+day+"</b></a></span>"; k=special }
	      		if(k==special-1) html +="<span style='color: #FF7553'><b>"+day+"</b></span>";
      		}
      	}
      	else 
      	{
      		for (var l = 0; l < special; l++) 
      		{
      			var txt = special_note[l];
      			var date= popupdate(day);
      		    if((this.month==special_month[l] || special_month[l]=="") && (day==special_day[l])) { html +="<span title='header=["+date+"] body=["+txt+"]' style='text-decoration: underline'><a href='#' onclick='return false;'>"+day+"</a></span>"; l=special }
	      		if(l==special-1) 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><tr><td colspan="7"><hr color="#FF7553"></td></tr></table>';

  document.getElementById('cal').innerHTML = html;
}


