function refreshRem() {
  document.documentElement.style.fontSize =
    Math.min(window.innerWidth, screen.width, document.body.offsetWidth) /
      750 *
      100 +
    "px";
}
window.addEventListener(
  "resize",
  function() {
    clearTimeout(tid);
    tid = setTimeout(refreshRem, 300);
  },
  false
);
window.addEventListener(
  "pageshow",
  function(e) {
    if (e.persisted) {
      clearTimeout(tid);
      tid = setTimeout(refreshRem, 300);
    }
  },
  false
);
var tid;

document.addEventListener("DOMContentLoaded", refreshRem);

if (
  typeof WeixinJSBridge == "object" &&
  typeof WeixinJSBridge.invoke == "function"
) {
  handleFontSize();
} else {
  if (document.addEventListener) {
    document.addEventListener("WeixinJSBridgeReady", handleFontSize, false);
  } else if (document.attachEvent) {
    document.attachEvent("WeixinJSBridgeReady", handleFontSize);
    document.attachEvent("onWeixinJSBridgeReady", handleFontSize);
  }
}
function handleFontSize() {
  // 设置网页字体为默认大小
  WeixinJSBridge.invoke("setFontSizeCallback", { fontSize: 0 });
  // 重写设置网页字体大小的事件
  WeixinJSBridge.on("menu:setfont", function() {
    WeixinJSBridge.invoke("setFontSizeCallback", { fontSize: 0 });
  });
}

// window.onload = function () {
//     // 禁用 iOS10 双指缩放:

//     document.addEventListener('touchstart', function (event) {
//         if (event.touches.length > 1) {
//             event.preventDefault();
//         }
//     })

//     //   禁用 iOS10 手指双击缩放:

//     var lastTouchEnd = 0;
//     document.addEventListener('touchend', function (event) {
//         var now = (new Date()).getTime();
//         if (now - lastTouchEnd <= 300) {
//             event.preventDefault();
//         }
//         lastTouchEnd = now;
//     }, false)
// }

function addEvent(dom, event, fn, remove) {
  if (typeof dom === "string") {
    dom = document.querySelectorAll(dom);
  }
  if (dom.length) {
    for (var index = 0; index < dom.length; index++) {
      (function() {
        var i = index;
        var callback = function(e) {
          fn(e, i);
        };
        dom[index].addEventListener(event, callback);
        if (remove) dom.removeEventListener(event, callback);
      })();
    }
  } else {
    dom.addEventListener(event, fn);
    if (remove) dom.removeEventListener(event, fn);
  }
}

function addClass(dom, cName) {
  if (dom.length) {
    for (var index = 0; index < dom.length; index++) {
      _addClass(dom[index], cName);
    }
  } else {
    _addClass(dom, cName);
  }
}
function removeClass(dom, cName) {
  if (dom.length) {
    for (var index = 0; index < dom.length; index++) {
      _removeClass(dom[index], cName);
    }
  } else {
    _removeClass(dom, cName);
  }
}

function _hasClass(elements, cName) {
  return !!elements.className.match(new RegExp("(\\s|^)" + cName + "(\\s|$)"));
}
function _addClass(elements, cName) {
  if (!_hasClass(elements, cName)) {
    elements.className += " " + cName;
  }
}
function _removeClass(elements, cName) {
  if (_hasClass(elements, cName)) {
    elements.className = elements.className.replace(
      new RegExp("(\\s+|^)" + cName + "(\\s+|$)"),
      " "
    );
  }
}

function setCookie(name, value) {
  var Days = 1 / 24;
  var exp = new Date();
  if (Object.prototype.toString.call(value) === "[object Object]") {
    value = JSON.stringify(value);
  }
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie =
    name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getCookie(name) {
  var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"),
    arr = document.cookie.match(reg);
  if (arr) {
    try {
      return JSON.parse(unescape(arr[2]));
    } catch (error) {
      return unescape(arr[2]);
    }
  } else return "";
}

function getJSON(url, callback, fail) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url);
  xhr.onreadystatechange = handler;
  xhr.responseType = "json";
  xhr.setRequestHeader("Accept", "application/json");
  xhr.send();

  function handler() {
    if (this.readyState === this.DONE) {
      if (this.status === 200) {
        callback && callback(this.response);
      } else {
        if (fail) {
          throw new Error(
            "getJSON: `" + url + "` failed with status: [" + this.status + "]"
          );
        } else {
          fail(
            new Error(
              "getJSON: `" + url + "` failed with status: [" + this.status + "]"
            )
          );
        }
      }
    }
  }

  return xhr;
}

/**
 * 简易动画 使用css3 d-none => display:none
 * @param {string} el 元素
 * @param {string} showClass 显示动画class
 * @param {string} hideClass 隐藏动画class
 */
function aniFn(el, showClass, hideClass) {
  //hideClass = "hide"
  showClass = showClass || "show";
  hideClass = showClass || "hide";
  this.el = el;
  this.showClass = showClass;
  this.hideClass = hideClass;
}

aniFn.prototype.show = function() {
  removeClass(this.el, "" + this.hideClass + " d-none");
  addClass(this.el, this.showClass);
};

aniFn.prototype.hide = function() {
  var _this = this.el;
  removeClass(_this, this.showClass);
  addClass(_this, this.hideClass);
  setTimeout(addClass, 300, _this, 'd-none');
}

/**
 * 获取url参数
 * @param {String} name 参数名称
 */
function getQueryString (name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  var r = window.location.search.substr(1).match(reg)
  if (r != null) return unescape(r[2])
  return null
}
