﻿(function($) {
    $(function() {
        $("#openNews1").live("click", function() {
            $("#news1").dialog('open');
        });
        News.loadNews();
        $('#nextNewsButton').click(function() {
            $("#news1").remove();
            $("#news2").remove();
            $("#news3").remove();
            News.loadNews();
        });
        $("#messageModal").dialog({
            bgiframe: true,
            modal: true,
            width: 390,
            resizable: false,
            autoOpen: true,
            buttons: {
                OK: function() {
                    $(this).dialog('close');
                }
            }
        });
    });
    var News = {
        loadNews: function() {
            var nextNewsId = 0;
            var amountNews = 3;
            if ($("#inputNextNews").attr("value") != null)
                nextNewsId = parseInt($("#inputNextNews").attr("value"));
            showAjaxLoader("#news");
            $.ajax({
                type: "GET",
		cache: false,
                url: "/ObterProximasNoticias/" + nextNewsId + "/" + amountNews,
                dataType: "xml",
                success: function(data) {
                    News.writeNews(data, nextNewsId, amountNews);
                },
                error: function(request, textStatus, errorThrown) {
                    showAjaxError("#news", request, textStatus, errorThrown, "");
                }
            });
            nextNewsId += 3;
        },
        writeNews: function(data, nextNewsId, amountNews) {
            if (amountNews > 0) {
                $("#news").transform({
                    success: function() {
                        News.writeNewsUi();
                        afterLoadNews();
                    },
                    xmlstr: [xmlToString(data)],
                    xsl: "/App_Transform/Views/Home/News/List/Default.xsl"
                });
                $("#nextNews").html("<input id='inputNextNews' value='" + nextNewsId + "' /> ");
            } else {
                $("#news").html("<br /><div align='center'>Nenhuma Notícia</div><br />");
            }
        },
        writeNewsUi: function() {
            for (var i = 1; i <= 3; i++) {
                $("#news" + i).dialog({
                    bgiframe: true,
                    modal: true,
                    width: 750,
                    height: 500,
                    resizable: false,
                    autoOpen: false
                });
            }

            $("#openNews2").click(function() {
                $("#news2").dialog('open');
            });
            $("#openNews3").click(function() {
                $("#news3").dialog('open');
            });
        }
    };
})(jQuery);
