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

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

    clipboard.on('error', function(e) {
    	alert($.i18n.prop('playlist_copy_text_error'));
        console.log(e);
    });
}
function copyurl(url_text)
{
	clipboard_data=url_text;
    $("#clipboard").trigger("click");
	
}
function get_playlist() {
    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/get_playlist",
        dataType: "xml",
        cache: false,
        error: function () {
        },
        success: function (data) {
            var wnd = $(data).find("playlist").attr("wnd");
            $('#wnd').val(wnd);
            showdiv(wnd);
            var uri = "";
			var uri_audio = 0;
			var uri_pindex = 0;
            for (var i = 0; i < max_wnd; i++) {
                uri = $(data).find("uri" + i).text();
                $('#uri' + i).attr("value", uri);
                
                uri_audio = $(data).find("uri" + i).attr('audio');
                if(uri_audio==0){
		    		$("#playlist_audio_enable"+i).find("option[value='0']").attr("selected","selected");
		    	}else if(uri_audio==1){
		    		$("#playlist_audio_enable"+i).find("option[value='1'").attr("selected","selected");
		    	}
		    	
				uri_pindex = $(data).find("uri" + i).attr("pindex");
				$('#playlist_pid_select' + i).attr("value", uri_pindex);
				uri_pindexaudio = $(data).find("uri" + i).attr("pindexaudio");
				$('#playlist_pid_audio_select' + i).attr("value", uri_pindexaudio);
				var cache = $(data).find("uri" +i).attr('cache');
                $('#cache'+i).attr("value", cache);
            }
        }
    });
}
function change_onvifdive()
{
	var device_service=$("#onviflist_sel option:checked").text();
	$('#onvifdivce').attr("value", device_service);
}
//liulin
function get_onviflist() {
	$('#rtsp_url_div').hide();
	$('#rtsp_explain_div').hide();
	$.ajax({
		type : "GET",
		url : "http://" + xc_gethost() + "/onvifcfind",
		dataType : "xml",
		cache : false,
		error : function() {
		},
		success : function(data) {		
		         var result ="<select id='onviflist_sel' name='onviflist_sel' onchange='change_onvifdive()' class='playlist_select'>";
			     var i = 0;
			     $(data).find("d").each(function(){  			 
                 var onviflist_sel = $(this).text();
				 result += '<option value="' + i + '">' + onviflist_sel + '</option>';
				 i+=1;
    });
		   if(i==0)
			{
				 var playlist_serchonvif=$.i18n.prop('playlist_serchonvif')
				 result += '<option value="1000">'+playlist_serchonvif+'</option>';
			}
			
			result+="</select>";
			$('#onviflist_div').append(result);
			$('#onvifdivce').attr("value",$("#onviflist_sel option:checked").text());
		}
	});
}
//liulin

/*function get_ndilist() {
	$.ajax({
		type : "GET",
		url : "http://" + xc_gethost() + "/ndifind",
		dataType : "xml",
		cache : false,
		error : function() {
		},
		success : function(data) {		
		    var result ="<span id='playlist_ndilist_txt'>";
			var i = 0;
			 $(data).find("d").each(function(){  			 
                 var ndilist = $(this).text();
                  url_text="'"+ndilist+"'";
				 result += '<span>' + ndilist + '<span id="playlist_copy_'+i+'" class="copycss" onclick="copyurl('+url_text+');"></span></span><br/';				
				$('#content_box_ndi').show();		
				i+=1;
           });
			if(i==0)
			{
			  $('#content_box_ndi').hide();
			}
			else{
			 result+="</span>";
			 $('#ndilist_div').append(result);
		  	 for(var j=0; j<i; j++)
			 {
			 	$('#playlist_copy_'+j).html($.i18n.prop('playlist_copy'));
		     }
			}
		}
	});
}*/

//liulin
function getrtspurl()
{
	var device_service=$('#onvifdivce').val();
	var device_usr=$('#username_text').val();
	var device_pwd=$('#password_text').val();
	$.ajax({
		type : "GET",
		url : "http://" + xc_gethost() + "/onvifcrtsp?device_service="+device_service+"&device_usr="+device_usr+"&device_pwd="+device_pwd,
		cache : false,
		success : function(data) {
			$('#rtsp_url_div').show();
			var rtsp_url_text= $(data).find("rtsp").text();
			if(rtsp_url_text!="")
			{				
	          $('#rtsp_explain_div').show();
	          url_text="'"+rtsp_url_text+"'";
			  $('#rtsp_url_text').html(rtsp_url_text+'<span id="playlist_copy" class="copycss" onclick="copyurl('+url_text+');"></span>');
			   $('#playlist_copy').html($.i18n.prop('playlist_copy'));	
			}
			else
			{
			  $('#rtsp_explain_div').hide();
		      $('#rtsp_url_text').html($.i18n.prop('playlist_get_nortsp'));
			}
		},
		error:function() {
			$('#rtsp_url_text').html($.i18n.prop('playlist_get_rtsp'));			
		}
	});
}

function showdiv(wndnum) {
    for (var i = 0; i < max_wnd; i++) {
        if (i < wndnum) {
            $('#div' + i).show();
        } else {
            $('#div' + i).hide();
        }
    }

    if(wndnum == 1) {
        $('#full_btn_0').hide();
        $('#full_btn_1000').hide();
    }
    else{
        $('#full_btn_0').show();
        $('#full_btn_1000').show();
    }
}

function changewnd() {
    var wnd = $('#wnd').val();
    showdiv(wnd);
}

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

