﻿/**
*
* This is for MMOSite comment system (http://www.mmosite.com/)
*
* @package comment
* @author JJC <min43@163.com、jjc3901537@gmail.com>
* @version $Id: get_comment.js,v 1.0 2007.11.20 10:21:05 JJC Exp $
* @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($) != "function")
{
	$ = function(id){
		return doc.getElementById(id);
	}
}

// function: craete element
if(typeof($C) != "function")
{
	$C = 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: get comment info
function getcommentinfo(userList,conList,timeList,infoidList,diggList,buryList,fl)
{
	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/newpop/";
	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\"><p class=\"floor\">#"+fl[i]+"</p><a href=\"javascript:quote("+infoidList[i]+")\" class=\"quote\">Quote</a><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]+"\">"+diggList[i]+"</span>&nbsp;&nbsp;<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]+"\">"+buryList[i]+"</span>&nbsp;&nbsp;&nbsp;<\/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>&nbsp;&nbsp;"+timeList[i]+"<\/p><div style=\"clear:both\"><\/div><\/div>";
	}
	if(commentsContainer != null) commentsContainer.innerHTML = str;
}
// function: show comment info
function showComments(userList,conList,timeList,infoidList,diggList,buryList,fl)
{
	if(typeof(userList) == "undefined")
	{
		setTimeout("showComments(userList,conList,timeList,infoidList,diggList,buryList,fl)", 200);
	}
	else
	{
		/*for(var i = 0;i<userList.length;i++){
			alert(userList[i]);
		}*/
		getcommentinfo(userList,conList,timeList,infoidList,diggList,buryList,fl);
	}
}

// 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($('ScriptDoDigg')) return;
	var srcObj = $C('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 = $('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
			{
				$('diggnum_'+arr[1]).innerHTML = parseInt($('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($('ScriptDoDigg')) return;
	var srcObj = $C('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 = $('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
			{
				$('burynum_'+arr[1]).innerHTML = parseInt($('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 = $('commentinfoDiv');
var getCommentJsSrc = $('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_order.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(userList,conList,timeList,infoidList,diggList,buryList,fl)", 2000);

function ReloadCommentInfo()
{
	//userlist = [];
	/*infoidList = null;
	diggList = null;
	buryList = null;
	userList = null;
	conList = null;
	timeList = null; 
	ipList = null;
	if(typeof(userList) == 'undefined'){
		alert('undefind');
	}else{
		alert(userList);
	}*/
	
	var srcObj = $C('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(userList,conList,timeList,infoidList,diggList,buryList,fl)", 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);
 }