// JavaScript Document
function clearNoNumber(obj) {
  //先把非数字的都替换掉，除了数字和.
  obj.value = obj.value.replace(/[^\d]/g, "");
  if ($(obj).attr("data-max") !== '' && (parseInt($(obj).attr("data-max")) < parseInt(obj.value) || parseInt(obj.value) == parseInt($(obj).attr("data-min")))) {
    obj.value = obj.value.substring(0, obj.value.length - 1);
    if (obj.value > parseInt($(obj).attr("data-max"))) obj.value = $(obj).attr("data-max");
  }
  obj.value = obj.value == 0 ? '' : obj.value;
}

function clearNoPrice(obj) {
  //先把非数字的都替换掉，除了数字和.
  obj.value = obj.value.replace(/[^\d.]/g, "");
  //必须保证第一个为数字而不是.
  obj.value = obj.value.replace(/^\./g, "");
  //保证只有出现一个.而没有多个.
  obj.value = obj.value.replace(/\.{2,}/g, ".");
  //保证.只出现一次，而不能出现两次以上
  obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");

  if ($(obj).attr("data-max") !== '' && parseInt($(obj).attr("data-max")) < parseInt(obj.value)) {
    obj.value = obj.value.substring(0, obj.value.length - 1);
    if (obj.value > $(obj).attr("data-max")) obj.value = $(obj).attr("data-max");
  }
  //保留小数点后两位
  if (obj.value.indexOf(".") > 1) {
    var num = obj.value.split(".");
    obj.value = num[1].length > 2 ? obj.value.substring(0, obj.value.length - 1) : obj.value;
  }
}

//html字串转html标签  
function htmlSpecialChars(str) {
  str = str.replace(/&/g, '&amp;');
  str = str.replace(/</g, '&lt;');
  str = str.replace(/>/g, '&gt;');
  str = str.replace(/"/g, '&quot;');
  str = str.replace(/'/g, '&#039;');
  return str;
}



//======================获取url传递的参数=======================
(function () {
  var URL = document.location.toString();
  var QueryString, arr = [];
  QueryString = URL.substring(URL.indexOf("?") + 1, URL.length);
  arr = QueryString.split("&");

  //默认显示的tab标签
  if ($(".tab-pane").length > 0) {
    var paneId = null;
    if (URL.indexOf("pane") != -1) {
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].search("pane") != -1) paneId = "#" + arr[i].substring(5, arr[i].length);
      }
      $(paneId).addClass("active").siblings(".tab-pane").removeClass("active");
      $("a[data-toggle='tab'][href='" + paneId + "']").parent("li").addClass("active").siblings("li").removeClass("active");
      $(".nav li[rel='" + paneId + "']").addClass("active").siblings("li").removeClass("active");
    }
  }

  //默认打开模态窗
  if ($(".modal").length > 0) {
    var modalId = null;
    if (URL.indexOf("modal") != -1) {
      for (var j = 0; j < arr.length; j++) {
        if (arr[j].search("modal") != -1) modalId = "#" + arr[j].substring(6, arr[j].length);
      }
      $(modalId).modal();
    }
  }
  getUrlData = function (urlKey) {
    var urlValue = null;
    if (URL.indexOf(urlKey) != -1) {
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].search(urlKey) != -1) urlValue = arr[i].substring(urlKey.length + 1, arr[i].length);
      }
      urlValue = decodeURIComponent(urlValue);
    }
    return urlValue;
  };
})();

// =============================Cookie 操作==============================
//添加cookie
function addCookie(name, value, expiresHours) {
  var cookieString = name + "=" + escape(value);
  //判断是否设置过期时间,0代表关闭浏览器时失效
  if (expiresHours > 0) {
    var date = new Date();
    date.setTime(date.getTime() + expiresHours * 3600000);
    cookieString = cookieString + ";expires=" + date.toUTCString();
  }
  document.cookie = cookieString;
}
//根据名字获取cookie的值
function getCookieValue(name) {
  var strCookie = document.cookie;
  var arrCookie = strCookie.split("; ");
  for (var i = 0; i < arrCookie.length; i++) {
    var arr = arrCookie[i].split("=");
    if (arr[0] == name) {
      return unescape(arr[1]);
    }
  }
}
//根据cookie的键，删除cookie，其实就是设置其失效
function delCookie(name) {
  var nameText = escape(name);
  var expires = new Date(0);
  document.cookie = nameText + "=" + ";expires=" + expires.toUTCString();
}

