/**
* JavaScript source for {@link WidgetDate}.
* @package Api
* @subpackage Form
* @author Tõnis Tiigi
* @author Pavel Jartsev <papi@digitalfruit.ee>
*/

var WidgetDate = Class.create();
WidgetDate.prototype = {

	initialize : function(element,options){
		
		this.element     = $(element);

		this.el_day = $(element+"_day");	
    	this.el_month = $(element+"_month");	
    	this.el_year = $(element+"_year");	
    	
    	var val = this.element.value;
    	if(val.length){
    		var parts = val.split('-');
    		if(parts.length>2){
    			this.el_year.value = parseInt(parts[0],10);
    			this.el_day.value = parseInt(parts[2],10);
    			this.el_month.selectedIndex = parseInt(parts[1],10)-1;
    		}
    	}	
    	
    	
	 	Element.observe(this.el_day,"change",this.update.bind(this));
	 	Element.observe(this.el_month,"change",this.update.bind(this));
	 	Element.observe(this.el_year,"change",this.update.bind(this)); 	
	 	
	 	
	},
	update : function(){
		this.element.value=this.getYear()+'-'+this.getMonth()+'-'+this.getDay();
	},
	getYear : function(){
		return this.el_year.options[this.el_year.selectedIndex].text;
	},
	getMonth : function(){
		var m = this.el_month.selectedIndex+1;
		if(m<10) m="0"+m;
		return m;
	},
	getDay : function(){
		var d = this.el_day.value;
		if(d<10) d="0"+d;
		return d;
	}
	
}