﻿

/*
カレンダー更新用

*/

jQuery(function($)
{
	var today = new Date();
	var year = today.getFullYear();
	var month = today.getMonth();
//	month = (Math.max(Math.min(parseInt(prompt("何月のカレンダーを表示しますか？ [1～12]")||11),12),1)-1) || today.getMonth();
	var date = today.getDate();

	// うるう年の加算
	var feb_plus = ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0)) ? 1 : 0;
//	var ary_month1 = new Array("Jan", "Feb" ,"Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var ary_month1 = new Array("JAN", "FEB" ,"MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
	var ary_month2 = new Array(31, 28 + feb_plus, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var obj_calendar;

	var date1 = new Date(year, month, 1);
	var datenext = 0;

	$("#s-calendar").each(function()
	{
		var t = $(this);
		var tt;
		var dt = 0;

		var calendarlink = "";

		if (t.is("a[href *= 'seminar/calendar.html' ]") == true)
		{
			calendarlink = t.find("a[href *= 'seminar/calendar.html' ]").eq(0).attr("href");
		}
		else
		{
			calendarlink = t.closest("div[class*='side-navi']").find("a[href *= 'seminar/calendar.html' ]").eq(0).attr("href");
		}

		// 年月日
		var ty = 0;
		var tm = 0;
		var td = 0;
		var tnum = 0;

		t.find("p.yearmonth span[class ^= 'txt']").each(function()
		{
			tt = $(this);
//			console.log(tt.attr("class"))
			switch (tt.attr("class"))
			{
				case "txt1":
					tt.text(year + "年");
					break;
				case "txt2":
					tt.text((month + 1) + "月");
					break;
				case "txt3":
					tt.text(ary_month1[month] + ".");
					break;
				default:
					break;
			}
		});

		t.find("table[summary *= 'カレンダー']").each(function()
		{
			tt = $(this);
			tt.find("tr").eq(1).find("td").each(function(i)
			{
				var tt2 = $(this);
//				$(this).removeClass("sun sat on no");
				tt2.removeClass("on off no semi");
				if (i < date1.getDay())
				{
					tt2.html("&nbsp;");
					tt2.addClass("no");
				}
				else
				{
					datenext = new Date(date1.getTime() + (i - date1.getDay()) * 1000 * 60 * 60 * 24);
					tt2.text(datenext.getDate());

					switch (datenext.getDay())
					{
						case 0:
//							$(this).addClass("sun");
							tt2.addClass("off");
							break;
						case 6:
//							$(this).addClass("sat");
							tt2.addClass("off");
							break;
						default:
							tt2.addClass("on");
							break;
					}
//					if (isHoliday(datenext.getFullYear(), datenext.getMonth() + 1, datenext.getDate()) == true)
					if (getHolidayNum(datenext.getFullYear(), datenext.getMonth() + 1, datenext.getDate()) != -1)
					{
						tt2.addClass("on");
					}
				}
			});

			// 最後の週を判定
			var lastweek = false;
			for (var j = 1; j < 7; j++)
			{
				tt.find("tr").eq(j).find("td").each(function(i)
				{
					var tt3 = $(this);

					datenext = new Date(date1.getTime() + ((j - 1) * 7 + i - date1.getDay()) * 1000 * 60 * 60 * 24);
					tt3.text(datenext.getDate());
//					$(this).removeClass("sun sat on no");
					tt3.removeClass("on off no semi");

					switch (datenext.getDay())
					{
						case 0:
//							$(this).addClass("sun");
							tt3.addClass("off");
							break;
						case 6:
//							$(this).addClass("sat");
							tt3.addClass("off");
							break;
						default:
							tt3.addClass("on");
							break;
					}

					ty = datenext.getFullYear();
					tm = datenext.getMonth() + 1;
					td = datenext.getDate();

					// 当日
					if ((year == ty) && (month == (tm - 1)) &&  (date == td))
					{
						tt3.addClass("tod");
					}
					// 祝日
					tnum = getHolidayNum(ty, tm, td);
					if (tnum != -1)
					{
						tt3.addClass("off curp");
						tt3.attr("title", getHolidayStr(tnum));
					}

					// セミナー開催日
					tnum = getSeminarNum(ty, tm, td);
					if (tnum != -1)
					{
						tt3.addClass("semi curp");
						tt3.attr("title", getSeminarStr(tnum));
//						console.log( tnum+ "?"+ty + tm + td);

						tt3.wrapInner("<a href='" + calendarlink + "?" + getFullDateStr(ty, tm, td) + "'></a>");
						if (location.protocol == "https:")	// http＞https
						{
							tt3.find("a").click(function()
							{
								if (_gaq)
								{
//									alert("外部\n" + $(this).attr("href"));
									_gaq.push(["_link", $(this).attr("href")]);
//									return false;
								}
							});
						}
					}

					// 翌月なら非表示
					if (datenext.getMonth() != date1.getMonth())
					{
						// 翌月最初の土曜日が来ていない
						if (lastweek == false)
						{
							tt3.html("&nbsp;");
							tt3.removeClass("on off no semi");
							tt3.addClass("no");
						}
						else
						{
							tt3.html("&nbsp;");
							tt3.removeClass("on off no semi");
							tt3.hide();
						}
						// 翌月最初の土曜日だった場合
						if (datenext.getDay() == 6)
						{
							lastweek = true;
						}
					}
				});
			}
		});

		t.show();
//		t.parent().parent().show();
		t.parent().parent().removeClass("nodisp");

		function getHolidayNum(y, m, d)
		{
			var holen = (typeof ary_Holiday == "undefined") ? 0 : Math.ceil(ary_Holiday.length / 2);
			var fd = getFullDateStr(y, m, d);

			for (var i = 0; i < holen; i++)
			{
				if (ary_Holiday[i * 2] == fd)
				{
					return i;
				}
			}
			return - 1;
		}

		function getHolidayStr(tnum)
		{
			return ary_Holiday[tnum * 2 + 1];
		}
/*
		function getHolidayStr(y, m, d)
		{
			var holen = (typeof ary_Holiday == "undefined") ? 0 : Math.ceil(ary_Holiday.length / 2);
			var fd = getFullDateStr(y, m, d);

			for (var i = 0; i < holen; i++)
			{
				if (ary_Holiday[i * 2] == fd)
				{
					return ary_Holiday[i * 2 + 1];
				}
			}
			return "";
		}
*/
		function getSeminarNum(y, m, d)
		{
			var selen = (typeof ary_Seminar == "undefined") ? 0 : Math.ceil(ary_Seminar.length / 2);
			var fd = getFullDateStr(y, m, d);

			for (var i = 0; i < selen; i++)
			{
				if (ary_Seminar[i * 2] == fd)
				{
					return i;
				}
			}
			return - 1;
		}

		function getSeminarStr(tnum)
		{
			return ary_Seminar[tnum * 2 + 1];
		}

		function getFullDateStr(y, m, d)
		{
			var sy = y + "";
			var sm = ((m > 9) ? m : ("0" + m)) + "";
			var sd = ((d > 9) ? d : ("0" + d)) + "";

			return (sy + sm + sd);
		}
	});

});

