document.write("<div class='contentsWrapper' id='calendar'></div>");

mm = new RegExp("time=(.*)");
dateobj = new Date();
if (ret = document.URL.match(/time=([0-9]+)$/)) {
	if (ret[1]) dateobj = new Date(ret[1] * 1000);
}
drawCalendar(dateobj);

function drawCalendar(dateobj) {
	today = new Date();
	work  = new Date();

	if (dateobj.getMonth() > 4)
		prevtext = '<a href="#" onClick="prevMonth(' + dateobj.getTime() + ');">&lt;前月</a>';
	else
		prevtext = '';

	if (dateobj.getMonth() < 5)
		nexttext = '<a href="#" onClick="nextMonth(' + dateobj.getTime() + ');">翌月&gt;</a>';
	else
		nexttext = '';

	text = '<div class="contentsBox"><table summary="カレンダー"><caption>'
	     + prevtext
	     + '<span id="calendarTitle">' + (dateobj.getMonth() + 1) + '月の番組表</span>'
	     + nexttext + '</caption>';
	text += '<tr><th><span><img src="/image/calendar/sun.gif" alt="SUN" /></span></th><th><span><img src="/image/calendar/mon.gif" alt="MON" /></span></th><th><span><img src="/image/calendar/tue.gif" alt="TUE" /></span></th><th><span><img src="/image/calendar/wed.gif" alt="WED" /></span></th><th><span><img src="/image/calendar/thu.gif" alt="thu" /></span></th><th><span><img src="/image/calendar/fri.gif" alt="FRI" /></span></th><th><span><img src="/image/calendar/sat.gif" alt="SAT" /></span></th></tr>';

	work.setFullYear(dateobj.getFullYear());
	work.setDate(1);
	work.setMonth(dateobj.getMonth());
	nowMonth = dateobj.getMonth();
//	work.setHours(4);
//	work.setMinutes(0);
//	work.setSeconds(0);
//	work.setMilliseconds(0);
	text += '<tr>';
	for (i=0; i < work.getDay(); i++)
		text += '<td></td>';
	for (i=1; i<=32; i++) {
		work.setDate(i);
		if (work.getMonth() != nowMonth){
			if(work.getDay() % 7 == 0){
				break;
			}else{
				for (j=1; j<=7-work.getDay(); j++) {
					text += '<td></td>';
				}
				break;
			}
		}
		var d = work.getDay() % 7
		if (d == 0) {
			text += '<tr>';
		}
		if (work.getDate()==today.getDate() && work.getMonth()==today.getMonth()) {
			text += '<td class="today"><a href="/timet/?time='+ Math.floor(work.getTime() / 1000) + '">'+i+'</a></td>';
		} else {
			if (d == 0 || d ==6 ) {
				text += '<td class="holiday">'
			}else {
				text += '<td>'
			}
			text += '<a href="/timet/?time='+ Math.floor(work.getTime() / 1000) + '">'+i+'</a></td>';
		}
		if (d == 6) {
			text += '</tr>';
		}
	}
	text += '</tr></table></div>';
	calbase = document.getElementById('calendar');
	calbase.innerHTML=text;
}

function prevMonth(dateval) {
	dateobj = new Date(dateval);
	dateobj.setDate(1);
	dateobj.setMonth(dateobj.getMonth()-1);
	drawCalendar(dateobj);
}

function nextMonth(dateval) {
	dateobj = new Date(dateval);
	dateobj.setDate(1);
	dateobj.setMonth(dateobj.getMonth()+1);
	drawCalendar(dateobj);
}

