﻿TSN.calender.holidaySet.hk.day = {
	'2012': ["20120102", "20120123", "20120124", "20120125", "20120404", "20120406", "20120407", "20120409", "20120428", "20120501", "20120623", "20120702", "20121001", "20121002", "20121023", "20121225", "20121226"],
	'2011': ["20110101", "20110203", "20110204", "20110205", "20110405", "20110422", "20110423", "20110425", "20110502", "20110510", "20110606", "20110701", "20110913", "20111001", "20111005", "20111226", "20111227"],
	'2010': ["20100101", "20100213", "20100214", "20100215", "20100216", "20100402", "20100403", "20100405", "20100406", "20100501", "20100521", "20100616", "20100701", "20100923", "20101001", "20101016", "20101225", "20101227"],
	'2009': ["20090101", "20090126", "20090127", "20090128", "20090404", "20090410", "20090411", "20090413", "20090501", "20090502", "20090528", "20090701", "20091001", "20091003", "20091026", "20091225", "20091226"],
	'2008': ["20080101", "20080207", "20080208", "20080209", "20080321", "20080322", "20080324", "20080404", "20080501", "20080512", "20080609", "20080701", "20080915", "20081001", "20081007", "20081225", "20081226"],
	'2007': ["20070101", "20070217", "20070219", "20070220", "20070405", "20070406", "20070407", "20070409", "20070501", "20070524", "20070619", "20070702", "20070926", "20071001", "20071019", "20071225", "20071226"],
	'2006': ["20060102", "20060128", "20060130", "20060131", "20060405", "20060414", "20060415", "20060417", "20060501", "20060505", "20060531", "20060701", "20061002", "20061007", "20061030", "20061225", "20061226"],
	'2005': ["20050101", "20050209", "20050210", "20050211", "20050325", "20050326", "20050328", "20050405", "20050502", "20050516", "20050611", "20050701", "20050919", "20051001", "20051011", "20051226", "20051227"],
	'2004': ["20040101", "20040122", "20040123", "20040124", "20040405", "20040409", "20040410", "20040412", "20040501", "20040526", "20040622", "20040701", "20040929", "20041001", "20041022", "20041225", "20041227"],
	'2003': ["20030101", "20030201", "20030131", "20030203", "20030405", "20030418", "20030419", "20030421", "20030501", "20030508", "20030604", "20030701", "20030912", "20031001", "20031004", "20031225", "20031226"],
	'2002': ["20020101", "20020212", "20020213", "20020214", "20020329", "20020330", "20020401", "20020405", "20020501", "20020520", "20020615", "20020701", "20020921", "20021001", "20021014", "20021225", "20021226"]
}
TSN.calender.holidaySet.hk.dayName = {
	'2012': ["元旦", "農曆年初一", "農曆年初二", "農曆年初三", "清明節", "耶穌受難節", "耶穌受難節翌日", "復活節星期一", "佛誕", "勞動節翌日", "端午節", "香港特別行政區成立紀念日", "中秋節翌日", "國慶日", "重陽節", "聖誕節", "聖誕節後第一個周日"],
	'2011': ["元旦", "農曆年初一", "農曆年初二", "農曆年初三", "清明節", "耶穌受難節", "耶穌受難節翌日", "復活節星期一", "勞動節翌日", "佛誕", "端午節", "香港特別行政區成立紀念日", "中秋節翌日", "國慶日", "重陽節", "聖誕節後第一個周日", "聖誕節後第二個周日"],
	'2010': ["元旦", "除夕", "春節", "農曆年初二", "農曆年初三", "耶穌受難節", "耶穌受難節翌日", "復活節星期一", "清明節翌日", "勞動節", "佛誕", "端午節", "香港特別行政區成立紀念日", "中秋節翌日", "國慶日", "重陽節", "聖誕節", "聖誕節後第一個周日"],
	'2009': ["元旦", "農曆年初一", "農曆年初二", "農曆年初三", "清明節", "耶穌受難節", "耶穌受難節翌日", "復活節星期一", "勞動節", "佛誕", "端午節", "香港特別行政區成立紀念日", "國慶日", "中秋節", "重陽節", "聖誕節", "聖誕節後第一個周日"]
}
TSN.calender.holidaySet.cn = {};
TSN.calender.holidaySet.cn.day = {
	'2010': ["20100101", "20100213", "20100214", "20100215", "20100405", "20100501", "20100616", "20100922", "20101001", "20101002", "20101003"],
	'2009': ["20090101", "20090102", "20090103", "20090125", "20090126", "20090127", "20090128", "20090129", "20090130", "20090131", "20090404", "20090405", "20090406", "20090501", "20090502", "20090503", "20090528", "20090529", "20090530", "20091001", "20091002", "20091003", "20091004", "20091005", "20091006", "20091007", "20091008"]
}
TSN.calender.holidaySet.cn.dayName = {
	'2010': ["元旦", "除夕", "春節", "農曆年初二", "清明節", "勞動節", "端午節", "中秋節", "國慶日", "國慶日", "國慶日"],
	'2009': ["元旦", "元旦", "元旦", "除夕", "農曆年初一", "農曆年初二", "農曆年初三", "農曆年初四", "農曆年初五", "農曆年初六", "清明節", "清明節", "清明節", "勞動節", "勞動節", "勞動節", "端午節", "端午節", "端午節", "國慶日", "國慶日", "國慶日", "中國內地: 公休", "中國內地: 公休", "中國內地: 公休", "中國內地: 公休", "中國內地: 公休"]
}
TSN.calender.getHdName = function(date, type) {
	var year = date.substring(0,4);
	var idx = 0;
	if (typeof TSN.calender.holidaySet[type]['day'][year] != 'undefined' && typeof TSN.calender.holidaySet[type]['dayName'][year] != 'undefined') {
		var hds = TSN.calender.holidaySet[type]['day'][year];
		for (var i = 0; i < hds.length;i++) {
			if (hds[i] == date) {
				return TSN.calender.holidaySet[type]['dayName'][year][idx];
			}
			idx++;
		}
	}
	return '';
}
TSN.calender.adv = {
	curYear: 0,
	curMonth: 0,
	id: '#calender-adv',
	loaded: false,
	displayCnHd: false,
	selectBoxYear: null,
	selecBoxMonth: null,
	init: function() {
		if(this.curYear == 0) {
			yyyy = TSN.calender.pubDate.substring(0,4);
			mm = TSN.calender.pubDate.substring(4,6);
			$('#showChinaHd').click(function() { TSN.calender.adv.toggleDisplayChinaHd(); });
			$(this.id+' a.prev').click(function() { TSN.calender.adv.prev(); return false;});
			$(this.id+' a.next').click(function() { TSN.calender.adv.next(); return false;});
			$(this.id+' h2').append('<select class="archiveYear">'+this.getYearOption(TSN.calender.minYear, TSN.calender.maxYear)+'</select>');
			$(this.id+' h2').append('<select class="archiveMonth">'+this.getMonthOption()+'</select>');
			this.selectBoxYear = $(this.id+' .archiveYear');
			this.selecBoxMonth = $(this.id+' .archiveMonth');
			this.render(yyyy, mm);
			this.selectBoxYear.change(function() { TSN.calender.adv.selectDate($(this).val(), TSN.calender.adv.curMonth); });
			this.selecBoxMonth.change(function() { TSN.calender.adv.selectDate(TSN.calender.adv.curYear, $(this).val()); });
		}
	},
	selectDate: function(yyyy, mm) {
		yyyy = parseInt(yyyy, 10);
		mm = parseInt(mm, 10);
		maxMonth = parseInt(TSN.pubDate.substring(4,6), 10);
		if (yyyy == TSN.calender.maxYear && mm > maxMonth) {
			mm = maxMonth;
		}
		if (yyyy == TSN.calender.minYear && mm < TSN.calender.minMonth) {
			mm = TSN.calender.minMonth;
		}
		this.render(yyyy, mm);
	},
	getYearOption: function(minY, maxY) {
		var html = [];
		for(var i = maxY; i >= minY;i--) {
			html.push('<option value="'+i+'">'+i+'年</option>');
		}
		return html.join('');
	},
	getMonthOption: function() {
		var html = [];
		for(var i = 1; i <= 12;i++) {
			html.push('<option value="'+i+'">'+onccLib.digitPad('0',i,2)+'月</option>');
		}
		return html.join('');
	},
	getDayOption: function() {
		var html = [];
		for(var i = 1; i <= 31;i++) {
			html.push('<option value="'+i+'">'+onccLib.digitPad('0',i,2)+'日</option>');
		}
		return html.join('');
	},
	toggleDisplayChinaHd: function() {
		this.displayCnHd = $('#showChinaHd').is(':checked');
		this.render(this.curYear, this.curMonth);
	}
	,
	show: function(yyyy, mm) {
		this.render(yyyy,mm);
		$(this.id).show();
	},
	render: function(yyyy, mm) {
		this.curYear = parseInt(yyyy, 10);
		this.curMonth = parseInt(mm, 10);
		$(this.id + ' .calender-table .calender').html(this.renderTable(this.curYear,this.curMonth));
		$(this.id+' .archiveYear').val(this.curYear);
		$(this.id+' .archiveMonth').val(this.curMonth);
		
		if (this.curYear == TSN.calender.maxYear && this.curMonth >= parseInt(TSN.pubDate.substring(4,6), 10)) {
			$(this.id+' a.next').addClass('nextDisable');
			$(this.id+' a.next').attr('title', '');
		} else {
			$(this.id+' a.next').removeClass('nextDisable');
			$(this.id+' a.next').attr('title', '下月');
		}
		if (this.curYear == TSN.calender.minYear && this.curMonth == TSN.calender.minMonth) {
			$(this.id+' a.prev').addClass('prevDisable');
			$(this.id+' a.prev').attr('title', '');
		} else {
			$(this.id+' a.prev').removeClass('prevDisable');
			$(this.id+' a.prev').attr('title', '上月');
		}

	},
	renderTable: function(yyyy, mm) {
			var dayList = TSN.calender.getDayList(yyyy, mm);
			var rows = Math.ceil(dayList.length/7);;
			var html = ['<tr><th class="hd">日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>'];
			for(var i = 0;i < rows;i++) {
				html.push('<tr>');
				for(var j = 0; j < 7;j++) {
					var index = (i*7)+j;
					if (index < dayList.length && dayList[index] != -1) {
						
						var curDate = yyyy + onccLib.digitPad('0', mm, 2) + onccLib.digitPad('0', dayList[index], 2);
						var tdClass = '';
						if (curDate == TSN.pubDate) {
							tdClass = 't';
						}
						var isHkHd = TSN.calender.isHoliday(yyyy, mm, dayList[index]);
						if (j == 0 || isHkHd) {
							tdClass += ' hd';
						}
						if (tdClass != '') {
							tdClass = ' class="'+tdClass+'"';
						}
						html.push('<td'+tdClass+'>');
						html.push('<div class="hdDesc"><!--NULL--></div>');
						html.push(this.addLink(yyyy,mm,dayList[index], isHkHd));
						var cnClass = 'cnHoliday';
						if (this.displayCnHd) {
							var cnEventStr = '';
							if (TSN.calender.isHoliday(yyyy, mm, dayList[index], 'cn')) {
								cnClass += ' onDay';
								cnEventStr = this.getHdEventStr('cn', curDate);
							}
						}
						html.push('<div class="'+cnClass+'"'+cnEventStr+'><!--NULL--></div>');
						html.push('</td>');
					} else {
						html.push('<td></td>');
					}
				}
				html.push('</tr>');
				if (i < rows - 1) {
					html.push('<tr><td colspan="7"><div class="hr"><!--NULL--></div></td></tr>');
				}
			}
			return html.join('');
	},
	getHdEventStr: function(type, date) {
		return ' onmouseover="TSN.calender.adv.toolTips.show(this, \''+type+'\', \''+date+'\');" onmouseout="TSN.calender.adv.toolTips.hide(this, \''+type+'\');"'
	},
	addLink: function(yyyy, mm, dd, isHd) {
		var d = yyyy + onccLib.digitPad('0',mm,2) + onccLib.digitPad('0',dd,2);
		var eventStr = '';
		if (isHd) {
			eventStr = this.getHdEventStr('hk', d);
		}
		if (d <= TSN.pubDate) {
			return '<a href="'+TSN.getSectHref('main', d)+'"'+eventStr+'>'+dd+'</a>';
		}
		return '<div'+eventStr+'>'+dd+'</div>';
	},
	hide: function() {
		$(this.id).hide();
	},
	prev: function() {
		if (this.curYear >= TSN.calender.minYear) {
			var mm = this.curMonth;
			if (this.curYear > TSN.calender.minYear && mm > 1 && mm < 13) {
				mm--;
			} else if (this.curYear == TSN.calender.minYear && mm > TSN.calender.minMonth && mm < 13) {
				mm--;
			} else if (mm < 2 && this.curYear > TSN.calender.minYear) {
				this.curYear--;
				mm = 12;
			}
			this.curMonth = mm;
			this.render(this.curYear, this.curMonth);
		}
		return false;
	},
	next: function() {
		if (this.curYear + TSN.calender.digitPad(this.curMonth) < TSN.calender.pubDate.substring(0,6)) {
			var mm = this.curMonth;
			if (mm > 0 && mm < 12) {
				mm++;
			} else if (mm > 11) {
				this.curYear++;
				mm = 1;
			}
			this.curMonth = mm;
			this.render(this.curYear, this.curMonth);
		}
		return false;
	},
	toolTips: {
		show: function(obj, type, date) {
			var hdDesc = $(obj).parent().find('.hdDesc');
			var ttips = hdDesc.find('.'+type);
			if (ttips.length > 0) {
				ttips.show();
			} else {
				var dayName = TSN.calender.getHdName(date, type);
				if (dayName != '') {
					hdDesc.append(this.genDiv(dayName, type));
					var toolTips = hdDesc.find('.'+type);
					var ttWidth = hdDesc.find('.'+type+' .title span').width() + 8;
					toolTips.css('width', ttWidth + 'px');
					var ttLeft = -Math.round((ttWidth - hdDesc.width())/2);
					toolTips.css('left', ttLeft);
				}
			}
		},
		hide: function(obj, type) {
			var ttips = $(obj).parent().find('.hdDesc .'+type);
			ttips.hide();
		},
		genDiv: function(title, type) {
			return '<div class="'+type+' toolTips"><div class="title"><span>'+title+'</span></div><div class="downArrow"></div></div>';
		}
	}
}

TSN.searchForm = {
	
	init: function() {
		var maxYear = parseInt(TSN.pubDate.substring(0,4));
		with(TSN.calender.adv) {
			$('select[@name="fyr"]').html(getYearOption(2002, maxYear));
			$('select[@name="fmt"]').html(getMonthOption());
			$('select[@name="fdy"]').html(getDayOption());
			
			$('select[@name="tyr"]').html(getYearOption(2002, maxYear));
			$('select[@name="tmt"]').html(getMonthOption());
			$('select[@name="tdy"]').html(getDayOption());
		}
	}
}

$(function() {
TSN.calender.adv.init();
});

