// JavaScript Document
document.charset = "utf-8";
var playWay = sessionStorage.getItem("playstyle");
var xML;
var tnumber = 0;
/*************************************************
 Function:
 Description:  初始化程序
**************************************************/
$(document).ready(function() {
    $("#CHVCchid").select2({
        width: 220,
        minimumResultsForSearch: -1,
    });
    for (var i = 1; i <= config.deviceInfo.VideoInputChannels; i++) {
        $("#CHVCchid").append('<option class="option" value="' + i + '">' + i + '</option>'); //Channel列表
    }
    // 默认第一个通道
    $("#CHVCchid").val("1").trigger('change.select2');
    getyinsiPower(1);
    // 切换通道号
    $("#CHVCchid").change(function() {
        var chid = $("#CHVCchid").val() * 1;
        getyinsiPower(chid)
        if (sessionStorage.getItem("playstyle") == "EasyVideo") {
            NVReasyldspecialvideos(true, tnumber);
            getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 4, "", chid - 1);
            tnumber = chid - 1;
            getPrivacyZone(chid);
        } else {
            StopVideo(0);
            PreviewSpecificChannelInFirstWindow(chid - 1, 4);
            ClearDrawRegions(0);
            getPrivacyZone(chid);

        }
    });

    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"
    }
    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/privacySettings.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()
            }
        } else {
            if (sessionStorage.getItem("playstyle") == "EasyVideo" && window.location.hash == "#nvr/pages/image/privacySettings.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()
            } else if (window.location.hash == "#nvr/pages/image/privacySettings.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") {
            clearInterval(config.gs_interval);
            $("#playCanvas").hide();
            $("#axdiv").show();
            $("#revo").hide()
            getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 0, "", 0);
            ChangeLayout(1)
            SetActiveXMode(1);
            CloseAllChannels();
            PreviewSpecificChannelInFirstWindow(0, 4);
            StartDrawRegion(0); //开始绘制
            SetBrushColor(0, (143, 75, 75)); //设置画刷颜
            SetDisplayRegionsEnable(0, true); //显示选中区域
        } else if (sessionStorage.getItem("playstyle") == "MJPEG") {
            $("#playCanvas").hide();
            $("#axdiv").show();
            $("#axdiv").empty();
            clearInterval(config.gs_interval)
        }
        getPrivacyZone(1); //获取PrivacyZoneD信息
    } else {
        if (sessionStorage.getItem("xystyle") == "p6s" || sessionStorage.getItem("xystyle") == "p2p") {
            config.refreshplayvideo = true;
            $("#playCanvas").hide();
            $("#axdiv").show();
            getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 4, "", 0);
            getPrivacyZone(1); //获取PrivacyZoneD信息
        } else {
            if (sessionStorage.getItem("playstyle") == "EasyVideo") {
                $("#playCanvas").hide();
                $("#axdiv").show();
            }
            if (config.refreshplayvideo) {
                getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 4, "", 0);
                if (config.devicelose == false || sessionStorage.getItem("playstyle") != "EasyVideo") {
                    getPrivacyZone(1); //获取PrivacyZoneD信息
                }
            } else {
                if (sessionStorage.getItem("playstyle") == "EasyVideo") {
                    refershPlayer("privay")
                }

            }
        }
    }

    $("#psetsave").click(function() {
        var chid = $("#CHVCchid").val() * 1;
        setPrivacyZone(chid);
    });
    // 清除全部
    $("#revoall").click(function() {
            if (sessionStorage.getItem("playstyle") == "EasyVideo") {
                pracyFuntins(3, 1, 4)
            } else if (sessionStorage.getItem("playstyle") == "ActiveX") {
                ClearDrawRegions(0);
            }
        })
        // 单个删除
    $("#revo").click(function() {
        pracyFuntins(2, 1, 4)
    })
});

