﻿// ----- Главная страница по гостиницам ----- //
topCities = [
	{	Name: 'Прага', Country: 'Чехия', Id: '559e5181-c0e5-417e-b420-e242568f5546', Code: 'PRG' },
	{	Name: 'Стамбул', Country: 'Турция', Id: 'aeb7c40a-0f69-4956-86e8-cef31d661fed', Code: 'IST' },
	{	Name: 'Барселона', Country: 'Испания', Id: '3b641b3c-2adb-4835-b0f3-3beda491bc45', Code: 'BCN' },
	{	Name: 'Рим', Country: 'Италия', Id: '9b5ca40a-e1c1-45d2-8ff2-28610faff298', Code: 'ROM' },
	{	Name: 'Милан', Country: 'Италия', Id: '1d82932b-d249-4f1c-819b-923789cf79b8', Code: 'MIL' },
	{	Name: 'Мадрид', Country: 'Испания', Id: '8d26a8ab-4705-4e8d-bf9b-52bbf5d63780', Code: 'MAD' },
	{	Name: 'Париж', Country: 'Франция', Id: 'f86ac48f-55a1-40f5-be03-49989b3afe61', Code: 'PAR' },
	{	Name: 'Берлин', Country: 'Германия', Id: '35888de2-dba7-4a24-9dbe-a9a66d15d660', Code: 'BER' },
	{	Name: 'Пекин', Country: 'Китай', Id: 'cb63ca5b-f148-45af-b415-efd8426d2bc7', Code: 'PEK' },
	{	Name: 'Рига', Country: 'Латвия', Id: '80b704bb-2e59-442b-b6ca-b3d6466fbcd9', Code: 'RIX' },
	{	Name: 'Родос', Country: 'Греция', Id: 'dabba1fa-4373-4290-b0b3-af5988fcb59f', Code: 'RHO' },
	{	Name: 'Римини', Country: 'Италия', Id: 'f72fc99a-ebb7-4a65-b29e-23d260d6cdc8', Code: 'RMI' },
	{	Name: 'Санкт-Петербург', Country: 'Россия', Id: '9481c5b4-69b9-426d-92f6-402b70b22f20', Code: 'LED' },
	{	Name: 'Москва', Country: 'Россия', Id: '607b4aa2-5281-406c-a2ce-4bd52c29ef9a', Code: 'MOW' },
	{	Name: 'Сочи', Country: 'Россия', Id: '7a4cae2f-7e3a-449f-9d67-56ffc8b363cf', Code: '' },
	{	Name: 'Вена', Country: 'Австрия', Id: '9034d0fb-e946-4ec3-ba3a-b4a3f7cc8ed1', Code: 'VIE' },
	{	Name: 'Таллинн', Country: 'Эстония', Id: '4287d388-71e5-4628-b2c8-8f98eb707dc2', Code: 'TLL' },
	{	Name: 'Будапешт', Country: 'Венгрия', Id: '3034ea0e-df21-48ac-9a32-c95342c2df4e', Code: 'BUD' },
	{	Name: 'Амстердам', Country: 'Нидерланды', Id: '2ac8e8c4-d265-41db-a2cb-17393eadc9c2', Code: 'AMS' },
	{	Name: 'Бангкок', Country: 'Тайланд', Id: '5c075e86-b513-4207-988a-263bbdf0504c', Code: 'BKK' },
	{	Name: 'Белград', Country: 'Сербия', Id: 'bc82024e-e5e1-470b-92ab-a083ba9e916f', Code: 'BEG' },
	{	Name: 'Гонконг', Country: 'Гонконг', Id: '42e550fa-a56f-4317-9029-3fe4dcdcffcc', Code: 'HKG' },
	{	Name: 'Гавана', Country: 'Куба', Id: '1df4b413-086a-454a-a82d-b4b77075576e', Code: 'HAV' },
	{	Name: 'Гамбург', Country: 'Германия', Id: 'eb4928ca-1c58-448f-8895-2fd2ceea5c5a', Code: 'HAM' },
	{	Name: 'Дубай', Country: 'ОАЭ', Id: 'e209de54-0ee2-41f3-9c23-1ff040ea4ff1', Code: 'DXB' },
	{	Name: 'Киев', Country: 'Украина', Id: 'f7b635b6-8a5b-46c4-aa7d-f00fcf0c3796', Code: 'IEV' },
	{	Name: 'Женева', Country: 'Швейцария', Id: '1304111d-ae43-44bd-b6e7-136563e913b4', Code: 'GVA' },
	{	Name: 'Загреб', Country: 'Хорватия', Id: 'fb1a171d-aea4-42cb-aa2d-d4a17222a360', Code: 'ZAG' },
	{	Name: 'Дубровник', Country: 'Хорватия', Id: '1a808c32-40b1-4038-a6dc-653c820b3b2f', Code: 'DBV' },
	{	Name: 'Иерусалим', Country: 'Израиль', Id: 'e489fa79-6732-4ba1-aa8c-cd96e2b9f9a5', Code: 'JRS' },
	{	Name: 'Калининград', Country: 'Россия', Id: '6063f254-89f2-4e6b-8952-0524e5766941', Code: 'KGD' },
	{	Name: 'Казань', Country: 'Россия', Id: 'cd3bc419-be7f-48e9-b681-37bd49851337', Code: 'KZN' },
	{	Name: 'Копенгаген', Country: 'Дания', Id: '8d88c3e9-540e-4991-b9bb-d517e353b2e9', Code: 'CPH' },
	{	Name: 'Каир', Country: 'Египет', Id: 'e58160b9-b495-4adf-ade8-7ff713892362', Code: 'CAI' },
	{	Name: 'Лиссабон', Country: 'Португалия', Id: '6147bfeb-ad37-4cea-856c-c6018a19f1aa', Code: 'LIS' },
	{	Name: 'Лос Анджелес', Country: 'США', Id: 'bf3b3fbf-7b07-418f-9dbe-9b09c58c320f', Code: 'LAX' },
	{	Name: 'Минск', Country: 'Белоруссия', Id: '0edaf247-ee4f-46ea-9fd5-7a7f025163ea', Code: 'MSQ' },
	{	Name: 'Мюнхен', Country: 'Германия', Id: '5a6b926d-e1ff-499c-8518-a748536b83d0', Code: 'MUC' },
	{	Name: 'Нью-Йорк', Country: 'США', Id: '68f7fe81-fc4f-4208-8627-2dae1f410244', Code: 'NYC' },
	{	Name: 'Одесса', Country: 'Украина', Id: '8f502c85-009a-4155-ac8c-b67cabf1c941', Code: 'ODS' },
	{	Name: 'Осло', Country: 'Норвегия', Id: '06669563-682a-4fb0-b5d7-f339a137052b', Code: 'OSL' },
	{	Name: 'Тель-Авив', Country: 'Израиль', Id: '7eb65e8d-ef23-42a5-92fb-723aba8063da', Code: 'TLV' },
	{	Name: 'Хельсинки', Country: 'Финляндия', Id: '21988618-74a4-4e49-911a-eeff43dc5a9f', Code: 'HEL' },
	{	Name: 'Юрмала', Country: 'Латвия', Id: 'b54a5a63-6bab-4213-930f-cbad5bd48bca', Code: 'JURM' }
];

