﻿// JavaScript Document
jQuery(function() {
	//set max nights		   
	var maxNights = 15;
	//set max children
	var tempChildCount = 2;
	//set max adults
	var adults = 4;
	
	var calendarIco = "http://belarustourism.by/b2c/img/calendar_ico.png"
	
	for (i = 1; i<=maxNights; i++){
		jQuery("#nightsCount").append("<option value='" + i + "'>" + i + "</option>");
	}
	var minute=1000*60;
	var hour=minute*60;
	var day=hour*24;
	var dates = jQuery( "#startDate, #endDate" ).datepicker({
		//defaultDate: "+1d",
		showOn: "button",
		buttonImage: calendarIco,
		changeMonth: true,
		numberOfMonths: 2,
		dateFormat: "dd.mm.yy",
		onSelect: function( selectedDate ) {
			option = this.id == "startDate" ? "minDate" : "maxDate", instance = jQuery( this ).data( "datepicker" );
			if (option == "minDate") {
				setMinDate();
				//setEndDate();
			} else {
				interval = Date.parse(jQuery("#endDate").datepicker("getDate")) - Date.parse(jQuery("#startDate").datepicker("getDate"));
				nights=Math.round(interval/day);
				jQuery("#nightsCount").val(nights);
				jQuery(".select:eq(0)").text(nights);
			}
		}
	});
	function setEndDate() {
		interval = jQuery("#nightsCount").val();
		maxDate = Date.parse(jQuery("#startDate").datepicker("getDate")) + day*interval;
		maxDate = new Date(maxDate);
		maxDate = jQuery.datepicker.formatDate("dd.mm.yy", maxDate);
		jQuery("#endDate").datepicker("setDate", maxDate);
	}
	function setMinDate() {
		minDate = Date.parse(jQuery("#startDate").datepicker("getDate"));
		maxDate = new Date(day*maxNights + minDate);
		minDate = new Date(day + minDate);
		maxDate = jQuery.datepicker.formatDate("dd.mm.yy", maxDate);
		minDate = jQuery.datepicker.formatDate("dd.mm.yy", minDate);
		jQuery("#endDate").datepicker("option", "minDate", minDate);
		jQuery("#endDate").datepicker("option", "maxDate", maxDate);
	}
	jQuery("#nightsCount").change(function(){
		setEndDate();
	});
	date = new Date();
	date = jQuery.datepicker.formatDate("dd.mm.yy", date);
	jQuery("#startDate").datepicker("setDate", date);
	setMinDate();
	jQuery("#endDate").datepicker("setDate", minDate);
		
	jQuery.datepicker.regional['ru'] = { // Default regional settings
		closeText: 'Закрыть', // Display text for close link
		prevText: '&lt;Пред', // Display text for previous month link
		nextText: 'След&gt;', // Display text for next month link
		currentText: 'Сегодня', // Display text for current month link
		monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь',
		     		'Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'], // Names of months for drop-down and formatting
		monthNamesShort: ['Янв','Фев','Мар','Апр','Май','Июн','Июл','Авг','Сен','Окт','Ноя','Дек'], // For formatting
		dayNames: ['воскресенье','понедельник','вторник','среда','четверг','пятница','суббота'], // For formatting
		dayNamesShort: ['вск','пнд','втр','срд','чтв','птн','сбт'], // For formatting
		dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'], // Column headings for days starting at Sunday
		weekHeader: 'нед.', // Column header for week of the year
		dateFormat: 'mm/dd/yy', // See format options on parseDate
		firstDay: 0, // The first day of the week, Sun = 0, Mon = 1, ...
		isRTL: false, // True if right-to-left language, false if left-to-right
		showMonthAfterYear: false, // True if the year select precedes month, false for month then year
		yearSuffix: '' // Additional text to append to the year in the month headers
	};


	for (i = 1; i<=adults; i++){
		jQuery("#adults").append("<option value='" + i + "'>" + i + "</option>");
	}
	
	
	for (i = 0; i<=tempChildCount; i++){
		jQuery("#tempChildCount").append("<option value='" + i + "'>" + i + "</option>");
	}
	
	jQuery("#tempChildCount").change(function(){
		if (jQuery(this).val()) {
			jQuery("#childContainer *").remove();
			for (i = 0; i<jQuery(this).val(); i++){
				jQuery("#childContainer").append("<select id='child-" + i + "' name='request.rooms.room[0].children.child[" + i + "].age'>");
				childId = '#child-' + i;
				jQuery(childId).append("<option value='0'>&lt; 1</option>");
				for (age = 1; age<17; age++){
					jQuery(childId).append("<option value='" + age + "'>" + age + "</option>");
				}
			}
		} else {
			jQuery("#childContainer *").remove();
		}
	});
	
	jQuery("#b2cSearchForm").submit(function() {
		startDate = new Date(Date.parse(jQuery("#startDate").datepicker("getDate")));
		startDate = jQuery.datepicker.formatDate("yy-mm-dd", startDate);
		jQuery("#startDateParam").val(startDate);
		endDate = new Date(Date.parse(jQuery("#endDate").datepicker("getDate")));
		endDate = jQuery.datepicker.formatDate("yy-mm-dd", endDate);
		jQuery("#endDateParam").val(endDate);
		//return false;
	});
	
	//jQuery("#notifications").text(jQuery.url.param("notifications"));
	
	jQuery(".gp_bookmark").click(function(){
		jQuery(".gp_bookmark_sel").removeClass("gp_bookmark_sel wc_bookmark_sel").addClass("wc_bookmark");
		jQuery(".gp_block_sel").hide().removeClass("gp_block_sel").addClass("gp_block");
		blockId = '#' + jQuery(this).attr('id') + '_sec';
		jQuery(this).removeClass("wc_bookmark").addClass("gp_bookmark_sel wc_bookmark_sel");
		jQuery(blockId).show().removeClass("gp_block").addClass("gp_block_sel");
	});
	
	
	lang = jQuery('html').attr('lang');

	if (lang)
		jQuery.datepicker.setDefaults(jQuery.datepicker.regional[lang]);
	
	var city = new Array();
	if (lang == 'ru' || lang == 'by') {
		city[3855] = "Минск";
		city[207279] = "Барановичи";
		city[170769] = "Брест";
		city[207260] = "Витебск";
		city[170768] = "Гомель";
		city[207261] = "Гродно";
		city[207262] = "Могилев";
		city[207265] = "Новополоцк";	
	} else {
		city[3855] = "Minsk";
		city[207279] = "Baranovichi";
		city[170769] = "Brest";
		city[207260] = "Vitebsk";
		city[170768] = "Gomel";
		city[207261] = "Grodno";
		city[207262] = "Mogilev";
		city[207265] = "Novopolotsk";
	}
	for (cityId in city){
	  jQuery("#cityId").append("<option value='" + cityId + "'>" + city[cityId] + "</option>");
	}
	
	if (lang == 'ru' || lang == 'by') {
		jQuery("input[name='gpLocale']").val('ru');
	} else {
		jQuery("input[name='gpLocale']").val('en');
	}
	
	
	if (lang == 'ru' || lang == 'by') {
		jQuery(".submitButton").val('Поиск номера');
	} else {
		jQuery(".submitButton").val('Search');
	}
	label = [];
	label['ru'] = {
		cityId: 'Город' ,
		startDate: 'Дата заезда',
		endDate: 'Дата выезда' ,
		adults: 'Гостей',
		freeOnly: 'Только свободные',
		notFound: 'Извините, ничего не найдено',
		nightsCount: 'Ночей'
	};
	label['en'] = {
		cityId: 'City' ,
		startDate: 'Check-in date',
		endDate: 'Check-out date' ,
		adults: 'Guests',
		freeOnly: 'Instant cofirmation only',
		notFound: 'Sorry, nothing found',
		nightsCount: 'Nights'
	};
	if (jQuery.getUrlVar('notifications')){
		jQuery("#notifications").text(label[lang]['notFound']);
	}
	jQuery.each(label[lang], function(index, value) { 
		label = "label[for='" + index + "']";							  
		jQuery(label).text(value);								  
	});
	
	
});
