(function($) {
        $.fn.purge = function() {
                this.each(function() {
                        $(this).unbind();
                        $(this).find("*").each(function() {
                                $(this).purge();
                        });
                });
                return this;
        }

})(jQuery); 


function get_latest_data(suffix_words){
	//var $ = jQuery.noConflict();
	
	if(sendTime==null || Date.parse(new Date())<=sendTime) return;
	
	var t_sendTime = sendTime;
	//div_c.purge();
	$.ajax({
		type: 'GET',
		data: 'tid='+tid+'&diffBBSTime='+diffBBSTime,
		dataType: 'text',
		url: "../../includes/get_chat_tv_data.php",
		timeout: 5000,
		cache: false,
		success: function(s){
			if(sendTime==null || t_sendTime!=sendTime) return;
			//div_c.purge();
			//$.fn.purge();
			if(suffix_words!=null) s += suffix_words;
			div_c.html(s);
			
			//显示系统通知, 在比赛前5分钟, 比赛后50分钟和105分钟
			if(ifAnnounced.one==false){
				var diffTime = Date.parse(new Date())/1000-match_time;
				if(diffTime>-300 && diffTime<0){
					div_c.append(sys_words);
					ifAnnounced.one = true;
				}
			}
			if(ifAnnounced.two==false){
				var diffTime = Date.parse(new Date())/1000-match_time;
				if(diffTime>49*60 && diffTime<51*60){
					div_c.append(sys_words);
					ifAnnounced.one = true;
				}
			}
			if(ifAnnounced.three==false){
				var diffTime = Date.parse(new Date())/1000-match_time;
				if(diffTime>104*60 && diffTime<106*60){
					div_c.append(sys_words);
					ifAnnounced.one = true;
				}
			}
			
			div_c2.scrollTop=div_c2.scrollHeight;
			//alert(xhr);
			//xhr = null;
			//requestDone = null;
			//remote = null;
			//s = null;
			//alert('purge ok');
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			//if(textStatus == 'timeout') alert('5秒超时已到,未取到数据');
			//else alert(textStatus+":"+errorThrown);
			errorThrown = null;
		},
		complete: function(XMLHttpRequest, textStatus){
			XMLHttpRequest = null;
			textStatus = null;
			/*GarbageCollect();
			alert('a');
			alert(this.xhr);
			this.xhr = null;
			this.requestDone = null;
			this.remote = null;
			this.s = null;*/
		},
		ajaxComplete: function(XMLHttpRequest, textStatus){
			XMLHttpRequest = null;
			textStatus = null;
			/*GarbageCollect();
			alert('a');
			this.xhr = null;
			this.requestDone = null;
			this.remote = null;
			this.s = null;*/
		}
	});

}

function sendChat(){
	if(!$('#chat_content').val()){ alert('内容不能为空'); return; }
	var timeDiff = Date.parse(new Date()) - lastSendTime;
	if(timeDiff < 2000){
		$('#div_msg2').text('您发贴时间太快了,请歇会再发:)');
		var timeout1 = setTimeout("$('#div_msg2').text('');",1000);
		return;
	}
	
	sendTime = null;
	lastSendTime = Date.parse(new Date());
	$('#chat_content').val($('#chat_content').val().replaceAll('\n',' '));
	
	var data = 'tid='+tid+'&id='+strId.substring(strId.lastIndexOf('/')+1,strId.lastIndexOf('.'))+'&author='+user+'&authorid='+authorid+'&content='+$('#chat_content').val();
	$.ajax({
		type: 'POST',
		data: data,
		dataType: 'text',
		url: "../../includes/set_chat_tv_data.php",
		success: function(s){
			if(s!='ok'){alert(s);return;}
			div_c.append('<span class="blue">['+user+']</span> '+$('#chat_content').val()+'<br />');
			$('#chat_content').val('');
			div_c2.scrollTop=div_c2.scrollHeight;
			$('#chat_content').focus();
			sendTime = Date.parse(new Date());
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			//alert('b'+textStatus+errorThrown);
		}
	});
}

function if_enter(e){
	if(e.ctrlKey && ((e.keyCode || e.which) == 13)){
		sendChat();
	}
}

function get_video_info(){
	var options = {
		type: 'GET',
		data: 'tid='+tid,
		dataType: 'text',
		url: "/tv/video_info.php",
		success: function(s){
			$('#div_video_info').text(s);
		}
	};
	$.ajax(options);
}


function switch_channel(channel_id,softs_id){
	var data = 'channel_id='+channel_id+'&softs_id='+softs_id+'&live_title='+title;
	var options = {
		type: 'GET',
		data: data+'&type=videoInfo',
		dataType: 'text',
		url: "/tv/get_video.php",
		success: function(s){
			$('#span_videoInfo').html(s);
			$('a[name="a_channel"]').each(function(){
				$(this).attr('href','javaScript:switch_channel('+this.id.substring(10).replace('_',',')+')');
				$(this).removeClass('hui1');
			});
			$('#a_channel_'+channel_id+'_'+softs_id).removeAttr('href');
			$('#a_channel_'+channel_id+'_'+softs_id).addClass('hui1');
		}
	};
	$.ajax(options);
	options = {
		type: 'GET',
		data: data+'&type=video',
		dataType: 'text',
		url: "/tv/get_video.php",
		success: function(s){
			$('#span_video').html(s);
		}
	};
	$.ajax(options);
}

function change_userName(){
	user = $(':input[name="user"]').val();
	$('#div_user').text('欢迎 '+user);
}

function chat_content_click(){
	if(suffix_words == $('#chat_content').val())
		$('#chat_content').val('');
}

function copyToClipBoard(title){
	var clipBoardContent="";
	clipBoardContent+=title+': ';
	clipBoardContent+="\n";
	clipBoardContent+=window.location.toString();
	window.clipboardData.setData("Text",clipBoardContent);
	alert(title+"直播复制成功，请粘贴到QQ/MSN/论坛上让好友来一起加速吧！");
}

//进入影院模式
function openCinemaModel()
{
	var bH = $("body").height();
	var bW = $("body").width();
	if(bH < document.body.clientHeight) {
		bH = document.body.clientHeight;
	}
	if(bW < document.body.clientWidth) {
		bW = document.body.clientWidth;
	}
	 
	//var ve = $("embed").attr("wmode");  
	//$("EMBED").attr("wmode","window"); 
	$("#cinemaModelLay").css({width:bW,height:bH,display:"block"});
}

//退出影院模式
function exitCinemaModel()
{
	$("#cinemaModelLay").hide();
}

function optionModel()
{
	if(document.getElementById("cinemaModelLay").style.display == 'none') {
		openCinemaModel();
	} else {
		exitCinemaModel();
	}
}
