/*
 * Calendar for Mootools
 * Manuel Garcia (thekeeper)
 * http://www.mgarcia.info
 * Version 0.1
 *
 * Copyright (c) 2007 Manuel Garcia
 * http://www.opensource.org/licenses/mit-license.php
 */

window.addEvent('domready', function() {
	var elements = $$('input');
	$$(elements).each(function(el){
		if (el.hasClass('ncalendar')) {
			el.addEvent('click', function(event) {
				new Calendar(el);
			});
		}
	});
});

var Calendar = new Class({
	initialize: function(el,open) {
		this.input = $(el);
		this.month_name = ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'];
		this.day_name = ['L','M','M','J','V','S','D'];
		this.create_calendar(null);
	},
    create_calendar: function(fecha) {
		if ($('new_calendar')) $('new_calendar').remove();
		if (!fecha) {
			this.today = new Date();
			this.today.setDate(1);
		} else {
			this.today = fecha;
		}
		this.next_m = this.today.getMonth();
		this.next_m++;
		
		// capa contenedora //
		this.div = new Element('div');
		this.div.addClass('calendar');
		position = this.input.getCoordinates();
		this.div.setStyles({ 'font-size':'.8em','padding-top':'1em','cursor':'pointer','opacity':'0','position': 'absolute', 'top':(position.top+position.height)+'px', 'left':(position.left)+'px'});
		this.div.setProperty('id', 'new_calendar');
		
		// mes año y navegacion
		var div = new Element('div');
		div.setStyles({ 'podding':'1em','text-align':'center','height':'30px'});
		div.injectInside(this.div);

		var titulo = new Element('strong');
		titulo.appendText(this.month_name[this.today.getMonth()]+' ' + this.today.getFullYear());
		titulo.injectInside(div);

		var next = new Element('img');
		next.setProperty('src', 'fileadmin/acadomia/templates/img/cal_next.gif');
		next.setStyle('cursor','pointer');
		next.injectAfter(titulo);

		var before = new Element('img');
		before.setProperty('src', 'fileadmin/acadomia/templates/img/cal_prev.gif');
		before.setStyle('cursor','pointer');
		before.injectBefore(titulo);

		var localThis = this;

		// boton de cerrado
		var close = new Element('img');
		close.setProperty('src', 'fileadmin/acadomia/templates/img/cancel.gif');
		close.setStyles({'cursor':'pointer'});
		close.injectAfter(next);

		close.addEvent('click', function(e) {
			localThis.div.remove();
		});

		// navegación por meses => next
		next.addEvent('click', function(e) {
			var fecha = localThis.today;
			fecha.setMonth(localThis.next_m,1);
			localThis.div.remove();
			localThis.create_calendar(fecha);
		});
		
		// navegación por meses => before
		before.addEvent('click', function(e) {
			var fecha = localThis.today;
			fecha.setMonth(localThis.next_m-2,1);
			localThis.div.remove();
			localThis.create_calendar(fecha);
		});

		this.table = new Element('table');
		this.table.setStyles({'width':'190px','margin':'0 auto'});
		this.table.injectInside(this.div);

		var thead = new Element('thead');
		thead.injectInside(this.table);

		var tr = new Element('tr');
		tr.injectInside(thead);

		this.day_name.each(function (day) {
			var td = new Element('th');
			td.appendText(day);
			td.injectInside(tr);
		});

		var tbody = new Element('tbody');
		tbody.injectInside(this.table);

		var first_day = this.today;
		var last_day = this.today;
		this.month = this.today.getMonth();
		var tr = new Element('tr');
		tr.injectInside(tbody);

		var day=0;

		/* primera semana */
		first_day.setDate(1);

		if (first_day.getDay() != 0) {
			for (var i= 1; i <= 7; i++) {
				if (day) {
					day++;
					first_day.setDate(day);
					if (first_day.getDay() == 1) break; // domingo rompemos!
				} else {
					if (first_day.getDay() == i) {
						this.create_td(tr,1,first_day);
						day = 2;
						first_day.setDate(day);
					}
       			}
				this.create_td(tr,day,first_day);
			}
		} else {
		  for (var i= 1; i <= 6; i++) {
			  this.create_td(tr,'','');
		  }
		  this.create_td(tr,1,first_day);
			day = 2;
		}

		var tr = new Element('tr');
		tr.injectInside(tbody);
		
		/* demás dias */
		var fecha_s = this.today;
		if (!day) var day = 1;
		
		for (var i = day; i <= 31; i++) {
			fecha_s.setDate(i);
			var mes = fecha_s.getMonth();
			if ( mes == this.month) {
				if (fecha_s.getDay() == 1) {
					var tr = new Element('tr');
					tr.injectInside(tbody);
				}
				this.create_td(tr,i,fecha_s);
			}
		}

		this.div.injectAfter(this.input);
		this.effect(this.div,'show');
    },
	create_td: function(tr,i,fecha) {
		var localThis = this;
		var td = new Element('td');
		if (fecha) {
			var dia = fecha.getDate();
			var mes = (fecha.getMonth()+1);
			if (dia <= 9) var dia = "0"+ dia;
			if (mes <= 9) var mes = "0"+ mes;

			td.setProperty('id', dia + '/'+ mes +'/'+ fecha.getFullYear());
        }
		td.setStyles({'padding':'0em','text-align':'center'});
        td.addEvent('click', function(e) {
			localThis.input.value = this.id;
			localThis.div.remove();
  		});
		td.addEvent('mouseover', function(e) {
			this.addClass('dayselected');
  		});
		td.addEvent('mouseout', function(e) {
			 this.removeClass('dayselected');
  		});
    	
    	if (i == 0) { i = ''; td.addClass('noday');  }
    	if (fecha) {
    		if (fecha.getDay()==0) td.addClass('sunday');
    	}
		td.appendText(i);
		td.injectInside(tr);
	},
	effect: function(div,op) {
		div.setOpacity(1);
	}
});
