(function ($) {
    $(function () {
        ExchangeRate.loadRates(new Date());
    });
    var ExchangeRate = {
        loadRates: function (rateDate) {
            var rateCurrency = "'US$', 'EUR'";
            $.ajax({
                type: "GET",
                cache: false,
                url: "/Home/ObterParidades/" + dateFormat(rateDate, "yyyy-mm-dd") + "/" + rateCurrency,
                data: "{}",
                dataType: "xml",
                success: function (data) {
                    if ($(data).find("moedas")[0].childNodes.length > 0) {
                        ExchangeRate.WriteRates(data);
                        $("#data").html(dateFormat(rateDate, "dd/mm/yyyy"));
                    }
                    else {
                        $("#valores").html('<tr><td colspan="4" style="text-align: center;">N&#227;o h&#225; taxas cadastradas nesta data.</td></tr>');
                        $("#data").html(dateFormat(rateDate, "dd/mm/yyyy"));
                    }

                },
                error: function (request, textStatus, errorThrown) {
                    showAjaxError("#exchangeRate", request, textStatus, errorThrown, "");
                }
            });
        },
        WriteRates: function (data) {
            var json = $.xml2json(data);
            if ($(data).find("moedas")[0].childNodes.length > 1) {
                $("#valores").html("");
                $("#valores").html("<tr><td class='sigla'>MOEDA</td><td class='taxa'>TAXA</td></tr>");
                $.each(json.moeda, function (i, val) {
                    $("#valores").append('<tr>' +
                    '<td>' + val.simbolo + '</td>' +
                    '<td>' + val.valor2.replace(".", ",") + '</td>' +
                    '</tr>');
                });
            }
            else {
                $("#valores").html("");
                $("#valores").html("<tr><td class='sigla'>MOEDA</td><td class='taxa'>TAXA</td></tr>");
                $.each(json, function (i, val) {
                    $("#valores").append('<tr>' +
                    '<td>' + val.simbolo + '</td>' +
                    '<td>' + val.valor2.replace(".", ",") + '</td>' +
                    '</tr>');
                });
            }
        }
    };
})(jQuery);