//{{{{{{{{{{{{{Калькуляторы}}}}}}}}}}}}}}}}//
function progression(a) {
    return Math.round(0.1 * Math.pow(a, 2));
}

function reprogression(a) {
    return Math.round(Math.sqrt(a * 10));
}
$(document).ready(function () {
    /////////////// {{НАЧАЛО}} ВРЕМЕННЫЕ ///////////////
    if ($('body').hasClass('temporary')) {

        /////////////// Визуализузация ///////////////
        if ($('body').hasClass('bank_open_vklad')) {

            //переключение валюты
            $('#choice-currency span').click(function () {
                if (!$(this).hasClass('activeValuta')) {
                    $('.activeValuta').removeClass('activeValuta');
                    $(this).addClass('activeValuta');
                    $('#actualCurrency,#depositValuta').text($(this).text());
                    liveInCalc();
                }
                return false;
            });

            //переключение типа выплат
/*            $('#depositWay > div').click(function () {
                if (!$(this).hasClass('activeDeposite')) {
                    $('.activeDeposite').removeClass('activeDeposite');
                    $(this).addClass('activeDeposite');
                }
                return false;
            });*/

            $('#currencySlider').slider({
                orientation: "horizontal",
                range: "min",
                animate: true,
                value: 250,
                min: 0,
                max: 1000,
                slide: function (event, ui) {
                    var a = ui.value;
                    var c = 0;
                    var sum = 0;
                    if (a <= 250) {
                        c = a;
                        sum = c * 400;
                    }
                    if (a > 250 && a <= 500) {
                        c = a - 250;
                        sum = 100000 + c * 1600;
                    }
                    if (a > 500 && a <= 750) {
                        c = a - 500;
                        sum = 500000 + c * 18000;
                    }
                    if (a > 750 && a <= 1000) {
                        c = a - 750;
                        sum = 5000000 + c * 20000;
                    }
                    $('#currency').val(number_format(sum, '0', '', ' '));
                    liveInCalc();
                }
            });

            $('#durationSlider').slider({
                orientation: "horizontal",
                range: "min",
                animate: true,
                value: 250,
                min: 0,
                step: 1,
                max: 1000,
                stop: function (event, ui) {
                    if (ui.value <= 130) {
                        var vl = 0;
                        var sel = 35;
                    }
                    if (ui.value > 130 && ui.value <= 375) {
                        var vl = 250;
                        var sel = 95;
                    }
                    if (ui.value > 375 && ui.value <= 630) {
                        var vl = 500;
                        var sel = 185;
                    }
                    if (ui.value > 630 && ui.value <= 850) {
                        var vl = 750;
                        var sel = 370;
                    }
                    if (ui.value > 850) {
                        var vl = 1000;
                        var sel = 1100;
                    }
                    $('#duration').val(number_format(sel, '0', '', ' '));
                    $('#durationSlider').slider("option", "value", vl);
                    liveInCalc();
                    $('.activeBackground').width( Math.round( 100/1100 * sel ) + "%" );
                }
            });

            $('#currency').keyup(function () {
                var val = $(this).val().replace(/\s/g, '') * 1;
                if (val > Math.pow(10, 7)) {
                    val = Math.pow(10, 7);
                }
                if (val < 0) {
                    val = 0;
                }
                $(this).val(number_format(val, '0', '', ' '));
                var position = 0;
                if (val <= 100000) {
                    position = val / 400;
                }
                if (val > 100000 && val <= 500000) {
                    position = 250 + (val - 100000) / 1600;
                }
                if (val > 500000 && val <= 5000000) {
                    position = 500 + (val - 500000) / 18000;
                }
                if (val > 5000000 && val <= 10000000) {
                    position = 750 + (val - 5000000) / 20000;
                }
                $('#currencySlider').slider("option", "value", position);
                liveInCalc();
            });
        }
        $('.cuteRadio.vkladConfigurator ins').click(function () {
            liveInCalc()
        });
        $('.cuteRadio.vkladConfigurator label').click(function () {
            liveInCalc()
        });
        liveInCalc();
    }
    /////////////// {{КОНЕЦ}} ВРЕМЕННЫЕ ///////////////

    /////////////// {{НАЧАЛО}} ЮРЛИЦА ///////////////
    if ($('body').hasClass('deposit')) {



        /////////////// Визуализузация ///////////////
        if ($('body').hasClass('bank_open_vklad')) {

            $('#currencySlider').slider({
                orientation: "horizontal",
                range: "min",
                animate: true,
                value: 250,
                min: 0,
                max: 1000,
                slide: function (event, ui) {
                    var a = ui.value;
                    var c = 0;
                    var sum = 0;
                    if (a <= 250) {
                        c = a;
                        sum = c * 400;
                    }
                    if (a > 250 && a <= 500) {
                        c = a - 250;
                        sum = 100000 + c * 1600;
                    }
                    if (a > 500 && a <= 750) {
                        c = a - 500;
                        sum = 500000 + c * 18000;
                    }
                    if (a > 750 && a <= 1000) {
                        c = a - 750;
                        sum = 5000000 + c * 20000;
                    }
                    $('#currency').val(number_format(sum, '0', '', ' ')).change();

                }
            });

            $('#currency').keyup(function () {
                var val = $(this).val().replace(/\s/g, '') * 1;
                if (val > Math.pow(10, 7)) {
                    val = Math.pow(10, 7);
                }
                if (val < 0) {
                    val = 0;
                }
                $(this).val(number_format(val, '0', '', ' '));
                var position = 0;
                if (val <= 100000) {
                    position = val / 400;
                }
                if (val > 100000 && val <= 500000) {
                    position = 250 + (val - 100000) / 1600;
                }
                if (val > 500000 && val <= 5000000) {
                    position = 500 + (val - 500000) / 18000;
                }
                if (val > 5000000 && val <= 10000000) {
                    position = 750 + (val - 5000000) / 20000;
                }
                $('#currencySlider').slider("option", "value", position);
                $('#currency').change();
            });

            $('#duration').keyup(function () {
                var val = $(this).val().replace(/\s/g, '') * 1;
                if(val > 736)
                    val = 736;
                $(this).val(val);
                $('#durationSlider').slider("option", "value", val);

            });

            $('#durationSlider').slider({
                orientation: "horizontal",
                range: "min",
                animate: true,
                value: 100,
                min: 0,
                step: 1,
                max: 736,
                slide: function (event, ui) {
                    $('#duration').val(number_format(ui.value, '0', '', ' ')).change();
                    $('.activeBackground').width( Math.round( 100/736 * ui.value ) + "%" );
                }
            });
        }


        $('#currency,#duration').bind('keyup change', function () {
            var dayz = $('#duration').val().replace(/\s/g, '') * 1;
            var inSumm = $('#currency').val().replace(/\s/g, '') * 1;
            var st = 0;
            //Сначала ошибки
            $('.summSlider').show();
            $('.summSlider.alert').hide().children('div').hide();
            if(dayz < 1){
                $('.depositWarnings')
                    .html("<p>Укажите срок.</p>")
                    .css({'display':'block'});
                $('.blueButton').addClass('disabled');
                return false;
            }
            if (dayz < 35 && inSumm < 500000) {
                $('.depositWarnings')
                    .html("<p>Укажите сумму больше <span><strong>500 000</strong> рублей</span><br> или срок больше <span><strong>35</strong> дней</span>.</p>")
                    .css({'display':'block'});
                $('.blueButton').addClass('disabled');
                return false;
            }
            if (inSumm < 100000) {
                $('.depositWarnings')
                    .html("<p>Укажите сумму больше <span><strong>100 000</strong> рублей</span>.</p>")
                    .css({'display':'block'});
                $('.blueButton').addClass('disabled');
                return false;
            }
            $('.blueButton').removeClass('disabled');
            $('.depositWarnings').css({'display':'none'});

            //теперь ставки из таблички
            if (inSumm >= 500000 && dayz < 35) {
                if (dayz <= 15)
                    st = 0.0225;
                if (dayz >= 16 && dayz <= 25)
                    st = 0.0275;
                if (dayz >= 26)
                    st = 0.03;
            } else {
                if (inSumm <= 1000000) {
                    if (dayz <= 94)
                        st = 0.05;
                    if (dayz >= 95 && dayz <= 184)
                        st = 0.075;
                    if (dayz >= 185 && dayz <= 366)
                        st = 0.085;
                    if (dayz >= 367 && dayz <= 546)
                        st = 0.095;
                    if (dayz >= 547 && dayz <= 732)
                        st = 0.1;
                    if (dayz >= 733 && dayz <= 736)
                        st = 0.105;
                }
                if (inSumm >= 1000001 && inSumm <= 10000000) {
                    if (dayz <= 94)
                        st = 0.055;
                    if (dayz >= 95 && dayz <= 184)
                        st = 0.08;
                    if (dayz >= 185 && dayz <= 366)
                        st = 0.09;
                    if (dayz >= 367 && dayz <= 546)
                        st = 0.1;
                    if (dayz >= 547 && dayz <= 732)
                        st = 0.105;
                    if (dayz >= 733 && dayz <= 736)
                        st = 0.11;
                }
                if (inSumm >= 10000001) {
                    if (dayz <= 94)
                        st = 0.06;
                    if (dayz >= 95 && dayz <= 184)
                        st = 0.085;
                    if (dayz >= 185 && dayz <= 366)
                        st = 0.095;
                    if (dayz >= 367 && dayz <= 546)
                        st = 0.105;
                    if (dayz >= 547 && dayz <= 732)
                        st = 0.11;
                    if (dayz >= 733 && dayz <= 736)
                        st = 0.115;
                }
            }
            var ttl = (inSumm * (1 + (st / 365 * dayz)));
            var hmch = ttl - inSumm;
            $('#depositDuration').text(dayz);
            $('#depositSumma').text(number_format(hmch, 2, ',', ' '));
            $('#depositRate').text(st * 1000 / 10 + '%');
            $('#vkladForm input[name=summ]').val(inSumm);
            $('#vkladForm input[name=days]').val(dayz);
        });
        $('#currency').change();
    }
/////////////// {{КОНЕЦ}} ЮРЛИЦА ///////////////
});



