var serverHttp = "https://www.qkyxzx.com";
var serverHttpWss = "wss://www.qkyxzx.com/wss";
//var serverHttp = "http://localhost:8080";
//var serverHttpWss = "ws://localhost:8080";

var curWwwPath = window.document.location.href;
if (curWwwPath.indexOf("127.0.0.1") != -1) {
    serverHttp = "http://127.0.0.1:8080";
    serverHttpWss = "ws://127.0.0.1:8080";
} else if (curWwwPath.indexOf("localhost") != -1) {
    serverHttp = "http://localhost:8080";
    serverHttpWss = "ws://localhost:8080";
} else if (curWwwPath.indexOf("39.107.32.199") != -1) {
    serverHttp = "http://39.107.32.199:8080";
    serverHttpWss = "ws://39.107.32.199:8080";
} else if (curWwwPath.indexOf("test.qkyxzx.com") != -1) {
    serverHttp = "http://test.qkyxzx.com";
    serverHttpWss = "ws://test.qkyxzx.com";
}


var _width = parseInt(window.screen.width);
var scale = _width / 750;
var ua = navigator.userAgent.toLowerCase();
var result = /android (\d+\.\d+)/.exec(ua);
if (result) {
    var version = parseFloat(result[1]);
    if (version > 2.3) {
        document.write('<meta name="viewport" content="width=750, minimum-scale = ' + scale + ', maximum-scale = ' + scale + ', target-densitydpi=device-dpi">');
    } else {
        document.write('<meta name="viewport" content="width=750, target-densitydpi=device-dpi">');
    }
} else {
    document.write('<meta name="viewport" content="width=750, user-scalable=no, target-densitydpi=device-dpi">');
}

function AutoResizeImage(maxWidth, maxHeight, objImg) {
    // var img = new Image();
    // img.src = objImg.src;
    // console.log(img);
    var hRatio;
    var wRatio;
    var Ratio = 1;
    var w = objImg.width;
    var h = objImg.height;
    wRatio = maxWidth / w;
    hRatio = maxHeight / h;
    if (maxWidth == 0 && maxHeight == 0) {
        Ratio = 1;
    } else if (maxWidth == 0) {//
        if (hRatio < 1) Ratio = hRatio;
    } else if (maxHeight == 0) {
        if (wRatio < 1) Ratio = wRatio;
    } else if (wRatio < 1 || hRatio < 1) {
        Ratio = (wRatio <= hRatio ? wRatio : hRatio);
    }
    if (Ratio < 1) {
        w = w * Ratio;
        h = h * Ratio;
    }
    objImg.height = h;
    objImg.width = w;
}

function gotoViewIndex() {
    window.location.href = serverHttp + "/view/index?from=H5";
}

function gotoCourseIndex() {
    window.location.href = serverHttp + "/course/index?from=H5";
}

function gotoLiveIndex() {
    window.location.href = serverHttp + "/live/index?from=H5";
}

function liveRanking() {
    window.location.href = serverHttp + "/live/liveRanking?from=H5&liveId=" + $("#liveId").val();
}

function gotoUserCenter() {
    window.location.href = serverHttp + "/user/personalCenter?from=H5";
}

function gotoCourseWareIndex() {
    window.location.href = serverHttp + "/courseware/index?from=H5";
}

function gotoMeeting() {
    window.location.href = serverHttp + "/meeting/index?from=H5";
}

function toPay(objType, objId, objCount, orderId) {
    window.location.href = serverHttp + "/pay/pay?from=H5&objType=" + objType + "&objId=" + objId + "&objCount=" + objCount + "&orderId=" + orderId;
}

function toWebPay(objType, objId, objCount, orderId) {
    window.location.href = serverHttp + "/pay/pay?from=web&objType=" + objType + "&objId=" + objId + "&objCount=" + objCount + "&orderId=" + orderId;
}

