﻿(function($)
{
	$.fn.TodayInHistory = function(o)
	{
		o = $.extend(
		{
            btnPrev: null,
            btnNext: null,
			btnToday: null,
			container: null,
			selMonth: null,
			selDay: null
		}, o || {});
		
		var _this = this; 
		
		//初始化事件
		o.btnPrev.click(function(){_this.selPrevNext(-1);});
		o.btnToday.click(function(){_this.selToday();});
		o.btnNext.click(function(){_this.selPrevNext(1);});
		o.selMonth.change(function(){_this.selChange();});
		o.selDay.change(function(){_this.selChange();});
		
		var items;
		//初始化历史上的今天数据
		this.Init = function () {
			$.getJSON("/BeCool/Apps/TodayInHistory/xtHistory.js", function(data)
			{
				items = data.items; 
				_this.SelectInit();
				o.container.css("overflow-y","auto");
			});
		}
		
		//初始化控件的值
		this.SelectInit = function () {
			for (var i = 1; i <= 12; i++)
				selMonth.options.add(new Option(i, i));
			for (var i = 1; i <= 31; i++)
				selDay.options.add(new Option(i, i));                                
			var month = new Date().getMonth() + 1;
			var day = new Date().getDate();
			selDay[day - 1].selected = true;
			selMonth[month - 1].selected = true;
			_this.selDayChange(month, day);
		}
		
		//获取历史上的今天的值
		this.getValue = function (month, day) {
			for(var i in items){
				if(items[parseInt(i)].month == month && items[parseInt(i)].day == day)
					return items[parseInt(i)].content;
			}
			return "<Font style = \"color:#999999;\">本日无事</Font>";
		}
		
		//日期改变触发内容改变
		this.selDayChange = function (month, day) {
			o.container.html(this.getValue(month, day));
		}
		
		//选择下拉框
		this.selChange = function () {	
			this.selDayChange(selMonth.options[selMonth.selectedIndex].text,selDay.options[selDay.selectedIndex].text);   
		}
		
		//上一日[-1]、下一日[1]	
		this.selPrevNext = function (n) {
			d = selDay.selectedIndex + n;                                
			m = selMonth.selectedIndex;                                
			if (d == -1) { d = 30; m = m - 1; }                                
			if (d == 31) { d = 0; m = m + 1; }                                
			if (m == -1) { m = 11; }                                
			if (m == 12) { m = 0; }                                
			selDay[d].selected = true;                                
			selMonth[m].selected = true;                                
			this.selDayChange(m + 1,d + 1);
		}       
		
		//今天
		this.selToday = function () {	
			var m = new Date().getMonth() + 1;
			var d = new Date().getDate();                                
			selDay[d - 1].selected = true;                                
			selMonth[m - 1].selected = true;
			this.selDayChange(selMonth.options[selMonth.selectedIndex].text,selDay.options[selDay.selectedIndex].text);       
		}
		
		//初始化数据
		_this.Init();
	};
})(jQuery);

$(function()
{
	$(".jbecool_xthistory").TodayInHistory(
	{
		btnPrev: $(".jbecool_xthistory #btnPrev"),
		btnNext: $(".jbecool_xthistory #btnNext"),
		btnToday: $(".jbecool_xthistory #btnToday"),
		container: $(".jbecool_xthistory #container"),
		selMonth: $(".jbecool_xthistory #selMonth"),
		selDay: $(".jbecool_xthistory #selDay")
	});
});

