
$(document).ready(function(){

//// ローカル時間
//	this_date = new Date();
//	next_date = new Date(this_date.getFullYear(), this_date.getMonth() + 1, 1);
//	initCalendar(this_date, next_date);

// サーバー時間
$.ajax({
		url:'./php/servertime.php'
		,dataType:'jsonp'
		,jsonp:'callback'
		,success:function(json){
		var jst = json['time'] * 1000;
		// 日付取得（今月）
		var now = new Date();
		// 日付取得（来月）
		now.setTime(jst);
		var next = new Date(now.getFullYear(), now.getMonth() + 1, 1);
		initCalendar(now, next);
	},error:function(){
//		alert('error');
	}
});

function initCalendar(this_date, next_date){



	// 曜日定義
	week_array = new Array("日","月","火","水","木","金","土");
	// 月定義
	month_array = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	// 日付取得
//	this_date = new Date();

	// うるう年計算
	this_year = this_date.getYear();
	this_year = (this_year<2000) ? (1900+this_year) : (this_year);
	if (((this_year % 4) == 0 && (this_year % 100) != 0) || (this_year % 400) == 0){
		month_array[1] = 29;
	}
	// 月を取得(0月～11月)
	this_month = this_date.getMonth();
	// 今日を取得
	this_today = this_date.getDate();
	// 1日の曜日を取得
	this_date.setDate(1);
	this_week = this_date.getDay();

//	next_date = new Date(this_date.getFullYear(), this_date.getMonth() + 1, 1);
	// うるう年計算
	next_year = next_date.getYear();
	next_year = (next_year<2000) ? (1900+next_year) : (next_year);
	if (((next_year % 4) == 0 && (next_year % 100) != 0) || (next_year % 400) == 0){
		month_array[1] = 29;
	}
	// 月を取得(0月～11月)
	next_month = next_date.getMonth();
	// 今日を取得
	next_today = next_date.getDate();
	// 1日の曜日を取得
	next_date.setDate(1);
	next_week = next_date.getDay();

	// ファイル用
	file_this_year = this_year;
	buff_this_month = this_month + 1;
	if(String(buff_this_month).length == 1){
		file_this_month = '0' + buff_this_month;
	}else{
		file_this_month = buff_this_month;
  }
	file_next_year = next_year;

	buff_next_month = next_month + 1;
	if(String(buff_next_month).length == 1){
		file_next_month = '0' + buff_next_month;
	}else{
		file_next_month = buff_next_month;
	}

	$.ajax({
		url:"./tsv/" + file_this_year + file_this_month + ".tsv",
		cache: false,
		success: function(csv) {
			data_ary = new Array();
			data_ary = csv.split(/\r\n|[\r\n]/)
			
			$.ajax({
				url:"./tsv/" + file_next_year + file_next_month + ".tsv",
				cache: false,
				success: function(csv) {
					data_ary2 = new Array();
					data_ary2 = csv.split(/\r\n|[\r\n]/)
					
					$.ajax({
						url:"./tsv/category.tsv",
						cache: false,
						success: function(csv) {
							id_array = new Array();
							color_array = new Array();
							link_array = new Array();
							note_array = new Array();
							$.each(csv.split(/\r\n|[\r\n]/), function(i) {
								if(this != ''){
									_array = this.split(/\t/);
									id_array[i] = _array[0];
									color_array[i] = _array[1];
									link_array[i] = _array[2];
									note_array[i] = _array[3];
								}
							});
							setCalendar(data_ary, data_ary2, id_array, color_array, link_array, note_array);
						},
						error : function(){
//							alert("※データがありません。\nお使いのパソコンの日付をお確かめ下さい。");
						}
					});
					
				},
				error : function(){
//					alert("※データがありません。\nお使いのパソコンの日付をお確かめ下さい。");
				}
			});
			
		},
		error: function(){
//			alert("※データがありません。\nお使いのパソコンの日付をお確かめ下さい。");
		}
	});
}

function setCalendar(data_ary, data_ary2, id_array, color_array, link_array, note_array){

	html_string = '';

	// 今月 -----------------------------------
	html_string += "<div class='month'>";

	// ↓月出力↓
	html_string += "<img src='./img/fair_calendar/month_" + (this_month + 1) + ".gif' />";
	// ↑月出力↑

	// ↓曜日出力↓
	html_string += "<div class='week'>";
	for(i = 0; i < month_array[this_month]; i++){
		html_string += "<img src='./img/fair_calendar/week_" + (((this_week + i) % 7) + 1) + ".gif' />";
	}
	html_string += "</div>";
	// ↑曜日出力↑

	// ↓日出力↓
	html_string += "<div class='date'>";
	for(i = 0; i < month_array[this_month]; i++){
		key_int = data_ary[i] - 1;
		html_string += "<div style='background-color:" + color_array[key_int] + ";'>";
		if(link_array[key_int] == 'none'){
			html_string += "<img src='./img/fair_calendar/date_" + (i + 1) + ".gif' />";
		}else{
			html_string += "<a href='" + link_array[key_int] + "'><img src='./img/fair_calendar/date_" + (i + 1) + ".gif' /></a>";
		}
		html_string += "</div>";
	}
	html_string += "</div>";
	// ↑日出力↑

	html_string += "</div>";

	// 来月 -----------------------------------

	html_string += "<div class='month'>";

	// ↓月出力↓
	html_string += "<img src='./img/fair_calendar/month_" + (next_month + 1) + ".gif' />";
	// ↑月出力↑

	// ↓曜日出力↓
	html_string += "<div class='week'>";
	for(i = 0; i < month_array[next_month]; i++){
		html_string += "<img src='./img/fair_calendar/week_" + (((next_week + i) % 7) + 1) + ".gif' />";
	}
	html_string += "</div>";
	// ↑曜日出力↑

	// ↓日出力↓
	html_string += "<div class='date'>";
	for(i = 0; i < month_array[next_month]; i++){
		key_int = data_ary2[i] - 1;
		html_string += "<div style='background-color:" + color_array[key_int] + ";'>";
		if(link_array[key_int] == 'none'){
			html_string += "<img src='./img/fair_calendar/date_" + (i + 1) + ".gif' />";
		}else{
			html_string += "<a href='" + link_array[key_int] + "'><img src='./img/fair_calendar/date_" + (i + 1) + ".gif' /></a>";
		}
		html_string += "</div>";
	}
	html_string += "</div>";
	// ↑日出力↑

	html_string += "</div>";

	// ノート -----------------------------------

	html_string += "<p>本番さながらの演出とウェディング体験で、心に残る1日に。<br />オリエンタルホテル広島では、本番のウェディングを体感していただけるさまざまなフェアを開催しております。<br />平日には専門プランナーによる相談会＆会場見学を毎日開催。おふたりとの出会いを心よりお待ちいたしております。</p>";

	// カテゴリ -----------------------------------
	html_string += "<div class='cate'>";
	
	$.each(id_array, function(i) {
		if(i % 2 == 0){
			// 左側
			if(link_array[i] == 'none'){
				html_string += "<div class='bridalfair mr' style='cursor:default;'><div style='background-color:" + color_array[i] + ";'></div><img src='./img/fair_calendar/separate.gif' /><p>" + note_array[i] + "</p></div>";
			}else{
				html_string += "<div class='bridalfair mr' style='cursor:pointer;' onclick='location.hash=\"" + link_array[i] + "\"; return false;'><div style='background-color:" + color_array[i] + ";'></div><img src='./img/fair_calendar/separate.gif' /><p>" + note_array[i] + "</p></div>";
			}
		}else{
			// 右側
			if(link_array[i] == 'none'){
				html_string += "<div class='bridalfair' style='cursor:default;'><div style='background-color:" + color_array[i] + ";'></div><img src='./img/fair_calendar/separate.gif' /><p>" + note_array[i] + "</p></div>";
			}else{
				html_string += "<div class='bridalfair' style='cursor:pointer;' onclick='location.hash=\"" + link_array[i] + "\"; return false;'><div style='background-color:" + color_array[i] + ";'></div><img src='./img/fair_calendar/separate.gif' /><p>" + note_array[i] + "</p></div>";
			}
		}
	});
	html_string += "</div>";

	// HTML 出力
	$("#calendarArea").hide();
	$("#calendarArea").empty();
	$('#calendarArea').append(html_string);
	$.timer(1500, function (timer) {
		$("#calendarWrap").removeClass("calendarWrapH");
		$("#calendarArea").fadeIn('slow');
		timer.stop();
	});
}

});

