$(document).ready(function(){
    var c_from = 'EUR', c_to = 'EUR', f_amnt, t_amnt, f_rate, t_rate;
    // Fetch exchange rate data from api
    $.getJSON("https://www.cypruswebsites.com/amodules/rates-com/currency.json", function(data) {
        var currencies_f = [];
        var currencies_t = [];
        //console.log(data);
        $.each(data, function(currency, rate) {
            // Currency options dropdown menu
            //currencies.push("<option id='" + currency.toLowerCase() + "' value='" + rate + "' >" + currency + "</option>");
            //if(currency != "USD") {
            currencies_f.push('<li id="f_'+currency+'"><a href="#" class="f-currency-selector" data-value="'+rate+'" data-currency="'+currency+'">'+currency+'</a></li>');
            currencies_t.push('<li id="t_'+currency+'"><a href="#" class="t-currency-selector" data-value="'+rate+'" data-currency="'+currency+'">'+currency+'</a></li>');
            //}
            /*else {
                $('#f_' + currency + ' > a').data('value', rate);
                t_rate = rate;
            }*/
        });
        $(".currency-list-f").append(currencies_f);
        $(".currency-list-t").append(currencies_t);
        $('#amount_f').val(f_amnt);
        $('#amount_t').val(t_amnt);
        //console.log($('#f_USD > a').data('value'));


        $('.f-currency-selector').on('click', function(e){
            e.preventDefault();
            f_rate = $(this).data('value');
            c_from = $(this).data('currency');
            $('#f_btn').text(c_from);

            $('.currency-list-f li').removeClass('active');
            $('#f_' + c_from).addClass('active');

            exchangeCurrency();
        });

        $('.t-currency-selector').on('click', function(e){
            e.preventDefault();
            t_rate = $(this).data('value');
            c_to = $(this).data('currency');
            $('#t_btn').text(c_to);

            $('.currency-list-t li').removeClass('active');
            $('#t_' + c_to).addClass('active');

            exchangeCurrency();
        });

        $('#exchange').on('click', function(e){
            e.preventDefault();
            exchangeCurrency();
        });

    });



//Calculate and output the new amount
    function exchangeCurrency() {
        var amount = parseFloat($("#amount_f").val());

        if(amount < 0) {
            amount = amount * (-1);
            $("#amount_f").val(amount);
        }
        //console.log('amount = ' + amount + ', c_from = ' + c_from + ', c_to = ' + c_to + ', f_rate = ' + f_rate+ ', t_rate = ' + t_rate);

        if ((amount - 0) != amount || (''+amount).trim().length == 0) {
            $("#amount_t").val("0");
        } else {
            var result;

            if(c_from == 'EUR') {
                if(c_to != 'EUR') {
                    result = amount * parseFloat(t_rate);
                }
                else {
                    result = parseFloat(amount);
                }
            }
            else {
                if(c_to == 'EUR') {
                    result = amount * (1 / parseFloat(f_rate));
                }
                else {
                    result = (amount * (parseFloat(t_rate) * (1 / parseFloat(f_rate))));
                }
            }

            $("#amount_t").val( result.toFixed(3) );
        }
    }
});
