var ysmode = ['color', 'intelligent', 'variableinfraredlight', 'variablewhitelight', 'common', 'normal white'];
var txmode = ['normal', 'facenoexposure', 'licenseplate'];
var qwmode = ['0', '30', '60', '90'];
var ksmode = ['50HZ', '60HZ', 'close'];
var bgmode = ["auto", "1/3", "1/25", "1/30", "1/50", "1/60", "1/100", "1/120", "1/240", "1/480", "1/960", "1/1000", "1/2000", "1/4000", "1/8000", "1/10000", "1/100000"];
var zymode = ["auto", "6dB", "12dB", "18dB", "24dB", "30dB", "36dB", "42dB"];
var bphmode = ["incandescent", "warm", "fluorescent", "natural", "manual", "auto", "locked"];
var tdmode = ["automatic", "weak", "normal", "strong", "close"];
var jzmode = ["close", "weak", "normal", "strong"];
var tjmode = ["close", "leftright", "updown", "center"];
var lmd = ['high', 'middle', 'low'];
var hour = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"];
var minute = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"];
var gs_xml_doc, get_xml_doc;
var colorxml = "";
var Intelligentxml = "";
var checkboxvalue;
var intelligentLightCap = {},
    colorLightCap = {},
    normalWhiteLight = {},
    rebootObj = {};
var g_bIsIE = !(/(msie\s|trident.*rv:)([\w.]+)/.exec(navigator.userAgent.toLowerCase()) == null);
var rangInitIamges = false,
    rangInitDraw = false,
    rangInitWhiteBalance = false;
