define(['utils', 'config'], function (utils, config) {
    var apiUrl = config.apiUrl;
    //滚动播放所有获奖记录
    var count = 0;
    var num = 0;

    function removeAndNext($li,data) {
        var speed = ($li.width()/500)*10000;
        $li.animate({marginLeft: -$li.width() + "px"}, speed, "linear", function () {
            var $next = $(this).next();
            $(this).remove();
            count++;
            removeAndNext($next,data);
            if (count === (num - 10)) {
                roll(data);
            }
        });
    }

    function roll(data) {
        if(data){
            num += $.grep(data.list.rewards,function(item){
              return  item.CLASS_KEY === 'ticket'
            });
            $(".winners_list").append($.map(data.list.rewards, function (item) {
                return '<span class="winner_name">' + item.LOGIN_ACCOUNT + '&nbsp;' + '使出佛山无影脚，成功射球入门，获得<b>电影票</b>*1' + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
            }).join(""));
            if (num <= 10) {
                return;
            }
            if (count === 0) {
                removeAndNext($(".winners_list span").eq(0),data);
            }
            return;
        }
        utils._jsonP({
            url: apiUrl.all_rewards
        }, function (res) {

            if (+res.result === 1) {
                res.list.rewards = $.grep(res.list.rewards,function(item){
                    return  item.CLASS_KEY === 'ticket'
                });

                num += res.list.rewards.length;
                if(!res.list.rewards||res.list.rewards.length === 0){
                    num =0;
                    return;
                }

                $(".winners_list").append($.map(res.list.rewards, function (item) {
                    return '<span class="winner_name">' + item.LOGIN_ACCOUNT + '&nbsp;' + '使出佛山无影脚，成功射球入门，获得<b>电影票</b>*1' + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
                }).join(""));
                if (num <= 10) {
                    roll();
                }
                if (count === 0) {
                    removeAndNext($(".winners_list span").eq(0));
                }
            }
        });
    }

    roll();

    return roll;
});
