//评论类
var CComment = {};

CComment.id = "";

//被允许的vote行为
CComment.validateActions = [ "supported", "opposed" ];

CComment.targetURL = "http://www.evolife.cn/go/comment/ajax.php?segment=post&callback=?";//发评论

CComment.targetURLOfVote = "http://www.evolife.cn/go/comment/ajax.php?segment=vote&callback=?";//投票

//用户投票
CComment.vote = function( action, commentID ){
	var _t = CComment;
	commentID = Math.abs( parseInt( commentID ) );
	if( $.inArray( action, _t.validateActions ) == -1 || action == "" || commentID == 0 ){
		return false;
	}
	var remoteAddr = _t.targetURLOfVote + "&action=" + action + "&comment_id=" + commentID + "&callback=?";
	var supportedOptionSpan = $( "span[id=comment_supported_" + commentID + "]" );
	var opposedOptionSpan = $( "span[id=comment_opposed_" + commentID + "]" );
	$.getJSON( remoteAddr, function( data ){
		if( data == true ){
			return true;
		}else{
			return false;
		}
	} );
};

//发布评论
CComment.post = function( articleID, commentContent, commentUser ){
	var _t = CComment;
	articleID = parseInt( articleID );
	if( articleID == "NaN" || articleID == 0 ){
		return false;
	}
	if( $.trim( commentContent ) == "" ){
		return false;
	}
	commentContent = CToolset.htmlEncode( commentContent ); //对评论内容中的编程字符进行转换
	commentUser = $.trim( commentUser );
	if( commentUser == "" ){
		commentUser = "爱活网友";
	}
	commentUser = CToolset.htmlEncode( commentUser );
	CToolset.setCookie( "evo_user", commentUser, 720, "/", ".evolife.cn" );
	$.ajax( {
		url		: _t.targetURL,
		type	: "post",
		data	: "user=" + commentUser + "&content=" + commentContent + "&article_id=" + articleID,
		success : function( data ){
			if( parseInt( data ) > 0 ){
				$( "#comment_content" ).html( "" );
				alert( "您的评论已经成功发布！" );
				window.location.reload();
			}else{
				alert( data );
				$( "#comment_content" ).focus();
			}
		},
		error	: function(){
			alert( "NO!!!!!!!" );
		}
	} );
};

CComment.postSuccess = function( data ){
	if( data == true ){
		alert( "POST:OK" );
	}else{
		alert( "POST:NO" );
	}
};

CComment.initCommentUser = function(){
	var commentUserInput = $( "input[id=comment_user]" );
	var commentUser = unescape( CToolset.getCookie( "evo_user" ) );
	if( commentUser != "" ){
		commentUserInput.val( commentUser );
		return true;
	}else{
		return false;
	}
};

//评论集类
var CComments = {};

CComments.curPage = 1; //评论结果当前页码

CComments.parentID = 0; //父页面ID

CComments.usage = "article"; //父页面类型

CComments.docType = "json";

CComments.listAreaID = "comments_list"; //评论列表区域DIV ID

CComments.amountID = "comments_amount"; //评论总数span ID

CComments.amountHandle = null;

CComments.listAreaHandle = null; //评论列表区域DIV Handle

CComments.targetURL = ""; 

//初始化函数
CComments.init = function(){
	var _t = CComments;
	_t.initSubmitStyle();
	_t.initTextInputStyle();
	CComment.initCommentUser();
};

