/**
*	该脚本用于实现文章页面中的评论内容加载/显示/操作等功能
* @name					:	woaika javascript utils - comments
* @createdTime 	: 2006-08-10 13:00
* @author				: shixin
* @using				:	
*/
function woaika_comment(asset_id,node_id){
	/** 评论内容 */
	this.m_comments = null;
	/** 评论信息 */
	this.m_comments_info = null;
	/** 当前的评论页索引 */
	this.m_cpageindex = 0;
	
	/** 指定的资源ID */
	this.m_asset_id = asset_id;
	/** 资源类型 */
	this.m_asset_type = "art";
	
	this.m_render_node_id = node_id;
	
} 

function woaika_comment_util(){
	this.m_cmts = new Array();
	
	/** 获取评论数据脚本的URL  */
	this.m_woaika_loadinfourl = "http://app.51credit.com/index3.php?option=com_comment&task=getPageConfig";
	this.m_woaika_loadurl = "http://app.51credit.com/index3.php?option=com_comment&task=getJsPage";
	//this.m_woaika_loadurl = "/-js/test_comments.js";
	
	this.m_woaika_counturl = "http://app.51credit.com/index3.php/component/option,com_comment/task,getCommentCount/assetId,";
	
	this.m_woaika_submiturl = "http://app.51credit.com/index3.php/component/option,com_comment/task,addComment/";
	
	this.wak_comment_timeoutFlag = null;
	
	this.wak_last_submit_asset_id = null;
	
	this.getSubmitUrl = function(){
		return this.m_woaika_submiturl;
	}
	
	this.setComment = function(_cmt){
		this.m_cmts[_cmt.m_asset_id] = _cmt;	
	}
	
	this.getComment = function(_assetId){
		return this.m_cmts[_assetId];
	}
	/**
	* 加载评论数据
	*/
	this.loadCommentsData = function(_assetid,_pageNum,_comments){
		this.getComment(_assetid).m_comments = _comments;
		this.getComment(_assetid).m_cpageindex = _pageNum;
		this.renderCommentData(_assetid); 
	}
	/**
	* 加载评论信息
	*/
	this.loadCommentInfoData = function(_assetid,_commentinfo){
		this.getComment(_assetid).m_comments_info = _commentinfo;
		this.renderCommentInfoData(_assetid);
	}	
	
	/**
	* 呈现评论数量
	*/
	this.renderCommentInfoData = function(_assetid){
		if(document.getElementById("woaika_comment_navigator_"+_assetid)!=null){
			document.getElementById("woaika_comment_navigator_"+_assetid).innerHTML = this._getNavigator(_assetid);
		}
	}
	
	/**
	* 呈现评论
	*/
	
	this.renderCommentData = function(_assetid){
		var _cmts = this.getComment(_assetid).m_comments;
		var _cmtinfo = this.getComment(_assetid).m_comments_info;
		var _str = "<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#EEEEEE\">";
		for(var i=0,n=_cmts.comments.length;i<n;i++){
	    _str += "<tr>";
	    _str += "<td bgcolor=\"#F5F5F5\" class=\"text-12-18\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>" + _cmts.comments[i].createTime + "  <font color=red>" + _cmts.comments[i].userName + "</font> [" + _cmts.comments[i].userIp + "] 认为:</td><td><div align=\"right\">第"+_cmts.comments[i].cmtIndex+"楼</div></td></tr></table></td>"; 
	  	_str += "</tr>";
	  	_str += "<tr>";
	    _str += "<td bgcolor=\"#FFFFFF\"  class=\"text-12-18\"><div id=\"woaika_comment_item_"+_cmts.comments[i].cmtId+"\">" + _cmts.comments[i].content + "</div>";
	    _str += "<div align=\"right\"><a class='text-12-18-' href='javascript:void(0);' onclick=\"showReplyCommentDlg('"+_assetid+"','"+_cmts.comments[i].cmtId+"');\">回复</a>  <!-- <a href=''>支持</a>(0) <a href=''>不表态</a>(0) <a href=''>反对</a>(0)--></div></td>";
	  	_str += "</tr>";
  	}
    _str += "</table>";
    
    var _wcc = 0;
    var _wpc = 0;
    if(_cmtinfo!=null){
    	_wcc = _cmtinfo.CountOfComment;
    	_wpc = _cmtinfo.PagesOfComment;
    }
    //_str += "<div  class=\"text-12-18\">共(<span id=\"woaika_CountOfComment_"+_assetid+"\">"+ _wcc +"</span>)篇评论 共(<span id=\"woaika_PagesOfComment_"+_assetid+"\">"+_wpc+"</span>)页 当前第(<span>"+this.getComment(_assetid).m_cpageindex+"</span>)页  <a href=\"javascript:WoaikaCommentUtil.gotoPage('"+_assetid+"',1)\">第一页</a> <a href=\"javascript:WoaikaCommentUtil.gotoPage('"+_assetid+"',2)\">上一页</a> <a href=\"javascript:WoaikaCommentUtil.gotoPage('"+_assetid+"',2)\">下一页</a> <a href=\"javascript:WoaikaCommentUtil.gotoPage('"+_assetid+"',3)\">后一页</a></div>";
    _str += "<div id=\"woaika_comment_navigator_"+_assetid+"\">";
    _str += this._getNavigator(_assetid);
    _str += "</div>";
    var _nid = this.getComment(_assetid).m_render_node_id;
    if(document.getElementById(_nid)!=null){
    	document.getElementById(_nid).innerHTML = _str;
    }
	}
	
	this._getNavigator = function(_assetid){
		var _cmtinfo = this.getComment(_assetid).m_comments_info;
		var _wcc = 0;
    var _wpc = 0;
    var _cpn = this.getComment(_assetid).m_cpageindex;
    if(_cmtinfo!=null){
    	_wcc = _cmtinfo.CountOfComment;
    	_wpc = _cmtinfo.PagesOfComment+1;
    	if(_cpn==0){
    		_cpn = _wpc;
    	}
    }
    var _str = "";
    _str += "<div  class=\"text-12-18\">共(<span id=\"woaika_CountOfComment_"+_assetid+"\">"+ _wcc +"</span>)篇评论 共(<span id=\"woaika_PagesOfComment_"+_assetid+"\">"+_wpc+"</span>)页";
    _str += " 当前第(<span>"+_cpn+"</span>)页";
    _str += "  <a class='text-12-18-' href=\"javascript:WoaikaCommentUtil.gotoPage('"+_assetid+"',1)\">第一页</a>";
    if(_cpn>1){
    	_str += "  <a class='text-12-18-' href=\"javascript:WoaikaCommentUtil.gotoPage('"+_assetid+"',"+(_cpn-1)+")\">上一页</a>";
  	}
  	if(_cpn<_wpc){
    	_str += "  <a class='text-12-18-' href=\"javascript:WoaikaCommentUtil.gotoPage('"+_assetid+"',"+(_cpn+1)+")\">下一页</a>";
  	}
    _str += "  <a class='text-12-18-' href=\"javascript:WoaikaCommentUtil.gotoPage('"+_assetid+"',0)\">后一页</a>";
    _str += "  <a class='text-12-18-' href=\"javascript:WoaikaCommentUtil.gotoPage('"+_assetid+"',0)\" >刷新</a>";
    _str += "  </div>";
		return _str;
	} 
	
	this.submitComment = function(_assetid,_ret){
		if(_ret){
			this.gotoPage(_assetid,0);
		}else{
			alert("对不起,服务器繁忙,请稍候再试!");
		}
	}
	
	this.afterSubmitComment = function(_assetid){
		this.wak_last_submit_asset_id = _assetid;
		if(this.wak_comment_timeoutFlag){
			clearTimeout(this.wak_comment_timeoutFlag);
		}
		this.wak_comment_timeoutFlag = window.setTimeout(woaika_comment_refresh,2500);
	}
	
	this.refresh = function(){
		if(this.wak_last_submit_asset_id){
			this.gotoPage(this.wak_last_submit_asset_id,0);
		}
	}
	
	/**
	* 加载指定的页面
	*/
	this.gotoPage = function(_assetid,_pageIndex){
		var _postInfoUrl = this.m_woaika_loadinfourl;
		if(_postInfoUrl.indexOf("?")==-1){
			_postInfoUrl += "?";
		}else{
			_postInfoUrl += "&";
		}
		_postInfoUrl += "assetId=" + _assetid + "&action=conf&ts="+Math.random();
		
		var _postUrl = this.m_woaika_loadurl;
		if(_postUrl.indexOf("?")==-1){
			_postUrl += "?";
		}else{
			_postUrl += "&";
		}
		_postUrl += "assetId=" + _assetid
		if(_pageIndex>0){
			_postUrl += "&pageNum=" + _pageIndex;
		}else{
			_postUrl += "&ts=" + Math.random();
		}
		this._doPost(_postInfoUrl);
		this._doPost(_postUrl);
	}
	
	this._doPost = function(_postUrl){
		var _utilScript = null;
		var _number = Math.random();
		_utilScript = document.createElement('script');
		_utilScript.setAttribute("type","text/javascript");
		_utilScript.setAttribute("language","javascript");
		_utilScript.setAttribute("src", _postUrl);
		_utilScript.setAttribute("id","wak_comment_" + _number);
		document.getElementsByTagName("head").item(0).appendChild(_utilScript);
	}
	
	this.loadCommentsCounts = function(_countData){
		for(var i=0;i<_countData.counts.length;i++){
			//alert(_countData.counts[i].cmtId + " -> " + _countData.counts[i].cmtCount);
			var _fun = eval("window.woaika_setCommentCount");
			if(_fun!=null){
				_fun(_countData.counts[i].cmtId,_countData.counts[i].cmtCount);
			}
		}
	}
	
	this.fetchCommentsCounts = function(_assetIds){
		var _url = this.m_woaika_counturl + _assetIds;
		this._doPost(_url);
	}
	
	this.getCommentSubmitUrl = function(){
		return this.m_woaika_submiturl;
	}

}

function woaika_comment_refresh(){
	WoaikaCommentUtil.refresh();
	if(eval("window.on_woaika_comment_refresh")!=null){
		eval("window.on_woaika_comment_refresh")();
	}
}

/** 创建默认的评论工具类 */
var WoaikaCommentUtil = new woaika_comment_util(); 