var webRtcPanel;
function WebRtcPanel(){}

WebRtcPanel.queueKey= String("videoPlayQueue");
WebRtcPanel.queueArray=[];

WebRtcPanel.videoPlayDeQueue = function(){
	console.log(2929);
	top.$(window).dequeue(WebRtcPanel.queueKey);
}

WebRtcPanel.createPanel = function(opt){
	if(webRtcPanel) return webRtcPanel;
	
	var defaultOpt = {
		container:top.$("body"),
	    theme:       'primary',
	    headerTitle: 'ÊÓÆµ',
	    url:top.sysUrl +'/frame/demo/testrtc/globalrecordrtc/globalrecordrtc.jsp',
        position: "center-top 0 80",
	    contentSize: top.$(window).data('contentSize') || '560 420',
	    data:{},
	    headerControls: {
	        smallify: "remove"
	    },
	    onclosed: function(){
	    	var con = parseInt($(webRtcPanel)[0].currentData.width)+" "+(parseInt($(webRtcPanel)[0].currentData.height)-26);
	    	top.$(window).data('contentSize',con)
	    	webRtcPanel = null;
	    }
	};
	opt = $.extend({}, defaultOpt, opt || {});
	
	opt.content = '<iframe width="100%" height="100%" frameborder="0" src='+ opt.url +'></iframe>';
	webRtcPanel = jsPanel.create(opt);
	
	webRtcPanel.getFrame = function(){
    	var frame	=	$(webRtcPanel).find("iframe")[0];
		var win		=	frame.contentWindow||frame.window;
		return win;
    };
    
    webRtcPanel.playVideo = function(video,index){
    	index = !!index ? index : 0;
    	var frame = webRtcPanel.getFrame(),block;
		block = frame.BlockUtil.getBlockByIndex(index);
//		console.log("blockblockblock:"+block.uuid);
		block.setFdId(video.sbbh).setChannelId(video.tdbh).setHideTitle(video.tdmc).startMonitor();
    };
    
    webRtcPanel.playFun = function(obj,index){
    	return function(){
    		webRtcPanel.playVideo(obj,index);
    	}
    }
    webRtcPanel.playVideoArr = function(videoArr,index){
    	WebRtcPanel.queueArray.length = 0 ;
    	top.$(window).clearQueue(WebRtcPanel.queueKey);
    	var bm = 0 ;
    	for(var i = 0;i < videoArr.length;i++){
    		var obj = videoArr[i];
    		WebRtcPanel.queueArray.push(webRtcPanel.playFun( obj,i));
    		bm++;
    	}
    	
    	if(WebRtcPanel.queueArray.length){
    		top.$(window).queue(WebRtcPanel.queueKey,WebRtcPanel.queueArray);
    		WebRtcPanel.videoPlayDeQueue();
    	}
    };
    
    webRtcPanel.playRecord = function(record,index){
    	var frame = webRtcPanel.getFrame(),block;
    	if($.type(record) === "object"){
    		if(!!(index * 1)){
    			block = frame.BlockUtil.getBlockByIndex(index);
    		}else{
    			block = frame.BlockUtil.getNextIdleBlock();
    		}
    		block.setFdId(record.sbbh).setChannelId(record.tdbh).setHideTitle(record.tdmc).startPlayback(
    				record.begintime ,0,record.currtime,record.playbackurl,record.sbbh,record.tdbh,0);
    	}else if($.type(record) === "array"){
    		if(frame.BlockCacher.blockCache.length < record.length){
    			if(record.length <= 4) frame.BlockUtil.changeLayout(4);
    			else if(record.length <= 9) frame.BlockUtil.changeLayout(9);
    			else frame.BlockUtil.changeLayout(16);
    		}
    		for(var i = 0;i < record.length;i++){
    			var row = record[i];
    			if(!!(index * 1)){
    				block = frame.BlockUtil.getBlockByIndex(index+i);
    			}else{
    				block = frame.BlockUtil.getNextIdleBlock();
    			}
        		block.setFdId(row.sbbh).setChannelId(row.tdbh).setHideTitle(row.tdmc).startPlayback(
        				row.begintime ,0,row.currtime,row.playbackurl,row.sbbh,row.tdbh,0);
    		}
    	}
    };
    
    webRtcPanel.closeAllVideoBlock = function(){
    	var frame = webRtcPanel.getFrame();
    	for(var i = 0;i < frame.BlockCacher.blockCache.length;i++){
    		if(frame.BlockCacher.blockCache[i].fdId && frame.BlockCacher.blockCache[i].channelId != null){
    			frame.BlockUtil.stopMonitor(i);
    		}
    	}
    };
    
    webRtcPanel.closeAllRecordBlock = function(){
    	var frame = webRtcPanel.getFrame();
    	for(var i = 0;i < frame.BlockCacher.blockCache.length;i++){
    		if(frame.BlockCacher.blockCache[i].fdId && frame.BlockCacher.blockCache[i].channelId != null){
    			frame.BlockUtil.stopPlayback(i);
    		}
    	}
    };
    
    webRtcPanel.closePanel = function(){
    	webRtcPanel.close();
    };
    
	return webRtcPanel;
}

WebRtcPanel.createRecordPanel = function(opt){
	opt = opt || {};
	opt.url = top.sysUrl +'/frame/webRtc/globalrecordrtc/globalrecordrtc.jsp';
	return WebRtcPanel.createPanel(opt);
}

WebRtcPanel.createVideoPanel = function(opt){
	opt = opt || {};
	opt.url = top.sysUrl +'/frame/webRtc/globalvideortc/globalvideortc.jsp';
	return WebRtcPanel.createPanel(opt);
}


