nmdgf.widgets.DatePicker = {
		
	calendar : null,
	
	id : 'date-calendar', 
		
	container : 'date-calendar-container',
	
	activeInput : null,
		
	initialize : function() {
		var icons = nmdgf.query('.date-icon');
		nmdgf.addListener(icons, 'click', this.click, this);
		this.renderContainer();
		
		new YAHOO.widget.Tooltip('calendar-tooltip', { 
			context: icons,
			autodismissdelay: 20000
		});
	},
	
	renderContainer : function() {
		var container = document.createElement('div');
		container.id = this.container;
		container.style.display = 'none';
		document.body.appendChild(container);
	},
	
	click : function(e) {
		this.activeInput = nmdgf.byEvent(e).getAttribute('field');
		var input = nmdgf.byId(this.activeInput);
		if(input.disabled === false && input.readOnly === false) {
			if(this.calendar === null) {
				this.renderCalendar();
			}
			this.setCurrentDate();
			this.calendar.show(); 
			this.positionCalendar();
		}
	},
	
	setCurrentDate : function() {
		var date = Date.parse(nmdgf.byId(this.activeInput).value);
		if(date !== null) {
			this.calendar.select(date);
			this.calendar.setMonth(date.getMonth());
			this.calendar.setYear(date.getFullYear());
			this.calendar.render();
		}else {
			this.calendar.clear();
		}
	},
	
	renderCalendar : function() {
		this.calendar = new YAHOO.widget.Calendar(this.id, this.container, { title:'Choose a date:', close:true, navigator: true } );
		this.calendar.selectEvent.subscribe(this.populateDateField, this, true);
		this.calendar.render();
	},
	
	positionCalendar : function() {
		var position = YAHOO.util.Dom.getXY(this.activeInput);
		position[1] = position[1] + 25;
		YAHOO.util.Dom.setXY(this.container, position);
	},
	
	populateDateField : function() {
		var date = this.calendar.getSelectedDates()[0];
		nmdgf.byId(this.activeInput).value = date.toString('MM/dd/yyyy');
		this.calendar.hide();
	},
	
	hide : function() {
		if(this.calendar !== null) {
			this.calendar.hide();
		}
	}
	
};
nmdgf.registerWidget(nmdgf.widgets.DatePicker);
