
/**
	* Copyright (c) 2008, 计算机世界集团B2C互联网事业部
	* All rights reserved.
	* @file api.js
	* @brief 评论系统api
	* @author HonestQiao
	* 评论系统api
	* $Id$
*/

COMMENT_API_URL_ARTICLE = 'http://comment.pcw.com.cn/article_js.php';
COMMENT_API_URL_PRODUCT = 'http://comment.pcw.com.cn/product_js.php';

/**
 * @brief parseCommentConfig 解析配置数据
 *
 * @param config_object Object 包含评论配置的元素，一般为<script>...</script>
 *
 * @retval json 解析的json数据
 *
 * @access public
 */
function parseCommentConfig(config_object)
{
	var comment_info = jQuery(config_object).html().replace(/[\r\n]/g,'');
	var comment_json = {path:"",title:"",url:""};
	if((matches = comment_info.match(/\[comment\](.+?)\[\/comment\]/i)))
	{
		if((ms = matches[1].match(/\[([a-z0-9_\-]+)\](.+?)\[\/\1\]/gi)))
		{
			if(ms.length>0){
				for(var j=0,l=ms.length;j<l;j++)
				{
					var line = ms[j];
					if ((mls = line.match(/\[([a-z0-9_\-]+)\](.+?)\[\/\1\]/i)))
					{
						if (mls[2].toLowerCase()=='true')
						{
							mls[2] = 1;
						}
						else if (mls[2].toLowerCase()=='false')
						{
							mls[2] = 0;
						}
						var jsvalue = String(mls[2]).replace(/"/g,"＂");
						var jscmd = 'comment_json.'+mls[1]+'="'+jsvalue+'";';
						eval(jscmd);
					}
				}
			}
		}
	}
	return comment_json;
}

/**
 * @brief autoLoadComment 自动解析当前页面的评论配置并处理列表
 *
 * @param callback function 评论列表信息调用后的回调函数
 * @param callback2 function 评论发表界面调用后的回调函数
 *
 * @access public
 */
 function autoLoadComment(callback,callback2)
 {
	var comment_scripts = jQuery('script[id=comment]').get();
	if (comment_scripts.length)
	{
		for(var i=0,l=comment_scripts.length;i<l;i++)
		{
			var comment_json = parseCommentConfig(comment_scripts[i]);
			if(comment_json.auto==1 || comment_json.auto=='1')
			{
				if (comment_json.path && comment_json.path!=='')
				{
					jQuery(comment_scripts[i]).attr("name",comment_json.path);
					getPostList(comment_json,function(comment_json,data){
						var myTemplateObj;
						//检查返回数据是否包含HTML模板
						if (data && data.html && data.html!=='')
						{
							myTemplateObj = TrimPath.parseTemplate(data.html);
						}
						else
						{
							myTemplateObj = TrimPath.parseTemplate("");
						}
						if(!data.path || data.path==='')
						{
							data.path = comment_json.path;
						}
						//处理数据
						var result  = myTemplateObj.process(data);

						//输出
						jQuery('script[name='+comment_json.path+']').before(result);
						if(typeof callback!='undefined' && callback!=='' && callback!==null){callback(comment_json,data);}
						getPostInterface(comment_json,function(comment_json,data){
							//检查返回数据是否包含HTML模板
							var myTemplateObj;
							if (data && data.html && data.html!=='')
							{
								myTemplateObj = TrimPath.parseTemplate(data.html);
							}
							else
							{
								myTemplateObj = TrimPath.parseTemplate("");
							}

							if(!data.path || data.path==='')
							{
								data.path = comment_json.path;
							}
							if(!data.title || data.title==='')
							{
								data.title = comment_json.title;
							}
							if(!data.url || data.url==='')
							{
								data.url = comment_json.url;
							}
							//处理数据
							var result  = myTemplateObj.process(data);

							//输出
							jQuery('script[name='+comment_json.path+']').before(result);
							if(typeof callback2!='undefined' && callback2!=='' && callback2!==null){callback2(comment_json,data);}
						});
					});
				}
			}
		}
	}
 }

/**
 * @brief getPostList 读取评论数据
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param callback function 回调函数，读取评论数据成功时调用该回调函数
 *
 * @param:comment_json json 当前函数的传入json参数
 * @param:callback data json 服务器返回的Json数据，用于回调函数的参数
 *
 * @access public
 */
function getPostList(comment_json,callback)
{
  var url;
  if (comment_json.path.match(/^[1][0-9]_+/))
  {
	url = COMMENT_API_URL_ARTICLE;
  }
  else if(comment_json.path.match(/^[2][0-9]_+/))
  {
    url = COMMENT_API_URL_PRODUCT;
  }
  else
  {
	alert("当前类型不能使用JS调用");
	return false;
  }

	var params = {
		action:"getPostList",
		path:comment_json.path,
		url:comment_json.url,
		page:comment_json.page
	};
	var params = comment_json;
	params.action = 'getPostList';

	var tpl_html = window[ 'comment_html_' + comment_json.path];
	if (tpl_html && tpl_html!='')
	{
		params.tpl = '';
	}

  jQuery.getJSON(addJsonCallback(url),params,
	  function(data)
	  {
		if (tpl_html && tpl_html!='')
		{
			data.html = tpl_html;
		}
		else if(data && data.html && data.html!='')
		{
			window[ 'comment_html_' + comment_json.path] = data.html;
		}
		callback(comment_json,data);
	  }
   );
 }

/**
 * @brief getPostInterface 读取评论发表界面
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param callback function 回调函数，读取评论界面成功时调用该回调函数
 *
 * @param:comment_json json 当前函数的传入json参数
 * @param:callback data json 服务器返回的Json数据，用于回调函数的参数
 *
 * @access public
 */
function getPostInterface(comment_json,callback)
{
	if( comment_json.path.substring( 0, 2 ) == '16' )
	{
		return false;
	}
  var url;
  if (comment_json.path.match(/^[1][0-9]_+/))
  {
	url = COMMENT_API_URL_ARTICLE;
  }
  else if(comment_json.path.match(/^[2][0-9]_+/))
  {
    url = COMMENT_API_URL_PRODUCT;
  }
  else
  {
	alert("当前类型不能使用JS调用");
	return false;
  }
	var params = {
		action:"getPostInterface",
		path:comment_json.path,
		url:comment_json.url
	};
	var params = comment_json;
	params.action = 'getPostInterface';
  jQuery.getJSON(addJsonCallback(url),params,
	  function(data)
	  {
		callback(comment_json,data);
	  }
   );
}

/**
 * @brief deletePost 删除评论
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param callback function 回调函数，读取评论删除提交后调用该回调函数
 *
 * @param:comment_json json 当前函数的传入json参数
 * @param:callback data json 服务器返回的Json数据，用于回调函数的参数
 *
 * @access public
 */
function deletePost(comment_json,callback)
{
  var url;
  if (comment_json.path.match(/^[1][0-9]_+/))
  {
	url = COMMENT_API_URL_ARTICLE;
  }
  else if(comment_json.path.match(/^[2][0-9]_+/))
  {
    url = COMMENT_API_URL_PRODUCT;
  }
  else
  {
	alert("当前类型不能使用JS调用");
	return false;
  }
	var params = {
		action:"deletePost",
		path:comment_json.path,
		url:comment_json.url,
		pid:comment_json.pid
	};

  if (typeof callback=='undefined' || callback==='' || callback===null)
  {
	callback = deletePostResult;
  }

  jQuery.getJSON(addJsonCallback(url),params,
	  function(data)
	  {
		
		callback(comment_json,data);
	  }
   );
}

/**
 * @brief deletePostResult 删除评论提示
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param data function 作为回调函数时传入的json数据
 *
 * @access public
 */
function deletePostResult(comment_json,data)
{
	if (data.result)
	{
		alert('Success');
	}
	else
	{
		alert('Faile:' + data.error);
	}
}

/**
 * @brief addPost 发表评论
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param callback function 回调函数，读取评论发表提交后调用该回调函数
 *
 * @param:comment_json json 当前函数的传入json参数
 * @param:callback data json 服务器返回的Json数据，用于回调函数的参数
 *
 * @access public
 */
function addPost(comment_json,callback)
{
  var url;
  if (comment_json.path.match(/^[1][0-9]_+/))
  {
	url = COMMENT_API_URL_ARTICLE;
  }
  else if(comment_json.path.match(/^[2][0-9]_+/))
  {
    url = COMMENT_API_URL_PRODUCT;
  }
  else
  {
	alert("当前类型不能使用JS调用");
	return false;
  }
    comment_json.action = 'addPost';

	var options = {
		url:addJsonCallback(url),
		data:comment_json,
		resetForm:false,
		dataType:'jsonp',
		anchor:comment_json.anchor ? comment_json.anchor : 'comment_form_anchor_'+comment_json.path,
		success:function(data)
		{
			if (typeof callback=='undefined' || callback==='' || callback===null)
			{
				callback = addPostResult;
			}
			callback(comment_json,data);
		}
	};
	if (jQuery("a[name=comment_form_anchor_"+comment_json.path+"]").length<1)
	{
		jQuery("form[id=comment_form_"+comment_json.path+"]").before('<a name="comment_form_anchor_'+comment_json.path+'"></a>');
	}
    jQuery("form[id=comment_form_"+comment_json.path+"]").ajaxSubmit(options);
    return false;
}

/**
 * @brief addPostResult 发表评论提示
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param data function 作为回调函数时传入的json数据
 *
 * @access public
 */
function addPostResult(comment_json,data)
{
	if (data.result)
	{
		jQuery("form[id=comment_form_"+comment_json.path+"]").resetForm();
		alert('提交成功，请刷新页面');
	}
	else
	{
		if (data.message && data.message!='')
		{
			alert(data.message);
		}
		else
		{
			alert('提交失败');
		}
	}
}

/**
 * @brief getPostInfo 获取评论信息
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param data function 作为回调函数时传入的json数据
 *
 * @access public
 */
function getPostInfo(comment_json,callback)
{
  var url;
  if (comment_json.path.match(/^[1][0-9]_+/))
  {
	url = COMMENT_API_URL_ARTICLE;
  }
  else if(comment_json.path.match(/^[2][0-9]_+/))
  {
    url = COMMENT_API_URL_PRODUCT;
  }
  else
  {
	alert("当前类型不能使用JS调用");
	return false;
  }
	var params = {
		action:"getPostInfo",
		path:comment_json.path,
		url:comment_json.url,
		pid:comment_json.pid
	};
  jQuery.getJSON(addJsonCallback(url),params,
	  function(data)
	  {
		callback(comment_json,data);
	  }
   );
}

/**
 * @brief checkUser 检查用户权限
 *
 * @param comment_json Json 包含评论配置的json数据，其中caction表示检查的类型，分为：post和admin
 * @param data function 作为回调函数时传入的json数据
 *
 * @access public
 */
function checkUser(comment_json,callback)
{
  var url;
  if (comment_json.path.match(/^[1][0-9]_+/))
  {
	url = COMMENT_API_URL_ARTICLE;
  }
  else if(comment_json.path.match(/^[2][0-9]_+/))
  {
    url = COMMENT_API_URL_PRODUCT;
  }
  else
  {
	alert("当前类型不能使用JS调用");
	return false;
  }
	var params = {
		action:"checkUser",
		path:comment_json.path,
		url:comment_json.url,
		caction:comment_json.action
	};
  jQuery.getJSON(addJsonCallback(url),params,
	  function(data)
	  {
		callback(comment_json,data);
	  }
   );
}

/**
 * @brief getThreadInfo 获取评论主题信息
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param data function 作为回调函数时传入的json数据
 *
 * @access public
 */
function getThreadInfo(comment_json,callback)
{
  var url;
  if (comment_json.path.match(/^[1][0-9]_+/))
  {
	url = COMMENT_API_URL_ARTICLE;
  }
  else if(comment_json.path.match(/^[2][0-9]_+/))
  {
    url = COMMENT_API_URL_PRODUCT;
  }
  else
  {
	alert("当前类型不能使用JS调用");
	return false;
  }
	var params = {
		action:"getThreadInfo",
		path:comment_json.path,
		url:comment_json.url
	};
  jQuery.getJSON(addJsonCallback(url),params,
	  function(data)
	  {
		callback(comment_json,data);
	  }
   );
}

/**
 * @brief addJsonCallback 生成跨域Ajax调用的网址
 *
 * @param url string 传入的网址
 *
 * @retval string 带有跨域调用参数的url网址
 *
 * @access public
 */
function addJsonCallback(url)
{
	if (url.match(/jsoncallback/))
	{
		return url;
	}
	if (url.match(/\?/))
	{
		url = url + "&jsoncallback=?";
	}
	else
	{
		url = url + "?jsoncallback=?";
	}
	return url;
}

/**
 * @brief getVoteList 读取投票评论数据
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param callback function 回调函数，读取评论数据成功时调用该回调函数
 *
 * @param:comment_json json 当前函数的传入json参数
 * @param:callback data json 服务器返回的Json数据，用于回调函数的参数
 *
 * @access public
 */
function getVoteList(comment_json,callback)
{
  var url;
  if (comment_json.path.match(/^[1][0-9]_+/))
  {
	url = COMMENT_API_URL_ARTICLE;
  }
  else if(comment_json.path.match(/^[2][0-9]_+/))
  {
    url = COMMENT_API_URL_PRODUCT;
  }
  else
  {
	//alert("当前类型不能使用JS调用");
	return false;
  }

	var params = {
		action:"getVoteList",
		path:comment_json.path
	};
	var params = comment_json;
	params.action = 'getVoteList';

  jQuery.getJSON(addJsonCallback(url),params,
	  function(data)
	  {
		callback(comment_json,data);
	  }
   );
 }

/**
 * @brief addVote 投票评论提交
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param callback function 回调函数，读取评论发表提交后调用该回调函数
 *
 * @param:comment_json json 当前函数的传入json参数
 * @param:callback data json 服务器返回的Json数据，用于回调函数的参数
 *
 * @access public
 */
function addVote(comment_json,callback)
{
  var url;
  if (comment_json.path.match(/^[1][0-9]_+/))
  {
	url = COMMENT_API_URL_ARTICLE;
  }
  else if(comment_json.path.match(/^[2][0-9]_+/))
  {
    url = COMMENT_API_URL_PRODUCT;
  }
  else
  {
	alert("当前类型不能使用JS调用");
	return false;
  }
    comment_json.action = 'addVote';

	var options = {
		url:addJsonCallback(url),
		data:comment_json,
		global: false,
		dataType:'jsonp',
		success:function(data)
		{
			if (typeof callback=='undefined' || callback==='' || callback===null)
			{
				callback = addVoteResult;
			}
			callback(comment_json,data);
		}
	};
	//jQuery.ajaxSetup(options);
    jQuery.ajax(options);
    return false;
}

/**
 * @brief addVoteResult 投票评论提交返回提示
 *
 * @param comment_json Json 包含评论配置的json数据
 * @param data function 作为回调函数时传入的json数据
 *
 * @access public
 */
function addVoteResult(comment_json,data)
{
	if (data.result)
	{
		alert('提交成功，请刷新页面');
	}
	else
	{
		if (data.message && data.message!='')
		{
			alert(data.message);
		}
		else
		{
			alert('提交失败');
		}
	}
}

//评论支持、反对
function comment_commit( path, pid, apply )
{
	if ( $.cookie("pk_comment_vote_flag_"+pid) )
	{
		alert('您已经投过票了，谢谢您的参与');
	}
	else
	{
		url = COMMENT_API_URL_ARTICLE;
		var options = {
			url:addJsonCallback(url),
			data:{
					action:'commentvote',
					path:path,
					pid:pid,
					apply:apply
				},
			global: false,
			dataType:'jsonp',
			success:function(data)
			{
				if( apply == 'yes' )
				{
					var vote_num = parseInt( $('#apply_num'+pid).text() ) + 1;
					$('#apply_num'+pid).text( vote_num );
				}
				else
				{
					var vote_num = parseInt( $('#abose_num'+pid).text() ) + 1;
					$('#abose_num'+pid).text( vote_num );
				}
				alert('投票成功，谢谢您的参与');
				$.cookie("pk_comment_vote_flag_"+pid,1,{ expires: 1, path: '/', domain: 'pcw.com.cn'});
			}
		};
		//jQuery.ajaxSetup(options);
		jQuery.ajax(options);
		return false;
	}
}
//获取点评信息的回复数
function flushReplyNum( path, pid )
{
	var url = 'http://comment.pcw.com.cn/api/ListPostNeed?path='+path+'&pid='+pid+'&floor=2&jsoncallback=?';
	$.getJSON(
		url,
		function( comments ){
			if(comments.num)$("#reply_num"+pid).text( comments.list[0].reply_num );
		}
	);
}

//获取鲜花鸡蛋数目
function getYesAndNo( path, pids )
{
	var url = 'http://comment.pcw.com.cn/api/?action=getYesAndNo&path='+path+'&pids='+pids+'&jsoncallback=?';
	$.getJSON(
		url,
		function( comments ){
			$.each( comments.list, function(i,comment)
				{
					$("#apply_num"+comment.pid).text( comment.yes );
					$("#abose_num"+comment.pid).text( comment.no );
				}
			);
		}
	);
}