DateSelector=function(id,options){
	this.id=id;
	this.date=new Date();
	this.Config = {
                     months:		['stycze&#324;','luty','marzec','kwiecie&#324;','maj','czerwiec','lipiec','sierpie&#324;','wrzesie&#324;','pa&#378;dziernik','listopad','grudzie&#324;'],
					 days:			['nd','pn','wt','&#347;r','cz','pt','sb'],
					 select_class:	null,
					 start_year:	1900,
					 end_year:		this.date.getFullYear(),
					 
    };
    if(options)
    {
		jQuery.extend(this.Config, options);
	};
	this.select={
		year:null,
		month:null,
		day:null,
	};
	this.input=null;
	
	this.initDate=function(time){
		this.date=new Date(time);
	}
	this.isRokPrzestepny=function(Y){
	  return ((Y % 4 == 0) && ((Y % 100 != 0) || (Y % 400 == 0)));
	}
	this.ileDniMaMiesiac=function(Y,m){
		if(m==1)
			return this.isRokPrzestepny(Y)?29:28;
		return this.xor((m>6),(m%2==0))?31:30;
	}	
	this.xor=function(a, b){
		return (( a && !b ) || ( !a && b ));
	}
	this.showYears=function(){
		this.select.year.empty();
		for(var i=this.Config.start_year;i<=this.Config.end_year;i++){
			this.select.year.append($('<option></option>').html(i).val(i).attr("selected",this.date.getFullYear()==i?'selected':''));
		}
	}
	this.showMonths=function(){
		this.select.month.empty();
		for(var i=0;i<12;i++){
			this.select.month.append($('<option></option>').html(this.Config.months[i]).val(i).attr("selected",this.date.getMonth()==i?'selected':''));
		}
	}
	this.showDays=function(){
		this.select.day.empty();		
		var iter_date=new Date();
		iter_date.setTime(this.date.getTime());
		for(var i=0;i<this.ileDniMaMiesiac(this.date.getFullYear(),this.date.getMonth());i++){
			iter_date.setDate(i+1);
			this.select.day.append($('<option></option>').html(iter_date.getDate()+' ('+this.Config.days[iter_date.getDay()]+')').val(iter_date.getDate()).attr("selected",this.date.getDate()==i+1?'selected':'').css('color',(iter_date.getDay()==0?'#fa0000':'#000')));
		}
	}
	this.updateDate=function(){
		if(this.ileDniMaMiesiac($('#year_'+this.id).val(),$('#month_'+this.id).val())<$('#day_'+this.id).val())
			day=1;
		else
			day=$('#day_'+this.id).val();
		this.date=new Date($('#year_'+this.id).val(),$('#month_'+this.id).val(),day);
		this.input.val(this.date.getTime());
	}
	this.getDate=function(){
		return this.date.getFullYear()+'-'+(this.date.getMonth()+1)+'-'+this.date.getDate();
	}
	this.show=function(){
		tmp_this=this;
		this.select.day=$('<select id="day_' + this.id + '"></select>').change(function(){
			tmp_this.updateDate();
		});
		$('#'+this.id).append(this.select.day);
		this.showDays();
		
		$('#'+this.id).append(' - ');		
		
		this.select.month = $('<select id="month_' + this.id + '"></select>').change(function(){
				tmp_this.updateDate();
				tmp_this.showDays();
			});
		$('#'+this.id).append(this.select.month);
		this.showMonths();
		
		$('#'+this.id).append(' - ');
		
		this.select.year = $('<select id="year_' + this.id + '"></select>').change(function(){
				tmp_this.updateDate();
				tmp_this.showDays();
			});
		$('#'+this.id).append(this.select.year);
		this.showYears();
		
		this.input=$('<input name="datetime_'+this.id+'" type="hidden">').val(this.date.getTime());
		$('#'+this.id).append(this.input);		
	}
}

