var user = config.user,
    pwd = config.password;
var quality = ['high', 'middle', 'low'];
var xmlDe;
var rWidths, rHeights;
var Resolutions = [];
var gs_arrResolution = [{
        Resolution: "352*288",
        ResolutionVal: "CIF(352*288)"
    },
    {
        Resolution: "704*576",
        ResolutionVal: "D1(704*576)"
    },
    {
        Resolution: "1280*960",
        ResolutionVal: "1280*960"
    },
    {
        Resolution: "1280*1024",
        ResolutionVal: "1280*1024"
    },
    {
        Resolution: "1600*1200",
        ResolutionVal: "1600*1200"
    },
    {
        Resolution: "1280*720",
        ResolutionVal: "720P(1280*720)"
    },
    {
        Resolution: "1920*1080",
        ResolutionVal: "1080P(1920*1080)"
    },
    {
        Resolution: "640*480",
        ResolutionVal: "640*480"
    },
    {
        Resolution: "2048*1536",
        ResolutionVal: "QXGA(2048*1536)"
    },
    {
        Resolution: "640*360",
        ResolutionVal: "640*360"
    },
    {
        Resolution: "320*240",
        ResolutionVal: "320*240"
    },
    {
        Resolution: "2592*1520",
        ResolutionVal: "UWXGA(2592*1520)"
    },
];
$(document).ready(function() {
    initAudioPage();
    // getSnapCapability();
    getashortcfgfromdevice();
    if (config.deviceCap.TimeSnapshot == "true") {
        $(".isAll").show();
        getShorttimeCaoture();
    } else {
        $(".isAll").hide();
    }
    $('#AudioStreamsave').click(function() {
        if ("admin" == user || "super_admin" == user) {
            setashortcfgfromdevice();
            if (config.deviceCap.TimeSnapshot == "true") {
                setShorttimeCaoture();
            }
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    });

});

function initAudioPage() {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 360
        });
    }
}
// 获取当前抓图配置
function getashortcfgfromdevice() {
    $.ajax({
        url: config.baseUrl + "/Snapshot/Config",
        type: "GET",
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            xmlDe = xmlDoc
            $("#CaptureFbjso").val($(xmlDoc).find('SnapshotConfig').eq(0).find('SnapshotCount').eq(0).text());
            $("#CaptureQuality").val($(xmlDoc).find('SnapshotConfig').eq(0).find('SnapshotIntervalTime').eq(0).text());
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

function setashortcfgfromdevice() {
    $(xmlDe).find('SnapshotConfig').eq(0).find('SnapshotCount').eq(0).text($("#CaptureFbjso").val());
    $(xmlDe).find('SnapshotConfig').eq(0).find('SnapshotIntervalTime').eq(0).text($("#CaptureQuality").val());
    var cyURL = config.baseUrl + "/Snapshot/Config";
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: true,
        processData: false,
        data: xmlDe,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
        }
    });
}
// 以前就没有备注看逻辑好像是获取当前支持的分辨率
function getSnapCapability() {
    $("#CaptureFbjso").empty();
    $.ajax({
        url: config.baseUrl + "/Snapshot/Capability",
        type: "get",
        async: false,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            var ResolutionLength = $(xmlDoc).find('SnapshotCapabilityList').eq(0).find('SnapshotCapability').length;
            for (var i = 0; i < ResolutionLength; i++) {
                rWidths = $(xmlDoc).find('SnapshotCapabilityList').eq(0).find('SnapshotCapability').eq(i).find('ResolutionWidth').text();
                rHeights = $(xmlDoc).find('SnapshotCapabilityList').eq(0).find('SnapshotCapability').eq(i).find('ResolutionHeight').text();
                Resolutions.push(rWidths + '*' + rHeights);
            }
        },
        error: function(xhr, textStatus, errorThrown) {}
    });

}