/**
 * A repjegyes utasszam kivalasztokat kezelo osztaly
 */
var CmooPartsSelects = new Class ({
	MAX_PARTS : 9,
	adults : 1,
	children : 0,
	infants : 0,
	aSel : null,
	cSel : null,
	iSel : null,
	oCont : null,
	initialize : function (oCont) {
		if ($chk($(oCont))) {
			this.oCont = $(oCont);
		}
		var sL = null;
		if (sL = $(oCont).getElements('select')) {
			for (var i= 0; i < sL.length; i++) {
				switch(sL[i].get('name')) {
				case 'adults':
					this.aSel = sL[i];
					break;
				case 'children':
					this.cSel = sL[i];
					break;
				case 'infants':
					this.iSel = sL[i];
					break;	
				}
			}
		}
		else {
			var s = null
			if ($chk(s = $('felnott'))) {
				this.aSel = s;
			}
			if ($chk(s = $('gyerek'))) {
				this.aSel = s;
			}
			if ($chk(s = $('csecsemo'))) {
				this.aSel = s;
			}
		}
		if ($chk(this.aSel)) {
			this.aSel.addEvent('change',this._onSelect.bind(this));
		}
	},
	_onSelect : function (oe) {
		var s = oe.target;
		var n = 0;
		if (s == this.aSel) {
			n = this._getPCount(s);
			this._fillSelect(this.iSel,n);
			this._fillSelect(this.cSel, this.MAX_PARTS - n);
		}
		else if (s == this.cSel) {
			
		}
	},
	_fillSelect : function (s,max) {
		if ($chk($(s))) {
			var curr = this._getPCount(s);
			var i = 0;
			var ol = $(s).getElements('option');
			if ($chk(arguments[2])) {
				curr = arguments[2];
			}
			if (max >= ol.length) {
				var op = null;
				for (i = ol.length; i <= max;i++) {
					op = new Element('option',{html:i,value:i});
					op.inject(s);
				}
			}
			else if (max < ol.length) {
				for (i = ol.length-1; i > 0; i--) {
					if (ol[i].get('value') > max) {
						ol[i].destroy();
					}
				}
			}
			if (s.getElements('option').length > curr) {
				s.set('selectedIndex',curr);
			}
		}
	},
	/**
	 * egy select elem kivalasztott mennyiseget kerdezi le.
	 */
	_getPCount : function (s) {
		r = 0;
		if ($chk($(s))) {
			var l;
			if ($chk(l = $(s).getSelected()) && l.length) {
				r = l[0].get('value').toInt();
			}
		}
		return r;
	},
	/**
	 * osszeszamolja a kivalasztott utasszamot.
	 */
	getPartsCount : function () {
		return this._getPCount(this.aSel)+this._getPCount(this.cSel)+this._getPCount(this.iSel);
	}
 });