var shuffledExamples = shuffleArray( topCities );

var hotelExamples = shuffledExamples.slice( 0, 3 );
/*[
	{	Name: 'Санкт-Петербург', Country: 'Россия', Id: '9481c5b4-69b9-426d-92f6-402b70b22f20', Code: 'LED' },
	{	Name: 'Прага', Country: 'Чехия', Id: '559e5181-c0e5-417e-b420-e242568f5546', Code: 'PRG' },
	{	Name: 'Москва', Country: 'Россия', Id: '607b4aa2-5281-406c-a2ce-4bd52c29ef9a', Code: 'MOW' }
];*/
// -------------------------------------------------------------------------- //

HotelController = function() {
	HotelController.superClass.apply( this, arguments );
	
	this.Id = 'HotelController';
	this.searchUrl = '/hotel/search/';
	this.form = this.createForm( hotelForm, this.getEl( 'form' ) );

	this.calendar = new Calendar( this.getEl( 'calendar-wrap' ), today, date365 );
	this.calendar.changeMode( 2 );

	this.calendar.attachObserver( 'CalendarDaySelected', this.form.setInputDate, this.form );
	this.calendar.attachObserver( 'CalendarDayUnSelected', this.form.unsetInputDate, this.form );

	this.form.attachObserver( 'DateInputSetted', this.calendar.setDate, this.calendar );

	this.persons = new hotelPersons( this.getEl( 'persons' ) );
	this.persons.attachObserver( 'PersonCountChanged', function(){ $('#resetForm').removeClass( 'hidden' ); }, this );
}
HotelController.inheritsFrom( Controller );