//清空所有cookie
function clearAllCookie() {
  var strCookie = document.cookie;
  var arrCookie = strCookie.split("; ");
  var arr;
  for (var i = 0; i < arrCookie.length; i++) {
    arr = arrCookie[i].split("=");
    if (arr.length > 0) delCookie(arr[0]);
  }
}

//测试某个字符是属于哪一类.
function CharMode(iN) {
  if (iN >= 48 && iN <= 57) //数字
    return 1;
  if (iN >= 65 && iN <= 90) //大写字母
    return 2;
  if (iN >= 97 && iN <= 122) //小写
    return 4;
  else
    return 8; //特殊字符
}
//计算出当前密码当中一共有多少种模式
function bitTotal(num) {
  modes = 0;
  for (i = 0; i < 4; i++) {
    if (num & 1) modes++;
    num >>>= 1;
  }
  return modes;
}
//返回密码的强度级别
function checkStrong(sPW) {
  if (sPW.length <= 5)
    return 0; //密码太短
  Modes = 0;
  for (i = 0; i < sPW.length; i++) {
    //测试每一个字符的类别并统计一共有多少种模式.
    Modes |= CharMode(sPW.charCodeAt(i));
  }
  return bitTotal(Modes);
}

//延迟60秒重新发送验证码
function waitingRetry() {
  var countdown = getCookieValue("secondsremained") || 0;
  if (countdown == 0) {
    $("#getMobileCheckCode").removeClass("disabled").text("获取验证码");
  } else {
    $("#getMobileCheckCode").addClass("disabled").text("重新获取验证码(" + countdown + ")");
    countdown--;
    addCookie("secondsremained", countdown, countdown + 1);
    setTimeout(function () { waitingRetry() }, 1000)
  }
};

//====================图片预览效果=======================
(function ($) {
  $.fn.extend({
    viewLargePhoto: function () {
      return this.each(function () {
        var obj = $(this);
        var skewX = 0, skewY = 0,
          winWidth = $(window).width(),
          winHeight = $(window).height();
        obj.on("mouseover", function (e) {
          var imageSrc = $(this).children("img").attr("src"),
            tooltip = '<div id="tooltip" class="phototool"><img src="' + imageSrc + '"></div>',
            scrollTop = $(window).scrollTop();
          $("body").append(tooltip);
          var tipWidth = $("#tooltip").width(), tipHeight = $("#tooltip").height();
          e.pageX > (winWidth - tipWidth) ? skewX = e.pageX - tipWidth - 10 : skewX = e.pageX + 10;
          (e.pageY - scrollTop) > (winHeight - tipHeight) ? skewY = e.pageY - tipHeight - 20 : skewY = e.pageY + 20;
          $("#tooltip").css({
            "top": skewY + "px",
            "left": skewX + "px"
          }).show("fast");
        }).on("mouseout", function () {
          $("#tooltip").remove();
        }).on("mousemove", function (e) {
          var tipWidth = $("#tooltip").width(), tipHeight = $("#tooltip").height(),
            scrollTop = $(window).scrollTop();
          e.pageX > (winWidth - tipWidth) ? skewX = e.pageX - tipWidth - 10 : skewX = e.pageX + 10;
          (e.pageY - scrollTop) > (winHeight - tipHeight) ? skewY = e.pageY - tipHeight - 20 : skewY = e.pageY + 20;
          $("#tooltip").css({
            "top": skewY + "px",
            "left": skewX + "px"
          });
        });
      });
    }
  });
})(window.jQuery);

