/**
 * 和讯新闻评论系统javascript文件
 * 
 * @Author: WangChao
 * @Date: 2007-11
 */

/**
 * 时间倒计时函数
 */
function showTime() {
	var time;
	var timer = $("comment_timer");
	if(timer){
		var value = timer.value;
		value--;
		if(value < 1)
			value = 0;
		timer.value = value;
	}
	setTimeout("showTime()", 1000);
}

/**
 * 支持
 */
function comment_support(id){
	comment_vote(id, "sp");
}

/**
 * 反对
 */
function comment_oppose(id){
	comment_vote(id, "op");
}

/**
 * 发送支持和反对请求，新
 */
function comment_vote(id, type) {
	var server = "http://" + document.domain + ":" + document.location.port;
	var page = "/service/" + type + "comment.action?commentId=" + id;
	var url = server + page;
	var timer = $("comment_timer");
	var lastid = $("comment_lastid");
	if(timer && timer.value > 0 && lastid && lastid.value==id){
		alert("请不要重复操作，请稍后再试");
		return;
	}
	timer.value = 5;
	lastid.value = id;
	
	var ajax = new Ajax.Request(
	url,
	{
		method: 'get',
		onFailure: function(originalRequest){alert("操作失败，请稍候再试");},
		onSuccess: function(originalRequest){comment_change_count(id, type);}
	});
}

/**
 * 发表后更新支持和反对数
 */
function comment_change_count(id, type){
	var count = $(type + "count_" + id);
	if(count){
		count.innerHTML = Number(count.innerHTML) + 1;
	}
	alert ("感谢您的参与");
}

function debate_vote(id, type) {
	var server = "http://" + document.domain + ":" + document.location.port;
	var page = "/service/" + type + "comment.action?commentId=" + id;
	var url = server + page;
	var timer = $("comment_timer");
	var lastid = $("comment_lastid");
	if(timer && timer.value > 0 && lastid && lastid.value==id){
		alert("请不要重复操作，请稍后再试");
		return;
	}
	timer.value = 5;
	lastid.value = id;
	
	var ajax = new Ajax.Request(
	url,
	{
		method: 'get',
		onFailure: function(originalRequest){alert("操作失败，请稍候再试");},
		onSuccess: function(originalRequest){debate_change_count(id, type);}
	});
}

function topic_support(id){
	topic_vote(id, "sp");
}

function topic_oppose(id){
	topic_vote(id, "op");
}

function topic_vote(id, type) {
	var server = "http://" + document.domain + ":" + document.location.port;
	var page = "/service/" + type + "topic.action?topicId=" + id;
	var url = server + page;
	alert(url);
	var ajax = new Ajax.Request(
	url,
	{
		method: 'get',
		onFailure: function(originalRequest){alert("操作失败，请稍候再试");},
		onSuccess: function(originalRequest){topic_change_count(originalRequest, id, type);}
	});
}

function topic_change_count(originalRequest, id, type){
	var typecount = $(type + "count");
	var count = $("count");
	var result = originalRequest.responseXML;
	var tc = result.getElementsByTagName("response")[0].getElementsByTagName("result")[0].firstChild.nodeValue;
	var c = result.getElementsByTagName("response")[0].getElementsByTagName("count")[0].firstChild.nodeValue;
	if(typecount){
		typecount.innerHTML = Number(tc);
		count.innerHTML = Number(c);
	}
	alert ("感谢您的参与");
}