//カレンダー

//


// グローバル変数
var aDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // 月毎の日数
var aWeek = new Array("日", "月", "火", "水", "木", "金", "土");  // 曜日表示文字列
var objParent;        // 親オブジェクト
var tCalendar;        // カレンダーオブジェクト（テーブル）
var nYear = 0;        // 現在表示中の年
var nMonth = 0;      // 現在表示中の月
var holidaythisMonth = new Array("日", "月", "火", "水", "木", "金", "土");  // 曜日表示文字列
var holidaynextMonth = new Array("日", "月", "火", "水", "木", "金", "土");  // 曜日表示文字列

// 初期化（最初に呼び出す）
function InitCalendar(parent_id) {
    if (document.getElementById) {
        objParent = document.getElementById(parent_id);
        SetThisMonth();
        //SetNextMonth();
    }
}

// カレンダーの表示
// 引数 : year（表示年）, month（表示月）
function SetCalendar(year, month) {
    // 変数
    var nDate = new Date();            // 現在のDateオブジェクト
    var nToday = nDate.getDate();   // 今日の日にち 
    var nDays;            // 表示月の日数
    var nFirstDay;       // 表示月初日の曜日インデックス
    var bToday;          // 表示月に今日が含まれるかどうか
    var i, j;

    nYear = year;
    nMonth = month;
    bToday = (nYear == nDate.getFullYear() && nMonth == nDate.getMonth() + 1)? true: false;

    // ひと月の日数
    nDays = aDays[nMonth-1];
    if (nMonth == 2) {    /* 閏月のチェック */
        if (nYear % 400 == 0) nDays =29;
        else if (nYear % 100 == 0) nDays = 28;
        else if (nYear % 4 == 0) nDays= 29;
    }
		
		// 表題の描画
		fy = new String(nYear);
		var gy = fy.slice(2);
		$('div#calendar').append('<p id="caltitle">'+gy+'年'+nMonth+'月診療カレンダー</p>');
		
    // 年月の描画
    tCalendar = document.createElement("table");
    
    if (nMonth == nDate.getMonth() + 1) {
    	tCalendar.className += 'thisMonth'
    } else if  (nMonth == nDate.getMonth() + 2){
    	tCalendar.className += 'nextMonth'
		}
    var tbCalendar = document.createElement("tbody");
    var tcCalendar = document.createElement("caption");
		tCalendar.appendChild(tcCalendar);
		var month = document.createTextNode(nMonth + "月");
		tcCalendar.appendChild(month);

    // 曜日行
    var trWeek = document.createElement("tr");
    for (i = 0; i < 7; i++) {
        var tdWeek = document.createElement("th");
        with (tdWeek) {
            appendChild(document.createTextNode(aWeek[i]));
        }
        trWeek.appendChild(tdWeek);
    }
    tbCalendar.appendChild(trWeek);  // 曜日行

    // 日にち行
    nDate.setFullYear(nYear);
    nDate.setMonth(nMonth-1);
    nDate.setDate(1);                // Dateオブジェクトに表示月の1日をセット
    nFirstDay = nDate.getDay();  // 月初の曜日インデックス
   
    // 月末まで表示
    var row = document.createElement("tr");
    var cell = new Array(nDays);
    var start_min = nYear + '-' + ('0' + nMonth).slice(-2) + '-01';
		// ↓翌年の1月を13月としても同じ内容が取得できる
		var start_max = nYear + '-' + ('0' + (nMonth + 1)).slice(-2) + '-01';
		var callback = '?';
		//var url = 'http://www.google.com/calendar/feeds/japanese__ja@holiday.calendar.google.com/public/full-noattendees?alt=json-in-script';
		//var url = 'http://www.google.com/calendar/feeds/vrt7clcck7df3ofdfnpbnv8cr4%40group.calendar.google.com/private-5f9a43f07e47acf48eeb52b94e10585c/full?alt=json';
		//var url = 'http://www.google.com/calendar/feeds/2rbl4dncq3qub4a53aeomfjiio%40group.calendar.google.com/private-2a78a174dd3a71eabd5abf0af95b6482/full?alt=json';
		var url = 'http://www.google.com/calendar/feeds/mhimtgc18a54ldduffcg56c97s%40group.calendar.google.com/private-fe4a1920a8c2049687594002e0c4e7ae/full?alt=json';
		url += '&callback=' + callback + '&start-min=' + start_min + '&start-max=' + start_max;
		$.getJSON(url,function(json){
			var c, e = json.feed.entry;
			var n = 0;
	    for (i = -nFirstDay, j = 0; i < nDays; i++) {
	        cell[i] = document.createElement("td");
	        with (cell[i]) {
	            if (i < 0) {    /* 一日まで空白で埋める */
	                appendChild(document.createTextNode(" "));
	            }else {
	                appendChild(document.createTextNode(i+1));    // 日付
	                // ルーティン休診
	                if (j == 0) {    //日曜日
	                	className +=' holiday';
	                }	else if (j == 6) { // 水曜日
	                	className +=' half';
	                }	else if (j == 3) { // 水曜日
	                	className +=' half';
	                }
									//n = i+1;
									if(json.feed.entry){
									  for (var k = 0, len = e.length; k < len; k++) {
									    var c = e[k].gd$when[0].startTime.slice(-2);
									    var t = e[k].title.$t;
											if(i+1 == c) {
												if(t == "休診") {
													className +=' holiday';
												} else if(t == "診療") {
													className +=' notHoliday';
												} else if(t == "午前診療") {
													className +=' half';
												}
											}
										}
									}
	            }
	            row.appendChild(cell[i]);
	        }
	        if (++j == 7 && i != nDays-1) {    /* 土曜でかつ月末以外の時 */
	            tbCalendar.appendChild(row);
	            row = document.createElement("tr");
	            j = 0;
	        }
	    }
    tbCalendar.appendChild(row);
		});

    //テーブルの表示
    tCalendar.appendChild(tbCalendar);
    objParent.appendChild(tCalendar);
} 

// 今月の表示
function SetThisMonth() {
    if (tCalendar) objParent.removeChild(tCalendar);
    var nDate = new Date();        // 現在の年、月の取得
    SetCalendar(nDate.getFullYear(), nDate.getMonth() + 1);
}
// 翌月の表示
/*function SetNextMonth() {
    var nDate = new Date();        // 現在の年、翌月の取得
    SetCalendar(nDate.getFullYear(), nDate.getMonth() + 2);
}*/


