 (function(exports) {
   var App = function() {
     this.page = 1;
     this.pageSize = 10;
     this.$content = $('#listWrap');
     this.gameZone = encodeURI($.cookie('boxGameZone')) || 'dx1',
     this.userId = encodeURI($.cookie('boxUserId')) || '2927006252';
     this.url = "http://api.lolbox.duowan.com/api/v2/player/"+ this.gameZone +"/" + this.userId + '/follows_followers/';
     this.lastType = 'follow';
     this.processType = 'follow';
     this.currentPage = 1;
     this.cacheData = {};

     this.init();
   };

   App.prototype.init = function() {
     var self = this;
     this.getData().done(function(data) {
      self.cacheData = data;
      self.render(1);
     });

     this.bindTabSwitchEvt();
   };
   
   App.prototype.render = function() {
     if(self.processType != self.lastType) this.page = 1;//重置 page

     var data = this.processType == 'follow' ? this.cacheData['follows'] : this.cacheData['followers'];

     data = data.slice((this.page - 1) * this.pageSize, this.page * this.pageSize);

     data.pageType = this.processType;
     data.page = this.page;
     data.pageSize = this.pageSize;

     var tmpl = _.template($('#dataTmpl').html());
     this.$content.html(tmpl({
       data: data
     }));
   };

   App.prototype.next = function(next) {
    if(next == this.page) return;

    this.page = next;
    this.render();
   };

   App.prototype.pre = function(pre) {
    if(pre == this.page) return;
    
    this.page = pre;
    this.render();
   };

   App.prototype.go = function() {
    var goValue = $('#go').val();
    if(!goValue || goValue < 0) return;
    
    this.page = goValue;
    this.render();
   };

   App.prototype.bindTabSwitchEvt = function() {
     var self = this;
     $('.mod-tabs-trigger').on('click', 'li', function() {
       var $this = $(this);
       $this.parent().find('li').removeClass('selected');
       $this.addClass('selected');
       self.processType = $this.attr('data-type');

       if(self.processType == self.lastType) return;
       self.lastType = self.processType;

       self.render();
     });
   };

   App.prototype.getData = function() {
    return $.ajax({
       url: this.url,
       dataType: 'jsonp',
       jsonpCallback: 'callback',
       jsonp: 'callback'
     });
   };

   function getURLParam(name) {
    var value = location.search.match(new RegExp("[?&]" + name + "=([^&]*)(&?)", "i"));
    return value ? decodeURIComponent(value[1]) : value;
   }

   exports.App = App;
 })(window);

