//top 右侧文字轮播
function topText() {
    var swiper0 = new Swiper('#swiper3', {
        direction: 'vertical',
        loop: true,
        speed: 1000,
        autoplay: {
            delay: 3000
        },
    })
}

//导航栏变固定定位
function nav() {
    var nav = document.querySelector(".nav");
    document.body.onscroll = function () {
        //判断被滚轮卷入的距离是否大于55
        if (document.documentElement.scrollTop >= 55) {
            //大于55改为固定定位
            nav.style.position = "fixed";
        } else if (document.documentElement.scrollTop < 55) {
            nav.style.position = "relative";
        }
    }
}

//二级导航
function subnav() {
    var liList = $(".have-subnav");
    var subnav = $(".subnav-item");
    var subnavBg = $(".subnav");
    for (var i = 0; i < liList.length; i++) {
        liList[i].index = i;
        subnav[i].index = i;
        liList[i].onmouseenter = function () {
            subnav[this.index].style.display = "block";
            subnavBg[0].style.display = "block";
        }
        liList[i].onmouseleave = function () {
            subnav[this.index].style.display = "none";
            subnavBg[0].style.display = "none";
        }
        subnav[i].onmouseenter = function () {
            $(liList[this.index]).addClass("active");
            this.style.display = "block";
            subnavBg[0].style.display = "block";
        }
        subnavBg[0].onmouseleave = function () {
            liList.removeClass("active");
            subnav.hide();
            subnavBg[0].style.display = "none";
        }
    }
}

//新闻轮播
function xinwen() {
    var rotation = document.getElementsByClassName("Rotation")[0];
    var newsList = document.getElementsByClassName('news-list')[0];
    var newsBtn = document.getElementsByClassName('news-btn')[0].getElementsByTagName('span');
    var auto;
    var autoplay;
    var index = 0;
    disappear(index, newsBtn[0], newsBtn[1]);
    newsBtn[1].onclick = function () {
        //防抖
        clearInterval(auto);
        index++;
        //调用定时器
        move(newsList, index);
        //调用按钮消失出现
        disappear(index, newsBtn[0], newsBtn[1]);
    }

    newsBtn[0].onclick = function () {
        //防抖
        clearInterval(auto);
        index--;
        //调用定时器
        move(newsList, index);
        //调用按钮消失出现
        disappear(index, newsBtn[0], newsBtn[1]);
    }

    //判断上一批下一批按钮什么时候显示消失
    function disappear(index, btn1, btn2) {
        btn1.style.display = index == 0 ? "none" : "inline-block";
        btn2.style.display = index == 3 ? "none" : "inline-block";
    }

    //定时器
    function move(el, index) {
        //获取盒子宽
        var width = el.offsetWidth / 4;
        auto = setInterval(function () {
            //获取margin-left
            var ml = parseInt(el.style.marginLeft) || 0;
            //计算速度
            var step = -(index * width - Math.abs(ml)) / 700 * 17;
            //让速度不要小于8 向左
            step = step < 0 && step > -8 ? -8 : step;
            //让速度不要小于8 向右
            step = step > 0 && step < 8 ? 8 : step;
            //运动
            el.style.marginLeft = ml + step + "px";
            ml = parseInt(el.style.marginLeft) || 0;
            //判断左右，停止条件
            if (ml <= -index * width && step < 0) {
                //复位
                el.style.marginLeft = -index * width + "px";
                // 清除定时器
                clearInterval(auto);
            } else if (ml >= -index * width && step > 0) {
                el.style.marginLeft = -index * width + "px";
                clearInterval(auto);
            }
        }, 17)
    }



    // 鼠标移入清除自动播放定时器
    rotation.onmousemove = function () {
        clearInterval(autoplay);
    }
    // 鼠标移出清除自动播放定时器
    rotation.onmouseleave = function () {
        start();
    }
    //自动播放
    start();

    //关闭页面，获得焦点
    window.onfocus = function () {
        clearInterval(autoplay);
        start();
    }

    //关闭页面，失去焦点
    window.onblur = function () {
        clearInterval(autoplay);
    }

    //封装自动播放
    function start() {
        autoplay = setInterval(function () {
            index++;
            index = index == 4 ? 0 : index;
            move(newsList, index);
            disappear(index, newsBtn[0], newsBtn[1]);
        }, 3000)
    }
}

//新闻新服切换
function xinfu() {
    //按钮
    var btn = document.getElementsByClassName("news-nav")[0].getElementsByTagName("p");
    //新闻，新服的盒子
    var xinwen = document.getElementsByClassName('xinwen')[0];
    var xinfu = document.getElementsByClassName('xinfu')[0];

    btn[0].onclick = function () {
        this.className = "on";
        btn[1].className = "";
        xinwen.style.zIndex = 10;
        xinfu.style.zIndex = 1;
        change(xinwen, 1, 999);
        change(xinfu, 0, 1);
    }

    btn[1].onclick = function () {
        this.className = "on";
        btn[0].className = "";
        xinfu.style.zIndex = 10;
        xinwen.style.zIndex = 1;
        change(xinfu, 1, 999);
        change(xinwen, 0, 1);
    }

    //将anime再次封装
    function change(el, opacity, zIndex) {
        anime({
            targets: el,
            opacity: opacity,
            //时间
            duration: 2000,
            //运动曲线，先快后慢
            easing: 'easeInOutQuad',
            //回调函数，在定时器结束时执行
            promise: function () {
                el.style.zIndex = zIndex;
            }
        })
    }
}