$(function () {
  $("#currnetYear").text(new Date().getFullYear());

  $("#mobileIcon, #mobilemenu .icon-double-angle-up").on("click", function () {
    $("#mobilemenu").slideToggle();
    $('#mobileIcon').toggleClass('active');
  });

  $(document).on("click", function (e) {
    var target = $(e.target);
    if (target.closest("#mobileIcon, #mobilemenu").length == 0) {
      $("#mobilemenu").slideUp();
      $('#mobileIcon').removeClass('active');
    }
  });

  //內頁側面導航
  $(".sidebar li:has(ul)").find(">a").addClass("nav-top-item");
  $(".sidebar>ul").find(">li:last").stop().addClass("last");
  $(".sidebar>ul>li>a").append("<em class='font_icon icon-angle-right'></em>");
  $(".sidebar>ul>li:not('.current')>a.nav-top-item").append("<span class='font_icon icon-plus'></span>");
  $(".sidebar>ul>li.current>a.nav-top-item").append("<span class='font_icon icon-minus'></span>");
  $(".sidebar li li>a.nav-top-item").append("<span class='font_icon icon-angle-right'></span>");
  $(".sidebar>ul>li.current").addClass("opened").find(">ul").show();
  $(".sidebar>ul>li>a.nav-top-item span").on("click", function (e) {
    var $sub_nav = $(this).parent().next("ul");
    if ($(this).parent().next("ul").is(":hidden")) {
      $sub_nav.slideToggle("normal");
      $(this).parent().parent().addClass("opened");
      $(this).addClass("icon-minus").removeClass("icon-plus");
    }
    else {
      $sub_nav.slideUp("normal");
      $(this).parent().parent().removeClass("opened");
      $(this).addClass("icon-plus").removeClass("icon-minus");

    }
    return false;
  });
  $(".sidebar li li:has(ul)").hover(
    function () {
      $(this).addClass("opened");
      $(this).find(">ul").css("display", "block");
    },
    function () {
      $(this).removeClass("opened");
      $(this).find(">ul").css("display", "");
    }
  );


  //验证错误样式
  $(".validate-form").on("keyup", "input.form-control, textarea.form-control", function () {
    var nameText = $(this).attr("name");
    $(this).removeClass("error");
    $("label.error[for='" + nameText + "']").remove();
  }).on("change", "select.form-control", function () {
    var nameText = $(this).attr("name");
    $(this).removeClass("error");
    $("label.error[for='" + nameText + "']").remove();
  }).on("click", "input[type='radio'],input[type='checkbox']", function () {
    var nameText = $(this).attr("name");
    $("label.error[for='" + nameText + "']").remove();
  });

  // tab標籤
  $(".tab li").on("click", function () {
    var num = $(this).index();
    $(this).siblings("li").removeClass("current");
    $(this).addClass("current");
    $(this).parents(".tab").next(".tab-content").find(".tab-pane.current").removeClass("current");
    $(this).parents(".tab").next(".tab-content").find(".tab-pane").eq(num).addClass("current");
  });

  $(".copytext li[class!='edit'][class!='dropdown-submenu']").children("a").on("click", function () {
    $(this).parents(".btn-group, .nav, .input-group-btn").find("span.text").stop().html($(this).html()).find(".badge").remove();
  });


  //全选取消功能
  $(".multichoice").on("click", "input[type='checkbox'][value='checkAll']", function () {
    var objName = $(this).attr("name");
    if ($(this).prop("checked")) {
      $("input[name='" + objName + "']").prop("checked", true);
    } else {
      $("input[name='" + objName + "']").prop("checked", false);
    };
  });

  //复选框的事件
  $(".multichoice").on("click", "input[type='checkbox'][value!='checkAll']", function (event) {
    var objName = $(this).attr("name");
    if (!$("input[name='" + objName + "']").checked) {
      $("input[name='" + objName + "'][value='checkAll']").prop("checked", false);
    };
    var chsub = $("input[name='" + objName + "'][value!='checkAll']").length;
    var checkedsub = $("input[name='" + objName + "'][value!='checkAll']:checked").length;
    if (checkedsub == chsub) {
      $("input[name='" + objName + "'][value='checkAll']").prop("checked", true);
    };
  });

  //展示隐藏搜索
  $("#btn-filter").on("click", function () {
    if ($(".filter-group").hasClass("opened")) {
      //已展开
      $(".filter-group").removeClass("opened");
      $(this).html('<i class="glyphicon glyphicon-chevron-down"></i> 搜索');
    } else {
      //已收起
      $(".filter-group").addClass("opened");
      $(this).html('<i class="glyphicon glyphicon-chevron-up"></i> 隐藏搜索');
    }
  });
})
