AviaSpecialsController = function( start, end ) {
	AviaSpecialsController.superClass.apply( this, arguments );

	this.searchUrl = '/flight/search/';
	this.calendar = new Calendar( this.getEl( 'calendar-wrap' ), this.Drag, [
		'Укажите дату вылета в календаре',
		'Укажите дату прилета в календаре',
		'Укажите дату вылета и прилета в календаре'
	], start, end );
	this.calendar.changeMode( 2 );
	this.calendar.attachObserver( 'calendarStatusChange', this.check, this );
	this.calendar.attachObserver( 'daySelect', this.setDate, this );

	this.persons = new aviaPersons( this.getEl( 'persons' ) );
	this.form = this.createForm( aviaSpecialsForm, 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();
	});

	if( this.getEl( 'prices' ) ) {
		var links = this.getEl( 'prices' ).getElementsByTagName( 'A' );
		for( var c in links ) {
			if( !links[ c ].nodeType ) continue;
			this.addHandler( links[ c ], 'click', function( params, ev ) {
				this.pad.show();
				this.preventDefault( ev );
			});
		}
	}
}
AviaSpecialsController.inheritsFrom( Controller );

/* -------------------------------------------------------------------------- */

AviaSpecialsController.prototype.send = function() {
	var oParams = new Object();
	var formResult = this.form.getParams();
	var calendarResult = this.calendar.getParams();
	var persons = this.persons.getValue();

	oParams[ 'DateFrom' ] = formatDate( calendarResult[0].date );
	if( calendarResult[1] && calendarResult[1].date ) oParams[ 'DateTo' ] = formatDate( calendarResult[1].date );

	if( window.prefAirline ) oParams[ 'PrefAirline' ] = window.prefAirline;

	this.searchStore.addParams({ 'Commander.Command': 'SearchAirTariffsCalendar' });
	this.searchStore.addParams( persons );
	this.searchStore.addParams( formResult );
	this.searchStore.addParams( oParams );
	this.searchStore.load();
}

//----------------------------------------------------------------------------//

AviaSpecialsController.prototype.checkState = function( result ) {
	if( Boolean( result[ 'CommandId' ] ) ) {
		this.fillForm(
			this.searchUrl,
			{
				'SearchId': result[ 'CommandId' ],
				'SpecialId': window[ 'specialId' ]
			}
		);
		if( window.prefAirline ) this.fillForm( false, { 'PrefAirline': window.prefAirline } );
		this.submit();
	} else {
		alert( 'Извините, произошла ошибка. Попрбуйте ещё раз.' );
	}
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

aviaSpecialsForm = function() {
	aviaSpecialsForm.superClass.apply( this, arguments );
	this.labels = new Array();
	
	var from = this.setInput( window.fromSet, 'Откуда' );

	this.setLabel();
	this.setInput( window.toSet, 'Куда' );

	var newLabel = this.setLabel();
	this.setClassName( 'return', true, newLabel.ico );
	
	this.back = this.setInput( window.fromSet, 'Обратно' );
	this.back.setDisabled( true );
	
	from.attachObserver( 'inputSetted', this.setBackData, this );
	this.setBackData();

	this.tripClass = new Select( this.getEl( 'class' ), [
		{ name: window.FlightClassName, value: window.FlightClass, HTML: ['<small>'+window.FlightClassName+'</small>'] }]);
	this.tripClass.setTip( 'класс' );

	this.check();
}
aviaSpecialsForm.inheritsFrom( Form );


//----------------------------------------------------------------------------//

aviaSpecialsForm.prototype.setBackData = function() {
	var data = this.getInput(0).getData();
	this.back.set( data, data[ 'name' ] );
}

/* -------------------------------------------------------------------------- */

aviaSpecialsForm.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;
}

/* -------------------------------------------------------------------------- */

aviaSpecialsForm.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;
}

/* -------------------------------------------------------------------------- */

aviaSpecialsForm.prototype.getParams = function() {
	var result = new Object();
	result.From = this.getInput(0).getData( 'Id' );
	result.FromName = this.getInput(0).getData( 'name' );
	result.FromCountry = this.getInput(0).getData( 'country' );
	result.To = this.getInput(1).getData( 'Id' );
	result.ToName = this.getInput(1).getData( 'name' );
	result.ToCountry = this.getInput(1).getData( 'country' );
	result[ 'ServiceClass' ] = this.tripClass.getData( 'value' ); 
	result.RoundTrip = this.getInput(2) ? true : false;
	if( !result.From || !result.To ) return false;
	return result;
}

/* ---------------------------------------------------------------------- */
/* ---------------------------- Persons --------------------------------- */
/* ---------------------------------------------------------------------- */

aviaPersons = function() {
	aviaPersons.superClass.apply( this, arguments );
	this.setLabel( 'Количество пассажиров:' );
	this.setControl( 'adult' );
	this.setControl( 'child' );
	this.setControl( 'baby' );
	
	this.checkOnDisable();
	this.setTitle();
}
aviaPersons.inheritsFrom( Persons );

/* ---------------------------------------------------------------------- */

aviaPersons.prototype.checkOnDisable = function() {
	this.setDisabled( 'adult', this.adult + this.child == 4 );
	this.setDisabled( 'child', this.adult + this.child == 4 );
	this.setDisabled( 'baby', this.baby == this.adult );

	this.lastAdult.setClassName( 'no', this.adult == 1 );
}

/* ---------------------------------------------------------------------- */

aviaPersons.prototype.check = function( obj ) {
	if( 
		( ( obj.age == 'adult' || obj.age == 'child' ) && this.adult + this.child == 4 ) ||
		( obj.age == 'baby' && this.baby == this.adult )
	) return false;
	return true;
}
