/**
*
* This is for 91 comment system (http://pz.91.com/)
*
* @package comment
* @author JJC <min43@163.com,jjc3901537@gmail.com>
* @version $Id: get_comment.js,v 1.0 2008.6.17 11:31:05 JJC Exp $
* @copyright (c) 2007 TM WEBSOFT INC
* @license http://opensource.org/licenses/gpl-license.JavaScript GNU Public License
*
*/
var doc = document;
/*
try {
	try {
		if (IsIE && netscape.security.PrivilegeManager.enablePrivilege) {
			netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
		}
	} catch (e) {
		alert(e);
	}
} catch(e) {
	alert(e);
}
*/
// function: get param from url
function getParameter(url, paramName)
{
	var re = new RegExp("(^|\\?|&)"+ paramName+"=([^&]*)(\s|&|$)", "i");

	if (re.test(url))
		return unescape(RegExp.$2);
	else
		return "";
}

// function: get comment path from url
function getPathByUrl(path)
{
	if(path.length == 0) return;
	re = new RegExp("/html/([0-9]{4}/[0-9]{2}/[0-9]{2})/([0-9]{1,})/index\.htm*", "gi");
	re.compile("/html/([0-9]{4}/[0-9]{1,2}/[0-9]{2})/([0-9]{1,})/index\.htm*", "gi");
	arr = re.exec(path);
	re = null;
	if (arr == null || arr == "") return;
	return arr;
}

// function: get comment info
function getcommentinfo_default()
{
	if(userList.length <= 0 || conList.length <= 0) return;

	var listnum = parseInt(comment_js_shownum);
	if(listnum == NaN || listnum < 1)
		listnum = userList.length;
	else
		listnum = (listnum > userList.length) ? userList.length : listnum;

	var str = "";
	for(i=0; i<listnum; i++)
	{
		str += "<table cellspacing=0 cellpadding=0 border=0 class=comm_js><tr><td class=comm_js_title>     "+userList[i]+"  said          Time: "+timeList[i]+"  </td></tr><tr><td class=comm_js_content><div>  "+conList[i]+"</div><br></td></tr></table>";
	}

	if(commentsContainer != null) commentsContainer.innerHTML = str;
}

// function: show comment info
function showComments()
{
	if(typeof(userList) == "undefined")
	{
		setTimeout("showComments()", 200);
	}
	else
	{
		if(typeof(getcommentinfo) == "function")
		{
			getcommentinfo();
		}
		else
		{
			getcommentinfo_default();
		}
	}
}

// function: do digg
function DoDigg(id)
{
	var url = "http://pz.91.com/ajax/getajaxinfo.php";
	var pars = "UseScript=1&Work=digg&id="+id+"&rid="+comment_id+"&path="+comment_path+"&template="+comment_template+"&page="+comment_page;

	var x = new Ajax('statusid', 'HTML');
	x.get(url + "?" + pars + "&time="+new Date().getTime(), function(s) {
		responseDiggInfo(s);
	});
}
// function: do digg call back
function responseDiggInfo(responseText)
{
	var arr = responseText.split("*");
	
	switch(arr[0])
	{
		case "-1":
			alert("Sorry, this comment doesn't support digg operation!");
		  break;
		case "0":
			window.location.reload();
		  break;
		case "2":
			alert("Sorry, you have digged this comment.");
		  break;
		case "3":
			alert("Parameter error, please try again.");
		  break;
		case "1":
			alert("OK, thank you for your participation.");
			if (arr[1].indexOf(",") == -1)	// return single id
			{
				doc.getElementById('diggnum_'+arr[1]).innerHTML = parseInt(doc.getElementById('diggnum_'+arr[1]).innerHTML)+1;
			}
		  break;
		default:
			alert("Sorry, an error occurred, please contact the administrators!");
		  break;
	}
	arr = null;
}

// function: do bury
function DoBury(id)
{
	var url = "http://pz.91.com/ajax/getajaxinfo.php";
	var pars = "UseScript=1&Work=bury&id="+id+"&rid="+comment_id+"&path="+comment_path+"&template="+comment_template+"&page="+comment_page;

	var x = new Ajax('statusid', 'HTML');
	x.get(url + "?" + pars + "&time="+new Date().getTime(), function(s) {
		responseBuryInfo(s);
	});
}
// function: do bury call back
function responseBuryInfo(responseText)
{
	var arr = responseText.split("*");
		
	switch(arr[0])
	{
		case "-1":
			alert("Sorry, this comment doesn't support digg operation!");
		  break;
		case "0":
			window.location.reload();
		  break;
		case "2":
			alert("Sorry, you have digged this comment.");
		  break;
		case "3":
			alert("Parameter error, please try again.");
		  break;
		case "1":
			alert("OK, thank you for your participation.");
			if (arr[1].indexOf(",") == -1)// 返回单一ID
			{
				doc.getElementById('burynum_'+arr[1]).innerHTML = parseInt(doc.getElementById('burynum_'+arr[1]).innerHTML)+1;
			}
			arr1 = null;
		  break;
		default:
			alert("Sorry, an error occurred, please contact the administrators!");
		  break;
	}
	arr = null;
}

function ReloadCommentInfo()
{
	userlist = [];
	var head = doc.getElementsByTagName("head")[0] || doc.documentElement;
	var srcObj = doc.createElement('script');
	try{
		if(comment_js_charset) srcObj.setAttribute('charset', comment_js_charset);
		srcObj.setAttribute('src', comment_js_src + "&time="+new Date().getTime());
		head.appendChild(srcObj);
	}catch(e){
		try{
			if(comment_js_charset) srcObj.src = comment_js_charset;
			srcObj.src = comment_js_src + "&time="+new Date().getTime();
			head.appendChild(srcObj);
			head.removeChild(srcObj);
		}
		catch(e){
		}
	}

	setTimeout("showComments()", 2000);
}

// vars for comment
var commentsContainer = doc.getElementById('commentinfoDiv');
var getCommentJsSrc = doc.getElementById('getCommentJs').src;	// eg. src = http://pz.91.com/get_comment.js?comment_url=http://pz.91.com/html/2007/39/25/144/index.htm&comment_num=5
var comment_js_refurl = getParameter(getCommentJsSrc, "comment_url");
var comment_js_shownum = getParameter(getCommentJsSrc, "comment_num");
var comment_js_charset = getParameter(getCommentJsSrc, "comment_charset") || '';
var temp_charset = comment_js_charset ? " charset=\"" + comment_js_charset + "\"" : "";
var comment_js_getnum = comment_js_shownum;
var comment_js_src = "http://comment.pandaapp.com/port/getcomment_info.php?comment=" + comment_js_refurl + "&num=" + comment_js_getnum;
var comment_js_sript = "<scr"+"ipt language=\"javascript\" src=\"" + comment_js_src + "&time="+new Date().getTime() + "\"" + temp_charset + "></scr"+"ipt>";

// vars for digg
var pathArr = getPathByUrl(comment_js_refurl);
var comment_path = pathArr[1]+"/";
var comment_id   = pathArr[2];
var comment_page = 20;
var comment_template = 1;

// Init comment info
ReloadCommentInfo();