
var waiting_timer;
var timeout_count=0;

var allow_venc_framerate=1;
var allow_venc_gop=1;
var allow_venc_bitrate=1;
var allow_venc_resolution=1;
var allow_ts_uri=1;
var allow_hls_uri=1;
var allow_flv_uri=1;
var allow_rtsp_uri=1;
var allow_rtmp_uri=1;
var allow_rtmp_publish_uri=1;
var allow_multicast_ip=1;
var allow_multicast_port=1;
var allow_srt_port=1;
var allow_srt_publish_uri=1;
var allow_srt_key=1;

function init_language() {
    jQuery.i18n.properties({
        name: 'common',
        path: '../lang/',
        mode: 'map',
        language: window.parent.get_language_setup(),
        callback: function () {
            $('#header_text').html($.i18n.prop('encode_config_header_text_txt'));
            $('#venc_codec_txt').html($.i18n.prop('encode_config_venc_codec_txt'));
            $('#venc_framerate_txt').html($.i18n.prop('encode_config_venc_framerate_txt'));
            $('#venc_gop_txt').html($.i18n.prop('encode_config_venc_gop_txt'));
            $('#venc_bitrate_txt').html($.i18n.prop('encode_config_venc_bitrate_txt'));
            $('#venc_resolution_txt').html($.i18n.prop('encode_config_venc_resolution_txt'));
            $('#venc_level_txt').html($.i18n.prop('encode_config_venc_level_txt'));
            $('#ts_uri_txt').html($.i18n.prop('encode_config_ts_uri_txt'));
            $('#hls_uri_txt').html($.i18n.prop('encode_config_hls_uri_txt'));
            $('#flv_uri_txt').html($.i18n.prop('encode_config_flv_uri_txt'));
            $('#rtsp_uri_txt').html($.i18n.prop('encode_config_rtsp_uri_txt'));
            $('#rtmp_uri_txt').html($.i18n.prop('encode_config_rtmp_uri_txt'));
            $('#rtmp_publish_uri_txt').html($.i18n.prop('encode_config_rtmp_publish_uri_txt'));
            $('#multicast_ip_txt').html($.i18n.prop('encode_config_multicast_ip_txt'));
            $('#multicast_port_txt').html($.i18n.prop('encode_config_multicast_port_txt'));
            $('#srt_port_txt').html($.i18n.prop('encode_config_srt_port_txt'));
            $('#srt_publish_uri_txt').html($.i18n.prop('encode_config_srt_publish_uri_txt'));
            $('#srt_key_txt').html($.i18n.prop('encode_config_srt_key_txt'));
            $('#encode_config_setting').val($.i18n.prop('encode_config_setting'));
            
            $('#ts_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#ts_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
            $('#hls_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#hls_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
            $('#flv_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#flv_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
            $('#rtsp_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#rtsp_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
            $('#rtmp_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#rtmp_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
            $('#rtmp_publish_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#rtmp_publish_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
            $('#multicast_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#multicast_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
			//
			$('#multicast_type_txt').html($.i18n.prop('encode_config_multicast_type_txt'));
			$('#multicast_udp_txt').html($.i18n.prop('encode_config_multicast_udp_txt'));
			$('#multicast_rtp_txt').html($.i18n.prop('encode_config_multicast_rtp_txt'));
			//
            $('#srt_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#srt_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
            $('#srt_publish_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#srt_publish_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
            $('#srt_key_enable_off_txt').html($.i18n.prop('encode_config_off_txt'));
            $('#srt_key_enable_on_txt').html($.i18n.prop('encode_config_on_txt'));
            
        }
    });
};

function checkIp(str) {
	var ss = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
	return ss.test(str);
}

function IsInteger(interger) {
	var IsInteger = true;
	if (parseInt(interger) == interger) {
		IsInteger = true;
	}
	else {
		IsInteger = false;
	}
	return IsInteger;
}

function get_resolution_support(){
	$.ajax({
	    type: "GET",
	    url: "../config/resolution.xml",
	    dataType: "xml",
	    cache: false,
	    error: function () {
	    },
	    success: function (data) {
	    		mode_note_length=$(data).find("mode").length;
	    		var option="";
	    		for(var i=0;i<mode_note_length;++i){
	    			var width=$(data).find("mode:eq("+i+")").find("width").text();
	    			var height=$(data).find("mode:eq("+i+")").find("height").text();
	    			if(width==0||height==0){
	    				option+="<option value='0x0'>"+$.i18n.prop('encode_config_width_height_same_as_input_txt')+"</option>";
	    			}else{
	    				option+="<option value='"+width+"x"+height+"'>"+width+"x"+height+"</option>";
	    			}
	    		}
	    		$("#venc_resolution").html(option);
	      }
	});
}

function get_encode_config() {
	$.ajax({
	    type: "GET",
	    url: "http://" + xc_gethost() + "/get_output",
	    dataType: "xml",
	    cache: false,
	    error: function () {
	    },
	    success: function (data) {
		        $(data).find("output");
		        
				var venc_codec = $(data).find("venc_codec").text();
		    	if(venc_codec==4){
		    		$("#venc_codec").find("option:eq(0)").attr("selected","selected");
		    	}else if(venc_codec==36){
		    		$("#venc_codec").find("option:eq(1)").attr("selected","selected");
		    	}else{
		    		alert("get_output error!venc_codec value is "+venc_codec);
		    	}
		    	
		    	$("#venc_codec").trigger("change");
			    
			    var venc_framerate = $(data).find("venc_framerate").text();
			    $('#venc_framerate').val(venc_framerate);
			    
			    var venc_gop = $(data).find("venc_gop").text();
			    $('#venc_gop').val(venc_gop);
			    
			    var venc_bitrate = $(data).find("venc_bitrate").text();
			    $('#venc_bitrate').val(venc_bitrate);
			    
			    venc_width=$(data).find("venc_width").text();
			    venc_height=$(data).find("venc_height").text();
			    $("#venc_resolution").find("option[value='"+venc_width+"x"+venc_height+"']").attr("selected","selected");
			    
			    var venc_level = $(data).find("venc_profile").text();
		    	if(venc_level==0){
		    		$("#venc_level").find("option[value='0']").attr("selected","selected");
		    	}else if(venc_level==1){
		    		$("#venc_level").find("option[value='1'").attr("selected","selected");
		    	}else if(venc_level==2){
		    		$("#venc_level").find("option[value='2']").attr("selected","selected");
		    	}else if(venc_level==3){
		    		$("#venc_level").find("option[value='3']").attr("selected","selected");
		    	}else{
		    		alert("get_output error!venc_level value is "+venc_codec);
		    	}
			    
			    var ts_uri = $(data).find("http_ts_uri").text();
			    $('#ts_uri').val(ts_uri);
			    
			    var ts_enable = $(data).find("http_ts_enable").text();
		    	if(ts_enable==1){
		    		$("#ts_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#ts_enable_off_txt").attr("selected","selected");
		    	}
			    
			    var hls_uri = $(data).find("http_hls_uri").text();
			    $('#hls_uri').val(hls_uri);
			    
			    var hls_enable = $(data).find("http_hls_enable").text();
		    	if(hls_enable==1){
		    		$("#hls_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#hls_enable_off_txt").attr("selected","selected");
		    	}
			    
			    var flv_uri = $(data).find("http_flv_uri").text();
			    $('#flv_uri').val(flv_uri);
			    
			    var flv_enable = $(data).find("http_flv_enable").text();
		    	if(flv_enable==1){
		    		$("#flv_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#flv_enable_off_txt").attr("selected","selected");
		    	}
			    
			    var rtsp_uri = $(data).find("rtsp_uri").text();
			    $('#rtsp_uri').val(rtsp_uri);
			    
			    var rtsp_enable = $(data).find("rtsp_enable").text();
		    	if(rtsp_enable==1){
		    		$("#rtsp_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#rtsp_enable_off_txt").attr("selected","selected");
		    	}
			    
			    var rtmp_uri = $(data).find("rtmp_uri").text();
			    $('#rtmp_uri').val(rtmp_uri);
			    
			    var rtmp_enable = $(data).find("rtmp_enable").text();
		    	if(rtmp_enable==1){
		    		$("#rtmp_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#rtmp_enable_off_txt").attr("selected","selected");
		    	}
			    
			    var rtmp_publish_uri = $(data).find("rtmp_publish_uri").text();
			    $('#rtmp_publish_uri').val(rtmp_publish_uri);
			    
			    var rtmp_publish_enable = $(data).find("rtmp_publish_enable").text();
		    	if(rtmp_publish_enable==1){
		    		$("#rtmp_publish_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#rtmp_publish_enable_off_txt").attr("selected","selected");
		    	}
			    
			    var multicast_ip = $(data).find("multicast_ip").text();
			    $('#multicast_ip').val(multicast_ip);
			    
			    var multicast_enable = $(data).find("multicast_enable").text();
		    	if(multicast_enable==1){
		    		$("#multicast_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#multicast_enable_off_txt").attr("selected","selected");
		    	}
			    
			    var multicast_port = $(data).find("multicast_port").text();
			    $('#multicast_port').val(multicast_port);
			    
				var multicast_rtp = $(data).find("multicast_rtp").text();
		    	if(multicast_rtp==1){
		    		$("#multicast_rtp_txt").attr("selected","selected");
		    	}else{
		    		$("#multicast_udp_txt").attr("selected","selected");
		    	}

			    var srt_port = $(data).find("srt_port").text();
			    $('#srt_port').val(srt_port);
			    
			    var srt_enable = $(data).find("srt_enable").text();
		    	if(srt_enable==1){
		    		$("#srt_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#srt_enable_off_txt").attr("selected","selected");
		    	}
			    
			    var srt_publish_uri = $(data).find("srt_publish_uri").text();
			    $('#srt_publish_uri').val(srt_publish_uri);
			    
			    var srt_publish_enable = $(data).find("srt_publish_enable").text();
		    	if(srt_publish_enable==1){
		    		$("#srt_publish_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#srt_publish_enable_off_txt").attr("selected","selected");
		    	}
			    
			    var srt_key = $(data).find("srt_key").text();
			    $('#srt_key').val(srt_key);
			    
			    var srt_key_enable = $(data).find("srt_key_enable").text();
		    	if(srt_key_enable==1){
		    		$("#srt_key_enable_on_txt").attr("selected","selected");
		    	}else{
		    		$("#srt_key_enable_off_txt").attr("selected","selected");
		    	}
		    	
	      }
	});
}

function network_setting() {
	
	if(!(allow_venc_framerate==1&&allow_venc_gop==1&&allow_venc_bitrate==1&&allow_ts_uri==1&&allow_hls_uri==1&&allow_flv_uri==1&&allow_rtsp_uri==1&&allow_rtmp_uri==1&&allow_rtmp_publish_uri==1&&allow_multicast_ip==1&&allow_multicast_port==1&&allow_srt_port==1&&allow_srt_publish_uri==1&&allow_srt_key==1)){
		return;
	}
	
    var ptmp = "";
    var venc_codec = $('#venc_codec').val();
    ptmp += "venc_codec=" + venc_codec + "&";
    
    var venc_framerate = $('#venc_framerate').val();
    ptmp += "venc_framerate=" + venc_framerate + "&";
    
    var venc_gop = $('#venc_gop').val();
    ptmp += "venc_gop=" + venc_gop + "&";
    
    var venc_bitrate = $('#venc_bitrate').val();
    ptmp += "venc_bitrate=" + venc_bitrate + "&";
    
    var resolution=$('#venc_resolution').val();
    
    var venc_height = resolution.substring(resolution.indexOf("x")+1,resolution.length);
    ptmp += "venc_height=" + venc_height + "&";
    
    var venc_width = resolution.substring(0,resolution.indexOf("x"));
    ptmp += "venc_width=" + venc_width + "&";
    
    var venc_level = $('#venc_level').val();
    ptmp += "venc_profile=" + venc_level + "&";
    
    var ts_uri = $('#ts_uri').val();
    ptmp += "http_ts_uri=" + ts_uri + "&";
    
    var ts_enable = $('#ts_enable').val();
    ptmp += "http_ts_enable=" + ts_enable + "&";
    
    var hls_uri = $('#hls_uri').val();
    ptmp += "http_hls_uri=" + hls_uri + "&";
    
    var hls_enable = $('#hls_enable').val();
    ptmp += "http_hls_enable=" + hls_enable + "&";
    
    var flv_uri = $('#flv_uri').val();
    ptmp += "http_flv_uri=" + flv_uri + "&";
    
    var flv_enable = $('#flv_enable').val();
    ptmp += "http_flv_enable=" + flv_enable + "&";
    
    var rtsp_uri = $('#rtsp_uri').val();
    ptmp += "rtsp_uri=" + rtsp_uri + "&";
    
    var rtsp_enable = $('#rtsp_enable').val();
    ptmp += "rtsp_enable=" + rtsp_enable + "&";
    
    var rtmp_uri = $('#rtmp_uri').val();
    ptmp += "rtmp_uri=" + rtmp_uri + "&";
    
    var rtmp_enable = $('#rtmp_enable').val();
    ptmp += "rtmp_enable=" + rtmp_enable + "&";
    
    var rtmp_publish_uri = $('#rtmp_publish_uri').val();
    rtmp_publish_uri=encodeURIComponent(rtmp_publish_uri);
    ptmp += "rtmp_publish_uri=" + rtmp_publish_uri + "&";
    
    var rtmp_publish_enable = $('#rtmp_publish_enable').val();
    ptmp += "rtmp_publish_enable=" + rtmp_publish_enable + "&";
    
    var multicast_ip = $('#multicast_ip').val();
    ptmp += "multicast_ip=" + multicast_ip + "&";
    
    var multicast_enable = $('#multicast_enable').val();
    ptmp += "multicast_enable=" + multicast_enable + "&";
    
    var multicast_port = $('#multicast_port').val();
    ptmp += "multicast_port=" + multicast_port + "&";

	var multicast_rtp = $('#multicast_rtp').val();
    ptmp += "multicast_rtp=" + multicast_rtp + "&";
    
    var srt_port = $('#srt_port').val();
    ptmp += "srt_port=" + srt_port + "&";
    
    var srt_enable = $('#srt_enable').val();
    ptmp += "srt_enable=" + srt_enable + "&";
    
    var srt_publish_uri = $('#srt_publish_uri').val();
    srt_publish_uri=encodeURIComponent(srt_publish_uri);
    ptmp += "srt_publish_uri=" + srt_publish_uri + "&";
    
    var srt_publish_enable = $('#srt_publish_enable').val();
    ptmp += "srt_publish_enable=" + srt_publish_enable + "&";
    
    var srt_key = $('#srt_key').val();
    ptmp += "srt_key=" + srt_key + "&";
    
    var srt_key_enable = $('#srt_key_enable').val();
    ptmp += "srt_key_enable=" + srt_key_enable + "&";

    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/set_output" + "?" + ptmp,
        cache: false,
        success: function (data) {
        	var ret = $(data).find("return").text();
            if ("ok" == ret) {
                alert($.i18n.prop('encode_config_success'));
            }
            else {
                alert($.i18n.prop('encode_config_failed'));
            }
        },
        error: function (){
        	alert($.i18n.prop('encode_config_failed'));
        }
    });
}

$(function () {
	init_language();
	get_resolution_support();
	$("#venc_codec").change(function(){
		if(this.value==36){
			$("#venc_level_div").attr("style","display:none;");
		}else{
			$("#venc_level_div").removeAttr("style");
		}
	});
	$("#venc_framerate").focusout(function(){
		var value=this.value;
		if(value<5||value>60){
			$("#venc_framerate_hint_image").removeClass("display_off");
			allow_venc_framerate=0;
		}else{
			$("#venc_framerate_hint_image").addClass("display_off");
			allow_venc_framerate=1;
		}
	});
	$("#venc_gop").focusout(function(){
		var value=this.value;
		if(value<5||value>300){
			$("#venc_gop_hint_image").removeClass("display_off");
			allow_venc_gop=0;
		}else{
			$("#venc_gop_hint_image").addClass("display_off");
			allow_venc_gop=1;
		}
	});
	$("#venc_bitrate").focusout(function(){
		var value=this.value;
		if(value<32||value>100000){
			$("#venc_bitrate_hint_image").removeClass("display_off");
			allow_venc_bitrate=0;
		}else{
			$("#venc_bitrate_hint_image").addClass("display_off");
			allow_venc_bitrate=1;
		}
	});
	$("#ts_uri").focusout(function(){
		var value=this.value;
		if(value==""){
			$("#ts_uri_hint_image").removeClass("display_off");
			$("#ts_uri_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_empty_txt'));
			allow_ts_uri=0;
		}else{
			$("#ts_uri_hint_image").addClass("display_off");
    		$("#ts_uri_hint_text").addClass("display_off").html("");
    		allow_ts_uri=1;
		}
	});
	$("#hls_uri").focusout(function(){
		var value=this.value;
		if(value==""){
			$("#hls_uri_hint_image").removeClass("display_off");
			$("#hls_uri_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_empty_txt'));
			allow_hls_uri=0;
		}else{
			$("#hls_uri_hint_image").addClass("display_off");
    		$("#hls_uri_hint_text").addClass("display_off").html("");
    		allow_hls_uri=1;
		}
	});
	$("#flv_uri").focusout(function(){
		var value=this.value;
		if(value==""){
			$("#flv_uri_hint_image").removeClass("display_off");
			$("#flv_uri_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_empty_txt'));
			allow_flv_uri=0;
		}else{
			$("#flv_uri_hint_image").addClass("display_off");
    		$("#flv_uri_hint_text").addClass("display_off").html("");
    		allow_flv_uri=1;
		}
	});
	$("#rtsp_uri").focusout(function(){
		var value=this.value;
		if(value==""){
			$("#rtsp_uri_hint_image").removeClass("display_off");
			$("#rtsp_uri_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_empty_txt'));
			allow_rtsp_uri=0;
		}else{
			$("#rtsp_uri_hint_image").addClass("display_off");
    		$("#rtsp_uri_hint_text").addClass("display_off").html("");
    		allow_rtsp_uri=1;
		}
	});
	$("#rtmp_uri").focusout(function(){
		var value=this.value;
		if(value==""){
			$("#rtmp_uri_hint_image").removeClass("display_off");
			$("#rtmp_uri_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_empty_txt'));
			allow_rtmp_uri=0;
		}else{
			$("#rtmp_uri_hint_image").addClass("display_off");
    		$("#rtmp_uri_hint_text").addClass("display_off").html("");
    		allow_rtmp_uri=1;
		}
	});
	$("#rtmp_publish_uri").focusout(function(){
		var value=this.value;
		if(value==""){
			$("#rtmp_publish_uri_hint_image").removeClass("display_off");
			$("#rtmp_publish_uri_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_empty_txt'));
			allow_rtmp_publish_uri=0;
		}else{
			$("#rtmp_publish_uri_hint_image").addClass("display_off");
    		$("#rtmp_publish_uri_hint_text").addClass("display_off").html("");
    		allow_rtmp_publish_uri=1;
		}
	});
	$("#multicast_ip").focusout(function(){
		var value=this.value;
		if(value==""){
			$("#multicast_ip_hint_image").removeClass("display_off");
			$("#multicast_ip_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_empty_txt'));
			allow_multicast_ip=0;
		}else{
			if(!checkIp(value)){
				$("#multicast_ip_hint_image").removeClass("display_off");
				$("#multicast_ip_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_format_txt'));
				allow_multicast_ip=0;
			}else{
				$("#multicast_ip_hint_image").addClass("display_off");
	    		$("#multicast_ip_hint_text").addClass("display_off").html("");
	    		allow_multicast_ip=1;
			}
		}
	});
	$("#multicast_port").focusout(function(){
		var value=this.value;
		if(IsInteger(value)){
			if(value<1||value>65535){
				$("#multicast_port_hint_image").removeClass("display_off");
				allow_multicast_port=0;
			}else{
				$("#multicast_port_hint_image").addClass("display_off");
				allow_multicast_port=1;
			}
		}else{
			$("#multicast_port_hint_image").removeClass("display_off");
			allow_multicast_port=0;
		}
	});
	$("#srt_port").focusout(function(){
		var value=this.value;
		if(IsInteger(value)){
			if(value<1||value>65535){
				$("#srt_port_hint_image").removeClass("display_off");
				$("#srt_port_hint_text").removeClass("display_off").html("[1-65535]");
				allow_srt_port=0;
			}else{
				$("#srt_port_hint_image").addClass("display_off");
	    		$("#srt_port_hint_text").addClass("display_off").html("");
	    		allow_srt_port=1;
			}
		}else{
			$("#srt_port_hint_image").removeClass("display_off");
			$("#srt_port_hint_text").removeClass("display_off").html("[1-65535]");
			allow_srt_port=0;
		}
	});
	$("#srt_publish_uri").focusout(function(){
		var value=this.value;
		if(value.indexOf("srt://")!=0){
			$("#srt_publish_uri_hint_image").removeClass("display_off");
			$("#srt_publish_uri_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_format_txt'));
			allow_srt_publish_uri=0;
		}else{
			$("#srt_publish_uri_hint_image").addClass("display_off");
    		$("#srt_publish_uri_hint_text").addClass("display_off").html("");
    		allow_srt_publish_uri=1;
		}
	});
	$("#srt_key").focusout(function(){
		var value=this.value;
		if(value.length < 10 || srt_key.length > 32){
			$("#srt_key_hint_image").removeClass("display_off");
			$("#srt_key_hint_text").removeClass("display_off").html($.i18n.prop('encode_config_hint_srt_pwd_error_txt'));
			allow_srt_key=0;
		}else{
			$("#srt_key_hint_image").addClass("display_off");
    		$("#srt_key_hint_text").addClass("display_off").html("");
    		allow_srt_key=1;
		}
	});
	setTimeout(function(){
    	get_encode_config();
	},200)
});
