dojo.provide("UI.Picker.Date");


dojo.declare("UI.Picker.Date", null, {
	constructor: function(container) {
		this.container = container;
		this.month     = dojo.query(".dp-month", container)[0];
		this.day       = dojo.query(".dp-day",   container)[0];
		this.year      = dojo.query(".dp-year",  container)[0];
	},
	
	getMonth: function() {
		return this.month.value;
	},
	
	setMonth: function(v) {
		this.month.value = v;
		return this;
	},
	
	getDay: function() {
		return this.day.value;
	},
	
	setDay: function(v) {
		this.day.value = v;
		return this;
	},
	
	getYear: function() {
		return this.year.value;
	},
	
	setYear: function(v) {
		this.year.value = v;
		return this;
	},
	
	getDate: function() {
		var y    = this.getYear(),
			m    = this.getMonth() - 1,
			d    = this.getDay(), 
			date = new Date(y, m, d);
		
		// If a date is out of range, e.g.: new Date(2010, 1, 500), it will be changed
		return date && date.getFullYear() == y && date.getMonth() == m && date.getDate() == d
			? date
			: false;
	},
	
	setDate: function(m, d, y) {
		if ( arguments.length == 1 ) {
			d = m.getDate();
			y = m.getFullYear();
			m = m.getMonth();
		}
		
		this.setMonth(m);
		this.setDay(d);
		this.setYear(y);
		
		return this;
	},
	
	getNodes: function() {
		return {
			month:     this.month,
			day:       this.day,
			year:      this.year,
			container: this.container
		};
	},
	
	isValid: function() {
		return this.getDate() !== false;
	},
	
	isToday: function() {
		return this.isValid() && +this.getDate() == +this.getToday();
	},
	
	isFuture: function() {
		return this.isValid() && +this.getDate() > +this.getToday(); 
	},
	
	isPast: function() {
		return this.isValid() && +this.getDate() < +this.getToday(); 
	},
	
	getToday: function() {
		var today = new Date();
		return new Date(today.getFullYear(), today.getMonth(), today.getDate());
	},
	
	enable: function() {
		dojo.removeClass(this.container, "disabled");
		this.month.disabled = false;
		this.day.disabled   = false;
		this.year.disabled  = false;
		
		return this;
	},
	
	disable: function() {
		dojo.addClass(this.container, "disabled");
		this.month.disabled = true;
		this.day.disabled   = true;
		this.year.disabled  = true;
		
		return this;
	}
});
