$(function() {
  var pageUrl = window.location.href.split("?")[0]; //当前页面的url
  var sn = encodeURIComponent(getURLParam("sn")) || "all";
  var pn = encodeURIComponent(getURLParam("pn")) || "YY直播";
  // var p = parseInt(getURLParam("p")) || 1;

  var Search = function() {
    this.pageUrl = pageUrl;
    // this.base = "http://lolbox.duowan.com/new/api/index.php?_do=inquiry/search";
    this.url = "http://api.lolbox.duowan.com/api/v2/player/search/?player_name_list=" + pn;
    // this.url = this.base + "&sn=" + sn + "&pn=" + pn + "&p=" + p;
  };

  Search.prototype = {
    constructor: Search,
    _getData: function() {
      var that = this;

      return $.ajax({
        url: this.url,
        type: "GET",
        dataType: "jsonp"
      });
    },
    init: function() {
      var that = this;
      
      $(".searchPn").html(getURLParam("pn"));

      $("#roleName").attr("href", "reparieLostPlayer.html?playerName=" + getURLParam("pn"));

      this._getData().done(function(data, status, xhr) {
        if (xhr && xhr.status && xhr.status) {
          if (xhr.status == "200") {
            $(".mod-tabs").show();
            $(".mod-tabs2").hide();
            that._render(data);

            setTimeout(function() {
              $(".main-wrap>ul").niceScroll({cursorcolor:"#D9D9D9",cursorwidth:"8",cursoropacitymin:1,cursorborder:'none'});
            }, 100);
          } else {
            $(".mod-tabs2").show();
            $(".mod-tabs").hide();
          }
        }
      }).fail(function() {});
    },
    _render: function(data) {
      var template = _.template($("#serversTemplate").html());
      $("#servers").html(template({
        data: data
      }));

      $("#scrollUl").niceScroll({
        cursorcolor: "#D9D9D9",
        cursorwidth: "8",
        cursoropacitymin: 1,
        cursorborder: 'none'
      });
    }
  };

  (new Search()).init();

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