
/**
 *
 * VSR-IBE-Selection
 * V 1.1.2
 * holger.kraemer@kn-media.de
 *
 *
 */

var VsrIbeSelection = Class.create();

VsrIbeSelection.prototype	= {
 Version	: '1.1.2',
 _relative	: null,
 _zindex	: 1,
 _formId	: null,
 _selectorId_num_adults : null,
 _selectorId_num_infants : null,
 _selectorId_age_infant1 : null,
 _selectorId_age_infant2 : null,
 _selectorId_age_infant3 : null,
 _selectorId_age_infant4 : null,
 _selectorId_departure : null,
 _selectorId_departureDate : null,
 _selectorId_duration : null,
 //
 _minPersons : '2',
 _maxPersons : '4',
 _minAdults : 1,
 _maxAdults : 3,
 _minInfants : 1,
 _maxInfants : 2,
 //
 _currentSelection : $H({
					"numAdults":"2",
					"numInfants":"0"
					}),
  _departureDateDurationOptionData : null,
 _selectOptions : $H({
					"minAdults":"1", 
					"maxAdults":"3",
					"minInfants":"0", 
					"maxInfants":"2"
					}),


	/* init */
	initialize	: function( h_p ) {

		/* arguments */
		if (h_p["formId"]) { this._formId = h_p["formId"]; }
		if (h_p["selectorId_num_adults"]) { this._selectorId_num_adults = h_p["selectorId_num_adults"]; }
		if (h_p["selectorId_num_infants"]) { this._selectorId_num_infants = h_p["selectorId_num_infants"]; }
		if (h_p["selectorId_age_infant1"]) { this._selectorId_age_infant1 = h_p["selectorId_age_infant1"]; }
		if (h_p["selectorId_age_infant2"]) { this._selectorId_age_infant2 = h_p["selectorId_age_infant2"]; }
		if (h_p["selectorId_age_infant3"]) { this._selectorId_age_infant3 = h_p["selectorId_age_infant3"]; }
		if (h_p["selectorId_age_infant4"]) { this._selectorId_age_infant4 = h_p["selectorId_age_infant4"]; }
		if (h_p["selectorId_departure"]) { this._selectorId_departure = h_p["selectorId_departure"]; }
		if (h_p["selectorId_departureDate"]) { this._selectorId_departureDate = h_p["selectorId_departureDate"]; }
		if (h_p["selectorId_duration"]) { this._selectorId_duration = h_p["selectorId_duration"]; }
		if (h_p["numAdults"]) { this._currentSelection.numAdults = h_p["numAdults"]; }
		if (h_p["numInfants"]) { this._currentSelection.numInfants = h_p["numInfants"]; }
		if (h_p["departureDateDurationOptionData"]) { this._departureDateDurationOptionData = h_p["departureDateDurationOptionData"]; }

		if (h_p["minPersons"]) { this._minPersons = h_p["minPersons"]; }
		if (h_p["maxPersons"]) { this._maxPersons = h_p["maxPersons"]; }
 
		if (h_p["minAdults"]) { this._minAdults = h_p["minAdults"]; }
		if (h_p["maxAdults"]) { this._maxAdults = h_p["maxAdults"]; }
		if (h_p["minInfants"]) { this._minInfants = h_p["minInfants"]; }
		if (h_p["maxInfants"]) { this._maxInfants = h_p["maxInfants"]; }

		 
		Event.observe($(this._selectorId_num_adults), 'change', function(){
			this._numPersonsChanged();
		}.bind(this));
		//@todo:
		Event.observe($(this._selectorId_departure), 'change', function(){
			this._updateDateDurationOptions();
		}.bind(this));
		Event.observe($(this._selectorId_departureDate), 'change', function(){
			this._updateDurationOptions();
		}.bind(this));
		
		this._updateInfantSelectors();

	},

	_numPersonsChanged : function() {

		this._updateInfantSelectors();
		this._updateDepartureDateDurationOptions();

	},
	
	_updateDepartureDateDurationOptions: function() {

		//console.log( "_updateDepartureDateDurationOptions");
		
		$(this._selectorId_departure).update();

		//add departure-options:
		this._departureDateDurationOptionData.keys().each(function(elementKey) {
			var departureOptionLabel = this._departureDateDurationOptionData.get(elementKey).label;
			//@todo: mark selection: // , 'selected': 'selected'
			var departureOption = new Element('option', {'value' : elementKey});
			$(this._selectorId_departure).appendChild(departureOption).update( departureOptionLabel );			
		}.bind(this));	
		
		this._updateDateDurationOptions();
	},

	_updateDateDurationOptions: function() {

		//console.log( "_updateDateDurationOptions");
		
		$(this._selectorId_departureDate).update();
		
		var currentDepartureKey = $F(this._selectorId_departure);
		
		var departureDateOptionList = $H();
		var currentDepartureOptionList = $H(this._departureDateDurationOptionData.get(currentDepartureKey)).get('departureDate');

		$H(currentDepartureOptionList).each(function(elementKey) {
			departureDateOptionList.set(elementKey.key,elementKey.key);
		}.bind(this));
		
		departureDateOptionList.each(function(element) {
			//@todo: mark selection: // , 'selected': 'selected'
			var departureDateLabel = element.key.substr(8,2)+'.'+element.key.substr(5,2)+'.'+element.key.substr(0,4);
			var departureDateValue = departureDateLabel;
			var departureDateOption = new Element('option', {'value' : departureDateValue});
			//$(this._selectorId_departureDate).appendChild(departureDateOption).update( element.value );	
			$(this._selectorId_departureDate).appendChild(departureDateOption).update( departureDateLabel );	
		}.bind(this));
		
		this._updateDurationOptions();
		
	},

	_updateDurationOptions: function() {

		$(this._selectorId_duration).update();
		
		var currentDepartureKey = $F(this._selectorId_departure);
		var tempSelectedDateHR = $F(this._selectorId_departureDate);
		var currentDepartureDateKey = tempSelectedDateHR.substr(6,4)+'-'+tempSelectedDateHR.substr(3,2)+'-'+tempSelectedDateHR.substr(0,2);
		var currentDurationOptionList = $H($H(this._departureDateDurationOptionData.get(currentDepartureKey)).get('departureDate')).get(currentDepartureDateKey);
		
		currentDurationOptionList.each(function(element) {
			//@todo: mark selection: // , 'selected': 'selected'
			var durationOption = new Element('option', {'value' : $H(element).get('value')});
			var durationOptionLabel = $H(element).get('value') + ' ' + (($H(element).get('value') > 1 ) ? '&Uuml;bernachtungen' : '&Uuml;bernachtung');
			//$(this._selectorId_duration).appendChild(durationOption).update( $H(element).get('label') );				
			$(this._selectorId_duration).appendChild(durationOption).update( durationOptionLabel );				
		}.bind(this));
		
		
	},
	
	_updateInfantSelectors: function() {

		$$("form#"+this.formId+" div.ibeForm-age-infant").each(function(element) {
			element.setStyle({
						'display' : 'none'
							});
		});
		
		var maxInfants = this._maxPersons - $F(this._selectorId_num_adults);

		for(var i = 1; i<=maxInfants; i++) {
			if($(this._formId+"-age-infant" + i + "-wrap")) {
				$(this._formId+"-age-infant" + i + "-wrap").setStyle({
															'display' : 'block'
																});
			}
		}
				
	}

};
