String.prototype.ucFirst = function() {
var str = this;
if(str.length) {
str = str.charAt(0).toUpperCase() + str.slice(1);
}
return str;
};
var bookingWidget = {
iata : null,
destination: null,
isClosed: true,
initedElements: {
ticket: false,
hotel: false,
car: false,
tour: false
},
current: null,
loading: false,
levelTravelCountriesIso: [],
useLevelTravel: false,
staticServer: '',
datepickerParams: {
'dateFormat': 'dd.mm.yy',
'dayNamesMin': ['вс', 'пн', 'вт', 'ср', 'чт', 'пт', 'сб'],
'defaultDate': new Date(),
'minDate': new Date(),
'firstDay': 1,
'monthNames': ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
'nextText': 'Вперед',
'prevText': 'Назад',
'showOn': 'both',
'buttonImage': '/ubb/calendar.gif',
'buttonImageOnly': true,
beforeShow: function(input) {
var x = 20;
var y = 20;
var field = $(input);
var left = field.offset().left + field.width() + x;
var bottom = y;
setTimeout(function(){
$('#ui-datepicker-div').css({'top':'', 'bottom':bottom + 'px', 'left': left + 'px'});
}, 1);
}
},
castInt: function(value, min){
value = parseInt(value);
if (isNaN(value) || value < min) value = min;
return value;
},
init : function(type) {
var _this = this;
_this.initDefaultValues();
_this.initVisual();
if (!$.cookie('bub_closed_24h')) {
_this.setActive(type);
}
_this.initButtons();
$('#ubb_Button_Toggle').bind('click', function(e) {
bookingWidget.toggle();
});
$('#ubb_hide').click(function(){
$.cookie('bub_closed_24h', '1', { expires: 1, path: '/' });
$(this).hide();
$('#ubb').hide("slide", { direction: "right" }, 800, function(){
$('#ubb_show').show();
});
});
$('#ubb_show').click(function(){
$.removeCookie('bub_closed_24h', { path: '/' });
$(this).hide();
$('#ubb').show("slide", { direction: "right" }, 800, function(){
$('#ubb_hide').show();
});
//fist init from initial hidden state
if (_this.current === null) {
_this.setActive(type);
}
});
$('body').append('
');
$('#ubb_overlay, #ubbWrap').bind('click', function(e){
if($(e.target).parents('#ubb').size() == 0 && !bookingWidget.isClosed)
bookingWidget.toggle();
});
},
initDefaultValues: function() {
$(function(){
if (typeof window.JSVARS == 'object') {
var jsvars = window.JSVARS;
if (jsvars['ubb_ticket_dest_iata'] && jsvars['ubb_ticket_dest_title']) {
$('#ubb_ticket_AirportDestination').val(jsvars['ubb_ticket_dest_title']);
$('#ubb_ticket_AirportDestinationHidden').val(jsvars['ubb_ticket_dest_iata']);
}
}
})
},
initVisual: function()
{
if ($.cookie('bub_closed_24h')) {
$('#ubb').hide();
$('#ubb_hide').hide();
$('#ubb_show').show();
} else{
$('#ubb').show();
$('#ubb_hide').show();
$('#ubb_show').hide();
}
$("#ubbWrap").show();
},
initButtons : function() {
var _this = this;
$('#ubb_tabs .tab').bind('click', function() {
var id = $(this).attr('id');
var tmp = id.split('_');
id = tmp[1];
if(_this.current != id) {
_this.setActive(id);
}
_this.toggle('open');
});
$('input.focus').bind('click', function(){
if(_this.isClosed){
_this.toggle('open');
}
return false;
});
},
initWidget : function(type) {
var method = 'init'+ type.ucFirst();
if(typeof this[method] == 'function') {
this.current = type;
return this[method]();
} else {
return false;
}
},
setActive : function(type) {
this.initWidget(type);
$('#ubb_tabs .tab.act').removeClass('act');
$('#ubbTab_'+ type).addClass('act');
$(".ubb_form").not(':hidden').toggleClass('hidden');
$('#ubbForm_' + type).toggleClass('hidden');
},
initTicket : function() {
if(this.initedElements[this.current]) return true;
var _this = this;
if(!this.destination) {
$.ajax({
url: '/aviasalesHotDeals',
data: {
month_num: 3
},
success: function(data, textStatus, jqXHR) {
if ($.trim(data) == '') {
//no hot deals detected
return;
};
$('#hotdeals').html(data);
_this.initedElements[_this.current] = true;
},
error: function(jqXHR, textStatus, errorThrown) {
//some error occured
}
});
} else {
// requesting IATA data for inputs
$.ajax({
url: '/ajax/ubb/ticket/iataDataFor/'+ _this.destination._id,
success: function(data, textStatus, jqXHR) {
if(data.result && data.result.title && data.result.IATA && data.result.IATA.length) {
$('#ubb_ticket_AirportDestination').val(data.result.title);
$('#ubb_ticket_AirportDestinationHidden').val(data.result.IATA);
}
},
error: function(jqXHR, textStatus, errorThrown) {
//some error occured
}
});
$.ajax({
url: '/aviasalesHotDeals/'+ _this.destination.path,
data: {
month_num: 3,
limit: 5
},
success: function(data, textStatus, jqXHR) {
if ($.trim(data) == '') {
//no hot deals detected
return;
};
$('#hotdeals').html(data);
_this.initedElements[_this.current] = true;
},
error: function(jqXHR, textStatus, errorThrown) {
//some error occured
}
});
}
var dest = { title: '', iata: '' };
$("#ubb_ticket_AirportDeparture, #ubb_ticket_AirportDestination").each(function() {
var selector = '#'+ $(this).attr('id');
var selectorHidden = selector +'Hidden';
$(this).autocomplete({
appendTo: $(this).data('autocomplete'),
source: function( request, response ) {
$.ajax({
url: "/ajax/tickets/search",
dataType: "json",
data: {
query: request.term
},
success: function( data ) {
response( $.map( data.result, function( item ) {
return {
label: item.title,
value: item.iata
}
}));
$(selector).removeClass("notact");
}
});
},
minLength: 2,
select: function( event, ui ) {
dest.title = ui.item.label;
dest.iata = ui.item.value;
$(selectorHidden).val(ui.item.value);
$(selector).val(ui.item.label);
//selected = true;
return false;
},
open: function() {
$(this).autocomplete('widget').css('z-index', 999);
//isSuggestOpen = true;
},
close: function() {
//isSuggestOpen = false;
// if (!selected && !focus1) {
$(selectorHidden).val(dest.iata);
if(dest.title.length > 0)
$(selector).val(dest.title);
if (dest.iata == '')
$(selector).addClass("notact");
else
$(selector).removeClass("notact");
// }
}
});
});
$('#ubb_ticket_OneWay').click(function() {
if($(this).is(":checked")) {
$("#ubb_ticket_ReturnDate").removeAttr("disabled").next().show();
//checkForm();
} else {
$("#ubb_ticket_ReturnDate").attr("disabled", "disabled").next().hide();
//checkForm();
}
});
var datepickerParams = this.datepickerParams;
$("#ubb_ticket_DepartureDate").datepicker(datepickerParams);
$("#ubb_ticket_DepartureDate").datepicker('option', 'onSelect', function() {
$("#ubb_ticket_ReturnDate").datepicker('option', 'minDate', $("#ubb_ticket_DepartureDate").datepicker('getDate'));
});
$("#ubb_ticket_ReturnDate").datepicker(datepickerParams);
$("#ubb_ticket_SearchButton").click(function(){
var family = $.cookie('family');
var urlParams = {
origin_iata : $("#ubb_ticket_AirportDepartureHidden").val(),
destination_iata : $("#ubb_ticket_AirportDestinationHidden").val(),
depart_date : $("#ubb_ticket_DepartureDate").val(),
adults : 1,
children : 0,
infants : 0,
trip_class : 0
};
if(family !== undefined && family !== '') {
family = JSON.parse(family);
if(family.adults !== undefined) {
urlParams.adults = family.adults;
}
if(family.kids_ages.length) {
family.kids_ages.forEach(function(el, index, kids_ages){
if (el < 12 && el >= 2) {
urlParams.infants++;
} else if(el < 2){
urlParams.children++;
} else {
urlParams.adults++;
}
});
}
}
if (!$("#ubb_ticket_OneWay").is(":checked")){
urlParams["oneway"] = 1;
} else {
urlParams["return_date"] = $("#ubb_ticket_ReturnDate").val();
}
//if ($("#range").is(":checked")) urlParams["range"] = 1;
var url = "http://aviasales.redigo.ru/searches/new?marker=13909";
if ($("#ubb_ticket_Referer").length) url += "." + $("#ubb_ticket_Referer").val();
url += "&with_request=true";
for(var i in urlParams) url += "&" + i + "=" + urlParams[i];
if (_gaq) _gaq.push(['_trackEvent', 'BookingBlockAvia', 'SearchAviaDealButton']);
window.open(url, "_blank");
});
return true;
},
showSpinner: function(el) {
var container = $('#flightdeals_container');
var ths = $('th', container);
$(el).parent().find('.monthName').each(function(index, el){
ths.eq(index + 1).text($(el).text());
});
ths.each(function(index, el){
$(el).css('width', $(el).width());
});
container.css('height', container.height());
$('tr:not(:first)', container).remove();
$('table', container).append(' |
');
},
navigateTicket: function(month, el) {
var _this = this;
if(_this.loading)
return;
_this.loading = true;
_this.showSpinner(el);
$.ajax({
url: '/aviasalesHotDeals/'+ _this.destination.path,
data: {
month_num: 3,
limit: 5,
start_month: month
},
success: function(data, textStatus, jqXHR) {
if ($.trim(data) == '') {
_this.loading = false;
return;
};
$('#hotdeals').html(data);
_this.initedElements[_this.current] = true;
_this.loading = false;
},
error: function(jqXHR, textStatus, errorThrown) {
//some error occured
_this.loading = false;
}
});
},
initHotel : function() {
if(this.initedElements[this.current]) return true;
var _this = this;
var destId = _this.destination ? _this.destination._id : 0;
var onlyOneItem = false;
var selectedItem = null;
if(destId) $('#ubb_hotelsList_more').show();
$.ajax({
url: '/ajax/ubb/hotel/destinationData/'+ destId,
success: function(data, textStatus, jqXHR) {
if(data.result && data.result.destination) {
var obj = data.result.destination;
$('#ubb_hotel_Destination').val(obj.label);
$('#ubb_hotel_DestinationHidden').val(obj.search);
if(data.result.destination.title_genetive.length > 0)
$('#ubb_hotel_DestinationPreposition').html(data.result.destination.title_genetive);
selectedItem = obj;
}
if(data.result && data.result.hotels && data.result.hotels.length > 0) {
$('#ubb_hotel_LeftSide').show();
$('#PopularHotelItemTemplate').template('popularHotelItem');
data.result.hotels.forEach(function(item) {
$.tmpl('popularHotelItem', item).appendTo("#ubb_hotel_HotelsList");
});
$('#ubb_hotel_HotelsList').show();
}
if(data.result && data.result.allHotels) {
$('#ubb_hotel_allHotels').attr('href', data.result.allHotels);
}
},
error: function(jqXHR, textStatus, errorThrown) {
//some error occured
}
});
var datepickerParams = this.datepickerParams;
$("#ubb_hotel_CheckinDate, #ubb_hotel_CheckoutDate").datepicker(datepickerParams);
$("#ubb_hotel_CheckinDate").datepicker('option', 'onSelect', function() {
$("#ubb_hotel_CheckoutDate").datepicker('option', 'minDate', $("#ubb_hotel_CheckinDate").datepicker('getDate'));
});
$("#ubb_hotel_Destination").autocomplete({
source: function( request, response ) {
selectedItem = null;
onlyOneItem = false;
$.ajax({
url: '/ajax/ubb/hotel/geo/suggest',
dataType: "json",
data: {
query: request.term
},
success: function( data ) {
response($.map( data.result.destinations, function(item) {
if(data.result.destinations.length == 1) {
selectedItem = item;
onlyOneItem = true;
}
return item;
}));
}
});
},
appendTo: $('#ubb_hotel_Destination').data('autocomplete'),
minLength: 2,
select: function( event, ui ) {
$('#ubb_hotel_DestinationHidden').val(ui.item.search);
$('#ubb_hotel_Destination').val(ui.item.label);
selectedItem = ui.item;
return false;
},
open: function(event, ui) {
},
close: function() {
if(onlyOneItem) {
$('#ubb_hotel_DestinationHidden').val(selectedItem.search);
$('#ubb_hotel_Destination').val(selectedItem.label);
}
},
change: function( event, ui ) {
if((!ui.item && !onlyOneItem) || !$('#ubb_hotel_Destination').val().length) {
selectedItem = null;
onlyOneItem = false;
}
}
});
function encodeQueryData(data)
{
var ret = [];
for (var d in data)
ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
return ret.join("&");
}
$("#ubb_hotel_SearchButton").click(function(){
var error = false;
$('.ubb_hotel_div').removeClass('ym-error');
if (!selectedItem) {
$('#ubb_hotel_DestinationHidden').val('');
$('#ubb_hotel_Destination').val('');
$('#ubb_hotel_Destination_div').addClass('ym-error');
error = true;
}
var checkin = $('#ubb_hotel_CheckinDate').datepicker('getDate');
var checkout = $('#ubb_hotel_CheckoutDate').datepicker('getDate');
if (!checkin) {
$('#ubb_hotel_CheckinDate_div').addClass('ym-error');
error = true;
}
if (!checkout) {
$('#ubb_hotel_CheckoutDate_div').addClass('ym-error');
error = true;
}
if (error) {
return;
}
var params = {
txtDestino: selectedItem.value,
list_id: selectedItem.id,
D1: $.datepicker.formatDate('dd', checkin),
MA1: $.datepicker.formatDate('mm_yy', checkin),
D2: $.datepicker.formatDate('dd', checkout),
MA2: $.datepicker.formatDate('mm_yy', checkout)
};
var guests = $('#ubb_hotel_GuestsNumber').val();
var elems = guests.split('-');
params['H'] = elems[1];
for(var O=1;O<=elems[1];O++) {
params['O' + O] = parseInt(elems[0] / O) + 'A0N';
}
var url = 'http://hotels.redigo.ru/resultadosBusqueda.php?' + encodeQueryData(params);
if (_gaq) _gaq.push(['_trackEvent', 'BookingBlockHotel', 'SearchHotelButton']);
$(this).attr('href', url);
});
this.initedElements[this.current] = true;
return true;
},
clearSelect : function(selectId) {
$('#' + selectId).empty();
$('#' + selectId)
.append($('