var calendarDate = new Date();
var currentYear = calendarDate.getYear();
var currentMonth = calendarDate.getMonth() + 1;
var currentDate = calendarDate.getDate();
if(currentYear < 1900) {
  currentYear += 1900;
}

$(document).ready(function() {
  init();
  $("a").hover(
    function() {
      this.style.color = "#00BFFF";
    },
    function() {
      this.style.color = "#0000FF";
    }
  );
  
  loadScheduleText();
});

function loadScheduleText() {
  writeCalendar(currentYear, currentMonth);
}

function writeCalendar(year, month) {
  var calendar = "";
  var monthDate = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  var firstDay;
  var i, date = 1, count;
  var maxYear = 2020, tmpMonth;
  var todayclass;
  
  if((!(year % 4) && (year % 100)) || !(year % 400)) {
    monthDate[1] = 29;
  }
  calendarDate.setYear(year);
  calendarDate.setMonth(month-1);
  calendarDate.setDate(1);
  firstDay = calendarDate.getDay();  //月初日の曜日

  calendar += "<center><table><tr><td style=\"text-align:center;\"><a href=\"JavaScript:writePrevMonth(" + year + ", " + (month-1) + ")\"><<</a>";
  calendar += "&nbsp;&nbsp;<span class=\"titleyear\">" + year + "</span>&nbsp;年&nbsp;&nbsp;<span class=\"titlemonth\">" + month + "</span>&nbsp;月";
  calendar += "&nbsp;&nbsp;<a href=\"JavaScript:writeNextMonth("+ year + ", " + (month+1) + ")\">>></a></td></tr>";
  calendar += "<tr><td style=\"text-align:right;\"><form id=\"selectCalendarDate\"><select id=\"selectCalendarYear\">";
  for(i = 2000; i <= maxYear; i++) {
    if(i == year) {
      calendar += "<option value=\"" + i + "\" selected>" + i + "</option>";
    } else {
      calendar += "<option value=\"" + i + "\">" + i + "</option>";
    }
  }
  calendar += "</select><select id=\"selectCalendarMonth\">";
  for(i = 1; i <= 12; i++) {
    if(i == month) {
      calendar += "<option value=\"" + i + "\" selected>" + i + "</option>";
    } else {
      calendar += "<option value=\"" + i + "\">" + i + "</option>";
    }
  }
  calendar += "</select><input type=\"button\" value=\"GO!\" onClick=\"writeSelectMonth()\"></form></td></tr>";
   
  calendar += "<tr><td><table class=\"calendar\"><tr><td width=\"75\" class=\"sunday\">日</td><td width=\"75\" class=\"weekday\">月</td><td width=\"75\" class=\"weekday\">火</td><td width=\"75\" class=\"weekday\">水</td><td width=\"75\" class=\"weekday\">木</td><td width=\"75\" class=\"weekday\">金</td><td width=\"75\" class=\"saturday\">土</td></tr><tr>";

  count = 0;
  while(count < firstDay) {
    calendar += "<td>&nbsp;</td>";
    count++;
  }
  if(month < 10) {
    tmpMonth = "0";
  } else {
    tmpMonth = "";
  }
  while(date < 10) {
    if(!(count % 7) && count) {
      calendar += "</tr><tr>";
    }
    if((month == currentMonth) && (date == currentDate)) {
      todayclass = "class=\"today\"";
    } else {
      todayclass = "";
    }
    calendar += "<td><div " + todayclass + ">"+ date + "</div><div id=\"" + year + tmpMonth + month + "0" + date + "\" class=\"eventtext\"></div></td>";
    count++;
    date++;
  }
  while(date <= monthDate[month-1]) {
    if(!(count % 7)) {
      calendar += "</tr><tr>";
    }
    if((month == currentMonth) && (date == currentDate)) {
      todayclass = "class=\"today\"";
    } else {
      todayclass = "";
    }
    calendar += "<td><div "+ todayclass + ">" + date + "</div><div id=\"" + year + tmpMonth + month + date + "\" class=\"eventtext\"></div></td>";
    count++;
    date++;
  }
  while(count % 7) {
    calendar += "<td>&nbsp;</td>";
    count++;
  }
  calendar += "</tr></table></td></tr></table></center>";
  
  $("#calendarArea").html(calendar);
  
  addCircleEvent(year, tmpMonth, month, monthDate[month-1]);
  addEvent(year, tmpMonth, month, monthDate[month-1]);
}

function addCircleEvent(year, tmpMonth, month, monthDate) {
  	$.ajax({
		url : "info/nocircle.txt",
		type : "get",
		timeout : 10000,
		success : function(data) {
			data = data.replace("\r\n", "");
			data = data.replace("\r", "");
			data = data.replace("\n", "");
			var array = data.split("'splitspace'");
			var array = array[0].split(" ");
			var noCircle = {};
			var len = array.length;
			var tmpid = "";
			var id, i;
			var date = new Date();
			var day;
			  
			date.setYear(year);
			date.setMonth(month-1);
			tmpid += year + tmpMonth + month;			
			for(i = 0; i < len; i++) {
				array[i] = array[i].substr(0, 8);
				noCircle[array[i]] = true;
			}
			
			for(i = 1; i < 10; i++) {
				date.setDate(i);
				day = date.getDay();
				if(day == 0 || day == 6) {
					id = tmpid;
					id += "0" + i;
					if(!(id in noCircle)) {
						$("#" + id).html("<img src=\"images/circle.gif\" alt=\"サークル\" />");
					}
				}
			  }
			for(; i <= monthDate; i++) {
				date.setDate(i);
				day = date.getDay();
				if(day == 0 || day == 6) {
					id = tmpid;
					id += i;
					if(!(id in noCircle)) {
						$("#" + id).html("<img src=\"images/circle.gif\" alt=\"サークル\" />");
					}
				}
			}
		},
		error : function() {
		}
	});
}

function addEvent(year, tmpMonth, month, monthDate) {
  $.ajax({
    url : "info/eventcalendar.txt",
    type : "get",
    timeout : 10000,
    success : function(data) {
      data = data.replace("\r\n", "");
      data = data.replace("\n", "");
      data = data.replace("\r", "");
      var array = data.split("'splitspace'");
      var array = array[0].split(" ");
      var event = {};
      var len = array.length;
      var tmpid = "";
      var id, i, j;

      for(i = 0; i < len; i+=2) {
        array[i] = array[i].substr(0, 8);
        event[array[i]] = array[i+1];
      }
      tmpid += year + tmpMonth + month;
      for(i = 1; i < 10; i++) {
        id = tmpid;
        id += "0" + i;
        if(id in event) {
          $("#" + id).html($("#" + id).html() + event[id]);
        }
      }
      for(; i <= monthDate; i++) {
        id = tmpid;
        id += i;
        if(id in event) {
          $("#" + id).html($("#" + id).html() + event[id]);
        }
      }
    },
    error : function() {
    }
  });
}

function writePrevMonth(year, month) {
  if(month == 0) {
    year--;
    month = 12;
  }
  writeCalendar(year, month);
}

function writeNextMonth(year, month) {
  if(month == 13) {
    year++;
    month = 1;
  }
  writeCalendar(year, month);
}

function writeSelectMonth() {
  writeCalendar(parseInt($("#selectCalendarYear").val()), parseInt($("#selectCalendarMonth").val()));
}