/**************************************************************************/
/*    	迷你留言本程序
/*		南方新闻网 2009.2 用类封装
/**************************************************************************/
document.domain="southcn.com";
/* 类定义 */
function ScGuestBook(){
	var _this = this;		/* this 指针，私有 */
	var ObjId;				/* 本实例的编号，用于区别多个实例同时应用 */
	var prContainer;		/* 细览页面上用于放置该感受投票模板的容器ID */
	var prTitle;			/* 文档标题 */
	var prUrl;				/* 文档地址 */
	var prShowSubmitBox;	/* 是否显示留言框 */
	var prShowListNew;		/* 是否显示最新留言列表 */
	var prInputBoxWidth;	/* 输入框宽度(px) */
	var prInputBoxHeight;	/* 输入框高度(px) */
	var prListNewHeight;	/* 最新留言列表的高度(px) */
	var prListNewCount;		/* 最新留言列表的条数 */
	var prSkin;				/* 使用的皮肤 */
	var prTemplate;			/* 使用的模板 */
	var configUrl = "http://newslink.southcn.com/v200811";	/* 后台(PHP程序)部署地址 */
	var defaultSkinUrl = "http://newslink.southcn.com/v200811/skin/guestbook/guestbook.css";	/* 默认皮肤地址 */
	var defaultTemplateUrl = "http://newslink.southcn.com/v200811/template/guestbook.htm";	/* 默认模板地址 */

	/* 通过ajaxproxy代理返回Ajax对象,ajaxproxy部署在后台服务器上，解决ajax跨域问题 */
	var InitAjax = function(){
		var ajax = false;
		try{
			ajax = document.getElementById("ajaxproxy_" + ObjId).contentWindow.InitAjax();
		}catch(e){
			alert("无法加载留言部件！");
			return null;
		}
		return ajax;
	};
	
	/* 初始化 */
	_this.Init = function(_prObjId,_prContainer,_prTitle,_prUrl,_prShowSubmitBox,_prShowListNew,_prListNewCount,_prListNewHeight,_prInputBoxWidth,_prInputBoxHeight,_prSkin,_prTemplate){
		ObjId = _prObjId;
		prContainer 	=     (_prContainer == null)?"conCommentBox":_prContainer;
		prShowSubmitBox =     (_prShowSubmitBox == null)?true:_prShowSubmitBox;
		prShowListNew	=     (_prShowListNew == null)?true:_prShowListNew;
		prListNewHeight	=	  (_prListNewHeight == null)?200:_prListNewHeight;
		prInputBoxWidth =	  (_prInputBoxWidth == null)?370:_prInputBoxWidth;
		prInputBoxHeight=	  (_prInputBoxHeight == null)?170:_prInputBoxHeight;
		prListNewCount 	=     (_prListNewCount == null)?5:_prListNewCount;
		prSkin 			=     (_prSkin == null)?defaultSkinUrl:_prSkin;
		prTemplate		=     (_prTemplate == null)?defaultTemplateUrl:_prTemplate;
		/* 如果标题未指定，自动获得本文标题 */
		if(_prTitle == null)	prTitle = document.getElementById("ScDetailTitle").innerHTML;
		else	prTitle = _prTitle;
		/* 如果文档地址未指定，自动获得本文地址 */
		if(_prUrl == null)		prUrl = location.href;
		else	prUrl = _prUrl;
		var url = encodeURIComponent(prUrl);
		/* 加载ajax代理 */
		document.write("<iframe src='" + configUrl + "/ajaxproxy_for_guestbook.php?objid=" + ObjId + "&prurl=" + url + "' width='0' height='0' frameborder='0' id='ajaxproxy_" + ObjId + "'></iframe>");
	};
	
	/* 内部初始化,在AJAX代理加载完成后由代理启动该进程 */
	_this.Init_2 = function(){
		/* 加载模板 */
		var ajax = InitAjax();
		ajax.open("GET",prTemplate,true); 
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) { 
				if(ajax.status == 200){
					/* 修改模板中调用的函数名称，加后缀,以避免在多实例应用时id冲突 */
					var str = ajax.responseText;
					var re = /ShowMsgBox/g;
					var str = str.replace(re,"ScGuestBookObj[" + ObjId + "].ShowMsgBox");
					var re = /HideMsgBox/g;
					var str = str.replace(re,"ScGuestBookObj[" + ObjId + "].HideMsgBox");
					var re = /More/g;
					var str = str.replace(re,"ScGuestBookObj[" + ObjId + "].More");
					var re = /SubmitIt/g;
					var str = str.replace(re,"ScGuestBookObj[" + ObjId + "].SubmitIt");
					/* 往容器中写入模板内容，出现页面效果 */
					document.getElementById(prContainer).innerHTML = str;
					/* 修改模板的所有元素id，加后缀，以避免在多实例应用时id冲突 */
					document.getElementById("ScCommentBox").setAttribute("id","ScCommentBox_" + ObjId); 
					document.getElementById("ScCommentBoxReplies").setAttribute("id","ScCommentBoxReplies_" + ObjId); 
					document.getElementById("ScCommentBoxSubmit").setAttribute("id","ScCommentBoxSubmit_" + ObjId); 
					document.getElementById("ScCommentBoxMsgCon").setAttribute("id","ScCommentBoxMsgCon_" + ObjId); 
					document.getElementById("ScCommentBoxForm").setAttribute("id","ScCommentBoxForm_" + ObjId); 
					document.getElementById("ScCommentBoxUserName").setAttribute("id","ScCommentBoxUserName_" + ObjId); 
					document.getElementById("ScCommentBoxUserMsg").setAttribute("id","ScCommentBoxUserMsg_" + ObjId); 
					document.getElementById("ScCommentBoxTips").setAttribute("id","ScCommentBoxTips_" + ObjId); 
					document.getElementById("ScCommentBoxUrl").setAttribute("id","ScCommentBoxUrl_" + ObjId); 
					document.getElementById("ScCommentBoxTitle").setAttribute("id","ScCommentBoxTitle_" + ObjId); 
					document.getElementById("ScCommentBoxListNew").setAttribute("id","ScCommentBoxListNew_" + ObjId);
					/* 加载模板完成，继续其他初始化 */
					/* 加载皮肤 */
					_this.LoadSkin(prSkin);				
					/* 读取URL */
					document.getElementById("ScCommentBoxUrl_" + ObjId).value = prUrl;
					/* 读取文档标题 */
					document.getElementById("ScCommentBoxTitle_" + ObjId).value = prTitle;
					/* 显示或隐藏留言框 */
					if(prShowSubmitBox)		_this.ShowMsgBox();
					else					_this.HideMsgBox();
					/* 限制最新留言列表div的高度 */
					document.getElementById("ScCommentBoxListNew_" + ObjId).style.height = prListNewHeight + "px";
					/* 限制输入框的尺寸 */
					document.getElementById("ScCommentBoxUserMsg_" + ObjId).style.width = prInputBoxWidth + "px";
					document.getElementById("ScCommentBoxUserMsg_" + ObjId).style.height = prInputBoxHeight + "px";
					/* 显示或隐藏最新评论列表 */
					if(prShowListNew){
						document.getElementById("ScCommentBoxListNew_" + ObjId).style.display = "";
						_this.LoadListNew(prListNewCount);
					}else
						document.getElementById("ScCommentBoxListNew_" + ObjId).style.display = "none";
					/* 读取评论总数 */
					_this.LoadReplies();
				}
			}
		}
		ajax.send(null);	
	};
	
	/* 显示(隐藏)输入框 */
	_this.ShowMsgBox = function(){
		var idl = eval("document.getElementById('ScCommentBoxMsgCon_" + ObjId + "')");
		idl.style.display = "";
		document.getElementById("ScCommentBoxListNew_" + ObjId ).style.display = "none";
		document.getElementById("ScCommentBox_" + ObjId).style.backgroundColor = "#EEEEEE";
	};
	_this.HideMsgBox = function(){
		var idl = eval("document.getElementById('ScCommentBoxMsgCon_" + ObjId + "')");
		idl.style.display = "none";
		document.getElementById("ScCommentBoxListNew_" + ObjId ).style.display = "";
		document.getElementById("ScCommentBox_" + ObjId).style.backgroundColor = "#FFFFFF";
	};
	
	/* 输入非空检测 */
	_this.CheckIt = function(){
		if(document.getElementById("ScCommentBoxUserMsg_" + ObjId).value == ""){
			alert("请填写内容！");
			return false;
		}else{
			return true;
		}
	};
	
	/* 提交留言 */
	_this.SubmitIt = function(){
		if(_this.CheckIt()){		
			var ajax = InitAjax();
			var url = encodeURIComponent(document.getElementById("ScCommentBoxUrl_" + ObjId).value);
			var title = encodeURIComponent(document.getElementById("ScCommentBoxTitle_" + ObjId).value);
			var message = encodeURIComponent(document.getElementById("ScCommentBoxUserMsg_" + ObjId).value);
			var postbody="message="+message; 
			ajax.open("POST",configUrl + "/ajax/post.php?title=" + title + "&url=" + url + "&action=post"); 
			ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			ajax.onreadystatechange = function() {
				if (ajax.readyState == 4) { 
					if(ajax.status == 200){
						var result = ajax.responseText;
						if(result==1){
							document.getElementById("ScCommentBoxTips_" + ObjId).innerHTML = "您的留言已经提交，请等待审核...";
							//预览自己留言内容start
							var tmpList=document.getElementById("ScCommentBoxListNew_" + ObjId);
							var tmpMyMsg="<dt><span class='time'>[审核中]</span> <span class='nickname'>您的评论：</span>" + document.getElementById("ScCommentBoxUserMsg_" + ObjId).value + "</dt>";
							if(tmpList.getElementsByTagName("dl").length<1){
								tmpList.innerHTML="<dl>" + tmpMyMsg + "</dl>";
							}else{
								var tmp=tmpList.getElementsByTagName("dl")[0];
								tmp.innerHTML= tmpMyMsg + tmp.innerHTML;
							}
							//预览自己留言内容end
							/* 2秒后自动清除 */
							setTimeout("ScCommentBoxObj[" + ObjId + "].ClearTips()",2000);
							document.getElementById("ScCommentBoxUserMsg_" + ObjId).value = "";
							return true;
						}else{
							alert("系统忙，请稍候再试。");
							return false;
						}
					}
				}
			}
			ajax.send(postbody); 
		}
	};
	
	/* 清除TIPS */
	_this.ClearTips = function(){
		document.getElementById("ScCommentBoxTips_" + ObjId).innerHTML = "";
		_this.HideMsgBox();
	};
	
	/* 获得留言数量 */
	_this.LoadReplies = function(){
		var ajax = InitAjax();
		var url = encodeURIComponent(document.getElementById("ScCommentBoxUrl_" + ObjId).value);
		ajax.open("POST",configUrl + "/ajax/load_replies.php?url=" + url); 
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) { 
				if(ajax.status == 200){
					var replies = ajax.responseText;
					document.getElementById("ScCommentBoxReplies_" + ObjId).innerHTML = replies;
					if(document.getElementById("ScCommentBoxTopMsg_" + ObjId))
						document.getElementById("ScCommentBoxTopMsg_" + ObjId).innerHTML = "网友评论 " + replies + " 条，<a href='javascript:ScCommentBoxObj[" + ObjId + "].More();'>点击查看</a>";
				}
			}
		}
		ajax.send(null); 
	};
		
	/* 加载最新评论列表 */
	_this.LoadListNew = function(prListNewCount){
		document.getElementById("ScCommentBoxListNew_" + ObjId).innerHTML = "&nbsp;&nbsp;正在加载最新留言，请稍候...";
		var ajax = InitAjax();
		var url = encodeURIComponent(document.getElementById("ScCommentBoxUrl_" + ObjId).value);
		ajax.open("POST",configUrl + "/ajax/load_listnew.php?url=" + url + "&n=" + prListNewCount); 
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) { 
				if(ajax.status == 200){
					document.getElementById("ScCommentBoxListNew_" + ObjId).innerHTML = ajax.responseText;				
				}
			}
		}
		ajax.send(null); 
	};
	
	/* 查看更多评论 */
	_this.More = function(){
		var url = encodeURIComponent(document.getElementById("ScCommentBoxUrl_" + ObjId).value);
		var title = encodeURIComponent(document.getElementById("ScCommentBoxTitle_" + ObjId).value);
		window.open(configUrl + "/more.php?url="+url+"&title="+title,"MoreList","");
	};
	
	/* 加载皮肤 */
	_this.LoadSkin = function(cssFile){
		if(window.navigator.userAgent.indexOf("MSIE")>=1){
			//如果浏览器为IE	  
			document.createStyleSheet(cssFile);
		}
		else if(window.navigator.userAgent.indexOf("Firefox")>=1){
			//如果浏览器为Firefox
			var objCSS = document.createElement('link');
			objCSS.rel = "stylesheet";
			objCSS.href = cssFile;
			objCSS.type = "text/css";
			document.body.appendChild(objCSS);
		}
	};
}


/* 动态数组解决一个页面应用多个实例的需要 */
var ScGuestBookCount;	/* 计数器 */
var ScGuestBookObj = new Array();	/* 实例数组(动态) */
ScGuestBookCount = 0;
function InitScGuestBook(_prContainer,_prTitle,_prUrl,_prShowSubmitBox,_prShowListNew,_prListNewCount,_prListNewHeight,_prInputBoxWidth,_prInputBoxHeight,_prSkin,_prTemplate){
	ScGuestBookObj[ScGuestBookCount] = new ScGuestBook();
	ScGuestBookObj[ScGuestBookCount].Init(ScGuestBookCount,_prContainer,_prTitle,_prUrl,_prShowSubmitBox,_prShowListNew,_prListNewCount,_prListNewHeight,_prInputBoxWidth,_prInputBoxHeight,_prSkin,_prTemplate);
	ScGuestBookCount ++;
}