var Cnaptar = new Class({
	mDays : [31,28,31,30,31,30,31,31,30,31,30,31],
	currentdate: null,
	mindate: null,
	maxdate: null,
	cmoocal: null,
	name: null,
	Oevho: null,
	Onap: null,
	maxDiff: null,
	onChangeCallback: null,
	/**
	* A konstruktor, parametere: 
	* name: a cnaptar neve
	* Oevho: az ev+honap valaszto SELECT element
	* Onap: a nap valaszto SELECT element
	* Ocalendar: a naptarhoz tartozo DIV element
	* mindate: a minimum valaszthato datum erteke tombben, pl: [2009, 6, 2]
	* mindate: a minimum valaszthato datum erteke tombben, pl: [2009, 6, 2]
	* onChangeCallback: valtozas eseten meghivando visszahivasi fuggveny
	*/
	initialize: function(name, Oevho, Onap, Ocalendar, mindate, maxdate, onChangeCallback){
		this.name=name;
		this.Oevho=Oevho,
		this.Onap=Onap,
		this.Ocalendar=Ocalendar,
		this.mindate=$A(mindate),
		this.currentdate=$A(mindate),
		this.maxdate=$A(maxdate),
		this.maxDiff = datum.daysBetweenDates(this.mindate, this.maxdate);
		this.Oevho.addEvent('change',this.evhoChange.bind(this));
		this.Onap.addEvent('change',this.napChange.bind(this));

		if ($type(onChangeCallback) == "function") this.onChangeCallback=onChangeCallback;
		
		this.fillYearMonths();
		this.fillDays();
		if (this.name != null && this.Ocalendar != null) {
			this.cmoocal = new CmooCalendar(this.name,this.Ocalendar,this.mindate[0],this.mindate[1],this.mindate[2],this.onPick.bind(this));
			this.Ocalendar.getElement('a').addEvent('click',function(oEvent){
				this.cmoocal.setMinDate(this.mindate[0], this.mindate[1], this.mindate[2]);
				this.cmoocal.setMaxDate(this.maxdate[0], this.maxdate[1], this.maxdate[2]);
				this.cmoocal.setDate(this.currentdate[0],this.currentdate[1],this.currentdate[2]);
				this.cmoocal.refresh();
				this.cmoocal.show();
				oEvent.stop();
				return false;
			}.bind(this));
		}
	},
	setDate: function(mydate){
		this.currentdate=$A(mydate);
		this.fillYearMonths();
		this.fillDays();
		if ($chk(this.onChangeCallback)) this.onChangeCallback();
	},
	getDate: function(){
		return $A(this.currentdate);
	},
	dateToEVHOtext: function(mydate){
		return ''+mydate[0]+'. '+_ho[mydate[1]]['hu'];
	},
	dateToEVHOvalue: function(mydate){
		return ''+mydate[0]+(mydate[1]<10?'0':'')+mydate[1];
	},
	fillYearMonths: function(){
		var tempdate = $A(this.mindate);
		tempdate[2]=1;
		this.Oevho.empty();
		while (this.dateToStamp(tempdate) <= this.dateToStamp(this.maxdate)){
			new Element('option',{'html':this.dateToEVHOtext(tempdate), 'value':this.dateToEVHOvalue(tempdate), 'selected':(this.dateToEVHOvalue(this.currentdate) == this.dateToEVHOvalue(tempdate))}).inject(this.Oevho);
			if (++tempdate[1]>12) {tempdate[1]=1; tempdate[0]++;}
		}
	},
	fillDays: function(){
		if (datum.dateToStamp(this.currentdate)<datum.dateToStamp(this.mindate)) this.currentdate=$A(this.mindate);
		if (datum.dateToStamp(this.currentdate)>datum.dateToStamp(this.maxdate)) this.currentdate=$A(this.maxdate);
		this.mDays[1] = (datum.isLeap(this.currentdate[0]) ? 29 : 28);
		this.Onap.empty();
		for (var i=1; i<=this.mDays[this.currentdate[1]-1]; i++){
			if (this.dateToStamp([this.currentdate[0],this.currentdate[1],i])>(this.dateToStamp(this.maxdate))) break;
			if (this.dateToStamp([this.currentdate[0],this.currentdate[1],i])<(this.dateToStamp(this.mindate))) continue;
			new Element('option',{'html':(i<10?'0':'')+i, 'value':i, 'selected':(this.currentdate[2] == i)}).inject(this.Onap);
		}
	},
	evhoChange : function(){
		this.currentdate[0]=Number(this.Oevho.get('value').substr(0,4));
		this.currentdate[1]=Number(this.Oevho.get('value').substr(4,2));
		this.fillDays();
		if ($chk(this.onChangeCallback)) this.onChangeCallback();
	},
	napChange : function(){
		this.currentdate[2]=Number(this.Onap.get('value'));
		if ($chk(this.onChangeCallback)) this.onChangeCallback();
	},
	dateToStamp: function(mydate){
		return Number(''+mydate[0]+(mydate[1]<10?'0':'')+mydate[1]+(mydate[2]<10?'0':'')+mydate[2]);
	},
	setNewRange: function(mindate,nights){
		this.mindate=$A(mindate);
		this.maxdate=$A(datum.shiftDate(mindate,this.maxDiff));
		this.currentdate=$A(datum.shiftDate(mindate,nights-1));
		this.fillYearMonths();
		this.fillDays();
	},
	onPick: function(ev,ho,nap){
		this.currentdate=[ev,ho,nap];
		this.Oevho.set('value',this.dateToEVHOvalue([ev,ho,nap]));
		this.fillDays();
		this.Onap.set('value',nap);
		if ($chk(this.onChangeCallback)) this.onChangeCallback();
	}
});

