/**
 * @author Rubén García
 * @usage InputWidgetBirthday(String elementName, Object options)
 * @example
 * <script type="text/javascript">
 * // <![CDATA[
 * var iW1 = new InputWidgetBirthday('bday');
 * var iW2 = new InputWidgetBirthday('bday2', {updateDaysAccordingToMonth: true});
 * // ]]>
 * </script>
 */


InputWidgetBirthday = Class.create();

InputWidgetBirthday.prototype= {
	initialize: function(element, options){
		options = options || {};
		this.input = $(element);
		if(!this.input) return;
		this.updateDaysAccordingToMonth = options.updateDaysAccordingToMonth || false;
		this.months = options.months || { '01': { days: 31, name:'01'}, '02': { days: 29, name:'02'}, '03': { days: 31, name:'03'}, '04': { days: 30, name:'04'}, '05': { days: 31, name:'05'}, '06': { days: 30, name:'06'}, '07': { days: 31, name:'07'}, '08': { days: 31, name:'08'}, '09': { days: 30, name:'09'}, '10': { days: 31, name:'10'}, '11': { days: 30, name:'11'}, '12': { days: 31, name:'12'} };
		this.years = options.years || { start: 1900, end: 2000 };
		this.bDayName = options.bDayName || element + 'bDay';
		this.bMonthName = options.bMonthName || element + 'bMonth';
		this.bYearName = options.bYearName || element + 'bYear';
		this.bDayHintName = options.bDayHintName || element + 'bDayHint';

		this.updateDaysListener = this.updateDays.bindAsEventListener(this);
		this.writeBackListener = this.writeBack.bindAsEventListener(this);

		this.replaceBirthday();
	},
	createDay: function(days) {
		var bDay = document.createElement('select');
		bDay.setAttribute('id', this.bDayName);
		for(var i = 1; i <= days; i++) {
			var d = document.createElement('option');
			d.setAttribute('value', i<10?'0'+i:i);
			d.appendChild(document.createTextNode(i));
			bDay.appendChild(d);
		}
		return bDay;
	},
	createMonth: function() {
		var bMonth = document.createElement('select');
		bMonth.setAttribute('id',this.bMonthName);
		for(var i in this.months) {
			var d = document.createElement('option');
			d.setAttribute('value', i);
			d.appendChild(document.createTextNode(this.months[i].name));
			bMonth.appendChild(d);
		}
		return bMonth;
	},
	createYear: function() {
		var bYear = document.createElement('select');
		bYear.setAttribute('id',this.bYearName);
		for(var i = this.years.end; i >= this.years.start; i--) {
			var d = document.createElement('option');
			d.setAttribute('value', i);
			d.appendChild(document.createTextNode(i));
			bYear.appendChild(d);
		}
		return bYear;
	},
	updateDays: function() {
		if (navigator.userAgent.indexOf("Firefox")!=-1) {
			$(this.bDayName).update(this.createDay(this.months[$(this.bMonthName).childNodes[$(this.bMonthName).selectedIndex].value].days).innerHTML);
		}
		this.writeBack();
	},
	writeBack: function() {
		this.input.value = $F(this.bDayName) + '.' + $F(this.bMonthName) + '.' + $F(this.bYearName);
	},
	replaceBirthday: function() {
		this.input.hide();
		if($(this.bDayHintName)) $(this.bDayHintName).hide();
		var bDay = this.createDay(31);
		var bMonth = this.createMonth();
		var bYear = this.createYear();
		this.input.up().insertBefore(bDay,this.input);
		this.input.up().insertBefore(bMonth,this.input);
		this.input.up().insertBefore(bYear,this.input);
		$(bDay).observe('change', this.writeBackListener);
		if(this.updateDaysAccordingToMonth) { $(bMonth).observe('change', this.updateDaysListener); }
		$(bYear).observe('change', this.writeBackListener);
		if(this.input.value != '') {
			var date = [];
			var i = 0;
			var bDay = $(this.bDayName);
			var bMonth = $(this.bMonthName);
			var bYear = $(this.bYearName);
			this.input.value.scan(/\w+/, function(match){date[i++] = match[0];})
			for(i = 0; i < bDay.length; i++) {
				if(bDay.childNodes[i].value == date[0])
				bDay.childNodes[i].selected = 'selected';
			}
			for(i = 0; i < bMonth.length; i++) {
				if(bMonth.childNodes[i].value == date[1])
					bMonth.childNodes[i].selected = 'selected';
			}
			for(i = 0; i < bYear.length; i++) {
				if(bYear.childNodes[i].value == date[2])
					bYear.childNodes[i].selected = 'selected';
			}
			/*
				$(bDay).childNodes[d].selected = 'selected';
				$(bMonth).childNodes[m].selected = 'selected';
				$(bYear).childNodes[y].selected = 'selected';
			*/
		}
		this.writeBack(); // prefill to prevent empty sending
	}
}