document.write("<script type='text/javascript' src='../js/ui.js'></script>");

var g_wnd_num=0;
var wifi_exist;
var wifi_enable;

var ts_url_enable=0;
var hls_url_enable=0;
var flv_url_enable=0;
var rtsp_url_enable=0;
var rtmp_url_enable=0;
var rtmp_publish_url_enable=0;
var multicast_url_enable=0;
var srt_url_enable=0;
var srt_publish_url_enable=0;

var clipboard_data="";

function scan_window_and_copy_init(){
    var clipboard = new ClipboardJS('#clipboard', {
        text: function() {
            return clipboard_data;
        }
    });

    clipboard.on('success', function(e) {
    	alert($.i18n.prop('status_copy_text_ok')+"\n"+clipboard_data);
        console.log(e);
    });

    clipboard.on('error', function(e) {
    	alert($.i18n.prop('status_copy_text_error'));
        console.log(e);
    });
	$(".uri").dblclick(function(){
		clipboard_data=$(this).html();
		$("#clipboard").trigger("click");
	});
	
	$(".uri").mouseover(function(e){
		var x = e.originalEvent.x || e.originalEvent.layerX || 0;
		var y = e.originalEvent.y || e.originalEvent.layerY || 0;
		$("#uri_scan").text($.i18n.prop('status_copy_hint_text')).attr("style","left:"+x+"px;top:"+y+"px;");
	}).mouseout(function(e){
		$("#uri_scan").attr("style","display:none;");
	});
}
function handle_wndnum(wnd_num,xml){
    if(g_wnd_num==0){
        for(var i=0;i<wnd_num;++i){
        	var temp_string="";
			temp_string+="<hr />";
			temp_string+="<div class='subtitle'>";
			temp_string+="<span id='status_wnd_text_"+i+"' class='status_subtitle'></span>";
			temp_string+="</div>";
			temp_string+="<div>";
			temp_string+="<span id='status_decode_uri_text_"+i+"' class='small_title'></span>\n\r";
			temp_string+="<span id='status_decode_uri_"+i+"' class='dynamic_data uri'></span>";
			temp_string+="</div>";
			temp_string+="<div>";
			temp_string+="<span id='status_wnd_status_text_"+i+"' class='small_title'></span>\n\r";
			temp_string+="<span id='status_wnd_status_"+i+"' class='dynamic_data'></span>";
			temp_string+="</div>";
			temp_string+="<div>";
			temp_string+="<span id='status_wnd_fps_text_"+i+"' class='small_title'></span>\n\r";
			temp_string+="<span id='status_wnd_fps_"+i+"' class='dynamic_data'></span>";
			temp_string+="</div>";
			temp_string+="<div>";
			temp_string+="<span id='status_wnd_bps_text_"+i+"' class='small_title'></span>\n\r";
			temp_string+="<span id='status_wnd_bps_"+i+"' class='dynamic_data'></span>";
			temp_string+="</div>";
			
			
			temp_string+="<hr />";
			temp_string+="<div class='subtitle'>";
			temp_string+="<span id='status_venc_text_"+i+"' class='status_subtitle'></span>";
			temp_string+="</div>";
			
			temp_string+="<div>";
			temp_string+="<span id='status_venc_codec_text_"+i+"' class='small_title'></span>\n\r";
			temp_string+="<span id='status_venc_codec_"+i+"' class='dynamic_data'></span>";
			temp_string+="</div>";
			
			temp_string+="<div>";
			temp_string+="<span id='status_venc_resolution_text_"+i+"' class='small_title'></span>\n\r";
			temp_string+="<span id='status_venc_resolution_"+i+"' class='dynamic_data'></span>";
			temp_string+="</div>";
			
			temp_string+="<div>";
			temp_string+="<span id='status_venc_bitrate_text_"+i+"' class='small_title'></span>\n\r";
			temp_string+="<span id='status_venc_bitrate_"+i+"' class='dynamic_data'></span>";
			temp_string+="</div>";
			
            ts_url_enable=$(xml).find("s"+i).find("venc").find("ts_url_enable").text();
            hls_url_enable=$(xml).find("s"+i).find("venc").find("hls_url_enable").text();
            flv_url_enable=$(xml).find("s"+i).find("venc").find("flv_url_enable").text();
            rtsp_url_enable=$(xml).find("s"+i).find("venc").find("rtsp_url_enable").text();
			rtmp_url_enable=$(xml).find("s"+i).find("venc").find("rtmp_url_enable").text();
			rtmp_publish_url_enable=$(xml).find("s"+i).find("venc").find("rtmp_publish_url_enable").text();
			multicast_url_enable=$(xml).find("s"+i).find("venc").find("multicast_url_enable").text();
			srt_url_enable=$(xml).find("s"+i).find("venc").find("srt_url_enable").text();
			srt_publish_url_enable=$(xml).find("s"+i).find("venc").find("srt_publish_url_enable").text();
			
			if(ts_url_enable==1){
				temp_string+="<div>";
				temp_string+="<span id='status_ts_url_text_"+i+"' class='small_title'></span>\n\r";
				temp_string+="<span id='status_ts_url_"+i+"' class='dynamic_data uri'></span>";
				temp_string+="</div>";
			}
			
			if(hls_url_enable==1){
				temp_string+="<div>";
				temp_string+="<span id='status_hls_url_text_"+i+"' class='small_title'></span>\n\r";
				temp_string+="<span id='status_hls_url_"+i+"' class='dynamic_data uri'></span>";
				temp_string+="</div>";
			}
			
			if(flv_url_enable==1){
				temp_string+="<div>";
				temp_string+="<span id='status_flv_url_text_"+i+"' class='small_title'></span>\n\r";
				temp_string+="<span id='status_flv_url_"+i+"' class='dynamic_data uri'></span>";
				temp_string+="</div>";
			}
			
			if(rtsp_url_enable==1){
				temp_string+="<div>";
				temp_string+="<span id='status_rtsp_url_text_"+i+"' class='small_title'></span>\n\r";
				temp_string+="<span id='status_rtsp_url_"+i+"' class='dynamic_data uri'></span>";
				temp_string+="</div>";
			}
			
			if(rtmp_url_enable==1){
				temp_string+="<div>";
				temp_string+="<span id='status_rtmp_url_text_"+i+"' class='small_title'></span>\n\r";
				temp_string+="<span id='status_rtmp_url_"+i+"' class='dynamic_data uri'></span>";
				temp_string+="</div>";
			}
			
			if(rtmp_publish_url_enable==1){
				temp_string+="<div>";
				temp_string+="<span id='status_rtmp_publish_url_text_"+i+"' class='small_title'></span>\n\r";
				temp_string+="<span id='status_rtmp_publish_url_"+i+"' class='dynamic_data uri'></span>";
				temp_string+="</div>";
			}
			
			if(multicast_url_enable==1){
				temp_string+="<div>";
				temp_string+="<span id='status_multicast_url_text_"+i+"' class='small_title'></span>\n\r";
				temp_string+="<span id='status_multicast_url_"+i+"' class='dynamic_data uri'></span>";
				temp_string+="</div>";
			}
			
			if(srt_url_enable==1){
				temp_string+="<div>";
				temp_string+="<span id='status_srt_url_text_"+i+"' class='small_title'></span>\n\r";
				temp_string+="<span id='status_srt_url_"+i+"' class='dynamic_data uri'></span>";
				temp_string+="</div>";
			}
			
			if(srt_publish_url_enable==1){
				temp_string+="<div>";
				temp_string+="<span id='status_srt_publish_url_text_"+i+"' class='small_title'></span>\n\r";
				temp_string+="<span id='status_srt_publish_url_"+i+"' class='dynamic_data uri'></span>";
				temp_string+="</div>";
			}
			
			$("#status").append(temp_string);
        }
        g_wnd_num=wnd_num;
		language_setup(g_wnd_num);
		parentpage_adjust_height();
		scan_window_and_copy_init();
    }else{
    	if(g_wnd_num!=wnd_num){
    		window.parent.location.reload(true);
    	}
    }
}

