/* 
 * 
 * Shukutoku Calendar Script
 * 
 */

var faculty;
var site_url;
var year;
var month;
var nowDate;
var xml;
var holiday = '/calendar/holiday.xml';	//祝日用XMLファイル


//カレンダー初期化（デフォルトの学部の今月のカレンダーを表示）
function initCalendar(faculty, dir){
	nowDate = new Date();
	site_url = dir;
	year = nowDate.getYear();
	month = nowDate.getMonth();
	if(year < 1900) year += 1900;
	changeFaculty(faculty);
}

//隣の月へ移動
function moveMonth(witch){
	if(witch == 'next'){
		month++;
		if(month > 11){
			year++;
			month = 0;
		}
	}else{
		month--;
		if(month < 0){
			year--;
			month = 11;
		}
	}
	drawCalendar();
}

//指定した学部のカレンダーを表示
function changeFaculty(selection){
	faculty = selection;
	var tab = document.getElementById("calendar_tab");
	tab.className = faculty;
	drawCalendar();
}

//指定年月のカレンダーを表示
function drawCalendar(){
	var this_month = document.getElementById("this_month");
	this_month.innerHTML = year + '年' + (month+1) + '月';
	
	var cal_table = document.createElement("table");
	cal_table.cellPadding = '0';
	cal_table.cellSpacing = '0';
	cal_table.border = '0';
	var cal_tbody = document.createElement("tbody");
	var th;
	var td;
	var tr;
	var nowMonth = new Date(year, month, 1);
	var lastDay;
	var cntWeek
	//曜日表示
	var heads = new Array('日', '月', '火', '水', '木', '金', '土');
	tr = document.createElement("tr");
	for(var i = 0; i < heads.length; i++){
		th = document.createElement("th");
		th.innerHTML = heads[i];
		if(i == 0){
			th.className = 'sun';
		}
		if(i == 6){
			th.className = 'sat';
		}
		
		tr.appendChild(th);
	}
	cal_tbody.appendChild(tr);

	startWeek = nowMonth.getDay();
	nowMonth.setMonth(month + 1);
	nowMonth.setDate(0);
	lastDay = nowMonth.getDate();
	var day = 1;
	var weekEnd;
	for(var i = 0; i < 7; i++){
		tr = document.createElement("tr");
		if(i == 0){
			for(var j = 0; j < startWeek; j++){
				tr.appendChild(document.createElement("td"));
			}
		}
		weekEnd = day + 6 - startWeek;
		for(var currentWeek = 0; currentWeek < 7; currentWeek++){
			if(day > lastDay){
				break;
			}
			if(currentWeek < startWeek){
				continue;
			}
			td = document.createElement("td");
			td.innerHTML = day;
			if(currentWeek == 0){
				td.className = 'sun';
			}
			if(currentWeek == 6){
				td.className = 'sat';
			}
			
			td.id = 'cal_' + day;
			
			tr.appendChild(td);
			day++;
		}
		startWeek = 0;
		cal_tbody.appendChild(tr);
		if(day > lastDay){
			break;
		}
	}
	var view = document.getElementById("calendar_view");
	var list = document.getElementById("calendar_list");
	list.innerHTML = '';
	view.innerHTML = '';
	cal_table.appendChild(cal_tbody);
	view.appendChild(cal_table);
	
	//イベントの取得
	var url_month;
	if(month < 9){
		url_month = '0' + (month + 1);
	}else{
		url_month = month + 1;
	}
	
	var dt = new Date(); 
	//var url = '/calendar/index.php?f='+faculty+'&s='+site_url+'&y='+year+'&m='+month+'&time=' + dt.getTime();
	var url = '/calendar/' + faculty + '/' + site_url + '/' + year + '/' + url_month + '/index.xml?time=' + dt.getTime();
	
	var item_title;
	var item_url;
	var item_oc_type;
	var item_date;
	var target_cel;
	var li;
	j$.ajax({
		url: url,
		type: 'GET',
		dataType: 'xml',
		timeout: 1000,
		error: function(){
		},
		success: function(xml){
			j$(xml).find('item').each(function(){
				var item = j$(this);
				item_title = item.find('title').text();
				item_url = item.find('url').text();
				if(item.find('oc').text() == 'オープンキャンパス'){
					item_oc = true;
				}else{
					item_oc = false;
				}
				
				item_date = item.find('date').text().match(/[0-9]{4}年( )*([0-9]{1,2})月( )*([0-9]{1,2})日/);
				if(item_date){
					item_month = item_date[2];
					item_day = item_date[4];
					target_cel = document.getElementById('cal_' + parseInt(item_day));
					if(item_oc){
						if(!target_cel.className){
							target_cel.className = 'oc';
						}else{
							target_cel.className = target_cel.className + ' oc';
						}
					}else{
						if(!target_cel.className){
							target_cel.className = 'other';
						}else{
							target_cel.className = target_cel.className + ' other';
						}
					}

					li = document.createElement("li");
					if(item_url){
						li_child = document.createElement("a");
						li_child.href = item_url;
						li_child.target = '_blank';
						li_child.innerHTML = item_month + '/' + item_day + '&nbsp;' + item_title;
						li.appendChild(li_child);
					}else{
						li.innerHTML = item_month + '/' + item_day + '&nbsp;' + item_title;
					}
					list.appendChild(li);
				}
			});
		}
	});
	//休日を取得
	j$.ajax({
		url: '/calendar/holiday.xml',
		type: 'GET',
		dataType: 'xml',
		timeout: 1000,
		error: function(){
		},
		success: function(xml){
			j$(xml).find('item').each(function(){
				var item = j$(this);
				item_title = item.find('title').text();
				item_date = item.find('date').text().match(/([0-9]{4})年( )*([0-9]{1,2})月( )*([0-9]{1,2})日/);
				if(item_date && parseInt(item_date[1]) == year && parseInt(item_date[3]) == month + 1){
					item_month = item_date[3];
					item_day = item_date[5];
					target_cel = document.getElementById('cal_' + parseInt(item_day));
					target_cel.className = target_cel.className + ' holiday';
				}
			});
		}
	});

}