HotelSpecialsController = function() {
	HotelSpecialsController.superClass.apply( this, arguments );

	this.searchUrl = '/hotel/search/';
	this.calendar = new Calendar( this.getEl( 'calendar-wrap' ), this.Drag, [
		'Укажите дату вылета в календаре',
		'Укажите дату прилета в календаре',
		'Укажите дату вылета и прилета в календаре'
	], new Date(), new Date( new Date().valueOf() + 364 * 24 * 60 * 60 * 1000 ) );
	this.calendar.changeMode( 2 );
	this.calendar.attachObserver( 'calendarStatusChange', this.check, this );
	this.calendar.attachObserver( 'daySelect', this.setDate, this );

	this.persons = new specialHotelPersons( this.getEl( 'persons' ) );
	this.form = this.createForm( hotelSpecialsForm, this.getEl( 'form' ) );
	this.pad = new Info();
	this.pad.setClosable( true );
	var formWrap = {
		element: this.getEl( 'special_controls' )
	}
	this.pad.fill( formWrap );
	var button = this.getEl( 'show_form' ).firstChild;
	this.addHandler( button, 'click', function() {
		this.pad.show();
	});
}
HotelSpecialsController.inheritsFrom( Controller );

/* -------------------------------------------------------------------------- */

HotelSpecialsController.prototype.send = function() {
	var oParams = new Object();
	var formResult = this.form.getParams();
	var calendarResult = this.calendar.getParams();
	var persons = this.persons.getValue();

	oParams[ 'CheckIn' ] = formatDate( calendarResult[0].date );
	oParams[ 'CheckOut' ] = formatDate( calendarResult[1].date );

	this.searchStore.addParams({ 'Commander.Command': 'SearchHotelTariffs' });
	this.searchStore.addParams( persons );
	this.searchStore.addParams( formResult );
	this.searchStore.addParams( oParams );
	this.searchStore.load();
}

//----------------------------------------------------------------------------//

HotelSpecialsController.prototype.checkState = function( result ) {
	if( Boolean( result[ 'CommandId' ] ) ) {
		this.fillForm(
			this.searchUrl,
			{
				'SearchId': result[ 'CommandId' ],
				'SpecialId': window[ 'specialId' ]
			}
		);
		this.submit();
	} else {
		alert( 'Извините, произошла ошибка. Попрбуйте ещё раз.' );
	}
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

hotelSpecialsForm = function() {
	hotelSpecialsForm.superClass.apply( this, arguments );
	this.labels = new Array();
	
	this.setInput( window.citySet, 'Где' );
	var newLabel = this.setLabel();
	newLabel.setContent( 'дата заезда' );
	var newLabel = this.setLabel();
	newLabel.setContent( 'дата выезда' );
	this.setClassName( 'return', true, newLabel.ico );
	this.check();
}
hotelSpecialsForm.inheritsFrom( Form );

/* -------------------------------------------------------------------------- */

hotelSpecialsForm.prototype.setInput = function( data, tip ) {
	var newInput = new Select( this.element, data );
	newInput.setTip( tip );

	newInput.attachObserver( 'inputError', this.setError, this );
	newInput.attachObserver( 'inputStatusChange', this.onInputChange, this );
	newInput.attachObserver( 'inputSetted', this.onInputSet, this );
	
	this.inputs[ this.inputs.length ] = newInput;
	return newInput;
}

/* -------------------------------------------------------------------------- */

hotelSpecialsForm.prototype.setLabel = function() {
	var newLabel = new Label( this.labels.length, this.element );
	newLabel.idx = this.labels.length;
	this.labels[ this.labels.length ] = newLabel;
	return newLabel;
}

/* -------------------------------------------------------------------------- */

hotelSpecialsForm.prototype.getParams = function() {
	var result = new Object();
	result.City = this.getInput(0).getData( 'Id' );
	return result;
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

specialHotelPersons = function() {
	specialHotelPersons.superClass.apply( this, arguments );
	this.setLabel( 'Количество гостей:' );
	this.setControl( 'adult' );
	this.setControl( 'child' );
	this.setControl( 'baby' );
	
	this.checkOnDisable();
	this.setTitle();
}
specialHotelPersons.inheritsFrom( Persons );

/* -------------------------------------------------------------------------- */

specialHotelPersons.prototype.checkOnDisable = function() {
	this.setDisabled( 'adult', this.adult + this.child == 4 );
	this.setDisabled( 'child', this.adult + this.child == 4 );
	this.setDisabled( 'baby', this.adult == this.baby );
	this.lastAdult.setClassName( 'no', this.adult == 1 );
}

/* ---------------------------------------------------------------------- */

specialHotelPersons.prototype.check = function( obj ) {
	if( 
		( obj.age == 'adult' && this.adult + this.child == 4 )
	) return false;
	return true;
}

