seajs.use([
        'mobi/zepto/1.1.3/zepto',
        'mobi/slider/1.0.0/slider',
        'mobi/lazyload/1.0.0/lazyload',
        'mobi/mo/1.0.0/mo'
    ],function($, Slider, Lazyload, LegoMobi){

    var mIndex = window.mIndex || {};

    mIndex = {
        config : {
            refreshIndex : 0,       //点击刷新序列
            scrollIndex : 0,        //滑动加载序列
            newGameData : {         //滑动加载数据
                "picList" : {},
                "newsList" : {}
            },
            isLoading : false
        },
        /**
         * 事件监听方法集合
         */
        initEventListener : function(){
            var _ts = this,
                _c = this.config;

            //返回键
            $(".m-menu__back").on("click", function(){
                history.back();
            });

            //顶部导航栏切换
            $(".m-header").on("touchstart", ".m-menu--toggle", function(){
                $(this).toggleClass('cur');
                var _nav = $('.m-header__nav');
                if(!_nav.hasClass('nav-toggle')){
                    _nav.show();
                    setTimeout(function(){
                        _nav.addClass('nav-toggle');
                    },20);
                }else{
                    _nav.removeClass('nav-toggle');
                    setTimeout(function(){
                        _nav.hide();
                    },300);
                }
            });

            //热门推荐换一批操作
            $(".btn-refresh").on("touchstart", function(){
                _ts.refreshHotNews();
            });

        },
        /**
         * 初始化函数
         */
        init : function(){
            var _ts = this;

            //首屏幻灯片初始化
            var slider = new Slider("#NormalSlider");
            slider.normal();

            //返回顶部按钮初始化
            LegoMobi.initScrollTop();

            //事件监听初始化
            _ts.initEventListener();

            //Lazyload
            $(".m-picList__pic img, .m-mobiGames__hot img, .m-article__bd img").lazyload();

            //添加收藏提示
            // seajs.use("http://assets.dwstatic.com/project/dw_mobi/1.0.0/js/addTipsToBottom.js");

            //判断是否是频道页
            if(typeof m_channelID != "undefined" && m_channelID == "newGame"){
                //页面初始化完毕请求数据
                _ts.requestData();

                //滚动至底部渲染数据
                _ts.scrollToBottom();
            }
        },
        /**
         * 换一批热门推荐
         */
        refreshHotNews : function(){
            var _ts = this,
                _c = this.config;

            if(typeof mData_hotNews == 'undefined'){
                return;
            }

            if(_c.refreshIndex >= mData_hotNews.length){
                _c.refreshIndex = 0;
            }

            var _curArray = mData_hotNews.slice(_c.refreshIndex,_c.refreshIndex+=2);

            $("#refresh-hotNews .m-picList__con").each(function(index, el) {
                $(el).find("a").attr('href',_curArray[index].href);
                $(el).find("img").attr('src',_curArray[index].src);
                $(el).find(".pic__tips").text(_curArray[index].tips);
                $(el).find(".m-picList__title").text(_curArray[index].title);
            });
        },
        /**
         * 渲染新游戏模板
         */
        buildHtmlForGameChannel : function(data){
            var _ts = this,
                _c = _ts.config;

            var _dom = '<div style="border-top:1px solid #d1d1d1;"></div><div class="m-picList--min"><ul>';

            _c.scrollIndex++;
            //图文列表
            for(var m = (_c.scrollIndex - 1) * 3, n = (m + 3); m < n; m++){
                _dom += '<li class="m-picList__con--min">\
                    <div class="m-picList__con--fl">\
                        <a href='+data.picList[m].url+' class="m-picList__pic">\
                            <img src='+data.picList[m].pic+' alt="">\
                            <span class="pic__bg"></span>\
                            <span class="pic__icon iconfont-custom">&#xe601;</span>\
                            <em class="pic__tips">'+data.picList[m].tips+'</em>\
                        </a>\
                    </div>\
                    <div class="m-picList__con--fr">\
                        <div class="item-title">\
                            <a href='+data.picList[m].url+' class="m-link">'+data.picList[m].title+'</a>\
                        </div>\
                        <div class="item-info">\
                            <span class="item-info--comment">\
                                <i class="iconfont-custom">&#xe60d;</i>3421\
                            </span>\
                            <span class="item-info--time">'+data.picList[m].posttime+'</span>\
                        </div>\
                    </div>\
                </li>';
            }

            _dom += '</ul></div>';

            _dom += '<div class="m-newsList"><div class="m-newsList__inner"><ul>';

            //新闻列表
            for(var j = (_c.scrollIndex - 1) * 4, k = (j + 4); j < k; j++){
                _dom += '<li class="m-newsList__con">\
                                <a href='+data.newsList[j].url+' class="m-link">'+data.newsList[j].title+'</a>\
                            </li>';

            }

            _dom += '</ul></div></div>';

            $(".m-gameChannel .m-inner").append(_dom);
        },
        /**
         * 请求新游戏频道数据
         */
        requestData : function(){
            var _ts = this,
                _c = _ts.config;

            _c.isLoading = true;

            $.ajax({
                url: 'http://www.duowan.com/s/dataJSON/newGame2.html',
                dataType: 'json',
                success: function(ret){
                    _c.newGameData = ret.data,
                    _c.isLoading = false;
                }
            });
        },
        /**
         * 滚动到底部事件
         */
        scrollToBottom : function(){
            var _ts = this,
                _c = _ts.config;

            //滑动加载&插入更多
            var _stScroll = null;
            $(window).on('scroll', function(){
                clearTimeout(_stScroll);
                _stScroll = setTimeout(function(){
                    if(!_c.isLoading){
                        if($(window).scrollTop() + 10 > $("body").get(0).scrollHeight - $(window).height()){
                            if(_c.scrollIndex >= 8){
                                return;
                            }
                            else{
                                _ts.buildHtmlForGameChannel(_c.newGameData);
                            }
                        }
                    }else{
                        event.stopPropagation();
                    }
                },300);
            });
        }
    }

    $(function(){
        //入口
        mIndex.init();


    });
});