

var Request = new Object();

Request.send = function(url, method, callback, data) {	
	var req;	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status < 400) {
				(method=="POST") ? callback(req) : callback(req,data);
			} else {
				//alert("Error :\n" + req.status+ "/" + req.statusText);
			}
		}
	}
	if (method=="POST") {
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(data);
	} else {
		req.open("GET", url, true);
		req.send(null);
	}
	
	return req;
}

Request.sendPOST = function(url, data, callback) {
	Request.send(url, "POST", callback, data);
}
Request.sendGET = function(url, callback, args) {
	return Request.send(url, "GET", callback, args);
}

UpdatePosts = function(url, size, otimestamp, oPostsTable, defaultIconSrc) {
	url += "&t=" + Math.random();
	
	function update(response) {
		var rootNode = response.responseXML.documentElement;	
		var items = rootNode.getElementsByTagName("item");
		var timestamp = rootNode.getElementsByTagName("ccs:timestamp")[0];
		try {
			timestamp = (timestamp.firstChild) ? new Date( timestamp.firstChild.nodeValue ) : new Date();
		}
		catch(e) {
			timestamp = new Date();
		}

		if (PopupManager && popupManager == null)
		{
			try{
				popupManager = new PopupManager();
				window.setInterval("popupManager.Heartbeat();", 1500);	// 更新
			}
			catch(e)
			{
			}
		}

		var lastUpdate = otimestamp.getAttribute("lastUpdate");
		if (lastUpdate == null)
		{
			lastUpdate = new Date(otimestamp.innerText.replace("-","/"));
		}
		
		for(var i=0; i<items.length; i++)
		{
			var cell;
			var item = items[i];
			var row = oPostsTable.rows[i];
			if (row != null)
			{
				var subject = item.getElementsByTagName("title")[0];
				subject = (subject.firstChild) ? subject.firstChild.nodeValue : '';

				var postLink = item.getElementsByTagName("link")[0];
				postLink = (postLink.firstChild) ? postLink.firstChild.nodeValue : '';

				var pubDate = item.getElementsByTagName("pubDate")[0];
				try {
					pubDate = (pubDate.firstChild) ? new Date( pubDate.firstChild.nodeValue ) : new Date();
				}
				catch(e) {
					pubDate = new Date();
				}

				var section = item.getElementsByTagName("ccs:section")[0];
				var forumicon = section ? section.getAttribute("forumicon") : "";
				if (forumicon == null)
					forumicon = defaultIconSrc;

				var forumurl = section ? section.getAttribute("forumurl") : "http://www.communityserver.cn";
				var sectionid = section ? section.getAttribute("sectionid") : -1;
				var sectionname = (section.firstChild) ? section.firstChild.nodeValue : '';

				var authorNode = item.getElementsByTagName("ccs:author")[0];
				var userid = authorNode ? authorNode.getAttribute("userid") : "";
				var profilelink = authorNode ? authorNode.getAttribute("profilelink") : "";
				var author = (authorNode.firstChild) ? authorNode.firstChild.nodeValue : '';

				var description = item.getElementsByTagName("description")[0];
				description = (description.firstChild) ? description.firstChild.nodeValue : '';
				description = description.replace(/<br>/ig, "\n");
				description = description.replace(/<br \/>/ig, "\n");
				description = description.replace(/<p>/ig, "\n");
				description = description.replace(/<\/p>/ig, "");
				description = GetText(description);
				if (description.length > 200)
				{
					description = description.substr(0, 200) + "...";
				}
				
				if (popupManager != null && lastUpdate != null && pubDate - lastUpdate > 0)
				{
					if ( !otimestamp.getAttribute("lastUpdate") )
					{
						otimestamp.setAttribute("lastUpdate", pubDate);
					}

					if (otimestamp.getAttribute("lastUpdate") < pubDate )
					{
						otimestamp.setAttribute("lastUpdate", pubDate);
					}

					p = popupManager.AddPopup("popupWin", description, "images/popup_icon_Post.gif", subject, func);
					p.postUrl = postLink;

					function func(popup)
					{
						window.open(popup.postUrl);
					}
				}

				cell = row.cells[0];
				oforumIcon = cell.getElementsByTagName("img")[0];
				if (oforumIcon != null && oforumIcon.src != forumicon)
					oforumIcon.src = forumicon;
				osectionlink = cell.getElementsByTagName("a")[0];
				if (osectionlink != null)
				{
					osectionlink.href = forumurl;
					osectionlink.innerText = sectionname;
				}

				cell = row.cells[1];
				opostlink = cell.getElementsByTagName("a")[0];
				if (opostlink != null)
				{
					opostlink.href = postLink;
					opostlink.innerHTML = subject;
					opostlink.title = description.replace("<br>", "\n");
				}

				cell = row.cells[2];
				cell.innerHTML = "<nobr>" + GetDateTime(pubDate, false) + "</nobr>";

				cell = row.cells[3];
				oauthorlink = cell.getElementsByTagName("a")[0];
				if (oauthorlink != null)
				{
					oauthorlink.href = profilelink;
					oauthorlink.innerHTML = author;
				}
			}
		}

		if (otimestamp != null)
		{
			otimestamp.className = "TimeStamp";
			otimestamp.disabled = false;
			if (otimestamp.children[1] != null)
				otimestamp.children[1].innerText = GetDateTime(timestamp, true);
		}
	}	

	if (otimestamp != null)
	{
		otimestamp.disabled = true;
		otimestamp.className = "TimeStampLoading";
	}

	if (oPostsTable != null)
		Request.sendGET(url, update, oPostsTable);

	function GetDateTime(d, l)
	{
		var s = ""
		if (l)
			s += d.getYear() + "-";
		s += (d.getMonth() + 1) + "-" 
		s += d.getDate() + " "
		s += d.getHours() + ":"
		s += d.getMinutes()
		if (l)
			s += ":" + d.getSeconds();
		return s;
	}
	
	function GetText(html)
	{
		var d = document.createElement("DIV");
		d.innerHTML = html;
		return d.innerText;
	}
}

var popupManager = null;