function getprivacyValue(v) {
    xML = setPloit(v)
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><Mosaic Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#isShowys").is(":checked") + "</Enable><MosaicRegionList Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>"
    cyXml = cyXml + xML + "</MosaicRegionList></Mosaic>";
    var cyURL = window.config.baseUrl + "/Pictures/" + $("#CHVCchid").val() * 1 + "/Mosaic";
    $.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 setPloit(v) {
    var Xml;
    if (v.privacyCover.bottom) {
        for (var i = 0; i < v.privacyCover.bottom.length; i++) {
            Xml += "<Region Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>";
            Xml += "<ID>" + (i + 1) + "</ID>";
            Xml += "<TopLeftX>" + v.privacyCover.left[i] + "</TopLeftX>";
            Xml += "<TopLeftY>" + v.privacyCover.top[i] + "</TopLeftY>";
            Xml += "<BottomRightX>" + v.privacyCover.right[i] + "</BottomRightX>";
            Xml += "<BottomRightY>" + v.privacyCover.bottom[i] + "</BottomRightY>";
            Xml += "</Region>";
        }
    } else {
        Xml = ""
    }
    return Xml
}
/*************************************************
 Function:        getPrivacyZone
 Description:     获取PrivacyZoneD信息
 *************************************************/
function getPrivacyZone(ch) {
    $.ajax({
        url: window.config.baseUrl + "/Pictures/" + ch + "/Mosaic",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            // if ("true" == $(xmlDoc).find('Mosaic').eq(0).find('Enable').eq(0).text()) {
            $("#isShowys").prop("checked", JSON.parse($(xmlDoc).find('Mosaic').eq(0).find('Enable').eq(0).text()));
            var regionLength = $(xmlDoc).find('Mosaic').eq(0).find('MosaicRegionList').eq(0).find('Region').length;
            if (sessionStorage.getItem("playstyle") == "EasyVideo") {
                var leftArr = []
                var rightArr = []
                var topArr = []
                var bottomArr = []
                for (var i = 0; i < regionLength; i++) {
                    leftArr.push(parseInt($(xmlDoc).find('MosaicRegionList').eq(0).find('Region').eq(i).find('TopLeftX').eq(0).text()));
                    topArr.push(parseInt($(xmlDoc).find('MosaicRegionList').eq(0).find('Region').eq(i).find('TopLeftY').eq(0).text()));
                    rightArr.push(parseInt($(xmlDoc).find('MosaicRegionList').eq(0).find('Region').eq(i).find('BottomRightX').eq(0).text()));
                    bottomArr.push(parseInt($(xmlDoc).find('MosaicRegionList').eq(0).find('Region').eq(i).find('BottomRightY').eq(0).text()));
                }
                pracyFuntins(1, 1, regionLength, leftArr, topArr, rightArr, bottomArr)
            } else if (sessionStorage.getItem("playstyle") == "ActiveX") {
                if (0 < regionLength) {
                    SetDisplayRegionsEnable($("#CHVCchid").val() - 1, true); //显示选中区域
                    for (var i = 0; i < regionLength; i++) {
                        AddDrawRegion(0,
                            $(xmlDoc).find('MosaicRegionList').eq(0).find('Region').eq(i).find('TopLeftX').eq(0).text(),
                            $(xmlDoc).find('MosaicRegionList').eq(0).find('Region').eq(i).find('TopLeftY').eq(0).text(),
                            $(xmlDoc).find('MosaicRegionList').eq(0).find('Region').eq(i).find('BottomRightX').eq(0).text(),
                            $(xmlDoc).find('MosaicRegionList').eq(0).find('Region').eq(i).find('BottomRightY').eq(0).text()
                        );
                    }
                }

            }
            // } else {
            // $("#isShowys").prop("checked", false);
            // }
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

/*************************************************
 Function:        setPrivacyZone
 Description:     保存PrivacyZone
 *************************************************/
function setPrivacyZone(chn) {
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><Mosaic Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#isShowys").is(":checked") + "</Enable><MosaicRegionList Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>"
    if (sessionStorage.getItem("playstyle") == "ActiveX") {
        var RegionList = GetDrawRegions($("#CHVCchid").val() - 1);
        var xmlDoc = parseXmlFromStr(RegionList);
        var elementLength = xmlDoc.getElementsByTagName("Region").length;
        if (0 < elementLength) {
            for (var i = 0; i < elementLength; i++) {
                var TopLeftX = parseInt(xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftX"));
                var TopLeftY = parseInt(xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftY"));
                var BottomRightX = parseInt(xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightX"));
                var BottomRightY = parseInt(xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightY"));
                if (TopLeftX < BottomRightX && TopLeftY < BottomRightY) {
                    cyXml += "<Region Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>";
                    cyXml += "<ID>" + (i + 1) + "</ID>";
                    cyXml += "<TopLeftX>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftX") + "</TopLeftX>";
                    cyXml += "<TopLeftY>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftY") + "</TopLeftY>";
                    cyXml += "<BottomRightX>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightX") + "</BottomRightX>";
                    cyXml += "<BottomRightY>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightY") + "</BottomRightY>";
                    cyXml += "</Region>";
                } else if (TopLeftX > BottomRightX && TopLeftY < BottomRightY) {
                    cyXml += "<Region Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>";
                    cyXml += "<ID>" + (i + 1) + "</ID>"
                    cyXml += "<TopLeftX>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightX") + "</TopLeftX>";
                    cyXml += "<TopLeftY>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftY") + "</TopLeftY>";
                    cyXml += "<BottomRightX>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftX") + "</BottomRightX>";
                    cyXml += "<BottomRightY>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightY") + "</BottomRightY>";
                    cyXml += "</Region>";
                } else if (TopLeftX < BottomRightX && TopLeftY > BottomRightY) {
                    cyXml += "<Region Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>";
                    cyXml += "<ID>" + (i + 1) + "</ID>"
                    cyXml += "<TopLeftX>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftX") + "</TopLeftX>";
                    cyXml += "<TopLeftY>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightY") + "</TopLeftY>";
                    cyXml += "<BottomRightX>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightX") + "</BottomRightX>";
                    cyXml += "<BottomRightY>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftY") + "</BottomRightY>";
                    cyXml += "</Region>";
                } else {
                    cyXml += "<Region Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>";
                    cyXml += "<ID>" + (i + 1) + "</ID>"
                    cyXml += "<TopLeftX>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightY") + "</TopLeftX>";
                    cyXml += "<TopLeftY>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("BottomRightY") + "</TopLeftY>";
                    cyXml += "<BottomRightX>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftX") + "</BottomRightX>";
                    cyXml += "<BottomRightY>" + xmlDoc.getElementsByTagName("Region")[i].getAttribute("TopLeftY") + "</BottomRightY>";
                    cyXml += "</Region>";
                }
            }
        }
        cyXml += "</MosaicRegionList></Mosaic>";
        var cyURL = window.config.baseUrl + "/Pictures/" + chn + "/Mosaic";
        $.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 if (sessionStorage.getItem("playstyle") == "EasyVideo") {
        pracyFuntins(1, 2, 4);
    }
}
// 获取隐私遮盖能力
function getyinsiPower(chid) {
    $.ajax({
        type: "get",
        url: config.baseUrl + '/System/DeviceCap/' + chid,
        dataType: 'XML',
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc) {
            var privacy = $(xmlDoc).find('PrivacyZone').eq(0).find("Support").text();
            if (!privacy) {
                $("#isShowys").attr('disabled', 'disabled')
            } else {
                $("#isShowys").removeAttr('disabled');
            }
        },
        error: function(data) {}
    });
}