//头部大轮播图

function bigimg() {
    var swiper1 = new Swiper('#swiper1', {
        effect: 'fade',
        loop: true,
        autoplay: true
    })
}

// 式神
function shishen() {
    //平安世界-式神录
    var btnLeft = $(".bigimg-btn-left");
    var btnRight = $(".bigimg-btn-right");
    var itemList = $(".bigimg-item");
    var imgleft = $(".big-img-item-left");
    var imgright = $(".big-img-item-right");
    var btnText = $(".bigimg-btn p");
    var count = 0;
    var arr = ["白藏王", "人面树", "页溟彼岸花", "般若"];
    //右按钮
    btnRight.click(function () {
        count++;
        //判断盒子是否到最后一个
        count = count === itemList.length ? 0 : count;
        changeImg(count);
        text(count);
    })

    //左按钮
    btnLeft.click(function () {
        count--;
        //判断盒子是否到第一个
        count = count === -1 ? itemList.length - 1 : count;
        changeImg(count);
        text(count);
    })

    function changeImg(count) {
        console.log(count);
        //让所有的消失（改变透明度）
        itemList.stop().fadeOut(900);
        //改变图片位置
        imgleft.stop().animate({ left: "-150px" }, 900);
        imgright.stop().animate({ left: "150px" }, 900, function () {
            //在前面的动画结束后回调，让下一个显示
            itemList.eq(count).stop().fadeIn(900);
            itemList.eq(count).find("a").stop().animate({ left: "0px" }, 900);
        });
    }

    function text(count) {
        //前一个名字的索引
        var count1 = count - 1;
        //判断边界值
        count1 = count1 == -1 ? itemList.length - 1 : count1;
        // 改变标签内容
        btnText.eq(0).text(arr[count1]);
        //后一个名字的索引
        var count2 = count + 1;
        count1 = count1 == itemList.length ? 0 : count1;
        btnText.eq(1).text(arr[count2]);
    }
}


//游戏攻略

function psp() {
    var box = $(".gonglue-part");
    var liList = $(".gonglue-tab ul li");
    var itemList = $(".gonglue-bottom .gonglue-con");
    var btn = $(".gonglue-tab span");
    var index = 0;
    var autoplay;
    liList.mouseenter(function () {
        if (!$(this).hasClass("on")) {
            liList.removeClass();
            $(this).addClass("on");
            itemList.stop().hide();
            index = $(this).index();
            $(itemList[$(this).index()]).stop().fadeIn(1000);
        }
    })
    btn.click(function () {
        index++;
        index = index == itemList.length ? 0 : index;
        itemList.stop().hide();
        $(itemList[index]).stop().fadeIn(1000);
        liList.removeClass();
        $(liList[index]).addClass("on");
    })
    box[0].onmouseenter = function () {
        clearInterval(autoplay);
    }
    box[0].onmouseleave = function () {
        start();
    }
    function start() {
        autoplay = setInterval(function () {
            btn.click();
        }, 5000)
    }
    start();
}

// 同人手杖
//左侧
function tongrenLeft() {
    var box = $(".tong-left");
    var btn1 = $(".left-tit-wrap .btn1");
    var btn = $(".tong-left-btn > span");
    var imglist = $(".tong-left-img > a");
    var index = 0;
    btn.click(function () {
        btn.removeClass();
        $(this).addClass("on");
        imglist.stop().fadeOut(900);
        index = $(this).index();
        $(imglist[$(this).index()]).stop().fadeIn(900);
    })

    btn1.click(function () {
        imglist.stop().fadeOut(900);
        btn.removeClass();
        index++;
        index = index == imglist.length ? 0 : index;
        $(btn[index]).addClass("on");
        $(imglist[index]).stop().fadeIn(900);
    })
    box[0].onmouseenter = function () {
        clearInterval(autoplay);
    }
    box[0].onmouseleave = function () {
        start();
    }
    var autoplay;
    function start() {
        autoplay = setInterval(function () {
            btn1.click();
        }, 4000)
    }
    start();
}

//右侧
function tongrenRight() {
    var btn = $(".tong-right .refresh");
    var ulList = $(".rm-pic ul");
    var index = 0;
    var box = $(".tong-right");
    btn.click(function () {
        $(ulList[index]).stop().fadeOut(900);
        index++;
        index = index == ulList.length ? 0 : index;
        $(ulList[index]).stop().fadeIn(900);
    })
    box[0].onmouseenter = function () {
        clearInterval(autoplay);
    }
    box[0].onmouseleave = function () {
        start();
    }
    var autoplay;
    function start() {
        autoplay = setInterval(function () {
            btn.click();
        }, 4000)
    }
    start();
}