CComments.load = function( parentID, usage, curPage, perPage, docType ){
	//对parentID和usage进行合法性处理
	parrentID = Math.abs( parseInt( parentID ) );
	usage = $.trim( usage ).toLowerCase();
	curPage = Math.abs( parseInt( curPage ) );
	perPage = Math.abs( parseInt( perPage ) );
	docType = $.trim( docType ).toLowerCase();
	if( parentID == 0 || usage == ""  ){
		return false;
	}
	if( usage == "" ){
		usage = "article";
	}
	if( docType == "" ){
		docType = "json";
	}
	
	//初始化各个参数
	var _t = CComments;
	_t.listAreaHandle = $( "#" + _t.listAreaID );
	_t.amountHandle = $( "#" + _t.amountID );

	//获取页面评论数据
	_t.parentID = parentID;
	_t.usage = usage;
	_t.docType = docType;
	_t.curPage = curPage;
	_t.perPage = perPage;
	_t.targetURL = "http://go.evolife.cn/comment/?id=" + articleID + "&doc_type=json&per_page=" + _t.perPage + "&page=" + _t.curPage + "&usage=" + _t.usage + "&callback=?";
	if( _t.docType == "json" ){
		$.getJSON( _t.targetURL, _t.showJSONData );
	}else{
		return false;
	}
};

//将获取到的JSON数据显示到页面
CComments.showJSONData = function( comments ){
	var _t = CComments;
	var _s = CToolset;
	try{
		if( comments.total == 0 ){ //如评论总数为0，则显示无评论提示
			_t.listAreaHandle.empty();
			_t.noCommentTip();
			return false;
		}
		_t.amountHandle.html( comments.total );
		$( "#comment_amount" ).html( comments.total );
		if( _t.curPage == 1 ){
			_t.listAreaHandle.empty();
		}
		var layerNumStart = comments.per_page *( comments.cur_page - 1 ); //楼层编号的起始值
		for( var i = 0; i < comments.data.length; i++ ){
			var commentItem = _s.createElement( "DL" );
			var comment = comments.data[i];
			commentItem.id = "comment_" + comment.id;
			commentItem.className = "comment_item";
			var commentHead = _s.createElement( "DT" );
			commentHead.className = "no_float";
			//' +  + '
			commentHead.innerHTML = '<div class="fl_left"><span class="clr-org">爱活' + comment.address + '网友' + ( comment.user == "爱活网友" ? "" : ( '[<strong>' + comment.user + '</strong>]' ) ) + '</span> <span class="clr-gray">在 ' + comment.pub_date + ' 说：</span></div><div class="fl_right">[ ' + ( comments.total - layerNumStart - i ) + '楼 ]</div>';
			var commentBody = _s.createElement( "DD" );
			commentBody.className = "comment_body";
			var commentContent = _s.createElement( "P" ); //评论内容
			commentContent.innerHTML = CToolset.htmlDecode( comment.content );
			$( commentBody ).append( $( commentContent ) );
			var commentBar = _s.createElement( "DIV" ); //评论工具栏
			commentBar.className = "comment_bar";
			var supported = _s.createElement( "SPAN" ); //支持
			supported.innerHTML = '<a href="#" id="comment_supported_' + comment.id + '">支持[<label>' + comment.supported + '</label>]</a>';

			$( commentBar ).append( $( supported ) );

			var opposed = _s.createElement( "SPAN" ); //反对
			opposed.innerHTML = '<a href="#" id="comment_opposed_' + comment.id + '">反对[<label>' + comment.opposed + '</label>]</a>';

			$( commentBar ).append( $( opposed ) );

			var reply = _s.createElement( "SPAN" ); //回复

		
			$( commentBody ).append( $( commentBar ) );
			$( commentItem ).append( $( commentHead ) );
			$( commentItem ).append( $( commentBody ) );
			_t.listAreaHandle.append( $( commentItem ) );
			$( supported ).click( function(){ //支持点击事件
				var aid =$( this ).children( "a" ).attr( "id" );
				var commentId = parseInt( aid.substring( aid.lastIndexOf( "_" ) + 1 ) ); //获得评论ID
				if( commentId == "NaN" )
					return false;
				CComment.vote( "supported", commentId );
				var supportedNum = parseInt( $( this ).children( "a" ).children( "label" ).html() );
				var opposedSpan = $( "#comment_opposed_" + commentId ).parent();
				var opposedNum = parseInt( opposedSpan.children( "a" ).children( "label" ).html() );
				if( supportedNum == "NaN" )
					return false;
				$( this ).html( "已支持[" + ( supportedNum + 1 ) + "]" );
				//alert( "#comment_opposed_" + commentId );
				this.className = "no_click";
				opposedSpan.html( "反对[" + opposedNum + "]" );
				opposedSpan.attr( "class", "no_click" );
				$( this ).unbind();
				return false;
			} );
			
			$( opposed ).click( function(){ //反对点击事件
				var aid = $( this ).children( "a" ).attr( "id" );
				var commentId = parseInt( aid.substring( aid.lastIndexOf( "_" ) + 1 ) ); //获取评论ID
				if( commentId == "NaN" )
					return false;
				CComment.vote( "opposed", commentId );
				var opposedNum = parseInt( $( this ).children( "a" ).children( "label" ).html() );
				var supportedSpan = $( "#comment_supported_" + commentId ).parent();
				var supportedNum = parseInt( supportedSpan.children( "a" ).children( "label" ).html() );
				if( opposedNum == "NaN" )
					return false;
				$( this ).html( "已反对[" + ( opposedNum + 1 ) + "]" );
				this.className = "no_click";
				supportedSpan.html( "支持[" + supportedNum + "]" );
				supportedSpan.attr( "class", "no_click" );
				$( this ).unbind();
				return false;
			} );

		}
		//判断是否已经到达最后一页
		var endPage = false;
		if( comments.per_page * comments.cur_page >= comments.total ){
			endPage = true;
		}
		_t.nextPage( endPage );
	}catch( e ){
	}
};

