﻿(function ($) {
    var dateNow = new Date;
    var ratesDate;
    $(function () {
        Ajax.Loader(".news ul", '<li class="load">', '</li>');
        $.getJSON((getURLRoot() + "/News/Get/Json/0/2"), null, function (data) {
            if (data.length > 0) {
                var count = 1;
                $(".news ul").html('');
                $.each(data, function (i, val) {
                    $(".news ul").append('<li><span class="titleNew">' +
                        val.Title + '</span><span>' +
                        val.Content.substring(0, 80) + '...</span><div class="readMore">' +
                        '<a href="' + getURLRoot() + '/News/#' + count + '" title="' + val.Title + '">Leia mais</a></div></li>');
                    count++;
                });
            } else {
                $(".news ul").html('<li style="text-align: center;">Nenhuma not&#237;cia dispon&#237;vel.</li>');
                $(".news a").remove();
            }
            PageSize();
        });
        Ajax.StartRates();
        $('.prevDate').live('click', function () {
            if (ratesDate == null)
                ratesDate = dateNow - 86400000;
            else
                ratesDate = ratesDate - 86400000;
            $(".exchangeRates table thead tr td a.nextDate").css("display", "block");
            Ajax.GetContent();
        });
        $('.nextDate').live('click', function () {
            ratesDate = ratesDate + 86400000;
            if (new Date(ratesDate).toDateString() == dateNow.toDateString())
                $(".exchangeRates table thead tr td a.nextDate").css("display", "none");
            Ajax.GetContent();
        });
    });
    var Ajax = {
        Loader: function (selector, htmlBegin, htmlEnd) {
            $(selector).html(htmlBegin + '<img src="' + getURLRoot() + '/App_Themes/Default/Images/Icon/Loader.gif" alt="..." />' + htmlEnd);
            PageSize();
        },
        GetContent: function () {
            Ajax.Loader(".exchangeRates table tbody", '<tr><td colspan="2" style="text-align: center;">', '</td></tr>');
            $(".exchangeRates table thead tr td .rateDate").html(dateFormat(ratesDate, "dd/mm/yyyy"));
            $.ajax({
                type: "POST",
                url: getURLRoot() + "/Home/GetJsonRates",
                data: { rateDate: dateFormat(ratesDate, "yyyy-mm-dd"), rateCurrency: "'US$', 'EUR'" },
                dataType: "xml",
                success: function (data) {
                    if ($(data).find("moedas")[0].childNodes.length > 0) {
                        Ajax.WriteRates(data);
                    }
                    else {
                        $(".exchangeRates table tbody").html('<tr><td colspan="2" style="text-align: center;">N&#227;o h&#225; taxas cadastradas nesta data.</td></tr>');
                    }
                    PageSize();
                }
            });
        },
        WriteRates: function (data) {
            var json = $.xml2json(data);
            if ($(data).find("moedas")[0].childNodes.length > 1) {
                $(".exchangeRates table tbody").html("");
                $.each(json.moeda, function (i, val) {
                    $(".exchangeRates table tbody").append('<tr>' +
                    '<td>' + val.simbolo + '</td>' +
                    '<td>' + val.valor2.replace(".", ",") + '</td>' +
                    '</tr>');
                });
            }
            else {
                $(".exchangeRates table tbody").html("");
                $.each(json, function (i, val) {
                    $(".exchangeRates table tbody").append('<tr>' +
                    '<td>' + val.simbolo + '</td>' +
                    '<td>' + val.valor2.replace(".", ",") + '</td>' +
                    '</tr>');
                });
            }
            PageSize();
        },
        StartRates: function () {
            ratesDate = dateNow;
            Ajax.GetContent();
        }
    }
})(jQuery);
function stCap(strObj) {
    return (strObj.charAt(0).toUpperCase() + strObj.substr(1).toLowerCase());
}
