function level_update() {
    var level = 0;
    $('.percents').find('.refer_percent').each(function () {
        level++;
        $(this).find('.level').text(level);
    })
}

function update_percents() {
    var percent_string = '',
        percents = [];

    if ($('#select_percent').val() != 'new') {
        var refer_percent = $('.percents > .refer_percent').clone()[0].outerHTML;

        percent_string = $('#select_percent option:selected').text().replace(/%/g, '');
        percents = percent_string.split(' - ');

        $('.percents > .refer_percent').remove();

        $.each(percents, function (index, value) {
            $('.percents').append(refer_percent);
            $('.percents > .refer_percent:last-child').find('.input').val(value);
        });

        level_update();
    }
}

/* Добавим новый % реф. системы */
$('.percents').on('click', '.level_add', function () {
    var refer_percent = $(this).parent().clone(),
        this_parent = $(this).parent();

    this_parent.after(refer_percent[0].outerHTML);
    this_parent.next().find('.input').val(this_parent.find('.input').val());

    level_update();
});

/* Удалим % реф. системы */
$('.percents').on('click', '.level_delete', function () {
    var count = $('.percents > .refer_percent').size();

    if (count > 1) {
        $(this).parent().remove();

        level_update();
    }
});

$('.percents').on('change keyup', '.input', function () {
    if ($('#select_percent').val != 'new') {
        $('#percent_notification').show();
    }
});

$('#select_percent').on('change', function () {
    update_percents();
});

$(document).ready(function () {
    update_percents();
})