// -------------------------------------------------------------------------- //

HotelController.prototype.send = function() {
	/*var formResult = this.form.getParams();
	var persons = this.persons.getValue();

	this.searchStoreSaveParams.addParams({ 'Commander.Command': 'SearchHotelTariffs' });
	this.searchStoreSaveParams.addParams( persons );
	this.searchStoreSaveParams.addParams( formResult );
	this.searchStoreSaveParams.load();
}

HotelController.prototype.checkStateOfStoreSaveParams = function() {*/
	var formResult = this.form.getParams();
	var persons = this.persons.getValue();

	this.searchStore.addParams({ 'Commander.Command': 'SearchHotelTariffs' });
	this.searchStore.addParams( persons );
	this.searchStore.addParams( formResult );
	this.searchStore.load();
}

//----------------------------------------------------------------------------//

HotelController.prototype.init = function( data ) {
	if(
		data[ 'CheckIn' ] &&
		data[ 'CheckIn' ] != '0001-01-01T00:00:00'
	) {
		var date = this.translateDate( data[ 'CheckIn' ] );
		if( date instanceof Date && date.valueOf() > window.today.valueOf() ) {
			this.form.dates[ 0 ].setDefault( date );
		}
	}

	if(
		data[ 'CheckOut' ] &&
		data[ 'CheckOut' ] != '0001-01-01T00:00:00'
	) {
		var date = this.translateDate( data[ 'CheckOut' ] );
		if( date instanceof Date && date.valueOf() > window.today.valueOf() ) {
			this.form.dates[ 1 ].setDefault( date );
		}	
	}

	if( data[ 'City' ] ){
		var cityCode = data[ 'City' ][ 'Code' ];
		var codes = cityCode.split( '/' );
		if ( codes[ 0 ] == '' ){
			cityCode = codes[ 1 ];
		}
		else {
			cityCode = codes[ 0 ];
		}
		data[ 'City' ][ 'Code' ] = cityCode;
		this.form.inputs[ 0 ].setDefault( data[ 'City' ], data[ 'City' ][ 'Name' ] );
	}
	if( data[ 'MinRating' ] != 'NO_RATING' ) this.form.rating.setInitialValue( data[ 'MinRating' ] );

	this.form.check();
	
	if( data[ 'PersonsCount' ] ) {
		this.persons.set(
			Number( data[ 'PersonsCount' ][ 'Adults' ] ),
			Number( data[ 'PersonsCount' ][ 'Children' ] ),
			Number( data[ 'PersonsCount' ][ 'Infants' ] )
		);
	}
}

// заполняем из данных по ссылке партнера
HotelController.prototype.initDataFromPartner = function( data ) {
	this.form.inputs[ 0 ].setDefault( data[ 'City' ], data[ 'City' ][ 'Name' ] );
}

//----------------------------------------------------------------------------//

HotelController.prototype.checkState = function( result ) {
	if( Boolean( result[ 'CommandId' ] ) ) {
		var SearchId = result[ 'CommandId' ];
		var checkerSG = new Checker( '/ajax/check-storage-global.html', { 'SearchId': SearchId });
		checkerSG.attachObserver( 'Loaded', function(){
			this.fillForm( this.searchUrl, { 'SearchId': SearchId } );
			if( this.form.rating.getData( 'min' ) ) {
				this.fillForm( false, { 'MinHotelRating': this.form.rating.getData( 'min' ) });
			}
			if( this.form.rating.getData( 'max' ) ) {
				this.fillForm( false, { 'MaxHotelRating': this.form.rating.getData( 'max' ) });
			}
			this.submit();
		}, this );
		checkerSG.load();
	} else {
		alert( 'Извините, произошла ошибка. Попробуйте ещё раз.' );
	}
}