//大触
function dachu() {
    var btn = $(".dachu-wrap .refresh");
    var ulList = $(".dachu-box ul");
    var index = 0;
    var box = $(".dachu-box");
    btn.click(function () {
        $(ulList[index]).stop().fadeOut(900);
        index++;
        index = index == ulList.length ? 0 : index;
        $(ulList[index]).stop().fadeIn(900);
    })
    //大触 出现二维码
    var aList = $(".dachu-title a");
    var code = $(".dachu-title a .dachu-code");
    aList.eq(1).mouseenter(function () {
        code.eq(0).fadeIn(600);
    })
    aList.eq(1).mouseleave(function () {
        code.eq(0).fadeOut(400);
    })
    aList.eq(2).mouseenter(function () {
        code.eq(1).fadeIn(600);
    })
    aList.eq(2).mouseleave(function () {
        code.eq(1).fadeOut(400);
    })
}

//京都商业街
// 用swiper写
function jd() {
    var jingduSwiper = new Swiper('#swiper2', {
        slidesPerView: 'auto',
        noSwiping: true,
        speed: 1000,
        scrollbar: {
            el: '.swiper-scrollbar',
            draggable: true,
            snapOnRelease: false,
        },
        freeMode: true,
        mousewheel: {
            sensitivity: 0.5
        }
    })
}

//泛娱乐
function yule() {
    var yuleItem = document.querySelector('.yule-item');
    var yuleBtnLeft = document.querySelector(".yule-btn-left");
    var yuleBtnRight = document.querySelector(".yule-btn-right");
    yuleAppear()
    yuleBtnLeft.onclick = function () {
        var ml = parseInt(yuleItem.style.marginLeft) || 0;
        yuleItem.style.marginLeft = (ml + 230) + "px"
        yuleAppear()
    }
    yuleBtnRight.onclick = function () {
        var ml = parseInt(yuleItem.style.marginLeft) || 0;
        yuleItem.style.marginLeft = (ml - 230) + "px"
        yuleAppear()
    }

    function yuleAppear() {
        var ml = parseInt(yuleItem.style.marginLeft) || 0;
        if (ml >= 0) {
            yuleBtnLeft.style.display = "none";
        } else if (ml <= -1380) {
            yuleBtnRight.style.display = "none";
        } else {
            yuleBtnLeft.style.display = "block";
            yuleBtnRight.style.display = "block";
        }
    }
}

//我们- 娃娃说话
function dollTalk() {
    var doll = document.querySelector(".role");
    var world = document.querySelectorAll(".word-item");
    var bool = true;
    var arr = [];
    doll.onclick = function () {
        if (bool) {
            bool = false;
            var index = parseInt(Math.random() * 6);
            //如果出现和之前相同，则重新获取
            (function fn() {
                arr.forEach(function (item) {
                    if (item == index) {
                        index = parseInt(Math.random() * 6);
                        fn();
                    }
                })
            })();
            //把此次出现的数字放入数组
            arr.push(index);
            // 数组为长度为4，删除第一个数
            if (arr.length == 4) {
                arr.shift();
            }
            world[index].style.opacity = 1;
            anime({
                targets: world[index],
                opacity: 0,
                delay: 2000,
                duration: 100,
                complete: function () {
                    bool = true;
                }
            })
        }
    }
}

// 娃娃转动
function dollRotate() {
    var bigBox = document.querySelector(".women-bg");
    var doll = document.querySelector(".role-wrap");
    bigBox.onmousemove = function (event) {
        //鼠标到body的位置-最大盒子到body的位置
        //直接用鼠标到body的位置，在不同分辨率电脑下会乱
        var place = event.clientX - bigBox.offsetLeft;
        if (place > 1170) {
        }
        else if (place > 720) {
            doll.style = "background-position: -812px 0px;";
        } else if (place > 665) {
            doll.style = "background-position: -609px 0px;";
        } else if (place > 565) {
            doll.style = "background-position: -406px 0px;";
        } else if (place > 510) {
            doll.style = "background-position: -203px 0px;";
        } else if (place > 0) {
            doll.style = "background-position: 0px 0px;";
        }
    }
}

//地下娃娃跑动
function run() {
    // 最后一次键盘鼠标操作的时间
    var lastTime = new Date();
    // 启动动画时间
    var time = 15000;
    // 当用户执行键盘、鼠标操作后，执行的函数
    function afterOperate() {
        var thisTime = lastTime = new Date();
        setTimeout(function () {
            if (thisTime === lastTime) {
                $(".bottom-animate").stop().fadeIn(1500);
            }
        }, time);
    }
    function clearOperate() {
        $(".bottom-animate").stop().fadeOut(1500);
    }
    // 注册事件
    document.addEventListener("mousemove",afterOperate);
    document.addEventListener("mousedown",afterOperate);
    document.addEventListener("scroll",afterOperate);
    document.addEventListener("keypress",afterOperate);
    document.addEventListener("mousemove",clearOperate);
    document.addEventListener("mousedown",clearOperate);
    document.addEventListener("scroll",clearOperate);
    document.addEventListener("keypress",clearOperate);
    afterOperate();
}


