// By lenhatanh | nhatanh.voxfamilyvn.com | a plugin of mootools
var DateTime = new Class({

	DayofMonth: function(m,y) {
		var _ = [,31,,31,30,31,30,31,30,31,30,31,30,31];
		if(m==2) return y % 4 == 0 ? 29 : 28;
		else return _[m];
	},

	DatetoWeekday: function(d,m,y) {
		var d = new Date(y,m-1,d);
		_1 = d.getDay();
		_2 = d.getMonth();
		if(_2 == m-1) return _1!=0 ? _1 : 7 //Sunday is 7
		else return false;
	}

});

var Calendar = DateTime.extend({

	info: function(opt) {
		this.opt = {
			min_month: 1,
			min_year: 1,
			max_month: 12,
			max_year: 9999,
			position: 'after',
			onSelect: Class.empty,
			link_next: '>>',
			link_prev: '<<',
			weekday_text: 'Mon|Tue|Wed|Thu|Fri|Sat|Sun',
			month_text: 'January|February|March|April|May|June|July|August|September|October|November|December',
			auto_show: true
		};
		Object.extend(this.opt, opt || {});

		this.year = this.opt.year ? this.opt.year : this.opt.c_year;
		this.month = this.opt.month ? this.opt.month : this.opt.c_month;
	},

	initialize: function(opt) {
		this.info(opt);
		if(!this.opt.c_day || !this.opt.c_year || !this.opt.c_month) return;
		if(this.opt.auto_show) this.Create();
		else this.ready_show = false;
	},

	preCreate1: function() { //Header of calendar's html
		this.html1 = '<div class="'+ this.opt.css_title +'">';

		mon = this.opt.month_text.split('|');
		this.html1 += '<span id="' + this.opt.e_name + '_prev">' + this.opt.link_prev + '</span> '
		+ '<span id="' + this.opt.e_name + '_moye">' + mon[this.month-1] + '.' + this.year + '</span>'
		+ ' <span id="' + this.opt.e_name + '_next">' + this.opt.link_next + '</span></div>';

		day = this.opt.weekday_text.split('|');
		for(i=0;i<=6;i++) {
			this.html1 += '<div class="' + this.opt.css_day + '">' + day[i] + '</div>';
		}
	},

	preCreate2: function() { //Footer of calendar's html
		if(this.year == this.opt.c_year) {
			if(this.month == this.opt.c_month) _3 = this.opt.c_day;
			else if(this.month > this.opt.c_month) _3 = 0;
			else _3 = 32
		} else if(this.year < this.opt.c_year) _3 = 32;
		else _3 = 0;

		_7 = this.DatetoWeekday(1,this.month,this.year);
		_4 = this.DayofMonth(this.month,this.year);
		_5 = _4 + _7 - 1;
		_6 = Math.ceil(_5 / 7) * 7;

		_8 = 1;
		var el = new Array()
		var el_v = new Array();
		this.html = '<div id="' + this.opt.e_name + '_allday">';
		for(i=1;i<=_6;i++) {
			if(i>=_7 && i<=_5) {

				id = this.opt.e_name + '_element_' + _8;
				el.push(id);
				el_v.push(_8);
				this.html += '<div id="' + id + '" class="';

				if(_8<_3)  this.html += this.opt.css_pastday;
				else if(_8==_3)  this.html += this.opt.css_currentday;
				else  this.html += this.opt.css_futureday;

				this.html += '">' + _8 + '</div>';
				_8++;

			} else this.html += '<div class="' + this.opt.css_emptyday + '">&nbsp;</div>';
		}
		this.html += '</div>';
		this.el = el;
		this.el_v = el_v;
	},

	dayEve: function() { // Set event when you click any date
		this.el.each(function(e, i){
			$(e).addEvent('click', function(){
				if(!this.opt.auto_show) this.hide();
				this.opt.onSelect([this.month,this.el_v[i],this.year], this);
			}.bind(this));
		}, this);
	},

	Create: function() {
		if(!$(this.opt.e_name)) {
			var _1 = new Element(this.opt.e_name);
			_1.id = this.opt.e_name;
		}
		else _1 = $(this.opt.e_name);

		this.preCreate1();
		this.preCreate2();
		_1.addClass(this.opt.css_calendar);
		_1.setStyles({display: 'block', opacity: 0});
		_1.setHTML(this.html1+this.html);
		if(!this.opt.auto_show) {
			y = $(this.opt.p_name).getTop() + 5;
			x = $(this.opt.p_name).getLeft() + 5;
			_1.setStyles({position: 'absolute', top: y + 'px', left: x + 'px'});
			_1.setStyle('z-index', '1000');
			_1.injectInside(document.body).effect('opacity').custom(0,1);
		} else {
			_1.inject(this.opt.p_name, this.opt.position).effect('opacity').custom(0,1);
		}

		this.dayEve();
		$(this.opt.e_name + '_prev').addEvent('click', function(){this.preMon()}.bind(this));
		$(this.opt.e_name + '_next').addEvent('click', function(){this.nexMon()}.bind(this));

		this.ready_show = true;
	},

	fixPos: function() {
			y = $(this.opt.p_name).getTop() + 5;
			x = $(this.opt.p_name).getLeft() + 5;
			$(this.opt.e_name).setStyles({top: y + 'px', left: x + 'px'});
	},

	fixCal: function() {
		$(this.opt.e_name + '_moye').setHTML(mon[this.month-1] + '.' + this.year);

		this.preCreate2();
		$(this.opt.e_name + '_allday').setHTML(this.html);

		this.dayEve();
	},

	hide: function() {
		if(!this.ready_show) return;
		else {
			$(this.opt.e_name).effect('opacity').custom(1,0);
			this.ready_show = false;
		}
	},

	show: function() {
		if(!$(this.opt.e_name)) {
			this.Create();
			this._1stclick = 1;
			document.addEvent('click', function(e) {
				this.fixPos();
				if(this._1stclick) {
					this._1stclick = 0;
				} else {
					var p = $(this.opt.e_name).getCoordinates();
					var e = new Event(e);
					if(p.left > e.page.x || p.right < e.page.x || p.top > e.page.y || p.bottom < e.page.y) {
						this.hide();
						this._1stclick = 1;
					}
				}
			}.bind(this));
		} else {
			if(this.ready_show) return;
			else {
				$(this.opt.e_name).effect('opacity').custom(0,1);
				this.ready_show = true;
				this._1stclick = 1;
			}
		}
	},

	preMon: function () {
		if(this.year==this.opt.min_year) {
			if(this.month ==this.opt.min_month) return;
			else this.month--;
		} else {
			if(this.month==1) {
				this.month=12;
				this.year--;
			} else this.month--;
		}
		this.fixCal();
	},

	nexMon: function () {
		if(this.year==this.opt.max_year) {
			if(this.month ==this.opt.max_month) return;
			else this.month++;
		} else {
			if(this.month==12) {
				this.month=1;
				this.year++;
			} else this.month++;
		}
		this.fixCal();
	}

});