﻿
$(function () {
    var dates = $(".textbox.datefrom, .textbox.dateto").datepicker({
        dateFormat: 'dd/mm/yy',
        minDate: 0,
        maxDate: '+12M',
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: true,
        showOn: 'both',
        showButtonPanel: true,
        closeText: 'Close calendar',
        buttonImage: cal_icon,
        buttonImageOnly: true,
        showAnim: 'slideDown',
        onSelect: function (selectedDate) {
            var option = $(this).hasClass('datefrom') ? "minDate" : "maxDate";
            var instance = $(this).data("datepicker");
            var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);

            //syncronize dropdowns

            if ($(this).hasClass('datefrom')) {
                $('.dropdown.datefrom.day').val(date.getDate());
                $('.dropdown.datefrom.month').val((date.getMonth() + 1) + '/' + date.getFullYear());
            } else {
                $('.dropdown.dateto.day').val(date.getDate());
                $('.dropdown.dateto.month').val((date.getMonth() + 1) + '/' + date.getFullYear());
            }
            if ($(this).hasClass('datefrom'))
                date.setDate(date.getDate() + 1);
            else
                date.setDate(date.getDate() - 1);

            dates.not(this).datepicker("option", option, date);

            updateMultipleRoomsLink();
        }
    });
    $('.dropdown.datefrom.day, .dropdown.datefrom.month').change(function () {
        var day = $('.dropdown.datefrom.day').val();
        var monthyear = $('.dropdown.datefrom.month').val();

        var strDate = day + '/' + monthyear;

        var dateParts = strDate.split("/");

        var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
        date.setDate(date.getDate() + 1);

        $('.dropdown.dateto.day').val(date.getDate());
        $('.dropdown.dateto.month').val((date.getMonth() + 1) + '/' + date.getFullYear());
    });
    $('.next_to_checkbox_').show();

    $('.searchbox .checkavailability').change(function (e) {
        $('.next_to_checkbox_').toggle();
    });
    $('.defaulttext').toggleVal({ populateFrom: 'title', changedClass: 'defaulttext_changed', focusClass: 'defaulttext_changed' });

    updateMultipleRoomsLink();
    $('.searchbox .textbox, .searchbox .dropdown, .searchbox .textbox').change(function () {
        updateMultipleRoomsLink();
    });

    $.widget("custom.catcomplete", $.ui.autocomplete, {
        _renderMenu: function (ul, items) {
            var self = this,
				currentCategory = "";
            $.each(items, function (index, item) {
                if (item.category != currentCategory) {
                    var label = item.category;
                    if (label == "City")
                        label = "Cities";
                    else if (label == "Town")
                        label = "Towns";
                    else if (label == "District")
                        label = "Districts";
                    else if (label == "County")
                        label = "Counties";
                    else if (label == "State")
                        label = "States";
                    else if (label == "Hotel")
                        label = "Hotels";
                    else if (label == "Region")
                        label = "Regions";
                    else if (label == "Landmark")
                        label = "Landmarks";
                    else if (label == "Airport")
                        label = "Airports";

                    ul.append("<li class='ui-autocomplete-category'><span>" + label + "</span></li>");
                    currentCategory = item.category;
                }
                self._renderItem(ul, item);
            });
            if (acTotal > acTotalMax) {
                ul.append("<span class='ui-autocomplete-footer'><em>Keep typing for more destinations (" + acTotal + " in total)</em></span>");
            }

        }
    });

    $('.actype').change(function () {
        if (document.getElementById('rb_destinations').checked) {
            $('.enterlocation').attr('value', 'Place, Hotel, Landmark');
            $('.enterlocation').attr('title', 'Place, Hotel, Landmark');
        } else {
            $('.enterlocation').attr('value', 'Enter Hotel Name');
            $('.enterlocation').attr('title', 'Enter Hotel Name');
        }
        $('.defaulttext').toggleVal({ populateFrom: 'title', changedClass: 'defaulttext_changed', focusClass: 'defaulttext_changed' });
    });

    var acTotal;
    var acTotalMax = 20;
    $(".enterlocation").catcomplete({
        source: function (request, response) {
            var acType = 'destinations';
            if (document.getElementById('rb_destinations') && document.getElementById('rb_destinations').checked == false)
                acType = 'hotels';

            $.ajax({
                url: "/New/Ajax/AutoCompleteLocations.aspx",
                dataType: "json",
                data: {
                    maxRows: acTotalMax,
                    name_startsWith: request.term,
                    type: acType
                },
                success: function (data) {
                    ShowFooter = data.ShowFooter;
                    acTotal = data.Total;
                    response(data.Terms);
                }
            });
        },
        minLength: 3,
        delay: 500,
        select: function (event, ui) {
            event.target.focus();
            event.target.select();
        },
        open: function () {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });

});

function updateMultipleRoomsLink() {
    var ss = ($('.defaulttext').val() == $('.defaulttext').attr('title') ? '' : $('.defaulttext').val());
    $('#multiple_rooms').attr('href',
    'http://www.booking.com/index.en-gb.html?aid=334487&ss=' + ss +
    '&checkin_monthday=' + $('.dropdown.datefrom.day').val() + '&checkin_year_month=' + $('.dropdown.datefrom.month').val().split('/')[1] + '-' + $('.dropdown.datefrom.month').val().split('/')[0] +
    '&checkout_monthday=' + $('.dropdown.dateto.day').val() + '&checkout_year_month=' + $('.dropdown.dateto.month').val().split('/')[1] + '-' + $('.dropdown.dateto.month').val().split('/')[0] + 
    '&do_availability_check=1&label=cobrand');
}