function check_url_valid(url, cache){
	var temp_url=$.trim(url);
	if(temp_url.indexOf(" ")!=-1){
		return -1;
	}
    
    var Intcache = parseInt(cache);
    var IsIntcache = IsInteger(cache);
    if(Intcache < 0 || Intcache > 60000 || IsIntcache == false){
        return -1;
    }

	return 0;
}

function playlist_setting() {
    //var wnd = $('#wnd').val();
    var ptmp = "";
    //ptmp += ("wnd=" + wnd + "&");
    for (var i = 0; i < max_wnd; i++) {
        var url = $('#uri' + i).val();
		var cache = $('#cache'+i).val();
        var valid=check_url_valid(url, cache);
        if(valid==-1){
        	alert($.i18n.prop("playlist_format_error_start_info")+i+$.i18n.prop("playlist_format_error_end_info"));
        	return;
        }
		
        audio=$('#playlist_audio_enable' + i).val();
		var url_playlist_pid = $('#playlist_pid_select' + i).val();
		var url_playlist_audio_pid = $('#playlist_pid_audio_select' + i).val();
        ptmp += ("uri" + i + "=" + encodeURIComponent(url) + "&uri" + i + "_audio=" + encodeURIComponent(audio) + "&uri" + i + "_pindex=" + url_playlist_pid + "&uri" + i + "_pindexaudio=" + url_playlist_audio_pid + "&uri" + i + "_cache=" + cache + "&");
    }
    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/set_playlist" + "?" + ptmp,
        cache: false,
        success: function (data) {
        	alert($.i18n.prop('playlist_setting_success'));
        },
        error: function (){
        	alert($.i18n.prop('playlist_setting_failed'));
        }
    });
}

function click_full_btn(id) {
    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/set_full" + "?chn=" + id,
        cache: false,
        success: function (data) {
            //alert($.i18n.prop('playlist_setting_success'));
            for (var i = 0; i < max_wnd; i++) {
                $('#playlist_audio_enable' + i + "option").removeAttr("selected");
            }
            $('#playlist_audio_enable' + i + "option[value='1']").attr("selected","selected");
        },
        error: function (){
        	//alert($.i18n.prop('playlist_setting_failed'));
        }
    });
}

function parentpage_adjust_height(){
	changewnd();
	window.parent.setup_iframe_height();
}

function language_setup() {
	var current_language=window.parent.get_language_setup();
    jQuery.i18n.properties({
        name: 'common',
        path: '../lang/',
        mode: 'map',
        language: current_language,
        callback: function () {
            $('#playlist_wndnum').html($.i18n.prop('playlist_wndnum'));
            for (var i = 0; i < max_wnd; i++) {
            	if(!(current_language=="zh"||current_language=="zh-CN")){
                	$('#playlist_url' + i).html($.i18n.prop('playlist_url')+(i+1)+" "+$.i18n.prop('playlist_url_symbol'));
            	}else{
                	$('#playlist_url' + i).html($.i18n.prop('playlist_url')+(i+1)+$.i18n.prop('playlist_url_symbol'));
            	}

				$('#playlist_cache' + i).html($.i18n.prop('playlist_cache'));
                $('#playlist_audio' + i).html($.i18n.prop('playlist_audio'));
                
                $('#playlist_audio_enable' + i + " option[value='0']").html($.i18n.prop('playlist_audio_disable'));
                $('#playlist_audio_enable' + i + " option[value='1']").html($.i18n.prop('playlist_audio_enable'));
                
                $('#full_btn_' + i).val($.i18n.prop('playlist_full_btn'));

				$('#playlist_pid' + i).html($.i18n.prop('playlist_pid'));
				$('#playlist_pid_audio' + i).html($.i18n.prop('playlist_pid_audio'));
				for (var pid = 0; pid < 32; pid++) {
					$('#playlist_pid_select' + i).append('<option value="' + pid + '">' + $.i18n.prop('playlist_pid_txt') + ' ' + (pid + 1) + '</option>');
					$('#playlist_pid_audio_select' + i).append('<option value="' + pid + '">' + $.i18n.prop('playlist_pid_txt') + ' ' + (pid + 1) + '</option>');
				}
            }
            $('#playlist_setting').val($.i18n.prop('playlist_setting'));
            $('#full_btn_1000').val($.i18n.prop('playlist_nofull_btn'));
            $('#header_text').html($.i18n.prop('index_playlist'));
		    $('#header_onvif').html($.i18n.prop('index_playlist_onvif'));
			//$('#header_ndi').html($.i18n.prop('index_playlist_ndi'));
			
			$('#playlist_config_discription_rtsp').html($.i18n.prop('playlist_config_discription'));
			//$('#playlist_config_discription_ndi').html($.i18n.prop('playlist_config_discription'));		
			$('#playlist_onviflist').html($.i18n.prop('playlist_onviflist'));
			$('#playlist_username').html($.i18n.prop('playlist_username'));
			$('#playlist_password').html($.i18n.prop('playlist_password'));
			$('#playlist_get_btu_url').val($.i18n.prop('playlist_get_btu_url'));
			$('#playlist_rtsp_explain').html($.i18n.prop('playlist_rtsp_explain'));
			//$('#playlist_ndi_explain').html($.i18n.prop('playlist_ndi_explain'));
			//$('#playlist_ndilist').html($.i18n.prop('playlist_ndilist'));
			$('#playlist_onvifdivce').html($.i18n.prop('playlist_onvifdivce'));
            $('#playlist_rtsp_url').html($.i18n.prop('playlist_rtsp_url'));			   		
        }
    });
}

$(function () {
	language_setup();
    get_playlist();
	get_onviflist();
	//get_ndilist();
	scan_window_and_copy_init();
});
