dojo.require('actijscore.parser');

dojo.provide('form.date');

dojo.create('link', {'type': 'text/css', 'rel': 'stylesheet', 'href': 'http://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/soria/soria.css'}, dojo.query('head')[0]);

dojo.declare('form.date', null, {
	arrHandler: [],
	domNode: null,
	domCalendar: null,
	objCalendar: null,
	intFocus: 0,
	objLastDate: null,

	constructor: function(params, srcNodeRef) {
		this.arrHandler = [];
		this.domNode = srcNodeRef;
		dojo.require('dijit._Widget');
		dojo.require('dijit._Calendar');
		var handler = dojo.connect(this.domNode, 'focus', this, this.onFocus);
		this.arrHandler.push(handler);
		handler = dojo.connect(this.domNode, 'blur', this, this.onBlur);
		this.arrHandler.push(handler);
	},

	destroy: function() {
		var h;
		while ((h = this.arrHandler.pop()) != undefined) {
			dojo.disconnect(h);
		}
		if (this.domCalendar) {
			dojo.destroy(this.domCalendar);
		}
		if (this.objCalendar) {
			this.objCalendar.destroy();
		}
	},

	onFocus: function(e) {
		this.intFocus++;
		this.updateFocus();
	},

	onBlur: function(e) {
		this.intFocus--;
		this.updateFocus();
	},

	onCalendarFocus: function(e) {
		this.intFocus++;
		this.updateFocus();
	},

	onCalendarBlur: function(e) {
		this.intFocus--;
		this.updateFocus();
	},

	onValueSelected: function(e) {
		var val = this.objCalendar.attr('value');
		this.domNode.value = dojo.date.locale.format(val, {'selector': 'date', 'datePattern': 'dd/MM/yyyy'});
		this.objLastDate = val;
		this.onCalendarBlur(null);
	},

	updateFocus: function() {
		if (this.intFocus > 0 && this.objCalendar == null) {
			// Créer le conteneur du calendrier
			var mb = dojo.coords(this.domNode, true);
			this.domCalendar = dojo.create('div', {}, 'jsCalendarBox');
			dojo.addClass(this.domCalendar, 'soria');
			dojo.style(this.domCalendar, 'position', 'absolute');
			dojo.style(this.domCalendar, 'zIndex', '10');
			dojo.style(this.domCalendar, 'left', mb.x + 'px');
			dojo.style(this.domCalendar, 'top', mb.y + mb.h + 'px');
			var div = dojo.create('div', {}, this.domCalendar);

			// Créer le calendrier
			this.objCalendar = new dijit._Calendar({}, div);
			if (this.objLastDate) {
				this.objCalendar.attr('value', this.objLastDate);
			}

			// Connection aux événements
			var handler = dojo.connect(this.objCalendar, 'onFocus', this, this.onCalendarFocus);
			this.arrHandler.push(handler);
			handler = dojo.connect(this.objCalendar, 'onBlur', this, this.onCalendarBlur);
			this.arrHandler.push(handler);
			handler = dojo.connect(this.objCalendar, 'onValueSelected', this, this.onValueSelected);
			this.arrHandler.push(handler);
		}

		if( this.intFocus == 0 && this.objCalendar != null )
		{
			var h, i;
			// Destruction des connecteurs sur le calendrier
			while(h = this.arrHandler.pop() && i < 3) {
				dojo.disconnect(h);
				i++;
			}
			// Destruction du calendrier
			this.objCalendar.destroy();
			this.objCalendar = null;
			dojo.destroy(this.domCalendar);
			this.domCalendar = null;
		}
	}
});

actiJSCoreParser.addDefinition('.jsCalendar', form.date, {});