// -------------------------------------------------------------------------- //
// --------------------------------- Form ----------------------------------- //
// -------------------------------------------------------------------------- //

hotelForm = function( target ) {
	hotelForm.superClass.apply( this, arguments );

	this.Id = 'HotelForm';	
	this.dates = new Array();

	this.dates[ 0 ] = new NDateInput({
		'target': this.getEl( 'date1' ),
		'type': 'd0',
		'name': 'DateFrom',
		'min': today,
		'max': date365,
		'settings': FieldsSettings[ 'IndexHotelCheckIn' ]
	});

	this.dates[ 0 ].elementInput.tabIndex = 1;
	this.dates[ 0 ].attachObserver( 'inputSetted', this.onInputSetted, this );
	this.dates[ 0 ].attachObserver( 'inputStatusChange', function(){ this.check(); }, this );

	this.inputs[ 0 ] = new NPredicateInput({
		'target': this.getEl( 'input1' ),
		'type': 'text predicate',
		'command': 'SearchHotelLocation',
		'name': 'To',
		'examples': hotelExamples,
		'settings': FieldsSettings[ 'HotelCityTo' ]
	});

	this.inputs[ 0 ].elementInput.tabIndex = 2;
	this.inputs[ 0 ].attachObserver( 'inputSetted', this.onInputSetted, this );
	this.inputs[ 0 ].attachObserver( 'inputStatusChange', function(){ this.check(); }, this );

	this.dates[ 1 ] = new NDateInput({
		'target': this.getEl( 'date2' ),
		'type': 'd1',
		'name': 'DateTo',
		'min': today,
		'max': date365,
		'settings': FieldsSettings[ 'IndexHotelCheckOut' ]
	});

	this.dates[ 1 ].elementInput.tabIndex = 3;
	this.dates[ 1 ].attachObserver( 'inputSetted', this.onInputSetted, this );
	this.dates[ 1 ].attachObserver( 'inputStatusChange', function(){ this.check(); }, this );

	/*this.rating = new Select({
		target: this.getEl( 'class' ),
		type: 'select',
		name: 'tripClass',
		label: 'класс',
		tip: 'класс',
		list: [
			{	Name: '1...5 *', Value: 'NO_RATING', HTML: ['<small>1...5 *</small>'] },
			{	Name: '1...3 *', Value: 'ONE_STAR', HTML: ['<small>1...3 *</small>'], min: 'ONE_STAR', max: 'THREE_STAR' },
			{	Name: '3...4 *', Value: 'THREE_STAR', HTML: ['<small>3...4 *</small>'], min: 'THREE_STAR', max: 'FOUR_STAR' },
			{	Name: '4...5 *', Value: 'FOUR_STAR', HTML: ['<small>4...5 *</small>'], min: 'FOUR_STAR', max: 'FIVE_STAR' }
		],
		initialValue: 'NO_RATING'
	});*/

	this.rating = new NSelect({
		'target': this.getEl( 'class' ),
		'type': 'select',
		'name': 'tripClass',
		'list': [
			{	Name: '1...5 *', Value: 'NO_RATING', HTML: ['<small>1...5 *</small>'] },
			{	Name: '1...3 *', Value: 'ONE_STAR', HTML: ['<small>1...3 *</small>'], min: 'ONE_STAR', max: 'THREE_STAR' },
			{	Name: '3...4 *', Value: 'THREE_STAR', HTML: ['<small>3...4 *</small>'], min: 'THREE_STAR', max: 'FOUR_STAR' },
			{	Name: '4...5 *', Value: 'FOUR_STAR', HTML: ['<small>4...5 *</small>'], min: 'FOUR_STAR', max: 'FIVE_STAR' }
		],
		'initialValue': 'NO_RATING',
		'settings': FieldsSettings[ 'HotelRaiting' ]
	});
	this.rating.elementInput.tabIndex = 4;
	this.rating.attachObserver( 'inputSetted', function(){ $('#resetForm').removeClass( 'hidden' ); }, this );
}
hotelForm.inheritsFrom( IndexForm );

