$(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 roomChildCountSR$ = $('#booking_request_singleRoomChildCount');
    var roomChildCountDR$ = $('#booking_request_doubleRoomChildCount');
    var roomChildCountTR$ = $('#booking_request_tripleRoomChildCount');


    //var travelersIndices = $('.st-traveller-index');

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

    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 toggleChevronIn(e) {
        console.log(e);
        var $i = $(e.target)
            .parent('.panel-group')
            .find("i.indicator");

        $i.toggleClass('icon-minus icon-plus');

        if($i.hasClass('icon-minus')){
            $i.parent('h5').find('span').html('Leistungen ausblenden');
        }
        if($i.hasClass('icon-plus')){
            $i.parent('h5').find('span').html('Leistungen einblenden');
        }
    }

    $('.booking_insurance_toogle').on('hidden.bs.collapse', toggleChevronIn);
    $('.booking_insurance_toogle').on('shown.bs.collapse', toggleChevronIn);

        

    function updateTravelers()
    {
        var travelersIndex = 1;

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

        var singleTravelerChildCount = parseInt(roomChildCountSR$.val()) * 2;
        var doubleTravelerChildCount = parseInt(roomChildCountDR$.val()) * 3;
        var tripleTravelerChildCount = parseInt(roomChildCountTR$.val()) * 4;

        var allTravelersCount = singleTravelerCount + doubleTravelerCount + tripleTravelerCount + singleTravelerChildCount + doubleTravelerChildCount + tripleTravelerChildCount;

        travelers$.hide();
        travelerFields$.prop('required', false);

        var counter = 1;
        var $offset = 1;

        //single
        if(singleTravelerCount > 0)
        {
            for(var i = $offset; 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(counter + ".) Reiseteilnehmer | Einzelzimmer | 1. Erwachsender");
                counter ++;
            }
        }
        //single + child
        $offset = $offset + parseInt(roomCountSR$.find('option').length) -1;
        //4 next 5

        if(singleTravelerChildCount > 0)
        {
            for(var i = $offset; i < singleTravelerChildCount + $offset; 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(counter + ".) Reiseteilnehmer | Einzelzimmer | 1. Erwachsender");
                counter ++;

                //child
                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(counter + ".) Reiseteilnehmer | Einzelzimmer | Kind");
                counter ++;
            }
        }
        //double
        $offset = $offset + (parseInt(roomChildCountSR$.find('option').length) -1) * 2;
        //4 + 4*2 next 13
        if(doubleTravelerCount > 0)
        {
            $m = 1;

            for(var i = $offset; i < doubleTravelerCount + $offset; 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(counter + ".) Reiseteilnehmer | Doppelzimmer | " +  $m + ". Erwachsender");
                counter ++;
                $m++;
                if($m == 3){
                    $m = 1;
                }
            }
        }
        //double + child
        $offset = $offset + (parseInt(roomCountDR$.find('option').length) -1) * 2;
        //12 + 3*2 next 19
        if(doubleTravelerChildCount > 0)
        {
            $m = 1;
            for(var i = $offset; i < doubleTravelerChildCount + $offset; 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(counter + ".) Reiseteilnehmer | Doppelzimmer | " +  $m + ". Erwachsender");
                counter ++;
                $m++;
                if($m == 3){
                    $m = 1;
                    //child
                    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(counter + ".) Reiseteilnehmer | Doppelzimmer | Kind");
                    counter ++;
                }
            }
        }

        //triple
        $offset = $offset + (parseInt(roomChildCountDR$.find('option').length) -1) * 3;
        //18 + 3*3 next 28
        if(tripleTravelerCount > 0)
        {
            $m = 1;
            for(var i = $offset; i < tripleTravelerCount + $offset; 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(counter + ".) Reiseteilnehmer | Dreibettzimmer | " +  $m + ". Erwachsender");
                counter ++;
                $m++;
                if($m == 4){
                    $m = 1;
                }
            }
        }

        //triple + child
        $offset = $offset + (parseInt(roomCountTR$.find('option').length) -1) * 3;
        //27 + 2*3 next 34
        if(tripleTravelerChildCount > 0)
        {
            $m = 1;
            for(var i = $offset; i < tripleTravelerChildCount + $offset; 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(counter + ".) Reiseteilnehmer | Dreibettzimmer | " +  $m + ". Erwachsender");
                counter ++;
                $m++;
                if($m == 4){
                    $m = 1;
                    //child
                    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(counter + ".) Reiseteilnehmer | Dreibettzimmer | Kind");
                    counter ++;
                }
            }
        }

    }

    roomCountSR$.change(updateTravelers);
    roomCountDR$.change(updateTravelers);
    roomCountTR$.change(updateTravelers);
    roomChildCountSR$.change(updateTravelers);
    roomChildCountDR$.change(updateTravelers);
    roomChildCountTR$.change(updateTravelers);
    updateTravelers();
});