$(document).ready(function() {

    var frm$ = $('.st-booking-form');
    var summary$ = $('.st-booking-summary');

    var roomCountSR$ = $('#booking_request_singleRoomCount');
    var roomCountDR$ = $('#booking_request_doubleRoomCount');
    var roomCountTR$ = $('#booking_request_tripleRoomCount');
    //var travelersIndices = $('.st-traveller-index');

    var travelers$ = $('.st-traveler');
    var travelerFields$ = travelers$.find('input,select');

    frm$.find('input, select').change(function() {
        var tmp = location.href.split('?');
        var tmp2 = tmp[0].split('/');
        tmp2.pop();
        var url = tmp2.join('/') + '/berechne-gesamtpreis';
        if (tmp[1])
        {
            url += '?'+ tmp[1];
        }

        $.ajax({
            url: url,
            type: 'post',
            data: frm$.serialize()

        }).then(function(r) {
            summary$.html(r);
           /*
            $('#st-booking-price-set-table .st-total-price-set').html($(r).find('.st-total-price').text());
            $('#st-booking-price-set-table .st-deposit-price-set').html($(r).find('.st-deposit-price').text());
            $('#st-booking-price-set-table .st-final-price-set').html($(r).find('.st-final-price').text());
            */

        }, function() {
            summary$.html('Aufgrund eines Fehlers konnte kein Angebot ermittelt werden.');
        })
    });

    $('#booking_request_departure').change(function () {
        //show nothing
        $('.flight_times_ele').hide();
        $('#no_flight_time').hide();
        $('#yes_flight_time').hide();

        console.log($(this).val());
        if(!$(this).val() || $(this).find("option:selected").text().indexOf("Eigenanreise") == 0){
            return;
        }
        console.log($('#flight_time_'+$(this).val()).length);
        if($('#flight_time_'+$(this).val()).length){
            $('#flight_time_'+$(this).val()).show();
            $('#yes_flight_time').show();
        }else{
            $('#no_flight_time').show();
        }
    });
    var toggle_first = false;

    function updateNationality(ele){
        var obj = ele.parents('.nationality_select').data('toggle');
        if(ele.val()){
           $(obj).show('slow');
           if(!toggle_first){
                $(obj).find('.accordion-toggle').click();
                toggle_first = true;
           }
        }else{
           $(obj).hide();
        }
    }
    
    $('.nationality_select').find('select').change(function(){
       updateNationality($(this));
    });


    function updateAllNationality(){
        $('.nationality_select').find('select').each(function(){
            updateNationality($(this));
        });
    }
    updateAllNationality();


    function toggleUpdateChevron(e) {
        var $i = $(e.target)
            .parent('.panel-group')
            .find("i.indicator");
        
        $i.toggleClass('icon-minus icon-plus');
    }
    $('.acc_nationality_panel').on('hidden.bs.collapse', toggleUpdateChevron);
    $('.acc_nationality_panel').on('shown.bs.collapse', toggleUpdateChevron);


    function updateTravelers()
    {
        var travelersIndex = 1;

        var singleTravelerCount = parseInt(roomCountSR$.val());
        var doubleTravelerCount = parseInt(roomCountDR$.val()) * 2;
        var tripleTravelerCount = parseInt(roomCountTR$.val()) * 3;

        var allTravelersCount = singleTravelerCount + doubleTravelerCount + tripleTravelerCount;

        travelers$.hide();
        travelerFields$.prop('required', false);
        var counter = 1;
        if(singleTravelerCount > 0)
        {
            for(var i = 1; i <= singleTravelerCount; i++)
            {
                var travelerSelector = '.st-traveler-' + i;
                var roomIndexSelector = travelerSelector+' .st-traveller-index';
                var currentRoomIndex = $(travelerSelector).attr("data-room-index");
                $(travelerSelector).show().find('input,select').prop('required', true);
                //$(roomIndexSelector).text(currentRoomIndex);
                $(roomIndexSelector).text(counter + ".) Reiseteilnehmer | Einzelzimmer");
                counter ++;
            }
        }

        if(doubleTravelerCount > 0)
        {
            var offset = 5 - singleTravelerCount;
            for(var j = 6; j <= doubleTravelerCount + 5; j++)
            {
                var travelerSelector = '.st-traveler-'+j;
                var roomIndexSelector = travelerSelector+' .st-traveller-index';
                var currentRoomIndex = $(travelerSelector).attr("data-room-index");
                $(travelerSelector).show().find('input,select').prop('required', true);
                //$(roomIndexSelector).text(currentRoomIndex - offset);
                $(roomIndexSelector).text(counter + ".) Reiseteilnehmer | Doppelzimmer");
                counter ++;
            }
        }

        if(tripleTravelerCount > 0)
        {
            var offset = 10 - (singleTravelerCount + (doubleTravelerCount / 2));
            for(var k = 16; k <= tripleTravelerCount + 15; k++)
            {
                var travelerSelector = '.st-traveler-'+k;
                var roomIndexSelector = travelerSelector+' .st-traveller-index';
                var currentRoomIndex = $(travelerSelector).attr("data-room-index");
                $(travelerSelector).show().find('input,select').prop('required', true);
                //$(roomIndexSelector).text(currentRoomIndex - offset);
                $(roomIndexSelector).text(counter + ".) Reiseteilnehmer | Dreibettzimmer");
                counter ++;
            }
        }

    }
    roomCountSR$.change(updateTravelers);
    roomCountDR$.change(updateTravelers);
    roomCountTR$.change(updateTravelers);
    updateTravelers();
});