function Calendar(opt,current,title){
	EventEmitter.call(this);
	this.title = title;
	this.selected = null;
	this.DAY_TIME = 864E5;
	this.opt = opt;
	this.current = current;
	if(this.current instanceof Date) {
		this.selected = this.current;
		setTimeout(function(){
			this.emit('change',this.selected);
		}.bind(this),0);
		this.current = this.current.getFullYear() + '/' + (this.current.getMonth() + 1);
	}
	this.template = TEMPLATES.CALENDAR;

	this.init();
}
Calendar.prototype = {
	init: function(){
		this.earliest = this.toDate(this.opt.earliest);
		this.latest = this.toDate(this.opt.latest);
		this.initDOM();
		this.addEventListener();
	},
	toDate: function(date){
		if (date) {
			var d = new Date(date);
			if (!isNaN(d)) return d
		};
		return null;
	},
	getDate: function(){
		return this.selected;
	},
	initDOM: function(){
		this.ele = $(this.template);
		this.ele.find('.title').html(this.title);
		this.repaintTable();
	},
	addEventListener: function(){
		//subtitle click
		this.ele.find('.subtitle').click(function(){
			this.switchCalendar();
		}.bind(this));
		//date cell click
		this.ele.find('td').click(function(e){
			var el = $(e.target);
			if (el.hasClass('disabled')) return;
			this.selected = new Date(el.attr('data-date'));
			el.parents('table:first').find('.selected').each(function(i,td){
				var e = $(td);
				if (e.attr('data-type') == 'prevMonth') {
					td.className = 'prevMonth';
					return;
				};
				if (e.attr('data-type') == 'nextMonth') {
					td.className = 'nextMonth';
					return;
				};
				td.className = '';
			});
			el[0].className = 'selected';
			this.emit('change',this.selected);
			this.todayShow();
		}.bind(this));
		//pre
		this.ele.find('.pre').click(function(){
			var current = this.current.split('/');
			var month = parseInt(current[1]),year = parseInt(current[0]);
			if (month > 1) {
				this.current = year+'/'+(month-1);
			}else{
				this.current = (year-1)+'/12';
			}
			this.repaintTable();
		}.bind(this));
		//next
		this.ele.find('.next').click(function(){
			var current = this.current.split('/');
			var month = parseInt(current[1]),year = parseInt(current[0]);
			if (month == 12) {
				this.current = (year+1)+'/1';
			}else{
				this.current = year+'/'+(month+1);
			}
			this.repaintTable();
		}.bind(this));
	},
	showMonthCalendar: function(){
		if (!this.monthEle) {
			this.createMonthDOM();
		};
		this.monthEle.show();
	},
	// TODO
	release: function(){
		// 解绑事件
		this.ele.add(this.monthEle).empty().remove();
		// 删除引用，释放内存
		this.ele = undefined;
		delete this.ele;
		this.monthEle = undefined;
		delete this.monthEle;
	},
	//TODO
	switchCalendar: function(){
		if (this.ele.css('display') == 'none') {
			this.ele.show();
			this.monthEle.hide();
		}else{
			this.ele.hide();
			this.showMonthCalendar();
		}
	},
	createMonthDOM: function(){
		this.monthEle = $(TEMPLATES.MONTHPICKER).insertAfter(this.ele);
		this.monthEle.find('.subtitle').html(this.current.split('/')[0] + '年');
		var m = this.current.split('/')[1];
		//this.monthEle.find('td:contains('+m+')').addClass('selected');

		//month cell click
		this.monthEle.find('td').click(function(e){
			var el = $(e.target);
			var month = parseInt(el.text());
			var t = this.current.split('/');
			t.pop();
			this.current = t.concat([month]).join('/');
			this.repaintTable();
			this.switchCalendar();
			this.todayShow();
		}.bind(this));
		//this.emit('monthEleCreated',this.monthEle)

		// month pre click
		this.monthEle.find('.pre').click(function(e) {
			var t = this.current.split('/');
			t.pop();
			var year = parseInt(t)-1;
			this.current = year + '/1';
			this.monthEle.find('.subtitle').html(year + '年');
		}.bind(this));
		// month next click
		this.monthEle.find('.next').click(function(e) {
			var t = this.current.split('/');
			t.pop();
			var year = parseInt(t)+1;
			this.current = year + '/1';
			this.monthEle.find('.subtitle').html(year + '年');
		}.bind(this));
	},

	//重绘表格，一般用于切换月份时刷新日期
	repaintTable: function(){
		//通过current计算当前月的第一天
		var start = new Date(this.current+'/1');
		
		//获取第一天是星期几，根据这个值来生成日历
		var n = start.getDay(), tds = this.ele.find('td');
		for (var i=0; i < 42; i++) {
			var d = new Date(start-n*this.DAY_TIME + i*this.DAY_TIME),td = tds[i];
			td.title = this.format(d);
			if (i<n) {
				//上个月的日期
				td.className = 'prevMonth';
				td.setAttribute('data-type','prevMonth');
			}else if(d.getMonth() > start.getMonth()){
				//下个月的日期
				td.className = 'nextMonth';
				td.setAttribute('data-type','nextMonth');
			}else{
				td.className = '';
			}
			//不可选择的日期
			if ((this.earliest && d - this.earliest < 0) || (this.latest && d - this.latest > 0)) {
				td.className = 'disabled';
				td.title = td.title + ' 该日期超出限制,不可选';
			};
			td.innerHTML = d.getDate();
			
			//将日期放入DOM节点的属性中，方便获取
			td.setAttribute('data-date',d.toString());
			//回填选中数据
			if (this.selected) {
				if (d - this.selected === 0) {
					td.className = 'selected';
				};
			};
		};

		// 回填title,subtitle
		var ele = this.ele, d = new Date(this.current);
		
		ele.find('.subtitle').html(this.current.replace('/', '年') + '月');
		this.todayShow();
	},
	setEarliest: function(earliest){
		this.earliest = earliest;
		this.repaintTable();
	},
	setLatest: function(latest){
		this.latest = latest;
		this.repaintTable();
	},
	format: function(date){
		if (!date) return '';
		return [date.getFullYear(),date.getMonth()+1,date.getDate()].join('/');
	},
	//当前日高亮显示
	todayShow: function(){
		var date = new Date(),
		today = new Date(date.getFullYear(),date.getMonth(),date.getDate()).toString();
		$('.calendarCon td[data-date="'+today+'"]').addClass('today');
	}
};