$(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);

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

    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);

        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("Einzelzimmer");
            }
        }

        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("Doppelzimmer");
            }
        }

        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("Dreibettzimmer");
            }
        }

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