var wdrOldvalue;
var initChid = 1, currentchannel = 0,deviceCap = {};
var tnumber = 0;
$(document).ready(function () {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 220,
        });
    }
    var onvifchannelList = JSON.parse(sessionStorage.getItem("onvifchannelList")) || [];
    var $select = $("#NVR_ChnOSDChannelMask");
    var firstEnabledOption = null;
    for (var i = 1; i <= config.deviceInfo.VideoInputChannels; i++) {
        var isDisabled = onvifchannelList.indexOf(i) !== -1; // 如果在列表中，就禁用
        var $option = $('<option class="option" value="' + i + '">' + i + '</option>');
        if (isDisabled) {
            $option.prop('disabled', true);
        } else if (firstEnabledOption === null) {
            firstEnabledOption = i; // 记录第一个可用的通道
        }
        
        $select.append($option);
    }

    // 设置第一个可用的通道为默认值
    if (firstEnabledOption !== null) {
        $select.val(firstEnabledOption).trigger('change.select2');
        initChid = firstEnabledOption;
        currentchannel = firstEnabledOption - 1;
    } else {
        console.warn("所有通道都被禁用了！");
    }
    config.chnelNum=0;
    getAbility();
    var bls = getRatio();
    var newHs = 420 * 100 / bls;
    var newWs = 560 * 100 / bls;
    var divds = document.getElementById("box-border");
    if (divds) {
        divds.style.height = newHs + "px";
        divds.style.width = newWs + "px";
    }
    // 监听滚动条滚动改变easyvideo位置
    $(".image-gun").scroll(function() {
        if (sessionStorage.getItem("xystyle") == "p6s" || sessionStorage.getItem("xystyle") == "p2p") {
            if (sessionStorage.getItem("playstyle") == "EasyVideo" && window.location.hash.indexOf("#nvr/pages/image/ispPage.html") !== -1 && sessionStorage.getItem('dpiRadio') != null) {
                var pt = GetAbsoluteLocationEx($("#axdiv")[0]);
                if (pt.Top > 80 && pt.Left > 250 * getRatio() / 100) {
                    preHiddenDisplaywindow(5);
                    preWindowfollowing(true);
                } else {
                    preHiddenDisplaywindow(4);
                }
            }
        } else {
            if (sessionStorage.getItem("playstyle") == "EasyVideo" && window.location.hash == "#nvr/pages/image/ispPage.html") {
                var pt = GetAbsoluteLocationEx($("#axdiv")[0]);
                if (pt.Top > 127 && pt.Left > 250 * getRatio() / 100) {
                    preHiddenDisplaywindow(5);
                    preWindowfollowing(true);
                } else {
                    preHiddenDisplaywindow(4);
                }
            }
        }
    });
    if (config.devicelose) {
        addEventListener("resize", function(event) {});
    }
    $(window).resize(function() {
        if (sessionStorage.getItem("xystyle") == "p6s" || sessionStorage.getItem("xystyle") == "p2p") {
            if (window.location.hash.indexOf("#nvr/pages/image/ispPage.html") !== -1 && sessionStorage.getItem('dpiRadio') != null) {
                var bl = getRatio();
                var newH = 420 * 100 / bl;
                var newW = 560 * 100 / bl;
                var divd = document.getElementById("box-border");
                if (divd) {
                    divd.style.height = newH + "px";
                    divd.style.width = newW + "px";
                }
                preWindowfollowing(true);
            }
        } else {
            if (sessionStorage.getItem("playstyle") == "EasyVideo" && window.location.hash == "#nvr/pages/image/ispPage.html") {
                var bl = getRatio();
                var newH = 420 * 100 / bl;
                var newW = 560 * 100 / bl;
                var divd = document.getElementById("box-border");
                if (divd) {
                    divd.style.height = newH + "px";
                    divd.style.width = newW + "px";
                }
                preWindowfollowing(true);
            } else if (window.location.hash == "#nvr/pages/image/ispPage.html") {
                var bl = getRatio();
                var newH = 420 * 100 / bl;
                var newW = 560 * 100 / bl;
                var divd = document.getElementById("box-border");
                if (divd) {
                    divd.style.height = newH + "px";
                    divd.style.width = newW + "px";
                }

            }
        }
    });
    var g_bIsIE = !(/(msie\s|trident.*rv:)([\w.]+)/.exec(navigator.userAgent.toLowerCase()) == null);
    if (g_bIsIE && !config.forceEV) {
        if (sessionStorage.getItem("playstyle") == "ActiveX") {
            $("#playCanvas").hide();
            $("#axdiv").show();
            clearInterval(config.gs_interval);
        }
        getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 2, "","",initChid-1);
        ChangeLayout(1);
        SetActiveXMode(1);
        CloseAllChannels();
        PreviewSpecificChannelInFirstWindow(0, 4);
    } else {
        if (sessionStorage.getItem("xystyle") == "p6s" || sessionStorage.getItem("xystyle") == "p2p") {
            config.refreshplayvideo = true;
            $("#playCanvas").hide();
            $("#axdiv").show();
            if (self.playervideo) {
                NVReasyldspecialvideos(true, "");
                getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 3,"","",initChid-1);
            }else{
                refershPlayer("");
            }
        } else {
            if (sessionStorage.getItem("playstyle") == "EasyVideo") {
                $("#playCanvas").hide();
                $("#axdiv").show();
            }
            if (config.refreshplayvideo || config.deviceloseSte == true) {
                NVReasyldspecialvideos(true, "");
                getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 3, "", initChid-1);
            } else {
                if (sessionStorage.getItem("playstyle") == "EasyVideo") {
                    refershPlayer("");
                }
            }
        }
    }
 
    initTimesmf();
    selectVale();
    getWhiteBalance(initChid);
    drawWhiteBalance();
    getImageBasic(initChid);
    getExposure(initChid);
    getScene(initChid);
    getIrCutFilter(initChid);
    getColor(initChid)
    $("#moren").click(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setImgDefault();

        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    // 白平衡改变
    $("#WhiteBalance").change(function() {
        changeb();
        if ("admin" == config.user || "super_admin" == config.user) {
            setWhiteBalance($("#NVR_ChnOSDChannelMask").val());
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    $('#boff').click(function() {
        document.getElementById('boff').classList.toggle('active');
        document.getElementById('bclos').classList.remove('active');
    });
    $('#bclos').click(function() {
        document.getElementById('boff').classList.remove('active');
        document.getElementById('bclos').classList.add('active');
    });
    // 光圈类型 曝光时间 增益改变后立即保存
    $("#ApertureType").change(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setExposure($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    $("#ExposureTime").change(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setExposure($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    $("#gain").change(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setExposure($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });

    // 去雾改变后立即保存
    $("#de-fogging").change(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setScene($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    // 宽动态改变后立即保存
    $("#Widedynamic").change(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            // var param = '';
            // if (["automatic", "weak", "normal", "strong"].indexOf(this.value) != -1) {
            //     param = '56';
            // } else {
            //     param = '57';
            // }
            // if (deviceCap[currentchannel].WDRFlag == "true") {
            //     rebootObj = needReboot(param);
            //     if (rebootObj.isReBoot == 'true') {
            //         var isUpplementary = rebootObj.hasOwnProperty('isUpplementary') ? rebootObj.isUpplementary : 0;
            //         $.RestartModel.mask(setImageBasic, isUpplementary, backWdrValue);
            //         return;
            //     }
            // }
            setImageBasic($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    // 图像镜像改变后立即保存
    $("#ImageMirroring").change(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setImageBasic($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    // 背光补偿改变后立即保存
    $("#boff").click(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setImageBasic($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    $("#bclos").click(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setImageBasic($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    // 降噪改变后立即保存
    $("#noisereduction").change(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setImageBasic($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    // 抗闪模式改变后立即保存
    $("#AntiflashMode").change(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setImageBasic($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    // 抗闪强度改变后立即保存
    $("#FlashoverResis").change(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setImageBasic($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    // 点击灵敏度设置
    $("#lmdsave").click(function(e) {
        e.preventDefault(); //阻止默认提交事件
        if ("admin" == config.user || "super_admin" == config.user) {
            setImageSens($("#NVR_ChnOSDChannelMask").val() - 0);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }

    });
    // 通道号改变
    $("#NVR_ChnOSDChannelMask").change(function () {
        var chid = $("#NVR_ChnOSDChannelMask").val() * 1;
        currentchannel = chid - 1;
        if (!config.isSupportDeviceCapV2&&config.deviceCapV1[currentchannel] == null) {
            getDeviceCap(chid);
        }
        deviceCap = config.isSupportDeviceCapV2 ? config.deviceCapV2[currentchannel] : config.deviceCapV1[currentchannel];
        console.log(deviceCap,'qiehuan');
        console.log( config.deviceCapV1[currentchannel],' config.deviceCapV1[currentchannel]');
        if (sessionStorage.getItem("playstyle") == "EasyVideo") {
            NVReasyldspecialvideos(true, tnumber)
            getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 2, "",true,chid - 1)
            tnumber = chid - 1;
            config.chnelNum = tnumber;
        } else {
            StopVideo(0);
            PreviewSpecificChannelInFirstWindow(chid - 1, 4);
        }
        // getIrCutFilter(chid);
        // getExposure(chid);
        // getImageBasic(chid);
        // getWhiteBalance(chid);
        getAbility();
        selectVale();
        getColor(chid);
        getWhiteBalance(chid);
        getImageBasic(chid);
        getExposure(chid);
        getScene(chid);
        getIrCutFilter(chid);
        getImageSens(chid);
    });
});
function getAbility() { 
    deviceCap = config.isSupportDeviceCapV2 ? config.deviceCapV2[currentchannel] : config.deviceCapV1[currentchannel];
    if (deviceCap.Defrog == 'true') {
        $("#fog").show();
    } else {
        $("#fog").hide();
    }
    // 曝光时间
    if (deviceCap.ExposureTime == 'true') {
        $(".sureTime").show();
    } else {
        $(".sureTime").hide();
    }
    if (deviceCap.PowerLineFrequency == 'true') {
        $("#powerLineFrequency").show();
    } else {
        $("#powerLineFrequency").hide();
    }
    if (deviceCap.AntiFlicker == 'true' || deviceCap.AntiFlicker == "") {
        $("#AntiFlicker").show();
    } else { //只有存在此能力且为false才隐藏，做反向逻辑处理
        $("#AntiFlicker").hide();
    }
}
// 取消恢复宽动态
function backWdrValue() {
    $("#Widedynamic").val(wdrOldvalue).trigger('change.select2');
}
// 点击默认
// 设置默认图像
function setImgDefault() {
    // 定义XML数据
    var cyXmlirc = "<?xml version='1.0' encoding='UTF-8' ?><IrCutFillter Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Mode>initiative</Mode><DayStartTime>07:00:00</DayStartTime><DayEndTime>19:00:00</DayEndTime><Sensitivity>50</Sensitivity><SwitchTime>3</SwitchTime></IrCutFillter>";
    var cyURL;
    var num = $("#NVR_ChnOSDChannelMask").val() ? $("#NVR_ChnOSDChannelMask").val() : 1; //默认通道1
    cyURL = window.config.baseUrl + "/Images/" + num + "/Restore?Function=1";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXmlirc,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            if (200 == xhr.status) {
                checkboxvalue = false;
                //特殊情况 把IR-CUT模式值给自动 致为默认
                $("#IR-CUT-mode").select2('val', 'passivity');
                getColor(num);
                getWhiteBalance(num);
                getImageBasic(num);
                getExposure(num);
                getScene(num);
                getIrCutFilter(num);
                getImageSens(num);
                document.getElementById('boff').classList.remove('active');
                document.getElementById('bclos').classList.remove('active');
                document.getElementById('tcutOpen').classList.remove('active');
                document.getElementById('tcutClose').classList.remove('active');

                $("#brightness").jRange('setValue', $("#brightness").val()); //亮度
                $("#contrastRatio").jRange('setValue', $("#contrastRatio").val()); //对比度
                $("#saturation").jRange('setValue', $("#saturation").val()); //饱和度
                $("#tone").jRange('setValue', $("#tone").val()); //色度
                $("#Sharpness").jRange('setValue', $("#Sharpness").val()); //锐度
                $("#whitelightness").jRange('setValue', $("#whitelightness").val()); //白光亮度
                $("#imintelligentthreshold").jRange('setValue', $("#imintelligentthreshold").val());
                $("#imcolorthreshold").jRange('setValue', $("#imcolorthreshold").val());
                $("#imImage-1").jRange('setValue', $("#imImage-1").val());
                $("#imImage-2").jRange('setValue', $("#imImage-2").val());
                $("#Switchingtimeness").jRange('setValue', $("#Switchingtimeness").val());
                $("#CUTsensitivity").jRange('setValue', $("#CUTsensitivity").val());
                $("#imturnlight").jRange('setValue', $("#imturnlight").val());
                $("#imturnofflight").jRange('setValue', $("#imturnofflight").val());
                $("#Infraredness").jRange('setValue', $("#Infraredness").val()); //亮度

            } else {
                SaveState(xhr);
            }
        }
    });

}
// 初始化时分秒
function initTimesmf() {
    for (var i = 0; i < hour.length; i++) {
        $("<option value='" + hour[i] + "'>" + hour[i] + "</option>").appendTo("#st_con_hour");
        $("<option value='" + hour[i] + "'>" + hour[i] + "</option>").appendTo("#en_ic_hour1");
        $("<option value='" + hour[i] + "'>" + hour[i] + "</option>").appendTo("#en_ic_hour2");
        $("<option value='" + hour[i] + "'>" + hour[i] + "</option>").appendTo("#st_con_hour1");
        $("<option value='" + hour[i] + "'>" + hour[i] + "</option>").appendTo("#en_ic_hour3");
        $("<option value='" + hour[i] + "'>" + hour[i] + "</option>").appendTo("#st_con_hour2");
    }
    for (var i = 0; i < minute.length; i++) {
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#st_con_minute");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#st_con_second");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#en_ic_minute1");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#en_ic_second1");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#st_con_minute1");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#st_con_second1");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#en_ic_minute2");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#en_ic_second2");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#st_con_minute2");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#st_con_second2");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#en_ic_minute3");
        $("<option value='" + minute[i] + "'>" + minute[i] + "</option>").appendTo("#en_ic_second3");

    }


}
// 初始化选择框
function selectVale() {
    // 去雾和3d降噪
    $("#de-fogging,#noisereduction").empty();
    var qw = $.i18n.prop("i18n-de-foggingValue").split(",");
    for (var i = 0; i < qw.length; i++) {
        $("<option value='" + qwmode[i] + "'>" + qw[i] + "</option>").appendTo("#de-fogging");
        $("<option value='" + jzmode[i] + "'>" + qw[i] + "</option>").appendTo("#noisereduction");
    }
    // 抗闪模式
    $("#AntiflashMode").empty();
    var ks = $.i18n.prop("i18n-AntiflashMode-value").split(",");
    for (var i = 0; i < ks.length; i++) {
        $("<option value='" + ksmode[i] + "'>" + ks[i] + "</option>").appendTo("#AntiflashMode");
    }
    // 曝光时间
    $("#ExposureTime").empty();
    var powerbg = deviceCap.ExposureTimeList.split(",");
    for (var i = 0; i < powerbg.length; i++) {
        if (powerbg[i] == '0') {
            $("<option value='auto'>" + $.i18n.prop("i18n-automatic") + "</option>").appendTo("#ExposureTime");
        } else { 
             $("<option value='1/"+powerbg[i]+"'>1/" + powerbg[i] + "</option>").appendTo("#ExposureTime");
        }
        
    }
    // 增益
    $("#gain").empty();
    var zy = $.i18n.prop("i18n-gainValue").split(",");
    for (var i = 0; i < zy.length; i++) {
        $("<option value='" + zymode[i] + "'>" + zy[i] + "</option>").appendTo("#gain");
    }
    // 白平衡
    $("#WhiteBalance").empty();
    var bph = $.i18n.prop("i18n-White-balanceValue").split(",");
    for (var i = 0; i < bph.length; i++) {
        $("<option value='" + bphmode[i] + "'>" + bph[i] + "</option>").appendTo("#WhiteBalance");
    }
    // 宽动态
    $("#Widedynamic").empty();
    var kdt = $.i18n.prop("i18n-Wide-dynamicValue").split(",");
    for (var i = 0; i < kdt.length; i++) {
        $("<option value='" + tdmode[i] + "'>" + kdt[i] + "</option>").appendTo("#Widedynamic");
    }
    // 图像镜像
    $("#ImageMirroring").empty();
    var txj = $.i18n.prop("i18n-ImageMirroring-value").split(",");
    for (var i = 0; i < txj.length; i++) {
        $("<option value='" + tjmode[i] + "'>" + txj[i] + "</option>").appendTo("#ImageMirroring");
    }
    // 灵敏度
    $("#ImageSensity").empty();
    var lmds = $.i18n.prop("i18n-sensitivity-value").split(",");
    for (var i = 0; i < lmds.length; i++) {
        $("<option value='" + lmd[i] + "'>" + lmds[i] + "</option>").appendTo("#ImageSensity");
    }

}
// 初始化进度条
function iamges() {
    rangInitIamges = true;
    // 亮度
    $('#brightness').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#brightnessvalues").text($("#brightness").val());
            throttle(setColor);
        },
        onSetPosition: function() {
            $("#brightnessvalues").text($("#brightness").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
    // 饱和度
    $('#saturation').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#saturationValue").text($("#saturation").val());
            throttle(setColor);
        },
        onSetPosition: function() {
            $("#saturationValue").text($("#saturation").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
    // 对比度
    $('#contrastRatio').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#contrastRatioValue").text($("#contrastRatio").val());
            throttle(setColor);
        },
        onSetPosition: function() {
            $("#contrastRatioValue").text($("#contrastRatio").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
    // 色度
    $('#tone').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#toneValue").text($("#tone").val());
            throttle(setColor);
        },
        onSetPosition: function() {
            $("#toneValue").text($("#tone").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
    // 锐度
    $('#Sharpness').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#SharpnessValue").text($("#Sharpness").val());
            throttle(setColor);
        },
        onSetPosition: function() {
            $("#SharpnessValue").text($("#Sharpness").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
}
// 全彩白光下的定时时间
function setTimed() {
    var startime = $(get_xml_doc).find("IrCutFillter").eq(0).find("CustomStartTime").eq(0).text();
    var endtime = $(get_xml_doc).find("IrCutFillter").eq(0).find("CustomStopTime").eq(0).text();
    var stime = startime.split('T')[1];
    var etime = endtime.split('T')[1];
    if (startime == "" && endtime == "") {
        $("#st_con_hour").val("18");
        $("#st_con_minute").val("00");
        $("#st_con_second").val("00");
        $("#en_ic_hour1").val("06");
        $("#en_ic_minute1").val("00");
        $("#en_ic_second1").val("00");
    } else {
        $("#st_con_hour").val(stime.substring(0, 2));
        $("#st_con_minute").val(stime.substring(2, 4));
        $("#st_con_second").val(stime.substring(4, 6));
        $("#en_ic_hour1").val(etime.substring(0, 2));
        $("#en_ic_minute1").val(etime.substring(2, 4));
        $("#en_ic_second1").val(etime.substring(4, 6));
    }
}
// 无极白下的定时时间
function setWJBtimed() {
    var startime = $(get_xml_doc).find("IrCutFillter").eq(0).find("VarWhiteModeStartTime").eq(0).text();
    var endtime = $(get_xml_doc).find("IrCutFillter").eq(0).find("VarWhiteModeStopTime").eq(0).text();
    var stime = startime.split('T')[1];
    var etime = endtime.split('T')[1];
    if (startime == "" && endtime == "") {
        $("#st_con_hour1").val("18");
        $("#st_con_minute1").val("00");
        $("#st_con_second1").val("00");
        $("#en_ic_hour2").val("06");
        $("#en_ic_minute2").val("00");
        $("#en_ic_second2").val("00");
    } else {
        $("#st_con_hour1").val(stime.substring(0, 2));
        $("#st_con_minute1").val(stime.substring(2, 4));
        $("#st_con_second1").val(stime.substring(4, 6));
        $("#en_ic_hour2").val(etime.substring(0, 2));
        $("#en_ic_minute2").val(etime.substring(2, 4));
        $("#en_ic_second2").val(etime.substring(4, 6));
    }

}


// 获取基本图像信息
function getIrCutFilter(ch) {
    var url;
    url = window.config.baseUrl + "/Images/" + ch + "/IrCutFilter";
    $.ajax({
        url: url,
        type: "GET",
        async: false,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc) {
            get_xml_doc = xmlDoc;
            var nightMode = $(xmlDoc).find('IrCutFillter').eq(0).find('Mode').eq(0).text();
            if (nightMode === "normal white" || nightMode === "color" || nightMode === "intelligent" || nightMode === "variableinfraredlight" || nightMode === "variablewhitelight") {
                $("#NightvisionMode").select2('val', nightMode);
                $("#IR-CUT-mode").select2('val', im_irmode ? im_irmode : 'passivity');
                if (im_irmode == "initiative") { //主
                    //IR-CUT切换时间 Switchingtimeness
                    $("#Switchingtimeness").val($(get_xml_doc).find("IrCutFillter").eq(0).find("InitiveSwitchTime").eq(0).text());
                    $("#SwitchingtimenessValue").text($(get_xml_doc).find("IrCutFillter").eq(0).find("InitiveSwitchTime").eq(0).text());
                } else {
                    $("#Switchingtimeness").val($(get_xml_doc).find("IrCutFillter").eq(0).find("PassivitySwitchTime").eq(0).text());
                    $("#SwitchingtimenessValue").text($(get_xml_doc).find("IrCutFillter").eq(0).find("PassivitySwitchTime").eq(0).text());
                };
            } else {
                $("#IR-CUT-mode").select2('val', nightMode); //IR-CUT模式
                if (nightMode == "initiative") { //主
                    $("#Switchingtimeness").val($(get_xml_doc).find("IrCutFillter").eq(0).find("InitiveSwitchTime").eq(0).text());
                    $("#SwitchingtimenessValue").text($(get_xml_doc).find("IrCutFillter").eq(0).find("InitiveSwitchTime").eq(0).text());
                } else { //被动
                    $("#Switchingtimeness").val($(get_xml_doc).find("IrCutFillter").eq(0).find("PassivitySwitchTime").eq(0).text());
                    $("#SwitchingtimenessValue").text($(get_xml_doc).find("IrCutFillter").eq(0).find("PassivitySwitchTime").eq(0).text());
                };
                $("#NightvisionMode").select2('val', 'common');
            }
            //初始化全彩白光
            initColors(xmlDoc);
            initIntelligent(xmlDoc);
            initWhitelight(xmlDoc);
            initRedlight(xmlDoc);
            initNormalWhite(xmlDoc);
            // if (!rangInitDraw) {
            //     initLightRange() //初始化不复用的滑块 必须放在后面
            // } 


            //智能双光----延时时间
            $("#interval").val($(xmlDoc).find('IrCutFillter').eq(0).find('ColorLastTime').eq(0).text());
            //IR-CUT反向 所有模式下都存在
            var IrCutReverse = $(xmlDoc).find('IrCutFillter').eq(0).find('IrCutReverse').eq(0).text();
            if ("notsupport" == IrCutReverse) {
                $(".ircutResev").hide();
            } else {
                $(".ircutResev").show();
                if (IrCutReverse == "reverse" || IrCutReverse == "true") {
                    $('#cutOpen').addClass('active');
                    $('#cutClose').removeClass('active');
                    checkboxvalue = true;
                } else {
                    $('#cutClose').addClass('active');
                     $('#cutOpen').removeClass('active');
                    checkboxvalue = false;
                }
            };
            //普通红外下的 定时
            initCommonRed(xmlDoc);
            // 图像模式
            $("#ImageMode").select2('val', $(xmlDoc).find('IrCutFillter').eq(0).find('ImageMode').eq(0).text());
            if (!rangInitDraw) {
                drawIRCutLevel(); //绘画IRCutlevel信息
            }

        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
var im_irmode;
// 保存基本图像信息
function setIrCutFilter(num) {
    num = num || $("#NVR_ChnOSDChannelMask").select2('val');
    var nightMode = $("#NightvisionMode").select2('val');
    var icMode = "";
    if ($("#NightvisionMode").val() != "common") {
        icMode = $("#NightvisionMode").select2('val');
        im_irmode = $("#IR-CUT-mode").select2('val'); //保存ircut模式值
    } else {
        icMode = $("#IR-CUT-mode").select2('val'); //保存ircut模式值
    }
    if (parseInt($("#offthresholdness").val()) > parseInt($("#Thresholdness").val())) {
        $.optionBootoast($.i18n.prop('i18n-trunlightmsg'), "danger");
        getIrCutFilter(num);

        return;
    }
    //全彩白下的定时
    var colorTimeS = $("#st_con_hour").val() + ":" + $("#st_con_minute").val() + ":" + $("#st_con_second").val();
    var colorTimeE = $("#en_ic_hour1").val() + ":" + $("#en_ic_minute1").val() + ":" + $("#en_ic_second1").val();
    if (!validateTime(colorTimeS, colorTimeE)) {
        getIrCutFilter(num);
        return;
    }
    //无极白下的定时
    var whihtTimeS = $("#st_con_hour1").val() + ":" + $("#st_con_minute1").val() + ":" + $("#st_con_second1").val();
    var whihtTimeE = $("#en_ic_hour2").val() + ":" + $("#en_ic_minute2").val() + ":" + $("#en_ic_second2").val();
    if (!validateTime(whihtTimeS, whihtTimeE)) {
        getIrCutFilter(num);
        return;
    }
    //普通红外下的定时
    var commonRedTimeS = $("#st_con_hour2").val() + ":" + $("#st_con_minute2").val() + ":" + $("#st_con_second2").val();
    var commonRedTimeE = $("#en_ic_hour3").val() + ":" + $("#en_ic_minute3").val() + ":" + $("#en_ic_second3").val();
    if (!validateTime(commonRedTimeS, commonRedTimeE)) {
        getIrCutFilter(num);
        return;
    }
    if (colorLightCap.isColorThreshold == 'true') {
        colorxml = "<ColorNightThreshold>" + $("#imcolorthreshold").val() + "</ColorNightThreshold>";
    };
    if (intelligentLightCap.isIntelligentThreshold == 'true') {
        Intelligentxml = "<IntelligentThreshold>" + $("#imintelligentthreshold").val() + "</IntelligentThreshold>";
    };
    var fxvalue;
    if (checkboxvalue) {
        fxvalue = "reverse";
    } else {
        fxvalue = "positive";
    }
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><IrCutFillter Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
        "<Mode>" + icMode + "</Mode>" +
        "<DayStartTime>" + commonRedTimeS + "</DayStartTime>" +
        "<DayEndTime>" + commonRedTimeE + "</DayEndTime>" +
        "<Sensitivity>" + $("#CUTsensitivity").val() + "</Sensitivity>" +
        "<SwitchTime>" + $("#Switchingtimeness").val() + "</SwitchTime>" +
        "<IrCutReverse>" + fxvalue + "</IrCutReverse>" +
        "<ColorLastTime>" + $("#interval").val() + "</ColorLastTime>" +
        "<ImageMode>" + $("#ImageMode").val() + "</ImageMode>" +
        "<VariableInfraredThreshold>" + $("#imImage-1").val() + "</VariableInfraredThreshold>" +
        "<VariableWhiteThreshold>" + $("#imImage-2").val() + "</VariableWhiteThreshold>" +
        "<VarWhiteWorkMode>" + $("#Controldirection").val() + "</VarWhiteWorkMode>" + //控制方向
        "<VarWhiteModeStartTime>00000000T" + $("#st_con_hour1").val() + $("#st_con_minute1").val() + $("#st_con_second1").val() + "</VarWhiteModeStartTime>" + //无极白开始时间
        "<VarWhiteModeStopTime>00000000T" + $("#en_ic_hour2").val() + $("#en_ic_minute2").val() + $("#en_ic_second2").val() + "</VarWhiteModeStopTime>" + //无极白结束时间
        "<NormalWhiteLightOffLimit>" + $("#imturnofflight").val() + "</NormalWhiteLightOffLimit><NormalWhiteLightOnLimit>" + $("#imturnlight").val() + "</NormalWhiteLightOnLimit>" +
        "<VarWhiteLightOnThreshold>" + $("#Thresholdness").val() + "</VarWhiteLightOnThreshold>" +
        "<VarWhiteLightOffThreshold>" + $("#offthresholdness").val() + "</VarWhiteLightOffThreshold>" +
        "<ColorWorkmode>" + $("#ControlMode").val() + "</ColorWorkmode>" + //全彩白的控制模式
        "<CustomStartTime>00000000T" + $("#st_con_hour").val() + $("#st_con_minute").val() + $("#st_con_second").val() + "</CustomStartTime>" + //全彩开始时间
        "<CustomStopTime>00000000T" + $("#en_ic_hour1").val() + $("#en_ic_minute1").val() + $("#en_ic_second1").val() + "</CustomStopTime>" + //全彩结束时间
        "<VarInfraredWorkMode>" + $("#workingModeRed").select2('val') + "</VarInfraredWorkMode>" +
        "<VarInfraredBrightness>" + $("#Infraredness").val() + "</VarInfraredBrightness>" + //红外亮度
        "<VarWhiteControlMode>" + $("#im_workmodewrite").select2('val') + "</VarWhiteControlMode>" + //无极白 工作模式
        "<VarWhiteBrightness>" + $("#whitelightness").val() + "</VarWhiteBrightness>" + colorxml + Intelligentxml +
        "</IrCutFillter>";
    var cyURL;
    if (num) {
        cyURL = window.config.baseUrl + "/Images/" + num + "/IrCutFilter";
    } else {
        cyURL = window.config.baseUrl + "/Images/1/IrCutFilter";
    }
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: false,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
            // if (config.isSupportMultiChannel) {
            //     getIrCutFilter(num);
            // } else {
            //     getIrCutFilter();
            // }
        }
    });
    // }
}

function validateTime(startTime, endTime) {
    if (startTime == endTime) {
        $.optionBootoast($.i18n.prop('i18n-storage_time_msg'), "danger");
        return false;
    }
    return true;
}

function initIntelligent(xmlDoc) {
    if (intelligentLightCap.isIntelligentThreshold == 'true') {
        $("#imintelligentthreshold").val($(xmlDoc).find('IrCutFillter').eq(0).find('IntelligentThreshold').eq(0).text());
        $("#imintelligentthresholdvalue").text($(xmlDoc).find('IrCutFillter').eq(0).find('IntelligentThreshold').eq(0).text());
        if (rangInitDraw) {
            $("#imintelligentthreshold").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find('IntelligentThreshold').eq(0).text());
        }
    };
}
//初始化全彩白光
function initColors(xmlDoc) {
    //全彩白-控制模式(只存在于全彩白下)
    $("#ControlMode").select2('val', $(xmlDoc).find("IrCutFillter").eq(0).find("ColorWorkmode").eq(0).text());
    if (colorLightCap.isColorThreshold == 'true') { //全彩白光起效
        $("#imcolorthreshold").val($(xmlDoc).find('IrCutFillter').eq(0).find('ColorNightThreshold').eq(0).text());
        $("#imcolorthresholdvalue").text($(xmlDoc).find('IrCutFillter').eq(0).find('ColorNightThreshold').eq(0).text());
        if (rangInitDraw) { //初始化过可以直接设置值
            $("#imcolorthreshold").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find('ColorNightThreshold').eq(0).text());
        }
    };
    setTimed(); //全彩白下的定时时间
}
//初始化无极白
function initWhitelight(xmlDoc) {
    $("#im_workmodewrite").select2('val', $(xmlDoc).find('IrCutFillter').eq(0).find('VarWhiteControlMode').eq(0).text());
    $("#Controldirection").select2('val', $(xmlDoc).find('IrCutFillter').eq(0).find('VarWhiteWorkMode').eq(0).text());
    $("#whitelightness").val($(xmlDoc).find('IrCutFillter').eq(0).find('VarWhiteBrightness').eq(0).text()); //白光亮度
    $("#whitelightnessValue").text($(xmlDoc).find('IrCutFillter').eq(0).find('VarWhiteBrightness').eq(0).text());
    $("#Thresholdness").val($(xmlDoc).find('IrCutFillter').eq(0).find("VarWhiteLightOnThreshold").text()); //开灯阈值
    $("#ThresholdnessValue").text($(xmlDoc).find('IrCutFillter').eq(0).find("VarWhiteLightOnThreshold").text());
    $("#offthresholdness").val($(xmlDoc).find('IrCutFillter').eq(0).find("VarWhiteLightOffThreshold").text()); //关灯阈值
    $("#offthresholdnessValue").text($(xmlDoc).find('IrCutFillter').eq(0).find("VarWhiteLightOffThreshold").text());
    //起效亮度
    $("#imImage-2").val($(xmlDoc).find('IrCutFillter').eq(0).find('VariableWhiteThreshold').eq(0).text());
    $("#imImagevalue-2").text($(xmlDoc).find('IrCutFillter').eq(0).find('VariableWhiteThreshold').eq(0).text());
    if (rangInitDraw) {
        $("#whitelightness").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find('VarWhiteBrightness').eq(0).text());
        $("#offthresholdness").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find("VarWhiteLightOffThreshold").text());
        $("#imImage-2").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find('VariableWhiteThreshold').eq(0).text());
        $("#Thresholdness").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find("VarWhiteLightOnThreshold").text());
    }
    setWJBtimed();
}
//初始化 红外数据
function initRedlight(xmlDoc) {
    //工作模式
    $("#workingModeRed").select2('val', $(xmlDoc).find('IrCutFillter').eq(0).find('VarInfraredWorkMode').eq(0).text());
    //红外亮度
    $("#Infraredness").val($(xmlDoc).find('IrCutFillter').eq(0).find('VarInfraredBrightness').eq(0).text());
    $("#InfrarednessValue").text($(xmlDoc).find('IrCutFillter').eq(0).find('VarInfraredBrightness').eq(0).text());
    //起效亮度
    $("#imImage-1").val($(xmlDoc).find('IrCutFillter').eq(0).find('VariableInfraredThreshold').eq(0).text());
    $("#imImagevalue-1").text($(xmlDoc).find('IrCutFillter').eq(0).find('VariableInfraredThreshold').eq(0).text());
    if (rangInitDraw) {
        $("#Infraredness").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find('VarInfraredBrightness').eq(0).text());
        $("#imImage-1").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find('VariableInfraredThreshold').eq(0).text());
    }
}

function initNormalWhite(xmlDoc) {
    $("#imturnlight").val($(xmlDoc).find('IrCutFillter').eq(0).find("NormalWhiteLightOnLimit").text());
    $("#imturnlightvalue").text($(xmlDoc).find('IrCutFillter').eq(0).find("NormalWhiteLightOnLimit").text());
    $("#imturnofflight").val($(xmlDoc).find('IrCutFillter').eq(0).find("NormalWhiteLightOffLimit").text());
    $("#imturnofflightvalue").text($(xmlDoc).find('IrCutFillter').eq(0).find("NormalWhiteLightOffLimit").text());
    if (rangInitDraw) {
        $("#imturnlight").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find("NormalWhiteLightOnLimit").text());
        $("#imturnofflight").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find("NormalWhiteLightOffLimit").text());
    }
}

function initCommonRed(xmlDoc) {
    //普通红外--自动模式---ircut灵敏度
    $("#CUTsensitivity").val($(xmlDoc).find('IrCutFillter').eq(0).find('Sensitivity').eq(0).text());
    $("#CUTsensitivityValue").text($(xmlDoc).find('IrCutFillter').eq(0).find('Sensitivity').eq(0).text());
    if (rangInitDraw) {
        $("#CUTsensitivity").jRange("setValue", $(xmlDoc).find('IrCutFillter').eq(0).find('Sensitivity').eq(0).text());
    }
    //普通红外-定时---时间
    var startTime = $(xmlDoc).find('IrCutFillter').eq(0).find('DayStartTime').eq(0).text();
    var endTime = $(xmlDoc).find('IrCutFillter').eq(0).find('DayEndTime').eq(0).text();
    var startHour = startTime.toString().substring(0, 2);
    var startMin = startTime.toString().substring(3, 5);
    var startSec = startTime.toString().substring(6, 8);
    var endHour = endTime.toString().substring(0, 2);
    var endMin = endTime.toString().substring(3, 5);
    var endSec = endTime.toString().substring(6, 8);
    $("#st_con_hour2").val(startHour);
    $("#st_con_minute2").val(startMin);
    $("#st_con_second2").val(startSec);
    $("#en_ic_hour3").val(endHour);
    $("#en_ic_minute3").val(endMin);
    $("#en_ic_second3").val(endSec);
}
//初始化所有基本配置进度条
function drawIRCutLevel() {
    rangInitDraw = true;
    //白光亮度
    $('#whitelightness').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function () {
            $("#whitelightnessValue").text($("#whitelightness").val());
            throttle(setIrCutFilter);
        },
        onSetPosition: function() {
            $("#whitelightnessValue").text($("#whitelightness").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
    //无极白光--手动阈值-开灯阈值
    $('#Thresholdness').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#ThresholdnessValue").text($("#Thresholdness").val());
            throttle(setIrCutFilter);
        },
        onSetPosition: function() {
            $("#ThresholdnessValue").text($("#Thresholdness").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
    //无极白光--手动阈值-关灯阈值
    $('#offthresholdness').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#offthresholdnessValue").text($("#offthresholdness").val());
            throttle(setIrCutFilter);
        },
        onSetPosition: function() {
            $("#offthresholdnessValue").text($("#offthresholdness").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
    $('#imturnlight').jRange({ //智能双关下的起效亮度
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#imturnlightvalue").text($("#imturnlight").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#imturnlightvalue").text($("#iimturnlight").val());
        },
    });
    $('#imturnofflight').jRange({ //智能双关下的起效亮度
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#imturnofflightvalue").text($("#imturnofflight").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#imturnofflightvalue").text($("#imturnofflight").val());
        },
    });
    $('#imImage-2').jRange({ //智能双关下的起效亮度
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#imImagevalue-2").text($("#imImage-2").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#imImagevalue-2").text($("#imImage-2").val());
        },
    });
    $('#imImage-1').jRange({ //智能双关下的起效亮度
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#imImagevalue-1").text($("#imImage-1").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#imImagevalue-1").text($("#imImage-1").val());
        },
    });
    $('#imcolorthreshold').jRange({ //智能双关下的起效亮度
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#imcolorthresholdvalue").text($("#imcolorthreshold").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#imcolorthresholdvalue").text($("#imcolorthreshold").val());
        },
    });
    $('#imintelligentthreshold').jRange({ //智能双关下的起效亮度
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#imintelligentthresholdvalue").text($("#imintelligentthreshold").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#imintelligentthresholdvalue").text($("#imintelligentthreshold").val());
        },
    });
    $('#Infraredness').jRange({ //红外亮度
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#InfrarednessValue").text($("#Infraredness").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#InfrarednessValue").text($("#Infraredness").val());
        },
    });
    $('#Thresholdness').jRange({ //开灯阈值
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#ThresholdnessValue").text($("#Thresholdness").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#ThresholdnessValue").text($("#Thresholdness").val());
        },
    });
    $('#offthresholdness').jRange({ //关灯阈值
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#offthresholdnessValue").text($("#offthresholdness").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#offthresholdnessValue").text($("#offthresholdness").val());
        },
    });
    $('#CUTsensitivity').jRange({ //ir-cut灵敏度
        from: 0,
        to: 100,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#CUTsensitivityValue").text($("#CUTsensitivity").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            $("#CUTsensitivityValue").text($("#CUTsensitivity").val());
        },
    });
    $('#Switchingtimeness').jRange({ //ir-cut切换时间
        from: 3,
        to: 15,
        step: 1,
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#SwitchingtimenessValue").text($("#Switchingtimeness").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setIrCutFilter);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            // if (gs_flag == 0 && gs_imred_flag != 0) {
            $("#SwitchingtimenessValue").text($("#Switchingtimeness").val());
            // }
            // gs_imred_flag = 1;
        },
    });

}
// 获取部分主要进度条数值
function getColor(ch) {
    var url;
    url = window.config.baseUrl + "/Images/" + ch + "/Color";
    $.ajax({
        url: url,
        type: "GET",
        async: false,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#brightness").val($(xmlDoc).find('Color').eq(0).find('Brightness').eq(0).text()); //亮度
            $("#brightnessvalues").text($(xmlDoc).find('Color').eq(0).find('Brightness').eq(0).text());
            $("#contrastRatio").val($(xmlDoc).find('Color').eq(0).find('Contrast').eq(0).text()); //对比度
            $("#contrastRatioValue").text($(xmlDoc).find('Color').eq(0).find('Contrast').eq(0).text());
            $("#saturation").val($(xmlDoc).find('Color').eq(0).find('Saturation').eq(0).text()); //饱和度
            $("#saturationValue").text($(xmlDoc).find('Color').eq(0).find('Saturation').eq(0).text());
            $("#tone").val($(xmlDoc).find('Color').eq(0).find('Hue').eq(0).text()); //色度
            $("#toneValue").text($(xmlDoc).find('Color').eq(0).find('Hue').eq(0).text());
            $("#Sharpness").val($(xmlDoc).find('Color').eq(0).find('Sharpness').eq(0).text()); //锐度
            $("#SharpnessValue").text($(xmlDoc).find('Color').eq(0).find('Sharpness').eq(0).text());
            if (!rangInitIamges) { //首次初始化
                iamges();
            } else {
                $("#brightness").jRange("setValue", $(xmlDoc).find('Color').eq(0).find('Brightness').eq(0).text());
                $("#contrastRatio").jRange("setValue", $(xmlDoc).find('Color').eq(0).find('Contrast').eq(0).text());
                $("#saturation").jRange("setValue", $(xmlDoc).find('Color').eq(0).find('Saturation').eq(0).text());
                $("#tone").jRange("setValue", $(xmlDoc).find('Color').eq(0).find('Hue').eq(0).text());
                $("#Sharpness").jRange("setValue", $(xmlDoc).find('Color').eq(0).find('Sharpness').eq(0).text());
            }

        }
    });
}
// 节流操作解决多次提示保存
function throttle(method, context) {
    clearTimeout(method.tId);
    method.tId = setTimeout(function() {
        method.call(context);
    }, 1000);
}
// 保存部分主要进度条数值
function setColor(num) {
    if ("admin" == config.user || "super_admin" == config.user) {
        var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><Color Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Mode>manual</Mode><Brightness>" + $("#brightness").val() + "</Brightness><Contrast>" + $("#contrastRatio").val() + "</Contrast><Saturation>" + $("#saturation").val() + "</Saturation><Hue>" + $("#tone").val() + "</Hue><Sharpness>" + $("#Sharpness").val() + "</Sharpness></Color>";
        var cyURL;
        num = num || 1;
        cyURL = window.config.baseUrl + "/Images/" + $("#NVR_ChnOSDChannelMask").select2("val") + "/Color";
        $.ajax({
            type: "PUT",
            url: cyURL,
            processData: false,
            data: cyXml,
            beforeSend: function(xhr) {
                xhr.setRequestHeader("If-Modified-Since", "0");
            },
            complete: function(xhr, textStatus) {
                SaveState(xhr);
            }
        });
    } else {
        $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
    }
}
// 获取部分图像配置（3D降噪，背光补偿，宽动态，图像镜像，抗闪模式，抗闪强度）
function getImageBasic(ch) {
    var url;
    url = window.config.baseUrl + "/Images/" + ch + "/Basic";
    $.ajax({
        url: url,
        type: "GET",
        async: true,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function (xmlDoc, textStatus, xhr) {
            $("#noisereduction").val($(xmlDoc).find('Basic').eq(0).find('NoiseReduce').eq(0).text()).trigger('change.select2'); //3D降噪
            // $("#BacklightCompensation").val($(xmlDoc).find('Basic').eq(0).find('BLC').eq(0).text()).trigger('change.select2'); //背光补偿
            var chek = $(xmlDoc).find('Basic').eq(0).find('BLC').eq(0).text();
            if (chek == "open") {
                $('#boff').addClass('active');
                document.getElementById('bclos').classList.remove('active');
            } else {
                document.getElementById('boff').classList.remove('active');
                $("#bclos").addClass('active');
            }
            $("#Widedynamic").val($(xmlDoc).find('Basic').eq(0).find('WDR').eq(0).text()).trigger('change.select2'); //宽动态
            wdrOldvalue = $(xmlDoc).find('Basic').eq(0).find('WDR').eq(0).text();
            if ('notsupport' == $(xmlDoc).find('Basic').eq(0).find('FlipMode').eq(0).text()) { //图像镜像
                $("#sufile").hide();
            } else {
                $("#sufile").show();
                $("#ImageMirroring").val($(xmlDoc).find('Basic').eq(0).find('FlipMode').eq(0).text()).trigger('change.select2');
            }
            $("#AntiflashMode").val($(xmlDoc).find('Basic').eq(0).find('powerLineFrequencyMode').eq(0).text()).trigger('change.select2'); //抗闪模式
            $("#FlashoverResis").val($(xmlDoc).find('Basic').eq(0).find('powerLineFrequencyStrength').eq(0).text()).trigger('change.select2'); //抗闪强度
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
// 保存部分图像配置（3D降噪，背光补偿，宽动态，图像镜像，抗闪模式，抗闪强度）
function setImageBasic(num) {
    var bgvalue;
    var bo = document.getElementById("boff");
    var arr = []
    for (var a = 0; a < bo.classList.length; a++) {
        arr.push(bo.classList[a])
    }
    if (arr.indexOf('active') != -1) {
        bgvalue = "open";
    } else {
        bgvalue = "close";
    }
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><Basic Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><powerLineFrequencyMode>" + $("#AntiflashMode").val() + "</powerLineFrequencyMode><NoiseReduce>" + $("#noisereduction").val() + "</NoiseReduce><BLC>" + bgvalue + "</BLC><WDR>" + $("#Widedynamic").val() + "</WDR><FlipMode>" + $("#ImageMirroring").val() + "</FlipMode>" +
        "<powerLineFrequencyStrength>" + $("#FlashoverResis").val() + "</powerLineFrequencyStrength></Basic>";

    var cyURL;
    num = num || 1;
    cyURL = window.config.baseUrl + "/Images/" + num + "/Basic";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
        }
    });
}
// 白平衡改变
function changeb() {
    if ($("#WhiteBalance").val() == 'manual') {
        $(".gain").show();
        // $('#Redgain').jRange('setValue', 50)
        // $('#Bluegain').jRange('setValue', 50)
    } else {
        $(".gain").hide();
    }

}
// 获取白平衡条件（白平衡以及红蓝增益）
function getWhiteBalance(ch) {
    url = window.config.baseUrl + "/Images/" + ch + "/WhiteBalance";
    $.ajax({
        url: url,
        type: "GET",
        async: true,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#WhiteBalance").val($(xmlDoc).find('WhiteBlance').eq(0).find('Mode').eq(0).text()).trigger('change.select2'); //白平衡
            changeb();
            $("#Redgain").val($(xmlDoc).find('WhiteBlance').eq(0).find('GainRed').eq(0).text()); //红增益
            $("#RedgainValue").text($(xmlDoc).find('WhiteBlance').eq(0).find('GainRed').eq(0).text());
            $("#Bluegain").val($(xmlDoc).find('WhiteBlance').eq(0).find('GainBlue').eq(0).text()); //蓝增益
            $("#BluegainValue").text($(xmlDoc).find('WhiteBlance').eq(0).find('GainBlue').eq(0).text());
            if (!rangInitWhiteBalance) {
                drawWhiteBalance();
            } else {
                $("#Redgain").jRange("setValue", $(xmlDoc).find('WhiteBlance').eq(0).find('GainRed').eq(0).text());
                $("#Bluegain").jRange("setValue", $(xmlDoc).find('WhiteBlance').eq(0).find('GainBlue').eq(0).text());
            }

        }
    });
}
// 保存白平衡条件（白平衡以及红蓝增益）
function setWhiteBalance(num) {
    if ("admin" == config.user || "super_admin" == config.user) {
        var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><WhiteBlance Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Mode>" + $("#WhiteBalance").val() + "</Mode><GainRed>" + $("#Redgain").val() + "</GainRed><GainBlue>" + $("#Bluegain").val() + "</GainBlue></WhiteBlance>";
        var cyURL;
        num = num || 1;
        cyURL = window.config.baseUrl + "/Images/" + num + "/WhiteBalance";
        $.ajax({
            type: "PUT",
            url: cyURL,
            processData: false,
            data: cyXml,
            beforeSend: function(xhr) {
                xhr.setRequestHeader("If-Modified-Since", "0");
            },
            complete: function(xhr, textStatus) {
                SaveState(xhr);
            }
        });
    } else {
        $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
    }
}
// 初始化增益进度条
function drawWhiteBalance() {
    rangInitWhiteBalance = true;
    $('#Redgain').jRange({ //红增益
        from: 0,
        to: 100,
        step: 1,
        //scale: [0,25,50,75,100],
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#RedgainValue").text($("#Redgain").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setWhiteBalance);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            // if (gs_flag == 0 && gs_imred_flag != 0) {
            $("#RedgainValue").text($("#Redgain").val());
            // }
            // gs_imred_flag = 1;
        },
        onMouseDown: function() {
            $("#RedgainValue").text($("#Redgain").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setWhiteBalance);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
            // gs_flag = 1;
        },
        onMouseUp: function() {
            $("#RedgainValue").text($("#imRedgainred").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setWhiteBalance);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
            // gs_flag = 0;
        }
    });
    $('#Bluegain').jRange({ //蓝增益
        from: 0,
        to: 100,
        step: 1,
        //scale: [3,5,10,15],
        format: '%s',
        width: 240,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#BluegainValue").text($("#Bluegain").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setWhiteBalance);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
        },
        onSetPosition: function() {
            // if (gs_flag == 0 && gs_iimblue_flag != 0) {
            $("#BluegainValue").text($("#Bluegain").val());
            // }
            // gs_iimblue_flag = 1;
        },
        onMouseDown: function() {
            $("#BluegainValue").text($("#Bluegain").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setWhiteBalance);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
            // gs_flag = 1;
        },
        onMouseUp: function() {
            $("#BluegainValue").text($("#Bluegain").val());
            if ("admin" == config.user || "super_admin" == config.user) {
                throttle(setWhiteBalance);
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
            }
            // gs_flag = 0;
        }
    });
}
//获取（光圈类型 曝光时间 增益）
function getExposure(ch) {
    url = window.config.baseUrl + "/Images/" + $("#NVR_ChnOSDChannelMask").val() + "/Exposure";
    $.ajax({
        url: url,
        type: "GET",
        async: true,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#ApertureType").val($(xmlDoc).find('Exposure').eq(0).find('IrisType').eq(0).text()).trigger('change.select2'); //光圈类型
            $("#ExposureTime").val($(xmlDoc).find('Exposure').eq(0).find('shutter').eq(0).text()).trigger('change.select2'); //曝光时间
            $("#gain").val($(xmlDoc).find('Exposure').eq(0).find('Gain').eq(0).text()).trigger('change.select2'); //增益

        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
// 保存（光圈类型 曝光时间 增益）
function setExposure(num) {
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><Exposure Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><IrisType>" + $("#ApertureType").val() + "</IrisType><shutter>" + $("#ExposureTime").val() + "</shutter><Gain>" + $("#gain").val() + "</Gain><Shuttermode></Shuttermode></Exposure>";
    var cyURL;
    num = num || 1;
    cyURL = window.config.baseUrl + "/Images/" + num + "/Exposure";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
        }
    });
}
// 获取去雾
function getScene(ch) {
    var url;
    url = window.config.baseUrl + "/Images/" + ch + "/Scene";
    $.ajax({
        url: url,
        type: "GET",
        async: true,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#imdefog").val($(xmlDoc).find('Scene').eq(0).find('FogValue').eq(0).text());
            $("#imedefogvalue").text($(xmlDoc).find('Scene').eq(0).find('FogValue').eq(0).text());
            var delopval = parseInt($(xmlDoc).find('Scene').eq(0).find('FogValue').eq(0).text());
            if ([0, 30, 60, 90].indexOf(delopval) == -1) {
                $("#de-fogging").val("0").trigger('change.select2');
            } else {
                $("#de-fogging").val($(xmlDoc).find('Scene').eq(0).find('FogValue').eq(0).text()).trigger('change.select2');
            }
        }
    });
}
// 保存去雾
function setScene(num) {
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><Scene Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Support></Support><Mode></Mode><LDC></LDC><Fog></Fog><FogValue>" + $("#de-fogging").val() + "</FogValue><Gamma></Gamma></Scene>";
    var cyURL;
    num = num || 1;
    cyURL = window.config.baseUrl + "/Images/" + num + "/Scene";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
        }
    });
}
// 获取灵敏度
function getImageSens(ch) {
    var url;
    url = window.config.baseUrl + "/Pictures/" + ch + "/Motion";
    $.ajax({
        url: url,
        type: "GET",
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            gs_xml_doc = xmlDoc;
            $("#ImageSensity").val($(xmlDoc).find('Motion').eq(0).find('Senstive').eq(0).text()).trigger('change.select2');
        }
    });
}
// 保存灵敏度
function setImageSens(num) {
    $(gs_xml_doc).find('Motion').eq(0).find('Senstive').eq(0).text($("#ImageSensity").val());
    var cyXml = gs_xml_doc;
    var cyURL;
    num = num || 1;
    cyURL = window.config.baseUrl + "/Pictures/" + num + "/Motion";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
        }
    });
}
function delayTimeSet() {
    if ("admin" == config.user || "super_admin" == config.user) {
        var interval = $("#interval").val();
        var reg = /^([3-9]|[1-9]\d|[1-5]\d{2}|600)$/;
        if (!reg.test(interval)) {
            $.optionBootoast($.i18n.prop('i18n-time-swith'), "danger");
            return false;
        }
        setIrCutFilter($("#NVR_ChnOSDChannelMask").val() - 0);
    } else {
        $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
    }


}