function init_wifi_option(){
	wifi_exist=window.parent.get_wifi_exist();
	if(wifi_exist==1){
		$("#wifi_status").removeClass("display_off");
	}else if(wifi_exist==0){
		$("#wifi_status").addClass("display_off");
	}

	wifi_enable = window.parent.get_wifi_enable();
	if(wifi_enable==1){
		$("#wifi_status_info").removeClass("display_off");
	}
	else if(wifi_enable==0){
		$("#wifi_status_info").addClass("display_off");
	}
}

function getNowFormatDate() {
	var now = new Date();
	var currenseconds = Math.round(now.getTime() / 1000);
	return currenseconds;
}

function systemtime_apply() {
	var nowtime = getNowFormatDate();
	$.ajax({
		type: "GET",
		url: "http://" + xc_gethost() + "/set_time?sync_time=" + nowtime,
		dataType: "text",
		cache: false,
		success: function(data) {
			if (data == "succeed") {
				window.location.href = "";
			} else {
			}
		}
	});
}
function get_status() {
    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/get_status",
        dataType: "xml",
        cache: false,
        error: function () {
        },
        success: function (sxml) {
            // cpu usage
            var cpu_usage = $(sxml).find("cpu_usage").text();
			$('#cpu_usage').html(cpu_usage + "%");
			// net status
			var net_status = $(sxml).find("net_status").text();
			if( "1" == net_status ){
				$("#status_net").html($.i18n.prop('status_net_INTERNET'));
			}
			else if( "2" == net_status){
				$("#status_net").html($.i18n.prop('status_net_INTERNET_IP'));
			}
			else if( "3" == net_status ){
				$("#status_net").html($.i18n.prop('status_net_INTERNET_LAN'));
			}
			else if( "4" == net_status ){
				$("#status_net").html($.i18n.prop('status_net_INTERNET_LOCAL'));
			}
			else{
				$("#status_net").html($.i18n.prop('status_net_INTERNET_LOCAL'));
			}
            // mem usage
            var mem_free = $(sxml).find("mem_free").text();
            var mem_total = $(sxml).find("mem_total").text();
            mem_free = mem_total - mem_free;
            mem_free = parseInt(mem_free / 1000);
            mem_total = parseInt(mem_total / 1000);
            $('#mem_free').html(mem_free + "MB/" + mem_total + "MB");
            var runtime = $(sxml).find("runtime").text();
            $('#status_runtime').html(runtime);
			var systime = $(sxml).find("systime").text();
            $('#system_runtime').html(systime);
            var vo = $(sxml).find("vo").text();
			for (var i = 0; i < system_vo_array.length; i++) {
				if(vo == system_vo_array[i][0]){
					$('#status_vo').html(system_vo_array[i][1]);
					break;
				}
			}

            var wnd_num=$(sxml).find("wndnum").text();
            $('#status_wnd_num').html(wnd_num);
            handle_wndnum(wnd_num,sxml);
            
            for(var i=0;i<wnd_num;++i){
            	var status_decode_uri=$(sxml).find("s"+i).find("uri").text();
            	if(status_decode_uri.length>128){
            		status_decode_uri=status_decode_uri.substring(0,128)+"......";
            	}
                $("#status_decode_uri_"+i).html(status_decode_uri);
            	var current_wnd_status=$(sxml).find("s"+i).find("alive").text();
            	if(current_wnd_status=="1"){
            		$("#status_wnd_status_"+i).html($.i18n.prop('status_wnd_status_text_ok'));
            	}else{
            		$("#status_wnd_status_"+i).html($.i18n.prop('status_wnd_status_text_error'));
            	}
                $("#status_wnd_fps_"+i).html($(sxml).find("s"+i).find("fps").text());
                $("#status_wnd_bps_"+i).html($(sxml).find("s"+i).find("bps").text());
                
                var venc_codec=$(sxml).find("s"+i).find("venc").find("codec").text();
                if(venc_codec==4){
                	$("#status_venc_codec_"+i).html("H.264");
                }else if(venc_codec==36){
                	$("#status_venc_codec_"+i).html("H.265");
                }else{
		    		alert("get_status error!venc_codec value is "+venc_codec);
                	$("#status_venc_codec_"+i).html("---");
                }
                
                var width=$(sxml).find("s"+i).find("venc").find("width").text();
                var height=$(sxml).find("s"+i).find("venc").find("height").text();
                $("#status_venc_resolution_"+i).html(width+"x"+height);
                
                $("#status_venc_bitrate_"+i).html($(sxml).find("s"+i).find("venc").find("bitrate").text());
                
                if(ts_url_enable==1){
                	$("#status_ts_url_"+i).html($(sxml).find("s"+i).find("venc").find("ts_url").text());
                }
                if(hls_url_enable==1){
                	$("#status_hls_url_"+i).html($(sxml).find("s"+i).find("venc").find("hls_url").text());
                }
                if(flv_url_enable==1){
                	$("#status_flv_url_"+i).html($(sxml).find("s"+i).find("venc").find("flv_url").text());
                }
                if(rtsp_url_enable==1){
                	$("#status_rtsp_url_"+i).html($(sxml).find("s"+i).find("venc").find("rtsp_url").text());
                }
                if(rtmp_url_enable==1){
                	$("#status_rtmp_url_"+i).html($(sxml).find("s"+i).find("venc").find("rtmp_url").text());
                }
                if(rtmp_publish_url_enable==1){
                	var rtmp_status=$(sxml).find("s"+i).find("venc").find("rtmp_status").text();
	            	var rtmp_publish_url=$(sxml).find("s"+i).find("venc").find("rtmp_publish_url").text();
	            	if(rtmp_publish_url.length>128){
	            		rtmp_publish_url=rtmp_publish_url.substring(0,128)+"......";
	            	}
                	if(rtmp_status>=0){
	            		$("#status_rtmp_publish_url_text_"+i).html($.i18n.prop('status_rtmp_publish_url_text')+$.i18n.prop('status_venc_ok_text'));
                		$("#status_rtmp_publish_url_"+i).html(rtmp_publish_url);
                	}else{
	            		$("#status_rtmp_publish_url_text_"+i).html($.i18n.prop('status_rtmp_publish_url_text')+$.i18n.prop('status_venc_error_text'));
                		$("#status_rtmp_publish_url_"+i).html(rtmp_publish_url);
                	}
                }
                if(multicast_url_enable==1){
                	$("#status_multicast_url_"+i).html($(sxml).find("s"+i).find("venc").find("multicast_url").text());
                }
                if(srt_url_enable==1){
                	$("#status_srt_url_"+i).html($(sxml).find("s"+i).find("venc").find("srt_url").text());
                }
                if(srt_publish_url_enable==1){
                	var srt_status=$(sxml).find("s"+i).find("venc").find("srt_publish_status").text();
	            	var srt_publish_url=$(sxml).find("s"+i).find("venc").find("srt_publish_url").text();
	            	if(srt_publish_url.length>128){
	            		srt_publish_url=srt_publish_url.substring(0,128)+"......";
	            	}
                	if(srt_status>=0){
	            		$("#status_srt_publish_url_text_"+i).html($.i18n.prop('status_srt_publish_url_text')+$.i18n.prop('status_venc_ok_text'));
                		$("#status_srt_publish_url_"+i).html(srt_publish_url);
                	}else{
	            		$("#status_srt_publish_url_text_"+i).html($.i18n.prop('status_srt_publish_url_text')+$.i18n.prop('status_venc_error_text'));
                		$("#status_srt_publish_url_"+i).html(srt_publish_url);
                	}
                }
            }
        }
    });
    if(wifi_exist==1){
	    $.ajax({
	        type: "GET",
	        url: "http://" + xc_gethost() + "/get_wifistatus",
	        dataType: "xml",
	        cache: false,
	        error: function () {
	        },
	        success: function (sxml) {
				if(wifi_enable == 1){
					$('#status_wifi_dev_enable').html($.i18n.prop('status_wifi_dev_enable_on_text'));
				}
				else if(wifi_enable == 0){
					$('#status_wifi_dev_enable').html($.i18n.prop('status_wifi_dev_enable_off_text'));
				}

	            var wifi_status = $(sxml).find("state").text();
	            var wifi_ssid = $(sxml).find("ssid").text();
	            var wifi_ip = $(sxml).find("ip").text();
	            var wifi_freq = $(sxml).find("freq").text();
	            var wifi_level = $(sxml).find("level").text();
                var wifi_mac = $(sxml).find("mac").text();
	            if(wifi_status=="COMPLETED"){
	            	$('#status_link_status').html($.i18n.prop('status_connected_text'));
	            }else if(wifi_status=="DISCONNECTED"){
	            	$('#status_link_status').html($.i18n.prop('status_disconneted_text'));
	            }else {
	            	$('#status_link_status').html($.i18n.prop('status_connecting_text'));
				}
	            $('#status_wifi_essid').html(wifi_ssid);
	            $('#status_wifi_ip').html(wifi_ip);
	            $('#status_freq').html(wifi_freq+"MHz");
	            $('#status_singal_level').html(wifi_level+"dB");
                $('#status_wifi_mac').html(wifi_mac);
	        }
	    });
    }
    
    
}

