/**
* 镜像js中只需修改这个文件
*/
window.splitDomainConfig = function(num,domainHost){
    var arr = domainHost.split("."),
    len = arr.length,
    array = [];
    if(num>len) return;
    for(var i = 0;i < num; i++){
        array.push(arr[len-(i+1)]);
    }
    return array.reverse().join('.');
};

window.getDomain = function(){
    domainHost=window.location.host;
    if(!domainHost) return ;
    var domain = domainHost.substring(domainHost.lastIndexOf(".")+1, domainHost.length);
    if(!domain) return;
    var arr = domainHost.split(".");
    var len = arr.length;
    if (domainHost.indexOf("mooc1.mooc") == 0){
        return splitDomainConfig(len - 1, domainHost);
    } else if(domain == 'cn'){
        return splitDomainConfig(3,domainHost);
    } else {
        return splitDomainConfig(2,domainHost);
    }
};

window.getCookie = function(name) {
    var oRegExp = new RegExp("(^|)" + name + "=([^;]*)(;|$)","gi").exec(document.cookie), aCookie;
    if(aCookie = oRegExp) {
        var e=unescape(aCookie[2]);
        if(e!=null && typeof(e) != "undefined"){
            return e;
        }
        return "";
    }
    return "";
};

window.ServerHost={
    host:location.host,
    pathMap: {},
    moocDomain: window.location.protocol + "//" + window.location.host,
    purl: "https://p.ananas.chaoxing.com",
    purlNew: "https://p.cldisk.com",
    dayaDomain: "https://dsa.dayainfo.com",
    passporturl: "http://passport2." + getDomain(),
    passporturlNew:"http://detect."+getDomain(),
    fanyaurl: "http://course.fanya." + getDomain(),
    photoDomain: "http://photo.fanya.chaoxing.com",
    domainurl_old: "http://fanya.chaoxing.com",
    cloudDomain: "http://cloud.ananas.chaoxing.com",
    groupDomain: "https://groupweb.chaoxing.com",
    uploadDomain: window.location.protocol + "//mooc-upload-ans.chaoxing.com",
    newCloudDomain: location.protocol  +  "//pan-yz.chaoxing.com",
    xueyinonlineDomain: location.protocol  +  "//www.xueyinonline.com",
    xueyinonlineChaoXingDomain: location.protocol  +  "//xueyinonline.chaoxing.com",
    pananas: "http\:\/\/p.ananas\.chaoxing\.com\/star\/origin\/",
    schoollist: "http://www.fanya." + getDomain() + "/school/schoollist.html",
    moocTJDomain: "https://fystat-ans.chaoxing.com",
    ztDomain: "https://special.rhky.com",
    xChaoXingDomain : "http://x.chaoxing.com",
    xChaoXingDomainS : "https://x.chaoxing.com",
    jcxyglDomain: "https://jcxygl.chaoxing.com",
    widgetCourseDomain: "https://widget-course.chaoxing.com",
    panDomain:  location.protocol + "//pan-yz." + getDomain(),
    noteydDomain: "https://noteyd.chaoxing.com",
    mooc2Domain: "https://mooc2-ans.chaoxing.com",
    mobilelearnDomain: window.location.protocol + "//mobilelearn." + getDomain(),
    aiCreationTextbookDomain: "https://ai-creation-textbook.chaoxing.com",
    noteExportydDomain: window.location.protocol + '//exportyd.chaoxing.com',
    authenDomain: window.location.protocol + '//authen.chaoxing.com',
    mobilelearnDomain: window.location.protocol + '//mobilelearn.chaoxing.com',
    liveDomain: window.location.protocol + '//live.chaoxing.com',
    appcdDomain: window.location.protocol + '//appcd.chaoxing.com',
    mobile3Domain: window.location.protocol + '//mobile3.chaoxing.com',
    zhiboDomain : window.location.protocol + "//zhibo.chaoxing.com",
    feDomain: window.location.protocol + "//fe.chaoxing.com",
    qikanDomain: window.location.protocol + '//qikan.chaoxing.com',
    ssDomain : window.location.protocol + "//ss.chaoxing.com",
    cvpDomain: window.location.protocol + "//cv-p" + getDomain(),
    svideoDomain: window.location.protocol + "//svideo.chaoxing.com",
    aiappsDomain: window.location.protocol + '//aiapps.cxfanya.com',
    init: function() {
        var defaultPathObj ={
                    "mooc2-ans":"",
                        };
        this.pathMap["www.mooc.whu.edu.cn"] = defaultPathObj;
        this.hostPathObj = this.pathMap[this.host];
        if(typeof this.hostPathObj != 'undefined'){
                        this.groupDomain = this.getDomain("groupweb");
            this.noteDomain = this.getDomain("noteyd");
            this.noteDomain1 = this.noteDomain;
            this.mooc2Domain = this.getDomain("mooc2-ans");
            if(this.mooc2Domain.endsWith("/mooc2-ans")){
                this.mooc2Domain = this.mooc2Domain.substr(0,this.mooc2Domain.length - "/mooc2-ans".length);
            }
                    }
    },
    createScript: function (srcUrl) {
        var oScript = document.createElement('script');
        oScript.type = 'text/javascript';
        oScript.src = srcUrl;
        var oHead = document.getElementsByTagName("head")[0];
        try {
            oHead = oHead || document.documentElement;
            oHead.appendChild(oScript);
        } catch (e) {
        }

    },

    getDomain: function (pathKey) {
        var pathValue = this.hostPathObj[pathKey];
        if(typeof pathValue != 'undefined'){
                        if(pathValue.indexOf('.')>-1){
                pathValue = pathValue.replace("http://",'').replace("https://",'');
                return location.protocol  +  '//' +  pathValue;
            }

            if(pathValue.length > 0){
                pathValue = "/" +  pathValue;
            }

                        return location.protocol  +  '//' + this.host +  pathValue;
        } else {
                        return location.protocol  +  '//' + this.host + "/" +  pathKey;
        }
    }
};
ServerHost.init();