// JavaScript Document
document.write('<scr' + 'ipt type="text/jav' + 'ascript" src="/Public/scripts/swfobject.js"></scr' + 'ipt>');
document.write('<scr' + 'ipt type="text/jav' + 'ascript" src="/Plugs/xheditor/xheditor-1.1.10-zh-cn.min.js\?v=1.1.10"></scr' + 'ipt>');

(function($){	
$.vodId = 0;
$.nameFilter = '';
$.contentFilter = '';
$.timeoutID = 0;
$.adTimeoutID = 0;
$.timer = 0;
$.completedInit = false;
$.flashvars = {};
$.parameters = {
	src: "",
	poster: "",
	autoHideControlBar: false,
	streamType: "recorded",
	autoPlay: true
};
$.attributes = {
	allowFullScreen: true,
	allowscriptaccess: "always",
	quality: "high",
	bgcolor: "#000000",
	wmode: "transparent"
};

$.extend({
	init: function() {
		// 初始化在线编辑器
		$.editor = $('#msg').xheditor({tools:'Bold,Italic,Underline,Strikethrough,Fontface,FontSize,FontColor,BackColor,Emot',shortcuts:{'enter':doSubmit}});
		$.guestbook_editor = $('#suggest').xheditor({tools:'Bold,Italic,Underline,Strikethrough,Fontface,FontSize,FontColor,BackColor,Emot',shortcuts:{'ctrl+enter':doGuestbookSubmit}});
		
		// 初始化页面数据
		$.ajax({ 
			url:"index.php?m=Filters&rnd" + Math.floor(Math.random()*10000), 
			type: 'GET', 
			dataType: 'xml',
			timeout: 5000, 
			success: function(xml){
				$(xml).find("filter").each(function(i){
					// 过滤字
					$.nameFilter=$(this).children("username").text().split(",");
					$.contentFilter=$(this).children("content").text().split(",");
					// 默认视频ID
					$.vodId = $('.vod-selector ul li:first').find('a').attr('vodid');
					// 显示默认视频
					$.setVodInfo($('.vod-selector ul li:first').find('a').attr('href'));
				}); 
				
				$.completedInit = true;
			}
		});
	},
	
	setVodInfo: function(xml) {
		$.timer = 5;
			
		$.ajax({ 
			url:xml + '&rnd' + Math.floor(Math.random()*10000), 
			type: 'GET', 
			dataType: 'xml',//这里可以不写，但千万别写text或者html!!! 
			timeout: 3000, 
			success: function(xml){
				$(xml).find("vod").each(function(i){ 
					var ad=$(this).children("ad").text();
					var url=$(this).children("url").text();
					var poster=$(this).children("poster").text();
					var title=$(this).children("title").text(); 
					var content=$(this).children("content").text(); 
					$("#vod-panel .headline").html(title);
					$(".vod-desc .content").html(content);
					if (ad) {
						$.setVod(ad, true, "");
						$("#vod-panel").append('<div id="vod-ad-timer"></div>');
						$('.vod-selector').append('<div id="vod-ad-mask"></div>');
						
						$.adTimer();
						
						setTimeout(function() {
							window.clearTimeout($.adTimeoutID);
							$("#vod-panel").find("#vod-ad-timer").remove();
							$(".vod-selector").find("#vod-ad-mask").remove();
							$.setVod(url, false, poster);							
						}, 6000);
					} else {					
						$.setVod(url, false, poster);
					}
					
					$.doLoad(); // 视频装载成功后，读取聊天记录
					$.doMatchLoad();
				}); 
			} 
		});
	},
	
	adTimer: function() {
		window.clearTimeout($.adTimeoutID);
		$("#vod-panel").find('#vod-ad-timer').html('广告剩余时间：' + $.timer  + '秒');
		$.timer--;
		$.adTimeoutID = setTimeout($.adTimer, 1000);					
	},
	
	setVod: function(src, isAd, poster) {
		$.parameters["src"] = src;
		$.parameters["poster"] = poster;
		if (isAd || (src.toUpperCase().indexOf(".JPG") != -1 || src.toUpperCase().indexOf(".GIF") != -1
		 || src.toUpperCase().indexOf(".PNG") != -1 || src.toUpperCase().indexOf(".JPEG") != -1)) {
			$.parameters["autoPlay"] = true;
		} else {
			$.parameters["autoPlay"] = false;		
		}
		
		if (src.toUpperCase().indexOf("RTMP") != -1 || src.toUpperCase().indexOf("LIVE") != -1) {
			$.parameters["streamType"] = 'live';
		}
		swfobject.embedSWF("/Public/flash/player.swf", "vod-player", 576, 324, "10.1.0", $.flashvars, $.parameters, $.attributes);
	},
	
	encode: function(s) {
		return  (encodeURIComponent)? encodeURIComponent(s):s;
	},	
	
	countChar: function(text) {
		var reg = /<\/?[^>]*>/g;
		var imgReg = /<img.*?(?:>|\/>)/gi;
		
		imgArr = text.match(imgReg);	
		text = text.replace(reg, '');
		
		return (imgArr == null) ? text.length : text.length + imgArr.length;
	},
	
	doLoad: function() {
		window.clearTimeout($.timeoutID);	
		
		$.ajax({ 
			url:"index.php?m=Chat&vodId=" + $.vodId + "&rnd" + Math.floor(Math.random()*10000), 
			type: 'GET', 
			dataType: 'html',
			timeout: 5000, 
			success: function(html){
				$("#chat-message-panel ul").html(html);
				$('#chat-message-panel').scrollTop($('#chat-message-panel')[0].scrollHeight);
			}
		});
	
		$.timeoutID = setTimeout($.doLoad, 5000);
	},

	doMatchLoad: function() {
		$.ajax({ 
			url:"index.php?m=Match&vodId=" + $.vodId + "&rnd" + Math.floor(Math.random()*10000), 
			type: 'GET', 
			dataType: 'html',
			timeout: 5000, 
			success: function(html){
				$("#match ul").html(html);
			}
		});
	}
});

doGuestbookSubmit = function() {
	var username = $("#username").val();
	var content = $.guestbook_editor.getSource();
	var len = $.countChar(content);
	if (len > 0 && len <= 300) {
		$.ajax({ 
			url:"index.php?m=Guestbook&a=update", 
			type: 'POST', 
			data: "username="+$.encode(username)+"&content="+$.encode(content),
			beforeSend : function (xhr) {
				xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			},
			success: function(msg){
				alert("谢谢您的参与!!!");
			}
		});
	}
	
	$("#username").val('');
	$.guestbook_editor.setSource('');
	$("#username").focus();
	
	return false;
}
	
doSubmit = function() {
	var nickName = $("#nickname").val();
	if (nickName.length <= 0) {
		alert("请输入昵称！");
		return false;
	}
	if ($.completedInit == false) {
		alert("页面初始化错误!");
		return false;
	}
	for (var i = 0; i < $.nameFilter.length; i++) {
		if (nickName.indexOf($.nameFilter[i]) != -1) {
			alert("请更改昵称后再发言！");
			return false;
		}
	}
	var content = $.editor.getSource();
	for (var i = 0; i < $.contentFilter.length; i++) {
		if (content.indexOf($.contentFilter[i]) != -1) {
			alert("内容含有敏感词汇，请更改！");
			return false;
		}
	}
	var len = $.countChar(content);
	if (len > 0 && len <= 100) {
		$.ajax({ 
			url:"index.php?m=Chat&a=update", 
			type: 'POST', 
			data: "vodid="+$.vodId+"&nickname="+$.encode(nickName)+"&content="+$.encode(content),
			beforeSend : function (xhr) {
				xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			},
			success: function(msg){
				$("#chat-message-panel ul").append('<li><strong>[' + nickName + ']</strong>' + content + '</li>');
				$('#chat-message-panel').scrollTop($('#chat-message-panel')[0].scrollHeight);
			}
		});
	}
	
	$.editor.setSource('');
	$.editor.focus();
	
	return false;
}

popWindow = function(src) {
	showModalDialog(src, "", "dialogWidth:576px; dialogHeight:432px; status:0");
}

$(function() {	
	$.init();
	
	// 初始化视频选择器
	$('.vod-selector ul li:first').addClass('active');
	$('.vod-selector ul li:last').addClass('last');
	
	// Tab初始化
	$(".ui-tabs-nav > li > a").click(function(e) {
		if (e.target == this) {
			var tabs = $(this).parent().parent().children("li");
			var panels = $(this).parent().parent().parent().find(".ui-tabs-panel");
			var index = $.inArray(this, $(this).parent().parent().find("a"));
			if (panels.eq(index).is(".ui-tabs-panel")) {
				tabs.removeClass("ui-tabs-selected")
					.eq(index).addClass("ui-tabs-selected");
				panels.addClass("ui-tabs-hide")
					.eq(index).removeClass("ui-tabs-hide");
			}
		}
	});
});
	
})(jQuery);
