/* イベントカレンダー起動用 JavaScript */

/* イベントカレンダー起動用 JavaScript */
YAHOO.namespace("example.calendar");
YAHOO.example.calendar.init = function() {
	// カレンダーをクリックしたら起動するプログラム
	function handleSelect(type, args, obj){
		// 引数 args[0] - 選択されたデータ内容
		var selected = args[0];
		var selDate = this.toDate(selected[0]);		// 日付取得
		event_table_out(selDate);					// テーブル出力処理
	}

	// カレンダーの初期設定 関数
	YAHOO.example.calendar.cal1 = new YAHOO.widget.CalendarGroup("cal1","cal1Container", {PAGES:3}); // 2列表示
	YAHOO.example.calendar.cal1.cfg.setProperty("MONTHS_SHORT", ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]); 
	YAHOO.example.calendar.cal1.cfg.setProperty("MONTHS_LONG", ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]); 
	YAHOO.example.calendar.cal1.cfg.setProperty("WEEKDAYS_1CHAR", ["日", "月", "火", "水", "木", "金", "土"]); 
	YAHOO.example.calendar.cal1.cfg.setProperty("WEEKDAYS_SHORT", ["日", "月", "火", "水", "木", "金", "土"]); 
	YAHOO.example.calendar.cal1.cfg.setProperty("WEEKDAYS_MEDIUM",["日", "月", "火", "水", "木", "金", "土"]); 
	YAHOO.example.calendar.cal1.cfg.setProperty("WEEKDAYS_LONG", ["日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"]); 
	YAHOO.example.calendar.cal1.cfg.setProperty("MY_YEAR_POSITION", 1);
	YAHOO.example.calendar.cal1.cfg.setProperty("MY_MONTH_POSITION", 2);
	YAHOO.example.calendar.cal1.cfg.setProperty("MDY_YEAR_POSITION", 1);
	YAHOO.example.calendar.cal1.cfg.setProperty("MDY_MONTH_POSITION", 2);
	YAHOO.example.calendar.cal1.cfg.setProperty("MDY_DAY_POSITION", 3);
	YAHOO.example.calendar.cal1.cfg.setProperty("MY_LABEL_YEAR_SUFFIX", "年 "); // 年表示の後処理
	YAHOO.example.calendar.cal1.cfg.setProperty("MY_LABEL_MONTH_POSITION", 2);  // 月を後（年の次）表示
	// イベント関数記述
	YAHOO.example.calendar.cal1.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.cal1, true);
	YAHOO.example.calendar.cal1.render();
	event_table_out();		// 初期表示
}

YAHOO.util.Event.onDOMReady(YAHOO.example.calendar.init);

// 曜日出力用配列
var yobi_str = new Array("日", "月", "火", "水", "木", "金", "土");

// 実際の行事出力処理
// 当日、月行事出力へ変更2009/10/30
function event_table_out(tar_date) {
	// 引数：tar_date - 描画対象の年月日 引数がない場合は、本日のデータ取得
	if (typeof tar_date == 'undefined' ) {	// 引数が未定義の場合
		tar_date = new Date();				// 本日設定
	}
	
	// サーバよりデータ取得
	try{
		var req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
		var req = new XMLHttpRequest();
	}
	theYear = tar_date.getFullYear();		// 年
	theMonth = tar_date.getMonth() + 1;		// 月
	theDate = tar_date.getDate();			// 日
	theYobi = tar_date.getDay();			// 曜日
	
	// JOLT 取得URLの作成 引数として取得する年月日を渡す。
	jolt_url = "/event_calendar.php?Year=" + theYear +
								 "&Month=" + theMonth +
								 "&Day=" + theDate;
	req.open("GET", jolt_url , false);	// JSONファイルの設定
	req.send(null);									// ダウンロード
	var json = req.responseText;					// 取得したテキスト
	//alert(json);									// 帰り値デバッグ
	all_eventdata = eval( "(" + json + ")" );		// JSON形式の展開
	
	//-------------本日の記事-------------------
	newsdata = all_eventdata[0];					// [0] 本日の記事
	// データ表示処理
	var arrLength = newsdata.length;			// 配列長取得
	if (arrLength > 0){
		// 記事あり
		tar_date_str = theYear + "年" + theMonth + "月" + theDate + "日 (" + yobi_str[theYobi] + ")";
		html =  '<table>';
		html += '  <caption>【 ' + tar_date_str + ' 検索結果 】</caption>';
		html += '  <tr>';
		html += '    <th width="70%">行 事 内 容</th>';
		html += '    <th>期　　間</th>';
		html += '  </tr>';
		// 一件毎の記事表示
		for(lp = 0; lp < arrLength ; lp ++){
			html += '  <tr>';
			// 指定数以下の存在するニュースを出力 記事ががあるかどうかでリンクの有無追加
			if (newsdata[lp].link == 1) {
				// リンクあり
				html += '    <td width="70%"><p class="kiji"><a href="' + newsdata[lp].title_url + '">' + newsdata[lp].title + '</a></p></td>';
			} else {
				// リンクなし
				html += '    <td width="70%"><p class="kiji">' + newsdata[lp].title + '</p></td>';
			}
			html += '    <td><p class="kiji">' + newsdata[lp].day_between + '</p></td>';
			html += '  </tr>';
		}
		html += '</table>';
	} else {
		// 記事なし
		osirase = theYear + "年" + theMonth + "月" + theDate + "日 (" + yobi_str[theYobi] + ") の行事はありません。";
		html = '<p class="kijinashi">' + osirase + '</p>';
	}
	
	html += "<br /> <br />"  // 記事区切りの段落
	
	//-------------今月の記事-------------------
	monthdata = all_eventdata[1];					// [1] 今月の記事
	// データ表示処理
	var arrLength = monthdata.length;			// 配列長取得
	if (arrLength > 0){
		// 記事あり
		tar_date_str = theYear + "年" + theMonth + "月";
		html +=  '<table>';
		html += '  <caption>【 ' + tar_date_str + ' の行事 】</caption>';
		html += '  <tr>';
		html += '    <th width="70%">行 事 内 容</th>';
		html += '    <th>期　　間</th>';
		html += '  </tr>';
		// 一件毎の記事表示
		for(lp = 0; lp < arrLength ; lp ++){
			html += '  <tr>';
			// 指定数以下の存在するニュースを出力 記事ががあるかどうかでリンクの有無追加
			if (monthdata[lp].link == 1) {
				// リンクあり
				html += '    <td width="70%"><p class="kiji"><a href="' + monthdata[lp].title_url + '">' + monthdata[lp].title + '</a></p></td>';
			} else {
				// リンクなし
				html += '    <td width="70%"><p class="kiji">' + monthdata[lp].title + '</p></td>';
			}
			html += '    <td><p class="kiji">' + monthdata[lp].day_between + '</p></td>';
			html += '  </tr>';
		}
		html += '</table>';
	} else {
		// 記事なし
		osirase = theYear + "年" + theMonth + "月" + " の行事はありません。";
		html += '<p class="kijinashi">' + osirase + '</p>';
	}
	
	// <div id="eventcalendartable"> の場所に出力
	document.getElementById("eventcalendartable").innerHTML = html;


}

// 左メニューアイコン操作
function menuMouseOver(obj){
  var tags=obj.getElementsByTagName('img');
  for(var i=0;i<tags.length;i++){
    tags[i].src="/images/event_calendar/aomaru.png";
  }
}
function menuMouseOff(obj){
  var tags=obj.getElementsByTagName('img');
  for(var i=0;i<tags.length;i++){
    tags[i].src="/images/event_calendar/haimaru.png";
  }
}


