
; /* Start:/style/js/custom/calculator.currency.js*/
var calculatorCurrency = {
    currencyRateList:false,
    currentCurrency:false,
    exchangeType:'sell',
    
    
    init: function(){
        calculatorCurrency.changeSliderValueInit();

        calculatorCurrency.currencyChangeInit();
        calculatorCurrency.getCurrencyRate();

        calculatorCurrency.changeTypeSwitchInit();
        calculatorCurrency.slidersInit();

    },
    getCurrencyRate:function(){
        $.post('/ajax/get-data.php', {'type': 'calculator.currency'},
            function( response ){
                if ( response.status == 'ok' ){
                    calculatorCurrency.currencyRateList = response.rate_list;
                }else{
                    console.log( 'error from get rate :' + response.message );
                }
        });
    },

    
    changeSliderValueInit:function(){
        
        $('#sellAmount').add('#buyAmount').change(function(){

            calculatorCurrency.calculate( $(this).val() );
        });    
    },
    currencyChangeInit:function(){
        $('#currencyList_sell').add('#currencyList_buy').change(function(){
            $('#' + calculatorCurrency.exchangeType + 'Amount').change();
        });
    },
    changeTypeSwitchInit:function(){
        $('.tab_control>div').unbind();
        $('.tab_control>div').on('click', function(){
            if ($(this).hasClass('sand')){
                return false;
            } else {
                calculatorCurrency.exchangeType = $(this).attr('type');

                $('#' + calculatorCurrency.exchangeType + 'Amount').change();
                $(this).addClass('sand').siblings('div').removeClass('sand');
                $('.tab_content>.tab', $(this).parents('.wrapper_tabs')).removeClass('active').eq($(this).index()).addClass('active');
            };
        });

    },
    
    calculate:function( amount ){

        var amount = parseFloat( str_replace(' ', '', amount ) );
        var index = 0;
        var exchangeCurrency = $('#currencyList_' + calculatorCurrency.exchangeType ).val();
        var currencyRateList = calculatorCurrency.currencyRateList;

        exchangeCurrencyRate = currencyRateList[exchangeCurrency];
        var calculateHtml = '';

        if ( calculatorCurrency.exchangeType == 'sell' ){

            var purchase_rate =  parseFloat( exchangeCurrencyRate.purchase_rate );
            exchangeRateValue = parseFloat( purchase_rate );

        }else{

            var sell_rate = parseFloat( exchangeCurrencyRate.sell_rate );
            exchangeRateValue = parseFloat( exchangeCurrencyRate.sell_rate );
        }
        exchangeRateValue = number_format( exchangeRateValue * amount, 0, '', ' ' );
        
        $('#calculateResultContainer_' + calculatorCurrency.exchangeType ).html( exchangeRateValue + ' RUR' );
        

    },

    slidersInit:function(){
        $('.scroller.exchange').each(function(){
            var val_2 = $('input:text', $(this).parents('.wrapper_scroller'));
            $(this).slider({
                range: 'min',
                min: 1000,
                max: 100000,
                step: 1000,
                value: 45000,
                slide: function( event, ui ) {
                    val_2.val(accounting.formatNumber(ui.value,0,' ')).change();
                }
            });
            val_2.val(accounting.formatNumber($(this).slider('value'),0,' '));;
        });
    }
    
};
$(function(){
    calculatorCurrency.init();
});

/* End */
;; /* /style/js/custom/calculator.currency.js*/