//////////////Калькулятор временных вкладов физлиц////////////////
var clsk = '';
function liveInCalc() {
    var inSumm = $('#currency').val().replace(/\s/g, '') * 1;
    var dayz = $('#duration').val().replace(/\s/g, '') * 1;
    var valute = $('#actualCurrency').text();
    var valuteForForm = valute == 'РУБ.' ? 'rur' : valute.toLowerCase();
    switch (valute) {
        case 'РУБ.':
            //показываем варианты порядокв выплат
            if (inSumm < 1000) {
                $('.depositWarnings')
                    .html("<p>Укажите сумму больше <span><strong>1 000</strong> рублей</span>.</p>")
                    .css({'display':'block'});
                $('.blueButton').addClass('disabled');
                return false;
            }


            //Если информации для смены варианта не появилось, используем данные текущего выбранного.
            if (clsk != '.rur' + dayz) {
                $('.cuteRadio.vkladConfigurator .rbl').hide();
                clsk = '.rur' + dayz;
                $(clsk).css("display", "inline-block");
                $('.cuteRadio.vkladConfigurator label:visible').first().click();
            }
            var jsn = JSON.parse($('.cuteRadio.vkladConfigurator input:checked').val());
            if (inSumm <= 10000000)
                var st = jsn.st1;
            else
                var st = jsn.st2
            break;
        default:
            if (inSumm * 1 < 300) {
                $('.depositWarnings')
                    .html("<p>Укажите сумму больше <span><strong>300</strong> "+ valute +"</span>.</p>")
                    .css({'display':'block'});
                $('.blueButton').addClass('disabled');
                return false;
            }
            $('.sectioned3.left.rlyLeft .alert.notify').hide();
            if (clsk != '.eur' + dayz) {
                $('.cuteRadio.vkladConfigurator .rbl').hide();
                clsk = '.eur' + dayz;
                $(clsk).css("display", "inline-block");
                $('.cuteRadio.vkladConfigurator label:visible').first().click();

            }
            var jsn = JSON.parse($('.cuteRadio.vkladConfigurator input:checked').val());
            var st = jsn.st3

            break;
    }
    $('.blueButton').removeClass('disabled');
    $('.depositWarnings').css({'display':'none'});
    var ttl = (inSumm * (1 + (st / 365 * dayz)));
    var hmch = ttl - inSumm;
    $('.summSlider.percent bdo').text(st * 1000 / 10);
    $('#depositSumma').text(number_format(hmch, 2, ',', ' '));
    $('#depositDuration').text(number_format(dayz, 0, ',', ' '));
    $('#depositRate').text(number_format((st * 100), 1, ',', ' ') + '%');

    $('#vkladForm input[name=summ]').val(inSumm);
    $('#vkladForm input[name=days]').val(dayz);
    $('#vkladForm input[name=currency]').val(valuteForForm);
}