seajs.use(['arale/switchable/1.0.2/carousel','arale/qrcode/1.0.3/qrcode','arale/dialog/1.3.1/dialog'], function(Carousel,Qrcode,Dialog) {

  var M = {
    ver : 1,
    os : 5,
    channel : "web",
    domain : "http://ms-api.duowan.com",
    installList : [],
    //获取游戏详情
    getDetail : function(gameId){
        var url = M.domain+"/mobileGame/detail.do";
        var data = {
          channel: M.channel,
          ver: M.ver,
          os: M.os,
          gameId: gameId
        };

        $.ajax({
          url: url,
          dataType: 'jsonp',
          data: data,
          success: function(ret){
            var detail = ret.data;
            detail.isInstall = false;

            if(M.installList){
              for(var i=0; i<M.installList.length; i++) {
                if(detail.packageName == M.installList[i]["packageName"]){
                  detail.isInstall = true;
                }
              }
            }
            
            V.renderDetail(detail);
          },
          error: function(ret){
            M.getDetail(gameId);
          }
        });
    },
    //浏览数据上报
    gameBrowse : function(gameId){
        var url = M.domain+"/mobileGame/browse.do";
        var data = {
          channel: M.channel,
          ver: M.ver,
          os: M.os,
          gameId: gameId
        };

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

  var V = {
    init : function(){
      M.os = commonUtil.checkPlatform();
      M.gameBrowse(commonUtil.getParam("gameId"));
      
      var info = $.parseJSON(window.installInfo);

      if (info) {
        if (info.data) {
          M.installList = info.data.list;
        } else {
          M.installList = [];
        }
      } else {
        M.installList = [];
      }

      M.getDetail(commonUtil.getParam("gameId"));

      $(".page-detail").niceScroll({
          cursorcolor: "#d9d9d9",
          cursorwidth: "6",
          cursorborder: 'none'
      });
    },
    renderDetail : function(data){
      var detailTpl = __inline('../../tpl/simulatorDetail.tmpl');
      var dom = detailTpl({
        data : data
      });

     
      $(".page-detail").html(dom);

      var carousel1 = new Carousel({
          element: '[data-role="carousel-banner"]',
          classPrefix: null,
          disabledBtnClass: 'is-disabled',
          hasTriggers: false, //隐藏触发器
          effect: 'scrollx',
          step: 3,
          viewSize: [326],
          circular: false, //禁用循环切换
          autoplay: false
      }).render();
    }
  }

  var C = {
    init : function() {
      var pop;
      var popFlag = false;
      var qrcode;

      $(document).on("click",".btn-phone",function(){
          var url = $(this).attr("data-url");
          var qrcode = new Qrcode({
              text: url,
              width: 160,
              height: 160
          });

          if (!popFlag) {
              pop  = new Dialog({ 
                contentType:4,
                content: '<div id="qcWrap"></div><p>打开手机扫一扫</p>',
                width:200,
                classPrefix:"pop-qrcode"
              });
              qrcode = new Qrcode({
                  text: url,
                  width: 160,
                  height: 160
              });
              pop.show();
              $("#qcWrap").html(qrcode);
              popFlag = true;
          } else {
            pop.show();
          }
      });

    }
  }

  function initData() {
    var timeout = setTimeout(function(){
      if(window.doneFlag) {
        V.init();
      } else {
        initData();
      }
    },200);
  }

  C.init();
  initData();
});



