/**
*
* This is for MMOSite comment system (http://www.mmosite.com/)
*
* @package comment
* @author JJC <min43@163.com銆乯jc3901537@gmail.com>
* @version $XId: get_comment.js,v 1.0 2007.11.20 10:21:05 JJC Exp $X
* @copyright (c) 2007 TM WEBSOFT INC
* @license http://opensource.org/licenses/gpl-license.JavaScript GNU Public License
*
*/

var doc = document;

// function: get element
if(typeof($X) != "function")
{
	$X = function(id){
		return doc.getElementById(id);
	}
}

// function: craete element
if(typeof($XC) != "function")
{
	$XC = function(id){
		return doc.createElement(id);
	}
}

// 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\.shtml*", "gi");
	arr = re.exec(path);
	re = null;
	if (arr == null || arr == "") return;
	return arr;
}

function hel(){alert('ss');}

// function: get comment info
function getcommentinfo()
{
	try{
		var tc = document.getElementById('total_con');
		if(tc) tc.innerHTML = "<font color=\"white\">"+total_con+" </font>";
	}catch(e){}

	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 = "";
	var imgurl = "http://images.mmosite.com/www/images/darknewpop/";
	for(i=0; i<listnum; i++)
	{
		str += "<table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"13\" height=\"15\"><img src=\""+ imgurl +"pop_01.gif\" width=\"13\" height=\"15\"><\/td><td background=\""+ imgurl +"pop_02.gif\"><\/td><td width=\"15\"><img src=\""+ imgurl +"pop_04.gif\" width=\"15\" height=\"15\"><\/td><\/tr><tr><td valign=\"top\" background=\""+ imgurl +"pop_09.jpg\"><img src=\""+ imgurl +"pop_05.gif\" width=\"13\" height=\"69\"><\/td><td height=\"50\" background=\""+ imgurl +"pop_06.gif\"  valign=\"top\" class=\"comm_js_bg\"><div class=\"comm_js_conten\"><div class=\"comm_dig\"><a title=\"Digg\" href=\"#notop\" onClick=\"DoDigg('"+infoidList[i]+"');\"><img alt=\"digg\" title=\"digg\" src=\"http://images.mmosite.com/comment/digg.gif\" align=\"absmiddle\" border=\"0\"></a> <span id=\"diggnum_"+infoidList[i]+"\" style=\"font-weight:bold; color:#FF3300;\">"+diggList[i]+"</span><a title=\"Bury\" href=\"#notop\" onClick=\"DoBury('"+infoidList[i]+"');\"><img alt=\"bury\" title=\"bury\" src=\"http://images.mmosite.com/comment/bury.gif\" align=\"absmiddle\" border=\"0\"></a> <span id=\"burynum_"+infoidList[i]+"\" style=\"font-weight:bold; color:#FF3300;\">"+buryList[i]+"</span><\/div>"+conList[i]+"<\/div><\/td><td valign=\"top\" background=\""+ imgurl +"pop_10.jpg\"><img src=\""+ imgurl +"pop_08.gif\" width=\"15\" height=\"69\"><\/td><\/tr><tr><td valign=\"top\"><img src=\""+ imgurl +"pop_11.gif\" width=\"13\" height=\"26\"><\/td><td background=\""+ imgurl +"pop_14.gif\"><img src=\""+ imgurl +"pop_12.gif\" width=\"54\" height=\"26\"><\/td><td><img src=\""+ imgurl +"pop_16.gif\" width=\"15\" height=\"26\"><\/td><\/tr><\/table><div class=\"comm_js_title\"><em><span>"+userList[i]+" <\/span> said<\/em> <p>"+timeList[i]+"<\/p><div style=\"clear:both\"><\/div><\/div>";
	}

	if(commentsContainer != null) commentsContainer.innerHTML = str;
}
// function: show comment info
function showComments()
{
	if(typeof(userList) == "undefined")
	{
		setTimeout("showComments()", 200);
	}
	else
	{
		getcommentinfo();
	}
}

