/*
    Calculator - Binbank (c) 2009
    2009 (c) Flipflip.ru
*/

var period = 6;
var step = 0.9795;

$(document).ready(function() {
    /* Обнуление параметров */

    $('#type').val('0');
    $('#money').val('').attr('disabled', 'disabled');

    $('#procent').attr('checked', '');
    $('#all').attr('checked', '');
    $('#chast').attr('checked', '');
    $('#popolnenie').attr('checked', '');


    $('#slider').slider({
        range: false,
        value: step * period,
        step: step,
        animate: true,
        min: 0,
        max: 15,
        change: function() {
            $('#period').css('color', '#074589');
            period = Math.round($(this).slider('option', 'value'));
            if (period == 0) {
                period = 1;
                $(this).slider('option', 'value', step * period);
            }

            showPeriod(period);
        }

    });

    showPeriod(period);

    /* Деньги */

    $('#money').keyup(function() {
        if ($(this).val() > 0) {
            $(this).val(parseInt($(this).val())); 
        }
        else {
            $(this).val('');
        }

        if ($(this).val() < arrayMinimum[$('#type').val()]) {
            $('#alert-money').show();
            $(this).css('color', '#e36a00');
        }
        else {
            $('#alert-money').hide();
            $(this).css('color', '#074589');
            showPeriod(period);
        }
        
    });

    $('#type').change(function() {
        var objMoney = $('#money');

        if ($(this).val() > 0) {
            $(this).css('color', '#074589');

            var val = $(this).val();
            objMoney.attr('disabled', '');

            switch (val) {
                case '1':
                    objMoney.val('0');
                    break;

                case '2':
                    objMoney.val('0');
                    break;

                case '3':
                    objMoney.val('0');
                    break;
            }

            $('#alert-money').hide();
        }
        else {
            $(this).css('color', '#000000');
            objMoney.attr('disabled', 'disabled').val('');
        }

        showPeriod($('#slider').slider(period));
    });


    if ($.browser.msie) {
        $('input[type=checkbox]').click(function() {
            if (true) {
                if ($(this).attr('name') == 'chast' && $(this).attr('checked')) {
                    if ($('#procent').attr('checked')) {
                        $('#procent').attr('checked', '')
                    }
                }

                if ($(this).attr('name') == 'procent' && $(this).attr('checked')) {
                    if ($('#chast').attr('checked')) {
                        $('#chast').attr('checked', '')
                    }
                }

                showPeriod(period);
            }
        });
    }
    else {
        $('input[type=checkbox]').change(function() {
            if (true) {
                if ($(this).attr('name') == 'chast' && $(this).attr('checked')) {
                    if ($('#procent').attr('checked')) {
                        $('#procent').attr('checked', '')
                    }
                }

                if ($(this).attr('name') == 'procent' && $(this).attr('checked')) {
                    if ($('#chast').attr('checked')) {
                        $('#chast').attr('checked', '')
                    }
                }

                showPeriod(period);
            }
        });
    }
});

