var now = new Date();
var thisMonday = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var scheduleFileName = "/user_file/poppop-sh/schedule/" + String(thisMonday.getFullYear()) + String(thisMonday.getMonth() + 1) + ".txt";
var nextMonth = new Date(now.getFullYear(), now.getMonth()+1, 1);
var scheduleFileName2 = "/user_file/poppop-sh/schedule/" + String(nextMonth.getFullYear()) + String(nextMonth.getMonth() + 1) + ".txt";
//alert(scheduleFileName);

var targetFiles = new Array(scheduleFileName, scheduleFileName2);

jQuery.noConflict();

jQuery(document).ready(function(){
	for(var i=0; i<targetFiles.length; i++){
		jQuery.ajax({
			url : targetFiles[i],
			dataType : "text",
			success : function(data , status){
				var arr1 = data.split("\n");
				//alert(data);
				//今週の月曜を取得。月曜になるまでdateを減らす
				while(thisMonday.getDay() != 1){
					thisMonday.setDate(thisMonday.getDate() - 1);
				}

				var thisSunday = new Date(thisMonday.getFullYear(), thisMonday.getMonth(), thisMonday.getDate() + 6);
				
				//alert(thisSunday.getFullYear());

				jQuery("#top_schedule_current").text(
					thisMonday.getFullYear() + "." + (thisMonday.getMonth() + 1) + "." + thisMonday.getDate() + "〜" + thisSunday.getFullYear() + "." + (thisSunday.getMonth() + 1) + "." + thisSunday.getDate()
				);
				for(var i=0; i<arr1.length; i++){
					//alert(arr1[i]);
					var arr2 = arr1[i].split("\t");
					/*
					arr2のインデックスと対応する情報
					0 : 年
					1 : 月
					2 : 日
					3 : 都道府県
					4 : 会期
					5 : 展示会名
					6 : 展示会名（英語）
					7 : 展示会HP URL
					8 : 会場名
					9 : 会場HP URL
					10 : 入場方法
					11 : 入場方法（英語）
					12 : 照会先
					13 : 照会先電話番号
					14 : 検索用名称
					*/

					//alert("現在:" + thisMonday.getFullYear() + "/" + (thisMonday.getMonth() + 1) + "/" + thisMonday.getDate());
					//alert(arr2[0] + "/" + (arr2[1] - 1) + "/" + arr2[2])
					var exhibition_date = new Date(arr2[0], arr2[1] - 1, arr2[2]);
					//alert("展示会:" + exhibition_date.getFullYear() + "/" + (exhibition_date.getMonth() + 1) + "/" + exhibition_date.getDate());


					//alert(exhibition_date.getTime() + "-" + thisMonday.getTime());
					//alert((Math.floor((exhibition_date.getTime() - thisMonday.getTime()) / 1000)) + " < " + (60 * 60 * 24 * 7));

					//alert(thisMonday.getTime() + "<" + exhibition_date.getTime() + "&&" + exhibition_date.getTime() + "<=" + thisSunday.getTime());
					if(thisMonday.getTime() <= exhibition_date.getTime() && exhibition_date.getTime() <= thisSunday.getTime()){
						//alert("表示");
						var exhibition_place = arr2[9] != "" ? "<a href='" + arr2[9] + "' target='_blank'>" + arr2[8] + "</a>" : arr2[8];
						var exhibition_name = arr2[7] != "" ? "<a href='" + arr2[7] + "' target='_blank'>" + arr2[5] + "</a>" : arr2[5];
						
						jQuery("#top_schedule_list_inner").append(
							jQuery("<div class='exhibition'></div>").append(
								jQuery("<div class='exhibition_header'></div>").append(
									jQuery("<div class='exhibition_term'>" + arr2[4] + "</div>")
								)
								.append(
									jQuery("<div class='exhibition_place'>" + exhibition_place + "</div>")
								)
							).append(
								jQuery("<div class='exhibition_name'>" + exhibition_name + "</div>")
							)
						);
					}//if

				}

				//次月のリンク設定。ファイルが存在する場合だけリンクを設定する。
				var nextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1);
				jQuery.ajax({
					url : "/user_file/poppop-sh/schedule/" + String(nextMonth.getFullYear()) + String(nextMonth.getMonth() + 1) + ".txt",
					dataType : "text",
					timeout : 5000,
					success : function(){
						jQuery("#schedule_next").attr("href", "/p/schedule/#" + nextMonth.getFullYear() + "_" + (nextMonth.getMonth() + 1))
					}
				});
			}//success
		});//jQuery.ajax
	}
});


