$(function(){

	$.get('base.html', function(html){

		// 凡例を非表示にする。
		$('#event_legend').hide();

		// ボタン等を表示する。
		$('h2.chapter').before(html);

		// ツールチップを追加する。
		var tip = $('<img src="img/tip.png" class="png" width="155" height="40" alt="" />').css({
			'position':'absolute','display':'none'
		});
		$('h2.chapter').before(tip);

		// 月。
		var month = (new Date()).getMonth() + 1;

		// 列の数。
		var thlen = $('#tr_date th').size();

		// セルの列番号を返す。
		var getcolidx = function(el){
			var tdidx = $('table#event_table_summer td').index(el);
			var colidx = tdidx % (month == 9 ? thlen - 1 : thlen) + 1;
			return colidx;
		};

		// 全てのイベント表示ボタン。
		$('#btn_show_all').click(function(){
			$('div.list').show();
			$('#event_title').text('全てのイベント一覧');
		});

		// 7月、8月、9月ボタン。
		$('#btn_show_7,#btn_show_8,#btn_show_9').click(function(){
			$(this).attr('id').match(/(\d)$/);
			month = RegExp.$1;

			// カレンダー月の表示/非表示を設定する。
			$('#calendar_7,#calendar_8,#calendar_9').hide();
			$('#calendar_' + month).show();

			// 曜日行の表示/非表示を設定する。
			$('#tr_month_7,#tr_month_8,#tr_month_9').hide();
			$('#tr_month_' + month).show();

			// 該当月のカレンダーhtmlを読み込む。
			$('table#event_table_summer tbody').load(month + '.html', function(){
				
				// 偶数行にclassを設定して色を付ける。
				$('table#event_table_summer tr:nth-child(even)').addClass('even');

				/*
				// マウス上の行の背景を設定する。
				$('table#event_table_summer tr:not(:first-child)').hover(function(){
					$(this).addClass('hover');
				},function(){
					$(this).removeClass('hover');
				});
				*/

				// カレンダーに入るとツールチップを表示する。
				$('table#event_table_summer tbody').hover(function(e){
					tip.show();
				},function(){
					tip.hide();
				});

				// ツールチップをマウスに追従させる。
				$('table#event_table_summer tbody').mousemove(function(e){
					tip.css({'top':(e.pageY+30)+'px','left':(e.pageX-70)+'px'});
				});

				// マウス上の列の背景を設定する。
				$('table#event_table_summer td').hover(function(){
					if ($(this).attr('class').match(/(td\d+)/)) {
						$('td.'+RegExp.$1).addClass('hover');
					} else {
						$(this).parent().addClass('hover');
					}
				},function(){
					if ($(this).attr('class').match(/(td\d+)/)) {
						$('td.'+RegExp.$1).removeClass('hover');
					} else {
						$(this).parent().removeClass('hover');
					}
				});

				// 日付けの列をクリックした場合は、その日のイベントを表示する。イベント名をクリックすると、そのイベントのみ表示する。
				var scrollPos = $('#event_title').position().top - 12;
				var scrollPlus = 32;
				var scrollInterval = 15;
				
				$('table#event_table_summer td').click(function(){
					
					// イベントを全て非表示にする。
					$('div.list').hide();
					
					// 日付の列がクリックされた場合。
					if ($(this).attr('class').match(/td(\d+)/)) {
						var day = RegExp.$1;
						var colidx = getcolidx(this);
						var cols = $('.td' + (colidx-1) + '.e');
						cols.each(function(){
							$(this).parent().attr('id').match(/tr(\d+)/);
							$('div#list'+RegExp.$1).show();
						});
						$('#event_title').text(month + '月' + day + '日のイベント');
						var nextPos = $(window).scrollTop();
						var timer = setInterval(function(){
							nextPos += scrollPlus;
							if (nextPos >= scrollPos) {
								clearInterval(timer);
								$(window).scrollTop(scrollPos);
								return;
							}
							$(window).scrollTop(nextPos);
						},scrollInterval);
					
					// イベント名の列がクリックされた場合。
					} else {
						$(this).parent().attr('id').match(/tr(\d+)/);
						$('div#list'+RegExp.$1).show();
						$('#event_title').text($(this).find('img').attr('alt'));
						var nextPos = $(window).scrollTop();
						var timer = setInterval(function(){
							nextPos += scrollPlus;
							if (nextPos >= scrollPos) {
								clearInterval(timer);
								$(window).scrollTop(scrollPos);
								return;
							}
							$(window).scrollTop(nextPos);
						},scrollInterval);
					}
				});

				// 9月の場合は31日を非表示にする。
				$('.th31,.td31').toggle(month != '9');
			});

		});

		// 7月、8月、9月なら該当月のカレンダーを表示する。それ以外は7月。
		if (month < 7 || month > 9) month = 7;
		$('#btn_show_' + month).click();

		// IE6のpngイメージ対応。
		if ($.browser.msie && (parseFloat($.browser.version) <= 6.0)) {
			DD_belatedPNG.fix('.png');
		}
	});
});