//无评论提示
CComments.noCommentTip = function(){
	var _t = CComments;
	var _s = CToolset;
	try{
		var divObj = _s.createElement( "DIV" );
		divObj.id = "no_comments";
		divObj.innerHTML = "暂无评论，欢迎您来说两句";
		_t.listAreaHandle.append( $( divObj ) );
		_t.amountHandle.html( "0" );
	}catch( e ){
	}
};

//下一页控制按钮
CComments.nextPage = function( endPage ){
	var _t = CComments;
	var _s = CToolset;
	$( "#next_page" ).remove();//先移除旧的下一页按钮
	if( !endPage ){ //如果不是最后一页，则添加新的下一页按钮
		var divNext = _s.createElement( "DIV" );
		divNext.id = "next_page";
		divNext.innerHTML = '<a href="#">下一页</a>';
		_t.listAreaHandle.after( divNext );
		$( divNext ).children( "a" ).click( function(){
			nextPageNum = _t.curPage + 1;
			_t.load( _t.parentID, _t.usage, nextPageNum, _t.perPage, _t.docType  );
			return false;
		} );
	}
};

//显示评论加载效果
CComments.showLoadingEffort = function( toShow ){
	if( toShow ){

	}else{
	}
};

//为评论提交按钮绑定光影效果
CComments.initSubmitStyle = function(){
	var _t = CComments;
	var button = $( ".comments_input_submit > span[class=comment_submit] > button" );
	var buttonSpan = $( ".comments_input_submit > span[class=comment_submit]" );
	button.mouseover( function(){
		this.style.background = "#333";
	} );
	button.mouseout( function(){
		this.style.background = "#666";
	} );
	buttonSpan.mouseover( function(){
		this.style.border = "1px solid #333";
	} );
	buttonSpan.mouseout( function(){
		this.style.border = "1px solid #666";
	} );
};

//为评论输入框和用户名输入框绑定光影效果
CComments.initTextInputStyle = function(){
	var _t = CComments;
	var ta = $( ".comments_input_body > textarea" );
	ta.focus( function(){
		this.style.border = "1px solid #FF6600";
	} );
	ta.blur( function(){
		this.style.border = "1px solid #CCCCCC";
	} );
};

