

$(document).ready(
    function() {
        $.getJSON('/English/resources/v4_includes/crawler/news.asp',

            function(data) {
                var populateTicker = function(data) {
                    var ticker = $(".hmeNewsTickerBox ul:first");
                    $.each(data.news,
                        function(index, value) {
                            var tickerItem =
                                "<li><a href='" + value.item.link + "'"
                                + (value.item.window == "Y" ? " target='_blank'" : "")
                                + ">"
                                + value.item.title
                                + "</a>";
                            ticker.append(tickerItem);  
                        }
                    );
                }

                var removeFirst = function() {
                    var first = $(".hmeNewsTickerBox ul li:first");
                    var firstHtml = first.html();
                    first.fadeOut("slow",
                        function() {
                            $(this).remove();
                        }
                    );
                    return "<li>" + firstHtml + "</li>";
                }

                var addLast = function(item) {
                    $(".hmeNewsTickerBox ul").append(item).fadeIn('slow');                
                }

                var animateTicker = function() {
                    var animateOne = function(){
                       addLast(removeFirst());
                    }

                    setInterval(animateOne, 5000);
                };

                if (data.news.length > 0) {
                    populateTicker(data);
                    setTimeout(removeFirst, 5000);
                    if (data.news.length > 1) {
                        animateTicker();
                    }
                }
            }
        );
    }
);

