/*
 * created by hobo
 */

try
{
	document.domain = "qq.com";
}
catch (e)
{
}

var Comment = new Object();

Comment.Configure = {
	version: "1.0",
	newline: "\n",
	index_domain: "http://comment1.qq.com/"
};

Comment.ContentFormat = {
	decode: function(content)
	{
		return content.split(Comment.Configure.newline);
	}
};

Comment.UrlFactory = {
	commentUrlFormatStr: Comment.Configure.index_domain + "{0}/normal/{1}/{2}.htm{3}",
	replySumUrlFormatStr: Comment.Configure.index_domain + "{0}/normal/{1}/reply/{2}/sum.htm{3}",

	getCacheString: function(cache)
	{
		return cache ? "?cache="+Math.random() : "";
	},

	getCommentDir: function(id)
	{
		var path = parseInt(id / 1000000) + '/';
		path += parseInt(id / 1000) + '/';
		path += id / 1;
		return path;
	},

	getReplyDir: function(key)
	{
		try
		{
			var path = key.substr(0, 8);
			path += '/' + (key.substr(8, 4));
			path += '/' + (key.substr(12, 2));
			path += '/' + key;
			return path;
		}
		catch (e)
		{
			return null;
		}
	},

	getCommentSumUrl: function(site, id, nocache)
	{
		return String.format(this.commentUrlFormatStr,
			site, this.getCommentDir(id), "sum", this.getCacheString(nocache));
	},

	getReplySumUrl: function(site, id, key, nocache)
	{
		return String.format(this.replySumUrlFormatStr,
			site, this.getCommentDir(id), this.getReplyDir(key), this.getCacheString(nocache));
	}
};

Comment.DataObjectFactory = function(content, model)
{
	content = Comment.ContentFormat.decode(content);
	var dataModel = Comment.DataObject[model];
	var dataObject = {};

	for (var i=0; i<dataModel.length; i++)
	{
		dataObject[dataModel[i]] = content[i];
	}

	return dataObject;
};

Comment.DataObject = {
	"sum": [
		"origin_count",
		"total_count",
		"top_count"
	],
	'pksum': [
		'agree',
		'disagree',
		'middle'
	]
};

Comment.Proxy = {
	printCommentSum: function(site, id, container)
	{
		var commentSumUrl = Comment.UrlFactory.getCommentSumUrl(site, id, true);
		ProxyLoader.load(commentSumUrl, {
			onSuccess: function(xmlHttp)
			{
				$(container).innerHTML = Comment.DataObjectFactory(xmlHttp.responseText, 'sum')['total_count'];
			},
			onFailure: function()
			{
				$(container).innerHTML = 0;
			}
		});
	},

	printReplySum: function(site, id, key, container)
	{
		var commentReplyUrl = Comment.UrlFactory.getReplySumUrl(site, id, key, true);
		ProxyLoader.load(commentReplyUrl, {
			onSuccess: function(xmlHttp)
			{
				$(container).innerHTML = Comment.DataObjectFactory(xmlHttp.responseText, 'pksum')['agree'];
			},
			onFailure: function()
			{
				$(container).innerHTML = 0;
			}
		});
	}
};

var Up = new Object();

Up.init = function(config)
{
	$("post_form").action = "http://input.comment.qq.com/post.cmt";
	$("post_form").c_site.value = config.site;
	$("post_form").c_id.value = config.id;
	$("post_form").r_key.value = config.key;
	$("post_form").r_type.value = 3;

	Comment.Proxy.printReplySum(config.site, config.id, config.key, config.sum_container);

	$(config.btn_container).innerHTML = "<a href='javascript:;' onclick='Up.up()'>" + 
		config.btn_text+"</a>";
};

Up.up = function()
{
	$("post_form").submit();
	$(config.btn_container).innerHTML = config.btn_done_text;

};

Up.done = function()
{
	$(config.sum_container).innerHTML = parseInt($(config.sum_container).innerHTML) + 1;
};

Comment.Post = {
	callback: function(url)
	{
		$('post_async').src = 'about:blank';
		var code = new UrlParser(url).getParam("code");
		if (code == "-")
		{
			code = -1;
		}
		this.doAlert(parseInt(code));
	},
		
	doAlert: function(code)
	{
		var errMsg = '';

		switch (code)
		{
			case -1:
				errMsg = '已提交审核，感谢您的参与';
				break;
			case 0:
				Up.done();
				break;
			case 1:
				errMsg = '很抱歉，您填写的内容不完整，请重新输入您的内容';
				break;
			case 2:
				errMsg = '很抱歉，您尚未登录或登录已经过期，请先登录';
				break;
			case 3:
				errMsg = '您的评论已提交审核，感谢您的参与';
				break;
			case 4:
				errMsg = '您的IP地址已暂时被屏蔽';
				break;
			case 5:
				errMsg = '您的QQ号已暂时禁止在本评论系统发言';
				break;
			case 6:
				errMsg = '很抱歉，您的QQ号码和密码不匹配';
				break;
			case 7:
				errMsg = '很抱歉，系统繁忙，请稍候再试';
				break;
			case 8:
				errMsg = '很抱歉，您发表的频率太快，请稍后再试';
				break;
			case 9:
				errMsg = '很抱歉，您的验证码输入错误';
				break;
			case 10:
				Up.done();
				break;
		}

		if (errMsg != '')
		{
			//alert(errMsg);
		}
	}
};