

mtCalendar = new Class({

	version: '1.0',
	Implements: [new Events, new Options],
	
	options: {
		months: ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
		week_days: ["Do", "Se", "Te", "Qu", "Qu", "Se", "Sa"],
		default_css: 'mtcalendar',
		n_weekstart: 0,
		currentDate: new Date(),
		currentDay: new Date(),
		selectedDate: 0
	},
	
	initialize: function(container,options){
		this.setOptions(options);
		this.container = $(container);
		
		this.container.addClass('mtc');
		
		this.options.selectedDate = this.options.currentDate.clone();

		
		this.precalculateCalendar();
		
		this.yearPlaceHolder = this.container.getElement('.year');
		this.monthPlaceHolder = this.container.getElement('.month');
		this.previousPlaceHolder = this.container.getElement('.previous');
		this.nextPlaceHolder = this.container.getElement('.next');
		this.weekdaysPlaceHolder = this.container.getElement('.week_days');
		this.daysPlaceHolder = this.container.getElement('.days');
		
		var previousButton = this.container.getElement('.previous').getElement('a');
		previousButton.addEvent('click', this.doPreviousMonth.bind(this) );
		var nextButton = this.container.getElement('.next').getElement('a');
		nextButton.addEvent('click', this.doNextMonth.bind(this) );
		
		this.fillCalendar();
		
		//alert(this.nextMonth.format('%d/%m/%Y %H:%M'));
		
		
		//alert(this.container);
		//alert(dateString);
	},
	
	precalculateCalendar: function() {
		this.currentMonth = this.options.currentDate.clone();
		this.currentMonth.setDate(1);
		this.previousMonth = this.currentMonth.clone().decrement('month');
		this.nextMonth = this.currentMonth.clone().increment('month');

		this.firstDay = this.currentMonth.clone();
		this.firstDay.setDate(1-(7+this.firstDay.getDay()-this.options.n_weekstart)%7);
		this.lastDay = this.nextMonth.clone();
		this.lastDay.setDate(0);
	},
	
	fillCalendar: function() {
		if (this.yearPlaceHolder) this.yearPlaceHolder.set('html', this.currentMonth.get('year') );
		if (this.monthPlaceHolder) this.monthPlaceHolder.set('html', this.options.months[this.currentMonth.getMonth()] );
		
		
		if (this.weekdaysPlaceHolder) {
			this.weekdaysPlaceHolder.empty();
			
			for (var n=0; n<7; n++)
			{
				var wkDiv = new Element('div', {
						'class': 'mtc_wk',
						'html': '<span>' + this.options.week_days[n] + '</span>'
					}
				);
				wkDiv.inject(this.weekdaysPlaceHolder);
			}
			
		}
		
		if (this.daysPlaceHolder) {
			this.daysPlaceHolder.empty();

			var totalDays = this.firstDay.diff( this.lastDay, 'day' );
			var daysCalendar = ((totalDays+1)/7).ceil()*7;
			var freeDays = daysCalendar-totalDays;
			

			var cDay = this.firstDay.clone();
		
			for (var n=0;n<daysCalendar;n++) {
			
				var className = 'mtc_day';
				
				if (cDay.diff(this.options.selectedDate) == 0) className = 'mtc_selected_day';
				else if (cDay.diff(this.options.currentDay) == 0) className = 'mtc_current_day';
				
				if (cDay.get('month') != this.currentMonth.get('month')) className = 'mtc_inactive_day';
				
				var dayButton = new Element('a', {
						'class': className,
						'html': '<span>' + cDay.get('date') + '</span>'
					}
				);
				
				if (cDay.get('month') == this.currentMonth.get('month')) dayButton.addEvent('click', this.doSelectDay.bind(this,Array(cDay.get('date'),cDay.get('month'),cDay.get('year'))) );

				dayButton.inject(this.daysPlaceHolder);
				
				cDay.increment('day');
			}
		}
	},
	
	doPreviousMonth: function() {
		this.options.currentDate.decrement('month');
		this.precalculateCalendar();
		this.fillCalendar();
	},
	
	doNextMonth: function() {
		this.options.currentDate.increment('month');
		this.precalculateCalendar();
		this.fillCalendar();
	},
	
	doSelectDay: function(day,month,year) {
		//alert(day + '/' + month + '/' + year);
		this.options.currentDate.set('date',day);
		this.options.currentDate.set('month',month);
		this.options.currentDate.set('year',year);
		this.options.selectedDate = this.options.currentDate.clone();
		this.precalculateCalendar();
		this.fillCalendar();
	}
	
	

});