function language_setup(wnd_num) {
    jQuery.i18n.properties({
        name: 'common',
        path: '../lang/',
        mode: 'map',
        language: window.parent.get_language_setup(),
        callback: function () {
            $('#status_system_status_text').html($.i18n.prop('status_system_status_text'));
            $('#status_cpu_usage_txt').html($.i18n.prop('status_cpu_usage_txt'));
            $('#status_mem_free_txt').html($.i18n.prop('status_mem_free_txt'));
            $('#header_text').html($.i18n.prop('index_status'));
			$('#system_runtime_text').html($.i18n.prop('system_runtime_text'));
			$('#system_runtime_setting_text').html($.i18n.prop('system_runtime_setting_text'));
            $('#status_runtime_text').html($.i18n.prop('status_runtime_text'));
            $('#status_vo_text').html($.i18n.prop('status_vo_text'));
			$('#status_wnd_num_text').html($.i18n.prop('status_wnd_num_text'));
			$('#status_net_text').html($.i18n.prop('status_net_text'));
            $('#status_wifi_status_text').html($.i18n.prop('status_wifi_status_text'));
			$('#status_wifi_dev_enable_text').html($.i18n.prop('status_wifi_dev_enable_text'));
            $('#status_link_status_text').html($.i18n.prop('status_link_status_text'));
            $('#status_wifi_essid_text').html($.i18n.prop('status_wifi_essid_text'));
            $('#status_wifi_ip_text').html($.i18n.prop('status_wifi_ip_text'));
            $('#status_wifi_mac_text').html($.i18n.prop('status_wifi_mac_text'));
            $('#status_freq_text').html($.i18n.prop('status_freq_text'));
            $('#status_singal_level_text').html($.i18n.prop('status_singal_level_text'));
            for(var i=0;i<=wnd_num;++i){
                $("#status_wnd_text_"+i).html($.i18n.prop('status_wnd_text')+(i+1));
                $("#status_decode_uri_text_"+i).html($.i18n.prop('status_decode_uri_text'));
                $("#status_wnd_status_text_"+i).html($.i18n.prop('status_wnd_status_text'));
                $("#status_wnd_fps_text_"+i).html($.i18n.prop('status_wnd_fps_text'));
                $("#status_wnd_bps_text_"+i).html($.i18n.prop('status_wnd_bps_text'));
                $("#status_venc_text_"+i).html($.i18n.prop('status_venc_text')+(i+1));
                $("#status_venc_codec_text_"+i).html($.i18n.prop('status_venc_codec_text'));
                $("#status_venc_resolution_text_"+i).html($.i18n.prop('status_venc_resolution_text'));
                $("#status_venc_bitrate_text_"+i).html($.i18n.prop('status_venc_bitrate_text'));
                $("#status_ts_url_text_"+i).html($.i18n.prop('status_ts_url_text'));
                $("#status_hls_url_text_"+i).html($.i18n.prop('status_hls_url_text'));
                $("#status_flv_url_text_"+i).html($.i18n.prop('status_flv_url_text'));
                $("#status_rtsp_url_text_"+i).html($.i18n.prop('status_rtsp_url_text'));
                $("#status_rtmp_url_text_"+i).html($.i18n.prop('status_rtmp_url_text'));
                $("#status_rtmp_publish_url_text_"+i).html($.i18n.prop('status_rtmp_publish_url_text')+$.i18n.prop('status_venc_error_text'));
                $("#status_multicast_url_text_"+i).html($.i18n.prop('status_multicast_url_text'));
                $("#status_srt_url_text_"+i).html($.i18n.prop('status_srt_url_text'));
                $("#status_srt_publish_url_text_"+i).html($.i18n.prop('status_srt_publish_url_text')+$.i18n.prop('status_venc_error_text'));
            }
        }
    });
}
  
function parentpage_adjust_height(){
	window.parent.setup_iframe_height();
}

$(function () {
	init_wifi_option();
	language_setup(g_wnd_num);
	get_status();
    setInterval(get_status, 1000);
});
    