var M = {
  ver : 1,
  os : 5,
  channel : "web",
  sortType : 1,
  gameType : 1, //1：全部2：某类型
  domain : "http://ms-api.duowan.com",
  recommendCache : {},
  //获取所有游戏类别
  getAllCategory : function(){
      var url = M.domain+"/category/all.do";
      var data = {
        channel: M.channel,
        ver: M.ver,
        os: M.os
      };

      $.ajax({
        url: url,
        dataType: 'jsonp',
        data: data,
        success: function(ret){
          V.renderAllCategory(ret.data);
        },
        error: function(ret){
          M.getAllCategory();
        }
      });
  },
  //获取所有游戏列表
  getAllGame : function(){
    var url = M.domain+"/mobileGame/all.do";
      var data = {
        channel: M.channel,
        ver: M.ver,
        os: M.os,
        sortType: M.sortType
      };

      var gameFlag = "all"+M.sortType;

      if (M.recommendCache.hasOwnProperty(gameFlag)) {
        V.showGameList(gameFlag);
        return;
      }

      $.ajax({
        url: url,
        dataType: 'jsonp',
        data: data,
        success: function(ret){
          M.recommendCache[gameFlag] = true;
          ret.data.role = gameFlag;
          V.renderGameList(ret.data,gameFlag);
        },
        error: function(ret){
          M.getAllGame();
        }
      });
  },
  //由推荐类型获取推荐游戏列表
  getRecommendByCategory : function(){
      var url = M.domain+"/mobileGame/byCategory.do";
      var data = {
        categoryId: M.categoryId,
        sortType: M.sortType,
        channel: M.channel,
        ver: M.ver,
        os: M.os
      };

      var gameFlag = "category"+M.categoryId+M.sortType;

      if (M.recommendCache.hasOwnProperty(gameFlag)) {
          V.showGameList(gameFlag);
        return;
      }

      $.ajax({
        url: url,
        dataType: 'jsonp',
        data: data,
        success: function(ret){
          M.recommendCache[gameFlag] = true;
          ret.data.role = gameFlag;
          V.renderGameList(ret.data,gameFlag);
        },
        error: function(ret){
          M.getRecommendByCategory();
        }
      });
  }
}

var V = {
  init : function(){
    M.os = commonUtil.checkPlatform();
    M.getAllCategory();
    M.getAllGame();
  },
  renderRecommenderType : function(data){
    var list = 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);
  },
  renderAllCategory : function(data){
    var list = data.list;
    var len = list.length;
    var dom = "";

    for (var i=0; i<len; i++){
      dom += '<li data-id="'+list[i].id+'">'+list[i].categoryName+'</li>';
    }

    $("#categoryWrap").append(dom);
  },
  renderGameList : function(data,role){
    var bannerTpl = __inline('../../tpl/simulatorGameList.tmpl');
    var dom = bannerTpl({
      data : data
    });

    $("#gameWrap .list-panel").addClass("u-hide");
    $("#gameWrap").append(dom);

    var PageTurn = new commonUtil.PageTurn({
      element : '[data-role='+role+']',
      showNum : 18
    });
  },
  showGameList : function(index){
    $("#gameWrap [data-role="+index+"]").removeClass("u-hide").siblings().addClass("u-hide");
  }
}

var C = {
  init : function(){
    //游戏排序
    var select = new commonUtil.Select({
        element : ".select-wrap",
        callback : function(val){
          M.sortType = val;
          
          if (M.gameType == 1) {
            M.getAllGame();
          } else {
            M.getRecommendByCategory();
          }
        }
      });

    //推荐位游戏
    $("#categoryWrap").on("click","li",function(){
      var $this = $(this);
      var id = $this.attr("data-id");

      $this.addClass("active").siblings().removeClass("active");

      if (id=="all") {
        M.gameType = 1;
        M.getAllGame();
      } else {
        M.categoryId = id;
        M.gameType = 2;
        M.getRecommendByCategory();
      }

    });

    //菜单展开
    var $btnSel = $(".btn-select");
    var $menuWrap = $(".menu-wrap");

    $btnSel.on("click",function(){
      var $this = $(this);

      if ($this.hasClass("opened")) {
        $this.removeClass("opened");
        $menuWrap.removeClass("opened");
      } else {
        $this.addClass("opened");
        $menuWrap.addClass("opened");
      }
    });
    $menuWrap.on("mouseleave",function(){
      var $this = $(this);

      if ($this.hasClass("opened")) {
        $this.removeClass("opened");
        $btnSel.removeClass("opened");
      } 
    });
  }
}

C.init();
V.init();

