var gs_xml_doc,user=config.user,pwd=config.password;
$(document).ready(function () {
    initAudioPage();
    getaudiocfgfromdevice();
    $('#AudioStreamsave').click(function() {
		if("admin" == user || "super_admin" == user) {
			setaudiocfgfromdevice();
		}
		else {
			$.optionBootoast($.i18n.prop('i18n-propoe'),"danger")
		}
	});

});
function initAudioPage() {
    if (!$('.select2').hasClass('select2-offscreen')) {
		$(".select2").select2({
			minimumResultsForSearch: -1,
			width:360
		});
	}
    var deviceCap=config.deviceCap;
    if(deviceCap.G711u=="true"){
        $('#auFormat').empty();
        $("<option value='G.711U'>G711U</option>").appendTo("#auFormat");
    }
    if(deviceCap.G711a=="true"){
        $("<option value='G.711A'>G711A</option>").appendTo("#auFormat");
    }
    var deviceInfo=config.deviceInfo;
    deviceInfo.TwoWayAudio=='true' ? $('#outVolumeItme').show() : $('#outVolumeItme').hide();
}
// 初始化进度条
function drawVolume() {
    $('#inputvolume').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 360,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#inputvolumevalues").text($("#inputvolume").val());
        },
        onSetPosition: function() {
            $("#inputvolumevalues").text($("#inputvolume").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
    $('#outvolume').jRange({
        from: 0,
        to: 100,
        // step: 1,
        scale: 1,
        format: '%s',
        width: 360,
        showLabels: false,
        showScale: false,
        onstatechange: function() {
            $("#outvolumevalues").text($("#outvolume").val());
        },
        onSetPosition: function() {
            $("#outvolumevalues").text($("#outvolume").val());
        },
        onMouseDown: function() {},
        onMouseUp: function() {

        }
    });
};
function getaudiocfgfromdevice() {
    $.ajax({
        url : config.baseUrl+ "/Streams/AudioStream",
        type : "GET",
        async : true,
        timeout : 15000,
        beforeSend : function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success : function(xmlDoc, textStatus, xhr) {
            //获取数据成功之后，刷新界面
            gs_xml_doc = xmlDoc;
            $("#AudioInMethod").select2('val',$(xmlDoc).find('AudioStream').eq(0).find('AudioInMethod').eq(0).text());
            $("#auFormat").select2('val',$(xmlDoc).find('AudioStream').eq(0).find('AudioInCodec').eq(0).text());
            $("#inputvolume").val($(xmlDoc).find('AudioStream').eq(0).find('AudioInVolume').text());
            $("#inputvolumevalues").text($(xmlDoc).find('AudioStream').eq(0).find('AudioInVolume').text());
            $("#outvolume").val($(xmlDoc).find('AudioStream').eq(0).find('AudioOutVolume').text());
            $("#outvolumevalues").text($(xmlDoc).find('AudioStream').eq(0).find('AudioOutVolume').text());
            drawVolume();

        },
        error : function(xhr, textStatus, errorThrown) {
        }
    });
}
function setaudiocfgfromdevice(){
    var cyXml = gs_xml_doc;
    $(cyXml).find('AudioStream').eq(0).find('AudioInMethod').text($("#AudioInMethod").val());
    $(cyXml).find('AudioStream').eq(0).find('AudioInCodec').text($("#auFormat").val());
    $(cyXml).find('AudioStream').eq(0).find('AudioInVolume').text($("#inputvolume").val());
    $(cyXml).find('AudioStream').eq(0).find('AudioOutVolume').text($("#outvolume").val());
   //保存
   var cyURL = config.baseUrl + "/Streams/AudioStream";
   $.ajax({
       type: "PUT",
       url: cyURL,
       processData: false,
       data: cyXml,
       beforeSend: function(xhr){
           xhr.setRequestHeader("If-Modified-Since", "0");
       },
       complete: function(xhr) {
            SaveState(xhr);
       },
       error:function(xhr){
            $.optionBootoast( $.i18n.prop('i18n-savefail'),"danger")
       }
   });
}