function showPeriod(val) {
    $('#period').val(arrayPeriod[val]);

    if (period < 4) {
        $('input[type=checkbox]').attr('checked', '');
    }

    var m = parseInt($('#money').val());

    /* Скрытие таблицы и периодов */

    var objType = $('#type');
    var checkedPoplnenie = $('#popolnenie').attr('checked');
    var checkedProcent = $('#procent').attr('checked');
    var checkedChast = $('#chast').attr('checked');
    var checkedAll = $('#all').attr('checked');

    $('#tbody tr').each(function() {

        /* Промежутки */

        if ($(this).attr('period') >= period + 1) {
            $(this).hide();
        }
        else {

            /* Выбрана валюта */

            if (objType.val() > 0) {
                if (!$(this).attr('type') || $(this).attr('type') != objType.val()) {
                    $(this).hide();
                }
                else {
                    $(this).show();
                }
            }
            else {
                if ($(this).attr('type')) {
                    $(this).hide();
                }
                else {
                    $(this).show();
                }
            }

        }

        if (parseInt($(this).attr('minimum')) > m && m != 0) {
            $(this).hide();
        }
        else {
            if (arrayPercent[$(this).attr('number')][$(this).attr('valuta')][0][0][period] == 0) {
                $(this).hide();
            }
        }


        /* Если можно полнять */

        if (checkedPoplnenie) {
            if ($(this).attr('add') != '1') {
                $(this).hide();
            }
        }

        /* Проценты каждый месяц */

        if (checkedProcent) {
            if ($(this).attr('month') != '1') {
                $(this).hide();
            }
        }

        /* Снять часть вклада */

        if (checkedChast) {
            if ($(this).attr('chast') != '1') {
                $(this).hide();
            }
        }

        /* Полное снятие */

        if (checkedAll) {
            if ($(this).attr('full') != '1') {
                $(this).hide();
            }
        }

    });

    /* Расчет процентов */

    var p = 0;
    var s = 0;
    var t = 1;
    var b = 0;
    var k = 0;

    $('#tbody tr td.percent').html('');
    $('#tbody tr td.summa').html('');

    $('#tbody tr td span').each(function() {
        if ($(this).hasClass('base')) {
            $(this).show();
        }

        if ($(this).hasClass('procent')) {
            $(this).hide();
        }
    });

    if ($('#money').attr('disabled')) {
    }
    else {


        if (objType.val() > 1) {
            t = 2;
        }

        for (var number in arrayPercent) {
            k = 0;
            //for (var valuta in arrayPercent[number])

            var valuta = objType.val(); 
            if (true) {
                //if ($('#money').attr('disabled')) {
                    //$('#table tbody tr[number=' + number + '][valuta=' + valuta + '] td.percent').html(arrayPercent[number][valuta][n][0][period]);
                    //$('#table tbody tr td.percent').html('');
                //}
                //else {
                for (var money in arrayPercent[number][valuta][k]) {
                    if (money > 0 && m >= money) {

                        if (!checkedProcent && !checkedChast && !checkedAll && typeof(arrayPercent[number][valuta][1]) == 'object') {
                            k = 1;
                        }
                        else {
                            k = 0;
                        }
                        p = b = arrayPercent[number][valuta][k][money][period];

                        if (p > 0) {

                            if (arrayPopravka[number]) {
                                if (checkedProcent) {
                                    p = p + arrayOption[1][arrayCorrectOption[number]][t];
                                }

	                            if (checkedAll) {
	                                p = p + arrayOption[2][arrayCorrectOption[number]][t];
	                            }

                                if (checkedChast) {
                                    p = p + arrayOption[3][arrayCorrectOption[number]][t];
                                }

	                            if (checkedPoplnenie) {
	                                p = p + arrayOption[4][arrayCorrectOption[number]][t];
	                            }
                            }

                            p = Math.round(p * 100) / 100;

                            if (k == 1) {
                                //s = Math.round(m * (Math.pow((1 + p / 100 / 12),  Math.round(arrayDays[period] / 30)) - 1) * 100) / 100;
                                s = Math.round((m * Math.pow((1 + p / 100 * arrayDays[period] / Math.round(arrayDays[period] / 30) / 365), Math.round(arrayDays[period] / 30)) - m) * 100) / 100;
                            }
                            else {
                                s = Math.round(m * p * arrayDays[period] / 365) / 100;
                            }

                            var obj = $('#tbody tr[number=' + number + '][valuta=' + valuta + ']');

                            $(obj).find('td span').each(function() {
                                if ($(this).hasClass('base')) {
                                    $(this).hide();
                                }

                                if ($(this).hasClass('procent')) {
                                    $(this).show().html(b);
                                }
                            });

                            if (k == 1) {
                                var e = Math.round((s / arrayDays[period]  * 365) / m * 10000) / 100;
                                $(obj).find('td.percent').html(e + ' *');
                            }
                            else {
                                $(obj).find('td.percent').html(p);
                            }

                            $(obj).find('td.summa').html(s);

                        }
                    }
                }
                //}
            }
        }
    }

    /* Если вклады не найдены */

    var n = 0;
    $('#tbody tr').each(function() {
        if ($(this).css('display') != 'none') {
            n++;
        }
    });

    if (n == 0) {
        $('#table').hide();
        $('#alert').slideDown();
    } else {
        $('#table').show();
        $('#alert').slideUp();
    }
}

