$(function() {
    remInit(100, 375);
    selectProductInit();
    copyInit();
    close_page();
});

function close_page(){
    window.onbeforeunload = onbeforeunload_handler;
    //window.onunload = onunload_handler;
}

function onbeforeunload_handler(){
    sessionStorage.clear();
    //alert("确认关闭www.someabcd.com?");
    //你的业务操作。。。。
}


function remInit(screenSize, picSize) {
    $(window)
        .on("resize", function(e) {
            var $body = $("body");
            var browser_width = $body.width();
            var browser_height = $body.height();
            var htmlWidth = picSize;
            var remResize;
            if (browser_width > browser_height) {
                remResize =
                    document.documentElement.clientHeight || document.body.clientHeight;
                //$uiBox.addClass("horizontal");
            } else {
                remResize =
                    document.documentElement.clientWidth || document.body.clientWidth;
                //$uiBox.removeClass("horizontal");
            }
            var size = ((remResize < 768 ? remResize : 768) / htmlWidth) * screenSize;
            $("html").css("font-size", size + "px");
        })
        .trigger("resize");
}

function selectProductInit() {
    var selectFlag = true;
    var $selectProduct = $(".select-product");
    if ($selectProduct.length > 0) {
        if ($selectProduct.find("li").length <= 0) {
            $selectProduct.addClass("no-item");
            $selectProduct.find(".title-2 span").text("暂无产品");
        } else {
            $selectProduct.find("li").on("click", function() {
                var $this = $(this);
                if ($this.find(".select-btn a").hasClass("current")) {
                    $this.find(".select-btn a").removeClass("current");
                } else {
                    $selectProduct.find(".select-btn a").removeClass("current");
                    $this.find(".select-btn a").addClass("current");
                }
            });
            $selectProduct.find(".title-2 a").on("click", function() {
                if (selectFlag) {
                    $selectProduct.find(".title-2 span").addClass("current");
                    $selectProduct.find("li").each(function() {
                        var $this = $(this);
                        if ($this.find(".select-btn a").hasClass("current")) {
                            $this.show();
                        } else {
                            $this.hide();
                        }
                    });
                } else {
                    $selectProduct.find(".title-2 span").removeClass("current");
                    $selectProduct.find("li").show();
                }
                selectFlag = !selectFlag;
            });
        }
    }
}

function copyInit() {
    if ($(".vip-download").length > 0) {
        $(".gift-list li").each(function() {
            var $this = $(this);
            var thisCopyBtn = $this.find(".gift-code a").attr("class");
            var clipboard = new ClipboardJS("." + thisCopyBtn);
            clipboard.on('success', function (e) {
                alert("复制成功");
            });
            clipboard.on('error', function (e) {
                var ua = navigator.userAgent;
                var isIOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
                if (isIOS && ua.match(/os\s+(\d+)/i)[1] - 0 < 10) {
                    alert("您的系统版本过低,请手动复制下方地址");
                }
            });
        });
    }
}