var ControlGroup=Class.eventClass(); 
Object.extend (ControlGroup.prototype, {
	
	initialize: function (f_gi) {
		this.items = $A();
		this.f_jv = $A();
	
		f_gi.each(function (pair) {
			switch (pair.key) {
				case "datepicker":
						this.f_eW(pair.value);
					break;
				case "cityinput":
						this.f_fe(pair.value);
					break;
				case "coolselect":
						this.f_eV(pair.value);
					break;
				case "htmlformelement":
						this.f_dS(pair.value);
				default:
			}
		}.bind(this));
		this.f_bD = this.checkAll.bind(this);
	},
	f_dS: function (f_ck) {
		f_ck.each(function(f_bs) {
			var f_cZ = $(f_bs.id);
			if (f_cZ) {
				this.items.push(f_cZ);
				if (!f_cZ.name) f_cZ.name=f_cZ.id;
			}
		}.bind(this));
	},
	f_eV : function (f_ck) {
		f_ck.each (function (f_bs) {
			if ($(f_bs.hostDiv)) {
				var f_hl = new CoolSelect( f_bs.id, f_bs.templateObj, f_bs.hostDiv,  f_bs.options);
				this.items.push(f_hl);
			}
		}.bind(this));
	},
	f_fe	: function (f_ck) {
		f_ck.each(function(f_aa) {				
			var f_c = $(f_aa.id)
			if (!f_c)  return;
   		if (!f_c.name)  f_c.name = f_aa.id
   		this.items.push(f_c);
			var f_hC = new CityInput(f_aa.id, f_aa.options);
			this.f_jv.push(f_hC);
		}.bind(this));
	},
	getCityList : function() {
		return this.f_jv;
	},
	f_eW : function (f_ck) {
		f_ck.each(function(f_aa) {		
			var f_c = $(f_aa.targetInput);
			if (!f_c)  return;
   		if (!f_c.name)  f_c.name = f_aa.targetInput;
   		this.items.push(f_c);
			var f_eu = new DatePicker(
        f_aa.targetInput,
        f_aa.template_container,
        f_aa.template_main,
        System.language._DATEPICKER,
        {   
          today: SERVER_TIME,
          maxDate:90,
          initDate: f_aa.dateOffset,
		  minDate:f_aa.minDate,
          specialDates:System.language._DATEPICKER_SPECIAL.specialDates,
          specialTipInfo:System.language._DATEPICKER_SPECIAL.specialTipInfo,
          pickerImg:f_aa.pickerImg,
          offsetTop:2
        },
        f_aa.id
        
   		);
   		if(f_aa.linkDP){
	    	Event.observe(f_eu,'changed',function(f_gj, f_gD, f_i){
	        var f_cm=$(f_gD).datepickerObj;
	        if(!f_cm){
	          return;
	        }
	        f_cm.setMinDate(new Date(f_i.getTime()));
	        f_cm.setMaxDate(new Date(f_i.getTime()+f_cm.dateOffset));
		       if(!f_cm.checkRange(f_cm.getValue())){
	          f_cm.setValue(new Date(f_i.getTime() + f_gj*1000*60*60*24));
	        }
	        f_cm.update(f_i);
	      }.bind(f_eu, f_aa.linkOffset, f_aa.linkDP));
	    }
		
		if (f_aa.specLinkDp) {
			DatePicker.departureDate = f_eu.getValue();
		} else {
			DatePicker.arrivalDate = f_eu.getValue();
		}
		
		Event.observe(f_eu,'changed',function(f_i) {
			if (f_aa.specLinkDp) {
				DatePicker.departureDate = f_i;
			} else {
				DatePicker.arrivalDate = f_i;
			}
		});
		
		if (!f_aa.calendarMode)	{
			f_aa.calendarMode = 1;
		}
		DatePicker.setCalendarType(f_aa.calendarMode);
		
		}.bind(this));
	
	},
	checkAll: function() {
	var f_eg = true;
		this.items.each(function(f_e) {
			if (f_e.validate) {
				
				if (f_e.asyncValidate && !f_e.validate()) Event.observe(f_e, "validatecomplete",this.f_bD);
				if (!f_e.validate()) {
					f_eg = false;
				} else {
					if (f_e.asyncValidate) Event.stopObserving(f_e, "validatecomplete",this.f_bD,true);
				}  
			}
		}.bind(this));
		if (f_eg)	{
			Event.fire(this, "checkpassed");
			return true;
		} else {
			Event.fire(this,"checkfailed");
			return false;
		}		
		return false;
	},
	serialize: function() {
    var data = this.items.inject({}, function(result, element) {
      var key = element.name, value = this.f_gz(element);
			if (typeof(value) == "string") value=value.trim();
      if (value != null) {
       	if (key in result) {
          if (result[key].constructor != Array) result[key] = [result[key]];
          result[key].push(value);
        }
        else result[key] = value;
      }
      return result;
    }.bind(this));
		return Hash.toQueryString(data);
	},
	f_gz: function(f_e) {
		if (f_e.tagName) return $F(f_e);
		else return f_e.value;
	}
});