seajs.use(['arale/switchable/1.0.2/slide','arale/dialog/1.3.1/dialog'], function(Slide,Dialog) {

  var M = {
        ver : 1,
        os : 5,
        channel : "web",
        domain : "http://ms-api.duowan.com",
        recommendCache : {},
        typeId : 1,
        sortType : 1,
        //banner图
        getBanner : function(){
            var url = M.domain+"/home/banner.do";
            var data = {
              channel: M.channel,
              ver: M.ver,
              os: M.os
            };

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                V.renderBanner(ret);
              },
              error: function(ret){
                M.getBanner();
              }
            });
        },
        //获取推荐类型
        getRecommenderType : function(){
            var url = M.domain+"/home/recommenderType.do";
            var data = {
              channel: M.channel,
              ver: M.ver,
              os: M.os,
              uid: M.uid,
              token: M.token
            };

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                M.typeId = ret.data.list[0]["id"];
                // V.renderRecommenderType(ret);
              },
              error: function(ret){
                M.getRecommenderType();
              }
            });
        },
        //由推荐类型获取推荐游戏列表
        getRecommendByCT : function(){
            var url = M.domain+"/mobileGame/byCommonType.do";
            var data = {
              typeId: M.typeId,
              channel: M.channel,
              ver: M.ver,
              os: M.os,
              uid: M.uid,
              token: M.token,
              sortType : M.sortType
            };

            var gamaFlag = "type"+M.typeId+M.sortType;
            if (M.recommendCache.hasOwnProperty(gamaFlag)) {
              V.renderRecommender(M.recommendCache[gamaFlag]);
              return;
            }

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                M.recommendCache[gamaFlag] = ret;
                V.renderRecommender(ret);
              },
              error: function(ret){
                M.getRecommendByCT();
              }
            });
        },
        //获取游戏下载排行
        getDownloadRank : function(){
            var url = M.domain+"/home/downloadRank.do";
            var data = {
              channel: M.channel,
              ver: M.ver,
              os: M.os,
              uid: M.uid,
              token: M.token
            };

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                V.renderDownloadRank(ret);
              },
              error: function(ret){
                M.getDownloadRank();
              }
            });
        }
    }

    var V = {
      init : function(){
        M.os = commonUtil.checkPlatform();
        M.getBanner();
        // M.getRecommenderType();
        M.getRecommendByCT(1);
        M.getDownloadRank();
      },
      renderBanner : function(ret){
        var bannerTpl = __inline('../../tpl/simulatorBannar.tmpl');
        var dom = bannerTpl({
          data : ret.data.list
        });

        $("#bannerWrap").html(dom);

        if(!ret.data.list){
          return;
        }
        var slide1 = new Slide({
            element: '[data-role="slide-banner"]',
            classPrefix: null,
            activeTriggerClass: 'is-active',
            autoplay: true,
            effect: 'scrollx'
        }).render();
      },
      renderRecommenderType : function(ret){
        var list = ret.data.list;
        var len = list.length;
        var dom = "";

        for (var i=0; i<len; i++){
          dom += '<option value="'+list[i]["id"]+'">'+list[i]["typeName"]+'</option>';
        }

        $("#recommendTypeWrap").html(dom);
        M.getRecommendByCT($("#recommendTypeWrap").val());
      },
      renderRecommender : function(ret){
        var recommendTpl = __inline('../../tpl/simulatorRecommend.tmpl');
        
        var dom = recommendTpl({
          data : ret.data.list
        });

        $("#recommendWrap").html(dom);
      },
      renderDownloadRank : function(ret){
        var rankTpl = __inline('../../tpl/simulatorDlRank.tmpl');
        var dom = rankTpl({
          data : ret.data.list
        });

        $("#dlRankWrap").html(dom);
      },
      initBanner : function(){
        var slide = new Slide({
            element: '[data-role="slide-banner"]',
            classPrefix: null,
            activeTriggerClass: 'is-active',
            autoplay: false,
            effect: 'scrollx'
        }).render();
      },
      popUp : function(){
        var popTpl = __inline('../../tpl/simulatorDialog.tmpl');
        var dom = popTpl();

        var pop = new Dialog({ 
            contentType:4,
            content: dom,
            width:520,
            height: 320,
            classPrefix:"pop-center"
          }).show();

          //弹框
          $(".pop-center .btn-go").click(function(){
            $(this).parent().fadeOut().siblings().fadeIn();
          });

          $(".pop-center .btn-open").click(function(){
            pop.hide();
          });
      }
    }

    var C = {
      init : function(){

        //排行榜
        $("#dlRankWrap").on("mousemove",".item-rank",function(){
          $(this).addClass("active").siblings().removeClass("active");
        });

        //推荐位游戏
        var select = new commonUtil.Select({
          element : ".select-wrap",
          callback : function(val){
            M.sortType = val;
            M.getRecommendByCT();
          }
        });
      }
    }

    function initPop() {
      var timeout = setTimeout(function(){
        if(window.doneFlag) {
          if (window.popStatus==1) {
            V.popUp();
          }
        } else {
          initPop();
        }
      },200);
    }

    C.init();
    V.init();
    initPop();
});
 
