/**
 * Created by webshow on 2018/3/16.
 */

$(function () {
    var language = (navigator.browserLanguage || navigator.language).toLowerCase();
    if (language.indexOf('zh') > -1) {
        lang = 2
    } else if (language.indexOf('en') > -1) {
        lang = 1
        $('.inp').attr("placeholder", "Chat with her…")
    } else {
        lang = 2
    }


    key = 'dzxj4c';
    console.log(returnCitySN["cname"])
    var ua = navigator.userAgent.toLowerCase();
    if (/android/.test(ua)) {
        province = ['北京', '天津', '河北', '山西', '内蒙古', '辽宁', '吉林', '黑龙江', '上海', '江苏', '浙江', '安徽', '福建', '江西', '山东 ', '河南', '湖北', '湖南', '广东', '广西', '海南 ', '重庆', '四川', '贵州', '云南', '西藏', '陕西', '甘肃', '青海', '宁夏', '新疆', '香港', '澳门']
        for (var i = 0; i < province.length; i++) {
            if (returnCitySN["cname"].indexOf(province[i]) != -1) {
                key = "dzxj4c";
            }
        }
    }
    else if (/iphone|ipad|ipod/.test(ua)) {
        key = 'dzxj4c';
    }
    // var $ip = "http://ceshi.yabolive.tv/web";
    var $ip = "https://webserver.yabolive.net";
    var bilibi = true;
    var bbb = location.href;
    var data = url_get_params(bbb)
    var userId = data.uid; //t
    //sessionStorage.kkk = data.token;//k
    function url_get_params(url_path) {
        var url = url_path;
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(url.indexOf("?") + 1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
    var h = $(window).height() + 80;
    $(".video-play").css("height", h + 'px');
    //获取主播相关列表
    room(userId)
    $.ajax({
        url: $ip + "/share/getHotList.html",
        type: "post",
        data: {
            //'type':1,
            'os': "WEB",
            'cid': "web_gw",
            'lang': 2,
        },
        error: function (msg) {
            console.log('请求出错');
        },
        success: function (msg) {
            var data = JSON.parse(msg);
            console.log(data);
            if (data.rooms) {
                var html = [];
                for (var i = 0; i < data.rooms.length; i++) {
                    html += '<li><img src=https://zhibocdn.yabolive.net/comm' + data.rooms[i].faceimg + '?x-oss-process=image/resize,w_200><div class="live"><span class="dian"></span><span class="live-text">Live</span></div><i style="float: left;font-size: 0">' + data.rooms[i].roomId + '</i></li>';
                }
                $(".more-ul").html(html);
                var id = data.rooms[0].roomId;
                $(".more-ul li").on("click", function () {
                    var oid = $(this).children("i").text();
                    console.log(oid)
                    $(".video-play-bg1").addClass('video-play-bg').removeClass('video-play-bg1');
                    room(oid);
                    $("html,body").animate({ scrollTop: 0 }, 0);
                })
            }

        }
    })
    //闪灯
    var t1 = setInterval(function () {

        if (bilibi == true) {
            $(".dian").fadeOut(200);
            bilibi = false;
        } else if (bilibi == false) {
            bilibi = true;
            $(".dian").fadeIn(200);
        }
    }, 700)

    $(window).on("scroll", function () {
        if ($(window).scrollTop() >= 50) {
            $(".down").fadeOut(0)
            $(".down-bottom").fadeIn(200)
        }
        else if ($(window).scrollTop() < 50) {
            $(".down").fadeIn(0)
            $(".down-bottom").fadeOut(0)
        }
    })
    //点击下载
    $(".down-btn").on("click", function () {
        browserRedirect();
    })
    $(".gift").on("click", function () {
        browserRedirect();
    })
    $(".inp").on("click", function () {
        browserRedirect();
    })
    $(".down-btn").on("click", function () {
        browserRedirect();
    })
    $(".guanzhu").on("click", function () {
        browserRedirect();
    })

    function room(o) {
        //获取直播间相关数据
        $.ajax({
            url: $ip + "/share/getInfo.html",
            type: "post",
            data: {
                'uid': 0,
                'os': "WEB",
                'cid': "web_gw",
                'lang': 2,
                'fid': o,
            },
            error: function (msg) {
                console.log('请求出错');
            },
            success: function (msg) {
                var data = JSON.parse(msg);
                console.log(data);
                if (data.code == 1) {
                    $(".play-btn").fadeIn(500);
                    var html = [];
                    html += '<div class="box-img">' +
                        '<img class="bg-img" src=https://zhibocdn.yabolive.net/comm' + data.room.faceImg + ' alt="">' +
                        '</div>';
                    $(".video-play-bg").html(html)
                    var html2 = [];
                    html2 += '<img src=https://zhibocdn.yabolive.net/comm' + data.room.faceImg + '?x-oss-process=image/resize,w_50 alt="">'
                    html2 += '<span class="name">' + data.room.roomName + '</span>'
                    if (lang == 1) html2 += '<span class="hot">' + data.room.lookNum + ' 围观</span>'
                    else html2 += '<span class="hot">' + data.room.lookNum + ' Viewers</span>'
                    $(".more").html(html2);
                    //var vplay=data.room.hlsUrl;

                    $(".play-btn").unbind();
                    if (data.room.roomType == 3) {
                        var html2 = [];
                        html2 += '<video id="videoID" class="video-js vjs-big-play-centered " controls="true" preload="auto" controlsList="nofullscreen" webkit-playsinline="true" playsinline="true" style="width:10rem;height:92%; object-fit:fill;position: absolute;z-index: 10"  poster=https://zhibocdn.yabolive.net/comm' + data.room.faceImg + '></video>';
                        $(".video-play-bg").html(html2);
                        $("video").on("contextmenu", function () { return false; });

                        var src = data.room.playUrl;
                        var Player = videojs("videoID", {
                            nativeControlsForTouch: false,
                            controlBar: {
                                volumePanel: { inline: false }
                            },
                        }, function () {
                            Player.src(src);
                            Player.on('ended', function () {

                                Player.play();
                            })
                        });
                        $(".play-btn").on("click", function () {
                            var html = [];
                            $(".video-play-bg").css("height", "100%");
                            '<div class="box-img">' +
                                '<img class="bg-img" src=https://zhibocdn.yabolive.net/comm' + data.room.faceImg + ' alt="">' +
                                '<img class="zhuan" src="images/Loading.png" alt="">' +
                                '</div>';
                            $(".video-play-bg").append(html);

                            $(".video-play-bg").addClass('video-play-bg1').removeClass('video-play-bg');
                            $(".play-btn").fadeOut(500);
                            Player.play();
                            $.ajax({
                                url: $ip + "/share/updateVisit.html",
                                type: "post",
                                data: {
                                    'uid': 0,
                                    'os': "WEB",
                                    'cid': "web_gw",
                                    'lang': 2,
                                    'fid': o,
                                },
                                success: function () {

                                }
                            })
                        })
                    } else {
                        var vplay = data.room.hlsUrl;
                        $(".play-btn").unbind();
                        $(".play-btn").on("click", function () {
                            var html = [];
                            $(".video-play-bg").css("height", "100%");
                            html += '<video  id="videoID" controlsList="nodownload"  webkit-playsinline="true" x-webkit-airplay="true"  playsinline="true" x5-video-player-type="h5" x5-video-player-fullscreen="true"style="width: 10rem; height:100%; object-fit:fill;position: absolute;z-index: 10"  poster="" src=' + vplay + '></video>' +
                                '<div class="box-img">' +
                                '<img class="bg-img" src=https://zhibocdn.yabolive.net/comm' + data.room.faceImg + ' alt="">' +
                                '<img class="zhuan" src="images/Loading.png" alt="">' +
                                '</div>';
                            $(".video-play-bg").html(html);

                            $(".video-play-bg").addClass('video-play-bg1').removeClass('video-play-bg');
                            $(".play-btn").fadeOut(500);
                            document.getElementById('videoID').play();
                        })
                    }
                } else {
                    var html = [];
                    html += '<div class="end">'
                    if (lang == 1) {
                        html += '<span class="text1">Livestream Ended</span>'
                        html += '<span class="text2">Enter the App to watch</span>'
                    } else {
                        html += '<span class="text1">直播已结束</span>'
                        html += '<span class="text2">进入APP看直播</span>'
                    }

                    html += '</div>'
                    $(".video-play-bg").html(html)
                    if (lang == 1) {
                        $(".end .text2").css("margin-left", "-1rem");
                        $(".end .text2").css("width", "6rem")
                        $(".end .text2").css("margin-top", "0.3rem")
                    }

                    $(".play-btn").fadeOut(0);
                    $(".name-more").fadeOut(0);
                    $(".guanzhu").fadeOut(0);
                    $(".video-play-bg").css("filter", "blur(0px)");

                    $(".text2").on("click", function () {
                        browserRedirect();
                    })
                }

            }

        });
        //用戶下載
        setInterval(function () {
            try {
                if (videoID.paused) {
                    $(".play-btn").fadeIn(500);
                    $(".zhuan").fadeOut(500);
                }
            } catch (msg) {

            }

        }, 1000)
    }

    function browserRedirect() {
        //安卓
        if (/android/.test(ua)) {
            window.location.href = "https://footseen.com/b/download.html";
        } else if (/iphone|ipad|ipod/.test(ua)) {
            window.location.href = "https://footseen.com/b/downloadIos.html";
        }
    }
})