﻿(function ($)
{
	$.jbecool.dateShower = function()
	{
		var _this=this;
		var Q = [19416, 19168, 42352, 21717, 53856, 55632, 91476, 22176, 39632, 21970, 19168, 42422, 42192, 53840, 119381, 46400, 54944, 44450, 38320, 84343, 18800, 42160, 46261, 27216, 27968, 109396, 11104, 38256, 21234, 18800, 25958, 54432, 59984, 28309, 23248, 11104, 100067, 37600, 116951, 51536, 54432, 120998, 46416, 22176, 107956, 9680, 37584, 53938, 43344, 46423, 27808, 46416, 86869, 19872, 42416, 83315, 21168, 43432, 59728, 27296, 44710, 43856, 19296, 43748, 42352, 21088, 62051, 55632, 23383, 22176, 38608, 19925, 19152, 42192, 54484, 53840, 54616, 46400, 46752, 103846, 38320, 18864, 43380, 42160, 45690, 27216, 27968, 44870, 43872, 38256, 19189, 18800, 25776, 29859, 59984, 27480, 21952, 43872, 38613, 37600, 51552, 55636, 54432, 55888, 30034, 22176, 43959, 9680, 37584, 51893, 43344, 46240, 47780, 44368, 21977, 19360, 42416, 86390, 21168, 43312, 31060, 27296, 44368, 23378, 19296, 42726, 42208, 53856, 60005, 54576, 23200, 30371, 38608, 19415, 19152, 42192, 118966, 53840, 54560, 56645, 46496, 22224, 21938, 18864, 42359, 42160, 43600, 111189, 27936, 44448, 84835];
		var K = "甲乙丙丁戊己庚辛壬癸";
		var J = "子丑寅卯辰巳午未申酉戌亥";
		var P = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
		var L = ["小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至"];
		var B = [0, 21208, 43467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758];
		var A = "零一二三四五六七八九";
		var M = "天一二三四五六";
		var F = ["正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊"];
		var D = "初十廿卅";
		var W = 
		{
			"0101": "*1元旦节",
			"0214": "情人节",
			"0305": "学雷锋纪念日",
			"0308": "妇女节",
			"0312": "植树节",
			"0315": "消费者权益日",
			"0401": "愚人节",
			"0501": "*1劳动节",
			"0504": "青年节",
			"0601": "国际儿童节",
			"0701": "中国共产党诞辰",
			"0801": "建军节",
			"0910": "中国教师节",
			"1001": "*3国庆节",
			"1224": "平安夜",
			"1225": "圣诞节"
		};
		var U = 
		{
			"0101": "*2春节",
			"0115": "元宵节",
			"0505": "*1端午节",
			"0815": "*1中秋节",
			"0909": "重阳节",
			"1208": "腊八节",
			"0100": "除夕"
		};
		this.V = function(Z)
		{
			function d(k, j)
			{
				var i = new Date((31556925974.7 * (k - 1900) + B[j] * 60000) + Date.UTC(1900, 0, 6, 2, 5));
				return (i.getUTCDate())
			}
			function e(l)
			{
				var j, k = 348;
				for (j = 32768; j > 8; j >>= 1) 
				{
					k += (Q[l - 1900] & j) ? 1 : 0
				}
				return (k + c(l))
			}
			//获取干支年
			function b(i)
			{
				return (K.charAt(i % 10) + J.charAt(i % 12))
			}
			
			function c(i)
			{
				if (h(i)) 
				{
					return ((Q[i - 1900] & 65536) ? 30 : 29)
				}
				else 
				{
					return (0)
				}
			}
			function h(i)
			{
				return (Q[i - 1900] & 15)
			}
			function f(j, i)
			{
				return ((Q[j - 1900] & (65536 >> i)) ? 30 : 29)
			}
			//计算是否为闰年闰月
			function C(n)
			{
				var l, k = 0, j = 0;
				var m = new Date(1900, 0, 31);
				var o = (n - m) / 86400000;
				_this.dayCyl = o + 40;
				_this.monCyl = 14;
				for (l = 1900; l < 2050 && o > 0; l++) 
				{
					j = e(l);
					o -= j;
					_this.monCyl += 12
				}
				if (o < 0) 
				{
					o += j;
					l--;
					_this.monCyl -= 12
				}
				_this.year = l;
				_this.yearCyl = l - 1864;
				k = h(l);
				_this.isLeap = false;
				for (l = 1; l < 13 && o > 0; l++) 
				{
					if (k > 0 && l == (k + 1) && _this.isLeap == false) 
					{
						--l;
						_this.isLeap = true;
						j = c(_this.year)
					}
					else 
					{
						j = f(_this.year, l)
					}
					if (_this.isLeap == true && l == (k + 1)) 
					{
						_this.isLeap = false
					}
					o -= j;
					if (_this.isLeap == false) 
					{
						_this.monCyl++
					}
				}
				if (o == 0 && k > 0 && l == k + 1) 
				{
					if (_this.isLeap) 
					{
						_this.isLeap = false
					}
					else 
					{
						_this.isLeap = true;
						--l;
						--_this.monCyl
					}
				}
				if (o < 0) 
				{
					o += j;
					--l;
					--_this.monCyl
				}
				_this.month = l;
				_this.day = o + 1
			}
			function G(i)
			{
				return i < 10 ? "0" + i : i
			}
			//将年份转换为汉字
			function H(i)
			{
				var result;
				if (i > 1000) 
				{
					result = A.charAt(i / 1000) + A.charAt(i / 100 % 10) + A.charAt(i / 10 % 100) + A.charAt(i % 10);
				}
				else 
					if (i > 10) 
						result = A.charAt(i / 10 % 100) + "十" + A.charAt(i % 10);
					else 
						result = A.charAt(i);
				return result;
			}
			function a(j, i)
			{
				var k;
				switch (j, i)
				{
					case 10:
						k = "初十";
						break;
					case 20:
						k = "二十";
						break;
					case 30:
						k = "三十";
						break;
					default:
						k = D.charAt(Math.floor(i / 10));
						k += A.charAt(i % 10)
				}
				return (k)
			}
			
			_this.date = Z;
			_this.isRestDay = false;
			_this.solarYear = Z.getFullYear();
			_this.solarYearInChinese = H(Z.getFullYear()); //A.charAt(Z.getFullYear().toString().substring(3)) //H(Z.getFullYear); //g(Z,"Y");
			_this.solarMonth = Z.getMonth() + 1;
			_this.solarMonthInChinese = H((Z.getMonth() + 1)); //g(Z, "M");
			_this.solarDate = Z.getDate(); //g(Z, "d");
			_this.solarDateInChinese = H(Z.getDate()); //g(Z, "D");
			_this.solarWeekDay = Z.getDay();
			_this.solarWeekDayInChinese = "星期" + M.charAt(_this.solarWeekDay);
			_this.solarHours = Z.getHours(); // g(Z, "h");
			_this.solarMinutes = Z.getMinutes(); //g(Z,"n");
			_this.solarSeconds = Z.getSeconds(); //g(Z, "s");
			var Y = new C(Z);
			_this.lunarYear = Y.year;
			_this.shengxiao = P.charAt((_this.lunarYear - 4) % 12);
			_this.lunarMonth = Y.month;
			_this.lunarIsLeapMonth = Y.isLeap;
			_this.lunarYearInChinese = H(Y.year);
			_this.lunarDateInChinese = H(Y.day);
			_this.lunarMonthInChinese = _this.lunarIsLeapMonth ? "闰" + F[Y.month - 1] : F[Y.month - 1];
			_this.lunarDate = parseInt(Y.day);
			_this.showInLunar = _this.lunarDateInChinese = a(_this.lunarMonth, _this.lunarDate);
			if (_this.lunarDate == 1) 
			{
				_this.showInLunar = _this.lunarMonthInChinese + "月"
			}
			_this.ganzhiYear = b(Y.yearCyl);
			_this.ganzhiMonth = b(Y.monCyl);
			_this.ganzhiDate = b(Y.dayCyl++);
			//节气
			_this.jieqi = "";
			_this.restDays = 0;
			if (d(_this.solarYear, (_this.solarMonth - 1) * 2) == Z.getDate().toString()) 
			{
				_this.showInLunar = _this.jieqi = L[(_this.solarMonth - 1) * 2]
			}
			if (d(_this.solarYear, (_this.solarMonth - 1) * 2 + 1) == Z.getDate().toString()) 
			{
				_this.showInLunar = _this.jieqi = L[(_this.solarMonth - 1) * 2 + 1]
			}
			if (_this.showInLunar == "清明") 
			{
				_this.showInLunar = "清明节";
				_this.restDays = 1
			}
			//节日
			_this.solarFestival = W[G((Z.getMonth() + 1)) + G(Z.getDate())];
			if (typeof _this.solarFestival == "undefined") 
			{
				_this.solarFestival = ""
			}
			else 
			{
				if (/\*(\d)/.test(_this.solarFestival)) 
				{
					_this.restDays = parseInt(RegExp.$1);
					_this.solarFestival = _this.solarFestival.replace(/\*\d/, "")
				}
			}
			_this.showInLunar = (_this.solarFestival == "") ? _this.showInLunar : _this.solarFestival;
			_this.lunarFestival = U[_this.lunarIsLeapMonth ? "00" : G(_this.lunarMonth) + G(_this.lunarDate)];
			if (typeof _this.lunarFestival == "undefined") 
			{
				_this.lunarFestival = ""
			}
			else 
			{
				if (/\*(\d)/.test(_this.lunarFestival)) 
				{
					_this.restDays = (_this.restDays > parseInt(RegExp.$1)) ? _this.restDays : parseInt(RegExp.$1);
					_this.lunarFestival = _this.lunarFestival.replace(/\*\d/, "")
				}
			}
			if (_this.lunarMonth == 12 && _this.lunarDate == f(_this.lunarYear, 12)) 
			{
				_this.lunarFestival = U["0100"];
				_this.restDays = 1
			}
			_this.showInLunar = (_this.lunarFestival == "") ? _this.showInLunar : _this.lunarFestival;
			_this.showInLunar = (_this.showInLunar.length > 4) ? _this.showInLunar.substr(0, 2) + "..." : _this.showInLunar
		}
	};
})(jQuery);
$(function ()
{
	var str = $(".becool_dateshower").attr("format");
	var ds = new $.jbecool.dateShower();
	ds.V(new Date());
	$(".becool_dateshower").text(str.replace("NY", ds.lunarYearInChinese).replace("NM", ds.lunarMonthInChinese).
	replace("ND", ds.lunarDateInChinese).replace("TY", ds.ganzhiYear).replace("TM", ds.ganzhiMonth).
	replace("TD", ds.ganzhiDate).replace("NF", ds.lunarFestival).replace("Ny", ds.lunarYear).
	replace("Nm", ds.lunarMonth).replace("Nd", ds.lunarDate).replace("y", ds.solarYear).
	replace("Y", ds.solarYearInChinese).replace("m", ds.solarMonth).replace("M", ds.solarMonthInChinese).
	replace("d", ds.solarDate).replace("D", ds.solarDateInChinese).replace("w", ds.solarWeekDay).
	replace("W", ds.solarWeekDayInChinese).replace("h", ds.solarHours).replace("n", ds.solarMinutes).
	replace("s", ds.solarSeconds).replace("X", ds.shengxiao).replace("F", ds.solarFestival).
	replace("J", ds.jieqi));
});

