// By lenhatanh | nhatanh.voxfamilyvn.com | Depends on mootools
// Extended/Adjusted by Ben Smeets
// - Added css for 'available' day and some functions for managing 
//   available days 
// - Adjusted multiple js entries
var DateTime = new Class({
	
	DayofMonth: function(m,y) {
		switch(m){
			case 1 :
			case 3 :
			case 5 :
			case 7 :
			case 8 :
			case 10 :
			case 12 :
				return 31; break;
			case 4 :
			case 6 :
			case 9 :
			case 11 :
				return 30; break;
			case 2 : return y % 4 == 0 ? 29 : 28; break;
		}
	},
	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
		else return false;
	}	
});

var Calendar = DateTime.extend({
	
	info: function(opt) {
		var Now = new Date();		
		this.opt = {
			min_month: 1,
			min_year: 1,
			max_month: 12,
			max_year: 9999,
			c_year: Now.getFullYear(),
			c_month: Now.getMonth() + 1,
			c_day: 999,
			position: 'after',
			onSelect: Class.empty,
			onNextMonth: Class.empty,
			onPreviousMonth: Class.empty,
			link_next: '<img src="style/img/ico_next.jpg" alt="volgende maand" />',
			link_prev: '<img src="style/img/ico_prev.jpg" alt="vorige maand" />',
			link_close: '',
			weekday_text: 'M|D|W|D|V|Z|Z',
			month_text: 'Januari|Februari|Maart|April|Mei|Juni|Juli|Augustus|September|October|November|December',
			auto_show: false
		};
		Object.extend(this.opt, opt || {});
				
		this.availDays = [0];
		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.auto_show) this.Create();
		else this.ready_show = false;
	},
	
	prepairCreate: function() {
		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;

		this.html = '';
		
		if (this.opt.link_close != '')
		{
			this.html += '<div style="text-align:right;margin:0;">';
			this.html += ' <span id="' + this.opt.e_name + '_close" style="cursor:pointer;">' + this.opt.link_close + '</span>';
			this.html += '</div>';
		}
		
		this.html += '<div class="'+ this.opt.css_title +'" style="margin:0;padding:0;">';
		_1 = this.opt.month_text.split('|');
		this.html += '<span id="' + this.opt.e_name + '_prev">' + this.opt.link_prev + '</span> '+ _1[this.month-1] + '.' + this.year;
		this.html += ' <span id="' + this.opt.e_name + '_next">' + this.opt.link_next + '</span></div>';
		
		this.html += '<div style="clear:both">';
		_2 = this.opt.weekday_text.split('|');
		for(i=0;i<=6;i++) {
			this.html += '<div class="' + this.opt.css_dayname + '">' + _2[i] + '</div>';
		}
		this.html += '</div>';

		this.html += '<div style="clear:both">';
		_8 = 1;
		var el = new Array()
		var el_v = new Array();
		
		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="' + this.opt.css_allday + ' ';

				if (this.isAvailDay(_8))  this.html += this.opt.css_availday;
				else if(_8<_3)  this.html += this.opt.css_pastday;
				else  this.html += this.opt.css_futureday;
				
				if(_8==_3)  this.html += ' ' + this.opt.css_currentday;

				this.html += '">' + _8 + '</div>';
				_8++;
				
			} else this.html += '<div class="' + this.opt.css_allday + ' ' + this.opt.css_emptyday + '">&nbsp;</div>';
		}
		this.html += '</div>';
		
		this.el = el;
		this.el_v = el_v;		
	},
	
	Create: function() {
		if(!this.opt.c_day || !this.opt.c_year || !this.opt.c_month) return;
		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.prepairCreate();
		_1.addClass(this.opt.css_calendar);
		_1.setStyle('display', 'block');
		_1.setStyle('opacity', 1);
		if(!this.opt.auto_show) {
			y = $(this.opt.p_name).getCoordinates().top + 15;
			x = $(this.opt.p_name).getCoordinates().left + 15;
			_1.setStyle('position', 'absolute');
			_1.setStyle('top', y + 'px');
			_1.setStyle('left', x + 'px');
			_1.setStyle('z-index', '1000');
			this.opt.position = 'before';
		}
		_1.setHTML(this.html);
		_1.inject(this.opt.p_name, this.opt.position);
		
		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);
		
		$(this.opt.e_name + '_prev').addEvent('click', function(){this.prevMonth()}.bind(this));
		$(this.opt.e_name + '_next').addEvent('click', function(){this.nextMonth()}.bind(this));
		
		if (this.opt.link_close != '')
			$(this.opt.e_name + '_close').addEvent('click', function(){this.hide()}.bind(this));
		
		this.ready_show = true;
	},
	
	toggle: function() {
		if(this.ready_show)
			this.hide();
		else
			this.show();
	},
	
	hide: function() {
		if(!this.ready_show) return;
			else {
			$(this.opt.e_name).setStyle('display', 'none');
			this.ready_show = false;
		}
	},
	
	show: function() {
		if(!$(this.opt.e_name)) this.Create();
		else {
			if(this.ready_show) return;
			else {
				$(this.opt.e_name).setStyle('display', 'block');
				this.ready_show = true;
			}
		}
	},
	
	prevMonth: function (blnSilently) {
		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--;
		}
		if (this.opt.onPreviousMonth == Class.empty)
			this.Create();
		else
			this.opt.onPreviousMonth(this.year,this.month,this,blnSilently);
	},
	
	nextMonth: function (blnSilently) {
		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++;
		}
	
		if (this.opt.onNextMonth == Class.empty)
			this.Create();
		else
			this.opt.onNextMonth(this.year,this.month,this,blnSilently);
	},
	
	getMonth: function () {
		return this.month;
	},
	
	getYear: function () {
		return this.year;
	},
	
	setAvailDays: function(arr) {
		if (arr != undefined)
			this.availDays = arr;
		else
			this.availDays = [];

		this.Create();
	},
	
	isAvailDay: function(day) {
		for (var i=0; i<this.availDays.length; i++)
			if (this.availDays[i].toInt() == day.toInt()) 
				return true;
		return false;
	}
});