/*
 * Get the latest 'n' Weblogs pings from the PingServer.
 */
function getRecentPings()
{
	var request = newXmlHttpRequest();
	request.onreadystatechange = getReadyStateXmlHandler(request, 
		updateBlogPingList);
		
	var d = new Date();
	// The second querystring parameter changes in the following request with 
	// time, to defeat the IE caching.
	request.open("GET", "/pingservice?action=weblogsping&1="+d.getTime(), true);
	request.send(null);
}

var blogScrollingTimer = null;
var blogRefresherTimer = null;

/*
 * Update the Blog ping list.
 */
function updateBlogPingList(xmlResponse)
{
	var pings = xmlResponse.getElementsByTagName("weblog");

	if (pings.length > 0)
	{
		var blogPingList = new Array();
		
		for (var i = 0; i < pings.length; i++)
		{
			var ping = pings[i];
			var localDate = new Date();
			var when = new Date((parseInt(ping.getAttribute("receivedOn"), 10))+(localDate.getTimezoneOffset()*60000));
			var hour = when.getHours();
			var ampm = hour > 12 ? "PM" : "AM";
			var minutes = when.getMinutes();
			hour = (hour % 12 == 0 ? 12 : hour % 12);

			var blogPingTime = hour + ":" + 
				(minutes < 10 ? "0" + minutes : minutes) +  " " + ampm;
			
			var blogName = ping.getAttribute("name");
			if (blogName.length > 70)
			{
				blogName = blogName.substr(0, 67) + "...";
			}

			var blogUrl = ping.getAttribute("url");
			
			var rssUrl = ping.getAttribute("rssUrl");
			
			var rssUrlHtml = (rssUrl != null && rssUrl.match("http://.+")) ?
				"<a href=\"" + encodeURI(ping.getAttribute("rssUrl")) + 
					"\" target=\"_blank\"><img src=\"/images/feed_icon.png\" title=\"XML feed\" alt=\"XML feed\" border=\"0\"></a>" :
				"<img src=\"/images/no_feed_icon.png\" title= \"No XML feed\" alt=\"No XML feed\" border=\"0\">";
				
			var pingAsHtml = "<table class=\"blog\"><tr>" +
					"<td class=\"blogName\">" +
	                    "<a class=\"pingLink\" href=\"" + encodeURI(blogUrl) + 
	                    	"\">" + blogName.escapeHTML() +
	                   	"</a>" + 
	                "</td>" +
	                "<td class=\"blogPingTime\">" +
	                    blogPingTime +
	                "</td>" +
	                "<td>" + 
                        rssUrlHtml +
	                "</td>" +
	            "</tr></table>";

			blogPingList.push(pingAsHtml);
		}

		blogListViewPort.setBackingList(blogPingList);
		
		if (blogScrollingTimer != null)
		{
			clearInterval(blogScrollingTimer);
		}
		
		blogScrollingTimer = setInterval("blogListViewPort.scrollList()", 2000);
	}
	
	blogRefresherTimer = setTimeout("getRecentPings()", 60 * 1000); // get the next batch in 1 min.
}

var blogTabEventHandlers = {
	'a#scrollingListImageLink' : function(el)
	{
		el.onclick = function()
		{
			$('blogList').style.display = 'block';
            $('pingform').style.display = 'none'; 
            $('changeLists').style.display = 'none'; 
            return false;
		}
	},
	'a#sendAPingImageLink' : function(el)
	{
		el.onclick = function()
		{
			$('blogList').style.display = 'none';
            $('pingform').style.display = 'block'; 
            $('changeLists').style.display = 'none'; 
            return false;
		}
	},
	'a#changeListImageLink' : function(el)
	{
		el.onclick = function()
		{
			$('blogList').style.display = 'none';
            $('pingform').style.display = 'none'; 
            $('changeLists').style.display = 'block'; 
            return false;
		}
	},
	'#close' : function(el)
	{
		el.onclick = function()
		{
			new Effect.Fade($('changeLists'));
		}
	},
	'body' : function(el)
	{
		el.onunload += function()
		{
			if (blogScrollingTimer != null)
			{
				clearInterval(blogScrollingTimer);
			}
			if (blogRefresherTimer != null)
			{
				clearTimeout(blogRefresherTimer);
			}
		}
	}
};

Behaviour.register(blogTabEventHandlers);
