jQuery.cookie = function (name, value, options) {
  if (typeof value != 'undefined') { // name and value given, set cookie
    options = options || {};
    if (value === null) {
      value = '';
      options.expires = -1;
    }
    var expires = '';
    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
      var date;
      if (typeof options.expires == 'number') {
        date = new Date();
        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
      } else {
        date = options.expires;
      }
      expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    }
    var path = options.path ? '; path=' + options.path : '';
    var domain = options.domain ? '; domain=' + options.domain : '';
    var secure = options.secure ? '; secure' : '';
    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  } else { // only name given, get cookie
    var cookieValue = '';
    if (document.cookie && document.cookie != '') {
      var cookies = document.cookie.split(';');
      for (var i = 0; i < cookies.length; i++) {
        var cookie = jQuery.trim(cookies[i]);
        // Does this cookie string begin with the name we want?
        if (cookie.substring(0, name.length + 1) == (name + '=')) {
          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
          break;
        }
      }
    }
    return cookieValue;
  }
};

$(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/v3/player/search/?game_zone=' + sn + '&player_name_list=' + pn;
    // this.url = this.base + "&sn=" + sn + "&pn=" + pn + "&p=" + p;
    var boxUrlData = $.cookie("boxUrlData");
    var wsSecret = boxUrlData.split("&")[1];
    var ts = boxUrlData.split("&")[0];
    this.url += '&wsSecret=' + wsSecret + '&ts=' + ts;
  };

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

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

      $(".searchPn").text(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;
  }
});