/*
 * Gestionnaire de calendrier
 * 
 * Permet l'affichage d'un calendrier et la sélection d'une date
 * 
 * Nécessite Mootools
 */

var Calendrier = new Class({

	Implements : [Events, Options],
	
	options	: {
		utiliser_fermer	: true,
		width_anime		: true,
		avec_heure		: false
	},
	
	initialize	: function ( date, options )
	{
		if (options)
			this.setOptions(options);
		this.liste_mois		= ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Décembre'];
		this.liste_jours	= ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'];
		this.nombre_jours	= new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		this.date_en_cours	= date;
		this.id				= this.options.id?this.options.id:'calendrier';
	},
	
	afficher	: function ()
	{
		this.construire_calendrier();
	},
	
	update		: function ()
	{
		this.construire_calendrier();
	},
	
	construire_calendrier	: function ()
	{
		if ( !this.options.container )
			return;
		
		this.jour				= this.date_en_cours.getDate();
		this.annee				= this.date_en_cours.getFullYear();
		this.mois				= this.date_en_cours.getMonth();
		this.premier_jour		= new Date(this.annee, this.mois, 1);
		this.dernier_jour		= new Date(this.annee, this.mois, this.nombre_jours[this.mois]);
		var premier_jour		= !this.premier_jour.getDay()?7:this.premier_jour.getDay();
		
		if ( this.annee % 4 == 0 && this.annee % 100 != 0 || this.annee % 400 == 0 )
			this.nombre_jours[1] = 29;
		else
			this.nombre_jours[1] = 28;
		
		if ( $(this.id) )
			var div_calendrier = $(this.id).empty();
		else
			var div_calendrier = new Element('div', {'id':this.id}).set('opacity', 0);
		
		if (this.options.className && !div_calendrier.hasClass(this.options.className))
			div_calendrier.addClass(this.options.className);
	
		var table = new Element('table');
		var tbody = new Element('tbody');
		table.adopt(tbody);
		
		/* Définition de l'en-tête du tableau */
		var thead			= new Element('thead');
		var tr_head			= new Element('tr');
		var th_haut			= new Element('th', {'colspan': 7});
		thead.adopt(th_haut);
		
		if ( this.options.utiliser_fermer )
		{
			var div_fermer		= new Element('div').addClass('fermer');
			var img_fermer		= new Element('img', {'src':'/images/calendrier_fermer.png','alt':''});
			div_fermer.adopt(img_fermer);
			img_fermer.addEvent('click', this.fermer.bind(this));
			th_haut.adopt(div_fermer);
		}
		
		var div_mois		= new Element('div').addClass('mois');
		var div_annee		= new Element('div').addClass('annee');
		th_haut.adopt(div_mois, div_annee);

		
		var img_change_mois_gauche	= new Element('img', {'src':'/images/calendrier_fleche_gauche.png','alt':''});
		var img_change_mois_droite	= new Element('img', {'src':'/images/calendrier_fleche_droite.png','alt':''});
		var text_mois				= new Element('div').set('text', this.liste_mois[this.mois]);
		div_mois.adopt(img_change_mois_gauche, text_mois, img_change_mois_droite);
		
		var img_change_annee_gauche	= new Element('img', {'src':'/images/calendrier_fleche_gauche.png','alt':''});
		var img_change_annee_droite	= new Element('img', {'src':'/images/calendrier_fleche_droite.png','alt':''});
		var text_annee				= new Element('div').set('text', this.annee);
		div_annee.adopt(img_change_annee_gauche, text_annee, img_change_annee_droite);
		
		thead.adopt( new Element('tr').adopt(th_haut) );
		
		img_change_annee_gauche.addEvent('click', this.diminuer_annee.bind(this));
		img_change_annee_droite.addEvent('click', this.augmenter_annee.bind(this));
		img_change_mois_gauche.addEvent('click', this.diminuer_mois.bind(this));
		img_change_mois_droite.addEvent('click', this.augmenter_mois.bind(this));

		var libelle_jour	= new Element('tr').addClass('header-jour');
		for ( i = 1; i <= 7; i++ )
			libelle_jour.adopt( new Element('td').set('text', (this.liste_jours[i%7]).substr(0,3)) );
		tbody.adopt(libelle_jour);
		
		var tr_date = new Element('tr');
		var time = this.premier_jour.getTime() - ((premier_jour-1)*3600*24*1000);
		for ( i = 1; i < premier_jour; i++ )
		{
			var jour_avant = new Date(time);
			tr_date.adopt(new Element('td').addClass('autre-mois').set('text', jour_avant.getDate() ));
			time += 3600*24*1000;
		}
		tbody.adopt(tr_date);
		var numero_jour_ligne;
		for ( i = 1; i <= this.nombre_jours[this.mois]; i++ )
		{
			numero_jour_ligne = i+premier_jour-1;
			var td_jour = new Element('td').addClass('jour-mois').set('text', i).addEvents({
				'click':	function (event)
				{
					event = new Event(event);
					this.jour_clique = event.target.get('text').toInt();
					this.fireEvent('clickdate');
				}.bind(this),
				'mouseover'	: function ()
				{
					this.addClass('jour-mois-hover');
				},
				'mouseout'	: function ()
				{
					this.removeClass('jour-mois-hover');
				}
			})
			if ( i == this.jour )
				td_jour.addClass('jour-selectionne');
			tr_date.adopt( td_jour );
			if ( numero_jour_ligne % 7 == 0 )
			{
				tr_date	= new Element('tr');
				tbody.adopt(tr_date);
			}
		}
		if ( numero_jour_ligne % 7 != 0 )
		{
			var time	= this.dernier_jour.getTime();
			var start	= (numero_jour_ligne % 7) + 1;
			for ( i = start; i <= 7; i++ )
			{
				time += 3600*24*1000;
				var jour_apres = new Date(time);
				tr_date.adopt(new Element('td').addClass('autre-mois').set('text', jour_apres.getDate() ));
			}
		}
		table.adopt(thead, tbody);
		div_calendrier.adopt(table);
		div_calendrier.injectInside(this.options.container);
		
		if (this.options.width_anime)
		{
			new Fx.Tween($(this.id), {wait: false, fps: 50, duration: 200}).start('opacity', 0, 1);			
		}
		else
		{
			$(this.id).setStyle('opacity', 1);
		}

		this.fireEvent('onUpdate');
	},
	
	diminuer_annee	: function ()
	{
		this.date_en_cours = new Date( this.annee-1, this.mois, this.jour );
		this.construire_calendrier();
		this.fireEvent('changeannee');
	},
	
	augmenter_annee	: function ()
	{
		this.date_en_cours = new Date( this.annee+1, this.mois, this.jour );
		this.construire_calendrier();
		this.fireEvent('changeannee');
	},
	
	augmenter_mois	: function ()
	{
		this.date_en_cours = new Date( this.annee, this.mois+1, this.jour );
		this.construire_calendrier();
		this.fireEvent('changemois');
	},
	
	diminuer_mois	: function ()
	{
		this.date_en_cours = new Date( this.annee, this.mois-1, this.jour );
		this.construire_calendrier();
		this.fireEvent('changemois');
	},
	
	fermer	: function ()
	{
		this.fireEvent('close');
		new Fx.Tween($(this.id), {wait: false, fps: 50, duration: 200, onComplete: function ()
		{ if ($(this.id)) $(this.id).dispose(); }.bind(this) }).start('opacity', 1, 0);
		//~ if ($(this.id)) $(this.id).dispose();
	},
	
	getJourClique	: function ()
	{
		return this.jour_clique;
	},
	
	getJour	: function ()
	{
		return this.jour;
	},
	
	getMois	: function ()
	{
		return this.mois+1;
	},
	
	getAnnee	: function ()
	{
		return this.annee;
	},
	
	getDate	: function ()
	{
		var date;
		date  = (this.getJourClique() < 10)?'0'+this.getJourClique():this.getJourClique();
		date += '-'+( (this.getMois() < 10)?'0'+this.getMois():this.getMois() );
		date += '-'+this.getAnnee();
		if ( this.options.avec_heure )
		{
			var _date = new Date();
			date += ' ';
			date +=	(_date.getHours().toInt()< 10)?'0':'';
			date +=	_date.getHours()+':';
			date +=	(_date.getMinutes().toInt()< 10)?'0':'';
			date +=	_date.getMinutes()+':';
			date +=	(_date.getSeconds().toInt()< 10)?'0':'';
			date +=	_date.getSeconds();
		}
		return date;
	}
	
});

var charger_calendrier = function (champ) {
	var id = 'calendier'+( champ.id?'-'+champ.id:'' )
	var calen = new Calendrier( new Date(), {id: id, className: 'calendrier', container: document.getElementsByTagName('body')[0]});
	champ.store('calendar', calen);
	
	if (champ.hasClass('avec-heure')) {
		calen.setOptions({avec_heure: true});
		var max_char = 19;
	} else {
		var max_char = 10;
	}
	champ.setStyles({'width':(1+max_char)+'ex','text-align':'center'});
	
	calen.addEvent(
		'clickdate', function () {
			champ.value = this.getDate();
			this.fermer();
		}
	);
	
	var img = new Element('img', {'src':'/images/calendrier_icone.png','alt':''}).addClass('calendrier-icone').addEvents({
		'click'	: function (event) {
			event = new Event(event).stop();
			if (!$(id)) {
				calen.afficher();
				$(id).setStyles({'position':'absolute', 'top': event.page.y, 'left': event.page.x});
			} else {
				calen.fermer();
			}
		}
	});
	img.injectAfter(champ);
}

window.addEvent('domready', function () {
	$$('.charge-calendrier').each(
		function (champ) {
			charger_calendrier(champ);
		}
	);
});