function gotoRcmdPub(zqFlag, rcmdType, rcmdObjId, rcmdUrl, from, artType, artContent) {
    if (!from) {
        from = 'H5'
    }
    if (rcmdType == 1) {
        //课程
        if (zqFlag == 0) {
            //普通课程
            window.location.href = serverHttp + "/course/courseDetails?from=" + from + "&id=" + rcmdObjId;
        } else {
            //专区
            window.location.href = serverHttp + "/course/coursePrefectureList?from=" + from + "&id=" + rcmdObjId;
        }
    } else if (rcmdType == 2) {
        //直播
        window.location.href = serverHttp + "/live/detail?from=" + from + "&liveId=" + rcmdObjId;
    } else if (rcmdType == 3) {
        if (!checkLogin()) {
            // goLogin();
            return false;
        }
        //资讯文章
        if (artType == 1) {
            saveUserDorec(1, 1, rcmdObjId, artContent, 1);
            window.location.href = serverHttp + "/view/detail?from=H5&artId=" + rcmdObjId;
        } else if (artType == 2) {
            updataClick(rcmdObjId, artContent);
        }
        //window.location.href = serverHttp + "/view/detail?from="+from+"&artId="+rcmdObjId;
    } else if (rcmdType == 4) {
        //会议
        window.location.href = serverHttp + "/meeting/details?from=" + from + "&conferId=" + rcmdObjId;
    } else if (rcmdType == 5) {
        //广告
        window.location.href = rcmdUrl;
    } else if (rcmdType == 6) {
        //品牌
        window.location.href = serverHttp + "/brand/detail?from=" + from + "&productId=" + rcmdObjId;
    } else if (rcmdType == 7) {
        //课件
        window.location.href = serverHttp + "/courseware/index?from=" + from;
    }
}

function updataClick(id, artContent) {
    $.ajax({
        type: 'POST',
        url: serverHttp + "/view/updataClick",
        data: {
            artId: id
        },
        success: function (res) {
            if (res.code == 200) {
                saveUserDorec(1, 1, id, artContent, 1);
                setTimeout(function () {
                    window.location.href = artContent;
                }, 1000);
            }
        },
        error: function (data) {
        }
    });
}

function saveUserDorec(oprType, objType, objId, url, source) {
    $.ajax({
        type: 'POST',
        url: serverHttp + "/user/saveUserDorec",
        data: {
            oprType: oprType,
            objType: objType,
            objId: objId,
            url: url,
            source,
        },
        success: function (data) {
            if (data.code == 200 && data.data) {
                v.$data.integralType = data.data.taskName;
                v.$data.integralCount = data.data.taskPoints;
                $(".integral-toast").show();
                playMusic();
                setTimeout(function () {
                    $(".integral-toast").fadeOut(200);
                }, 2000);
            }
        },
        error: function (data) {

        }
    });
}

$(function () {
// header高亮
    if (document.URL.indexOf('index/index') > -1) {
        $('#index').addClass('active')
    } else if (document.URL.indexOf('course/index') > -1 || document.URL.indexOf('course/courseDetails') > -1) {
        $('#course').addClass('active')
    } else if (document.URL.indexOf('live/index') > -1 || document.URL.indexOf('live/detail') > -1) {
        $('#live').addClass('active')
    } else if (document.URL.indexOf('meeting/index') > -1 || document.URL.indexOf('meeting/details') > -1) {
        $('#meeting').addClass('active')
    } else if (document.URL.indexOf('user/personalCenter') > -1) {
        $('#user').addClass('active')
    }
    // else if (document.URL.indexOf('course/index') > -1 || document.URL.indexOf('course/courseDetails') > -1 || document.URL.indexOf('/course/coursePrefectureList') > -1) {
    //     $('#courseHeader').addClass('navonclick')
    // } else if (document.URL.indexOf('view/index') > -1 || document.URL.indexOf('view/detail') > -1) {
    //     $('#viewHeader').addClass('navonclick')
    // } else if (document.URL.indexOf('courseware/index') > -1) {
    //     $('#coursewareHeader').addClass('navonclick')
    // }
    $('#conMusicBtn').on("click",()=>{
        let au = document.getElementById('conMusic')
        au.play();
    })
})

function playMusic() {
    // const body = document.body
    // const au = document.createElement('audio')
    // // 这里的路径使用相对路径 传入音频名字和格式就可以 assets下建一个audio存放所有音频
    // au.autoplay = true
    // // au.id = 'audio-player-snowt'
    // // au.loop = loop
    // body.appendChild(au)

    $('#conMusicBtn').trigger('click')

    // console.log('bofang')

}

function getparamcommon(){
    // 新对象
    let obj = new Object();
    let url = window.location.href  // url 地址
    let urlIndex = url.indexOf("?"); // 查看? 位置为多少  22
    // 截取字符串,urlIndex是从下标开始的,应此加一
    let str = url.substr(urlIndex+1,url.toString().length);
    let strList = str.split("&") // ['id=2', 'type=1', 'age=20']
    for (let i = 0; i < strList.length; i++) {
        obj[strList[i].split("=")[0]] = strList[i].split("=")[1];
    }
    return obj;
}
