// 没有jquery，引入
if (!window.jQuery) {
    const script_jswsc = document.createElement("script");
    script_jswsc.type = "text/javascript";
    script_jswsc.src = "//www.hteacher.net/public/division/js/jquery-2.1.4.min.js";
    document.body.appendChild(script_jswsc);
}
//引入神策统计 
(function (url) {
    var script_sc = document.createElement('script'); script_sc.type = 'text/javascript';
    script_sc.charset = "utf-8"; script_sc.src = url;
    var f = document.getElementsByTagName('script')[0];
    f.parentNode.insertBefore(script_sc, f);
})('//u3.huatu.com/bigdata/shence/sensors.js?rand=' + Math.floor(Math.random() * (10000000)));
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
}
function urlToJson(str) {
    if (str.indexOf('?') != -1) {
        str = str.substring(str.indexOf('?') + 1);
    }
    var json = {};
    var arr = str.split('&');
    for (var i = 0; i < arr.length; i++) {
        var arr2 = arr[i].split('=');
        json[arr2[0]] = arr2[1];
    }
    return json;
}
//CNZZ
(function (url) {
    const script_jswcnzz = document.createElement('script');
    script_jswcnzz.type = 'text/javascript';
    script_jswcnzz.src = url;
    var f = document.getElementsByTagName('script')[0];
    f.parentNode.insertBefore(script_jswcnzz, f);
})('https://s102.cnzz.com/stat.php?id=4934593&web_id=4934593&show=pic');
//教师网神策自定义事件
//百度统计代码
var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?72104d9092ca76a85c4e26ebd8fa94f1";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();
/*统计兼职课程*/
var href = window.location.href;
var oCidyId = getCookie('fxid');
var oServiceId = getCookie('serviceid');
if (href.indexOf('city') != -1) {
    var json = urlToJson(href);
    var fxcidy = json.city;
    setCookie('fxid', fxcidy, 3);
}
if (!oServiceId) {
    if (href.indexOf('service') != -1) {
        var json = urlToJson(href);
        var serviceid = json.service;
        setCookie('serviceid', serviceid, 7);
    }
}
/* 百度自动推送代码 */
(function () {
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();

/**
 * 公共函数
 */
function deepClone(obj) {
    if (Array.isArray(obj)) {
        return obj.map(deepClone)
    } else if (obj && typeof obj === 'object') {
        let cloned = {}
        let keys = Object.keys(obj)
        for (let i = 0, l = keys.length; i < l; i++) {
            let key = keys[i]
            cloned[key] = deepClone(obj[key])
        }
        return cloned
    } else {
        return obj
    }
}

function find(str, cha, num) {
    var x = str.indexOf(cha);
    for (var i = 0; i < num; i++) {
        x = str.indexOf(cha, x + 1);
    }
    return x;
}



var sc_class_id = sc_class_title = sc_article_id = sc_number = sc_page = sc_type = null;
var search_result = login_active_is = true;
var sc_href = window.location.href;
var sc_json = urlToJson(sc_href);
var source_url = document.referrer;
var source_host = sc_href.slice(0, find(sc_href, '/', 2));

var sc_api = {
    class: '/public/event_api/course_interface.php', // 课程详情页
    watch_class: '/public/event_api/lesson_interface.php', // 观看视频
    article: '/public/event_api/article_interface.php' // 文章详情页
};
var sc_event = {
    ClickAd: 'Hteacher_pc_m_Hteacher_ClickAd', // 广告统计
    ViewCourseDetail: 'Hteacher_pc_m_app_Hteacher_ViewCourseDetail', // 课程详情页
    BuyCourse: 'Hteacher_pc_m_app_Hteacher_BuyCourse', // 点击购买
    AddToShoppingCart: 'Hteacher_pc_m_Hteacher_AddToShoppingCart', // 加入购物车
    ListenCourseDetail: 'Hteacher_pc_m_app_Hteacher_ListenCourseDetail', // 试听课程
    StartWatchCourse: 'Hteacher_pc_m_app_Hteacher_StartWatchCourse', // 观看课程
    BrowseInformationDetail: 'Hteacher_pc_m_Hteacher_BrowseInformationDetail', // 文章详情页
    Advisory: 'Hteacher_pc_m_Hteacher_Advisory', // 咨询
    ClickOnTheSearchBox: 'Hteacher_pc_m_Hteacher_ClickOnTheSearchBox', // 点击搜索框
    InitiateSearchRequest: 'Hteacher_pc_m_Hteacher_InitiateSearchRequest', // 发起搜索请求
    ClickCourseSearchResult: 'Hteacher_pc_m_Hteacher_ClickCourseSearchResult', // 点击课程搜索结果
    ClickArticleSearchResult: 'Hteacher_pc_m_Hteacher_ClickArticleSearchResult', // 点击文章搜索结果
    ClickRegistrationButton: 'Hteacher_pc_m_Hteacher_ClickRegistrationButton', // 点击注册按钮
    ClickLoginButton: 'Hteacher_pc_m_Hteacher_ClickLoginButton', // 进入登录页面触发的事件
    SubmitLoginInformation: 'Hteacher_pc_m_Hteacher_SubmitLoginInformation', // 提交登录信息
    ClickShortcutLogin: 'Hteacher_pc_m_Hteacher_ClickShortcutLogin', // 点击快捷登录
    SubmitMobileNumber: 'Hteacher_pc_m_Hteacher_SubmitMobileNumber', // 提交手机号
    ReserveSuccess: 'Hteacher_pc_m_ReserveSuccess' //专题预约成功
};
var sc_ajax = {
    sc_class: function (url, id, type, source_url, source_host) {
        $.post(url, {
            id: id
        }, function (res) {
            if (!res.data.course_preferential_price) {
                res.data.course_preferential_price = 0;
            }
            if (res.message == 'success') {
                sa.track(type, {
                    source_url: source_url,
                    source_host: source_host,
                    course_title: res.data.course_title,
                    course_id: res.data.course_id,
                    course_number: res.data.course_number,
                    course_teaching_method: res.data.course_teaching_method,
                    course_examination_method: res.data.course_examination_method,
                    course_examination: res.data.course_examination,
                    course_study_section: res.data.course_study_section,
                    course_subject: res.data.course_subject,
                    course_class_type: res.data.course_class_type,
                    course_province: res.data.course_province,
                    course_class_hour: res.data.course_class_hour,
                    course_cashback_hour: res.data.course_cashback_hour,
                    course_courseware_quantity: res.data.course_courseware_quantity,
                    course_teacher: res.data.course_teacher,
                    course_video_review: res.data.course_video_review,
                    course_video_review_frequency: res.data.course_video_review_frequency,
                    course_creation_date: res.data.course_creation_date,
                    course_live_start_time: res.data.course_live_start_time,
                    course_live_end_time: res.data.course_live_end_time,
                    course_validity_period: res.data.course_validity_period,
                    course_offline_date: res.data.course_offline_date,
                    course_price: res.data.course_price,
                    course_audition_nature: res.data.course_audition_nature,
                    course_courseware_nature: res.data.course_courseware_nature,
                    course_1to1: res.data.course_1to1,
                    course_agreement_class: res.data.course_agreement_class,
                    course_series_class: res.data.course_series_class,
                    course_preferential_method: res.data.course_preferential_method,
                    course_preferential_price: res.data.course_preferential_price,
                    course_preferential_quantity: res.data.course_preferential_quantity,
                    course_preferential_start_time: res.data.course_preferential_start_time,
                    course_preferential_end_time: res.data.course_preferential_end_time,
                    course_discount_rate: res.data.course_discount_rate,
                    course_real_price: res.data.course_real_price,
                    course_single_coupons: res.data.course_single_coupons
                });
            }
        }, 'json');
    },
    sc_class_st: function (url, id, type, source_url, source_host) {
        $.post(url, {
            id: id
        }, function (res) {
            if (res.message == 'success') {
                if (!res.data.course_preferential_price) {
                    res.data.course_preferential_price = 0;
                }
                sa.track(type, {
                    source_url: source_url,
                    source_host: source_host,
                    course_title: res.data.course_title,
                    course_id: res.data.course_id,
                    course_number: res.data.course_number,
                    course_teaching_method: res.data.course_teaching_method,
                    course_examination_method: res.data.course_examination_method,
                    course_examination: res.data.course_examination,
                    course_study_section: res.data.course_study_section,
                    course_subject: res.data.course_subject,
                    course_class_type: res.data.course_class_type,
                    course_province: res.data.course_province,
                    course_class_hour: res.data.course_class_hour,
                    course_cashback_hour: res.data.course_cashback_hour,
                    course_courseware_quantity: res.data.course_courseware_quantity,
                    course_teacher: res.data.course_teacher,
                    course_price: res.data.course_price,
                    course_1to1: res.data.course_1to1,
                    course_agreement_class: res.data.course_agreement_class,
                    course_series_class: res.data.course_series_class,
                    course_preferential_method: res.data.course_preferential_method,
                    course_preferential_price: res.data.course_preferential_price,
                    course_discount_rate: res.data.course_discount_rate,
                    course_real_price: res.data.course_real_price,
                    course_single_coupons: res.data.course_single_coupons
                });
            }
        }, 'json');
    },
    sc_watch_class: function (url, id, type) {
        $.post(url, {
            id: id
        }, function (res) {
            console.log(res);
            if (res.message == 'success') {
                sa.track(type, {
                    course_id: res.data.course_id,
                    course_teaching_method: res.data.course_teaching_method,
                    course_examination_method: res.data.course_examination_method,
                    course_examination: res.data.course_examination,
                    course_study_section: res.data.course_study_section,
                    course_subject: res.data.course_subject,
                    course_class_type: res.data.course_class_type,
                    course_province: res.data.course_province,
                    course_courseware_id: res.data.course_courseware_id,
                    course_courseware_title: res.data.course_courseware_title,
                    course_courseware_live_start_time: res.data.course_courseware_live_start_time,
                    course_courseware_live_end_time: res.data.course_courseware_live_end_time,
                    course_courseware_live: res.data.course_courseware_live,
                    course_courseware_BJY_id: res.data.course_courseware_BJY_id,
                    course_courseware_hour: res.data.course_courseware_hour,
                    course_courseware_teacher: res.data.course_courseware_teacher,
                    course_courseware_sort: res.data.course_courseware_sort
                });
            }
        }, 'json');
    },
    sc_article: function (url, id, type, source_url, source_host) {
        $.post(url, {
            id: id
        }, function (res) {
            if (res.message == 'success') {
                sa.track(type, {
                    $referrer: source_url,
                    $url: window.location.href,
                    article_id: res.data.article_id,
                    article_title: res.data.article_title,
                    first_category: res.data.article_first_directory,
                    second_category: res.data.article_second_directory,
                    article_recruit_quantity: res.data.article_recruit_quantity
                });
            }
        }, 'json');
    }
};
var userInfo = getCookie('hteacherneta');
if (userInfo) {
    var userIdSc = urlToJson(userInfo).UserID;
    var userPhone = urlToJson(userInfo).user_phone;
    setTimeout(function(){
        if (userPhone) {
            sa.login(userPhone);
        } else {
            // $.ajaxSettings.async = false;
            $.get('/API/shence/setdistinctid.php', function (res) {
                if (res.code == 1) {
                    var distinct_id = res.data.distinct_id;
                    if (distinct_id) {
                        sa.login(distinct_id);
                    } else {
                        sa.login(userIdSc);
                    }
                } else {
                    sa.login(userIdSc);
                }
            }, 'json');
            // $.ajaxSttings.async = true;
    
        }
    },1000);
}
/*广告点击统计*/
$(document).on('click', "a[sctype='ads']", function () {
    sa.track(sc_event.ClickAd, {
        ad_title: $(this).attr("title"),
        ad_url: $(this).attr("href"),
        page_title: document.title,
        ad_module: $(this).attr('data-module')
    });
});
/* 咨询 */
$(document).on('click', '.qq_online', function () {
    sa.track(sc_event.Advisory, {
        current_url: window.location.href,
        current_page_title: document.title,
        course_title: sc_class_title,
        course_id: sc_class_id
    });
});
/* 文章详情页 */
if (sc_href.indexOf('/jiaoshi/') != -1) {
    sc_article_id = sc_href.substring((sc_href.lastIndexOf('/') + 1), (sc_href.lastIndexOf('.html')));
    sc_ajax.sc_article(sc_api.article, sc_article_id, sc_event.BrowseInformationDetail, source_url, source_host);
}
/* 搜索相关 */
var provinceCiteText = $("#divselinx cite").html();
if(provinceCiteText == '全国'){
    provinceCiteText = '';
}
$(document).on('click', '.sc_search_btn', function () {
    var search_classification_html = $('#divselinx cite').html();
    if (!search_classification_html) {
        search_classification_html = $('#selectMenu').html();
    }
    sa.track(sc_event.ClickOnTheSearchBox, {
        page_title: document.title,
        province: provinceCiteText,
        first_category: search_classification_html
    });
});
// 发起搜索请求--资讯搜索
if (sc_href.indexOf('so/news.php') != -1) {
    if ($('.article_list_left ul li').length == 0) {
        search_result = false;
    } else {
        search_result = true;
    }
    
    sa.track(sc_event.InitiateSearchRequest, {
        // page_title: document.title,
        first_category: '资讯',
        province: provinceCiteText,
        search_keyword: $('.sc_search_input').val(),
        search_result: search_result
    });
}
// 点击文章搜索结果
$(document).on('click', '.article_list_left li a', function () {
    if (sc_href.indexOf('so/news.php') != -1) {
        sc_href = $(this).attr('href');
        sc_article_id = sc_href.substring((sc_href.lastIndexOf('/') + 1), (sc_href.lastIndexOf('.html')));
        sc_number = $(this).parent('li').index();

        sa.track(sc_event.ClickArticleSearchResult, {
            search_keyword: $('.sc_search_input').val(),
            article_search_click_serial_number: String(sc_number),
            article_id: sc_article_id,
            article_title: $(this).html()
        });
    }
});
/* 登录注册 */
// 点击注册按钮
$(document).on('click', '.sc_go_reg', function () {
    sa.track(sc_event.ClickRegistrationButton, {
        current_page_title: document.title,
        current_url: window.location.href
    });
});
// 进入登录页面触发
if (sc_href.indexOf('user/login.php') != -1) {
    sc_type = sc_json.scType;
    if (sc_type == 1) {
        login_active_is = true;
    } else {
        login_active_is = false;
    }
    sa.track(sc_event.ClickLoginButton, {
        $referrer: source_url,
        $url: window.location.href,
        login_active_is: login_active_is
    });
}
// 提交登录信息
$(document).on('click', '.sc_login', function () {
    if (sc_href.indexOf('user/login.php') != -1) {
        sa.track(sc_event.SubmitLoginInformation, {
            user_name: $.trim($('#UserName').val()),
            login_way: '账号密码登录'
        });
    } else {
        sa.track(sc_event.SubmitLoginInformation, { // 模考
            user_name: $.trim($('#username').val()),
            login_way: '账号密码登录'
        });
    }
});
// 点击快捷登录 -- 微信
$(document).on('click', '.sc_logon_wx', function () {
    sa.track(sc_event.ClickShortcutLogin, {
        login_way: '微信扫描登录'
    });
});
// 点击快捷登录 -- app
$(document).on('click', '.sc_login_app', function () {
    sa.track(sc_event.ClickShortcutLogin, {
        login_way: 'app扫描登录'
    });
});
// 提交手机号
$(document).on('click', '#conwrap .code_btn', function () {
    sa.track(sc_event.SubmitMobileNumber, {
        login_way: '微信登录',
        user_phone: $.trim($('.reg_mobile').val())
    });
});
/*
* 教师网预约数据 - 神策
* @mobile 手机号
* @userName 姓名
* @provinceName 省份 
* @cityName  市区
* @countyName 县区
* @period 学段
* @subject 学科
* @title 标题
* @examType 考试类型
* @path 路径
* @source 来源
* @businessUnit 业务部门
*/
function jswFormSa(obj){
    const {mobile='',userName='',provinceName='',cityName='',countyName='',period='',subject='',title='',examType='',path='',source='',businessUnit=''} = obj;
    sa.login(mobile);
    sa.track(sc_event.ReserveSuccess, {
        user_name: userName,
        phone: mobile,
        province: provinceName,
        city: cityName,
        county: countyName,
        period: period,
        subject_name: subject,
        title: title,
        exam_type: examType,
        path: path,
        utm_source: source
    });
    sa.setProfile({
        user_name: userName,
        phone: mobile,
        province: provinceName,
        city: cityName,
        county: countyName,
        period: period,
        subject_name: subject,
        exam_type: examType,
        business_unit: businessUnit
    });
}
/* 
* 在文章页点击下载附件之后调用
* 
*/
//判断教师网文章页
if (sc_href.indexOf('hteacher.net/jiaoshi/') != -1 || sc_href.indexOf('hteacher.net/article/') != -1) {
    //加载js 文件
    if(sc_href.indexOf('xcx.hteacher.net') == -1){
        if( sc_href.indexOf('article_') == -1 ){
           /*  if($('.no_accessory').length > 0 || $('.phone_accessory').length > 0 || $('.code_accessory').length > 0){ 
                let scriptFj = document.createElement("script");
                scriptFj.type = "text/javascript";
                scriptFj.src = "//www.hteacher.net/js/htjswfj.js?v=1.6";
                document.body.appendChild(scriptFj);
            } */
        }
    }
}