// -------------------------------------------------------------------------- //

hotelForm.prototype.onInputSetted = function( params, obj ) {
	$('#resetForm').removeClass( 'hidden' );
	if ( obj[ 'Id' ] == 'DateInput' ){
		var date = obj.getData();
		var index = Number( obj.type.substr( 1 ) );
		this.notify( 'DateInputSetted', { index: index, data: date } );
	}
}

// -------------------------------------------------------------------------- //

hotelForm.prototype.reset = function(){
	this.inputs[ 0 ].reset();
	this.dates[ 0 ].clear();
	this.dates[ 1 ].clear();

	this.dates[ 0 ].setMin( today );
	this.dates[ 0 ].setMax( date365 );

  	this.dates[ 1 ].setMin( today );
	this.dates[ 1 ].setMax( date365 );

	this.rating.setInitialValue( this.rating.initialValue );
	this.status = false;
}

// -------------------------------------------------------------------------- //

hotelForm.prototype.getParams = function() {
	var result = new Object();
	result[ 'City' ] = this.inputs[ 0 ].getData( 'Id' );
	result[ 'CityName' ] = this.inputs[ 0 ].getData( 'Name' );
	result[ 'CityCountry' ] = this.inputs[ 0 ].getData( 'Country' );
	result[ 'CheckIn' ] = this.dates[ 0 ].getValueToParam();
	result[ 'CheckOut' ] = this.dates[ 1 ].getValueToParam();
	result[ 'MinHotelRating' ] = this.rating.getData( 'min' );
	result[ 'MaxHotelRating' ] = this.rating.getData( 'max' );
	return result;
}

// -------------------------------------------------------------------------- //
// -------------------------------- Persons --------------------------------- //
// -------------------------------------------------------------------------- //

hotelPersons = function() {
	
	this.Id = 'hotelPersons';

	this.adultTitle = 'Взрослый';
	this.childTitle = 'Ребенок';
	this.babyTitle = 'Люлька для младенца';

	this.babyNameNC = 'люлька для младенца';
	this.babyNameNCPl = 'люльки для младенца';
	this.babyNameGC = 'люльку для младенца';
	
	this.childNameNC = 'ребенок';
	this.childNameNCPl = 'детей';
	this.childNameGC = 'ребенка от 2-х до 12-ти лет с предоставлением места';
	
	this.adultNameNC = 'взрослый';
	this.adultNameNCPl = 'взрослых';
	this.adultNameGC = 'взрослого';

	hotelPersons.superClass.apply( this, arguments );

	this.setLabel( 'Количество проживающих:' );

	this.setControl( 'baby' );
	this.setControl( 'child' );
	this.setControl( 'adult' );

	this.checkOnDisable();
	this.setTitle();
}
hotelPersons.inheritsFrom( Persons );

// -------------------------------------------------------------------------- //
	
hotelPersons.prototype.checkChildCount = function() {
	if( this.adult <= this.child ) {
		for( var c in this.persons ) {
			var cP = this.persons[ c ];
			if( cP.age == 'child' ) {
				cP.notify( 'PersonClicked' );
				break;
			}
		}
	}
}

hotelPersons.prototype.checkOnDisable = function() {
	this.setDisabled( 'adult', this.adult + this.child == 4 );
	this.setDisabled( 'child', ( this.adult + this.child == 4 || this.adult == this.child ) );
	this.setDisabled( 'baby', ( this.baby == 2 ) );

	this.lastAdult.setClassName( 'no', this.adult == 1 );
}

hotelPersons.prototype.check = function( obj ) {
	if( 
		( obj.age == 'adult' && this.adult + this.child == 4 ) ||
		( obj.age == 'child' && ( this.adult + this.child == 4 || this.adult == this.child ) ) ||
		( obj.age == 'baby' && ( /*this.baby == this.adult ||*/ this.baby == 2 ) )
	) return false;
	return true;
}

// -------------------------------------------------------------------------- //

onload = function() {
	controller = new HotelController();

	if ( dataPromo ) controller.initDataFromPartner( dataPromo )
	else if( Global[ 'Hotel' ] ) controller.init( Global[ 'Hotel' ] );
}