// function: do digg
function DoDigg(id)
{
	var url = "http://comment.mmosite.com/ajax/getajaxinfo.php";
	var pars = "UseScript=1&Work=digg&id="+id+"&rid="+comment_id+"&path="+comment_path+"&template="+comment_template+"&page="+comment_page;
	
	if($X('ScriptDoDigg')) return;
	var srcObj = $XC('script');
	srcObj.id = 'ScriptDoDigg';
	
	try{
		srcObj.setAttribute('src', url + "?" + pars + "&time="+new Date().getTime());
		doc.body.appendChild(srcObj);
	}catch(e){
		try{
			srcObj.src = url + "?" + pars + "&time="+new Date().getTime();
			doc.body.appendChild(srcObj);
		}
		catch(e){
		}
	}
}
// function: do digg call back
function responseDiggInfo(responseText)
{
	var srcObj = $X('ScriptDoDigg');
	if(srcObj) srcObj.parentNode.removeChild(srcObj);
	
	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
			{
				$X('diggnum_'+arr[1]).innerHTML = parseInt($X('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://comment.mmosite.com/ajax/getajaxinfo.php";
	var pars = "UseScript=1&Work=bury&id="+id+"&rid="+comment_id+"&path="+comment_path+"&template="+comment_template+"&page="+comment_page;
	
	if($X('ScriptDoDigg')) return;
	var srcObj = $XC('script');
	srcObj.id = 'ScriptDoDigg';
	
	try{
		srcObj.setAttribute('src', url + "?" + pars + "&time="+new Date().getTime());
		doc.body.appendChild(srcObj);
	}catch(e){
		try{
			srcObj.src = url + "?" + pars + "&time="+new Date().getTime();
			doc.body.appendChild(srcObj);
		}
		catch(e){
		}
	}
}
// function: do bury call back
function responseBuryInfo(responseText)
{
	var srcObj = $X('ScriptDoDigg');
	if(srcObj) srcObj.parentNode.removeChild(srcObj);
	
	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
			{
				$X('burynum_'+arr[1]).innerHTML = parseInt($X('burynum_'+arr[1]).innerHTML)+1;
			}
			arr1 = null;
		  break;
		default:
			alert("Sorry, an error occurred, please contact the administrators!");
		  break;
	}
	arr = null;
}

// vars for comment
var commentsContainer = $X('commentinfoDiv');
var getCommentJsSrc = $X('getCommentJs').src;	// eg. src = http://comment.mmosite.com/get_comment.js?comment_url=http://comment.mmosite.com/html/2007/39/25/144/index.shtml&comment_num=5
var comment_js_refurl = getParameter(getCommentJsSrc, "comment_url");
var comment_js_shownum = getParameter(getCommentJsSrc, "comment_num");
var comment_js_getnum = comment_js_shownum;
var comment_js_src = "http://comment.mmosite.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() + "\"></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
doc.write(comment_js_sript);
setTimeout("showComments()", 2000);

function ReloadCommentInfo()
{
	
	var srcObj = $XC('script');
	if(arguments.length == 0){
		try{
			srcObj.setAttribute('src', comment_js_src + "&time="+new Date().getTime());
			doc.body.appendChild(srcObj);
		}catch(e){
			try{
				srcObj.src = comment_js_src + "&time="+new Date().getTime();
				doc.body.appendChild(srcObj);
			}
			catch(e){
			}
		}
	}else{
		try{
			srcObj.setAttribute('src', arguments[0] + "&time="+new Date().getTime());
			doc.body.appendChild(srcObj);
		}catch(e){
			try{
				srcObj.src = arguments[0] + "&time="+new Date().getTime();
				doc.body.appendChild(srcObj);
			}
			catch(e){
			}
		}
	}


	setTimeout("showComments()", 2000);
}

function orderBy(ord){
	//var order = getParameter(getCommentJsSrc,"orderBy");
	switch(ord){
		case "0":
			var srcStr = "http://comment.mmosite.com/port/getcomment_info_order.php?comment=" + comment_js_refurl + "&num=" + comment_js_getnum;
			break;
		case "1":
			var srcStr = "http://comment.mmosite.com/port/getcomment_info_order.php?comment=" + comment_js_refurl + "&num=" + comment_js_getnum + "&orderBy=hottest";
			break;
		case "2":
			var srcStr = "http://comment.mmosite.com/port/getcomment_info_order.php?comment=" + comment_js_refurl + "&num=" + comment_js_getnum + "&orderBy=buried";
			break;

	}
	//alert(srcStr);
	ReloadCommentInfo(srcStr);
 }