/*
 * Get the latest 'n' Weblogs pings from the PingServer.
 */
function getRecentPings()
{
	var localDate = new Date();
	var request = newXmlHttpRequest();
	request.onreadystatechange = getReadyStateXmlHandler(request, 
		updateBlogPingList);
	request.open("GET", "http://www.blogbeat.nl/getbeats.aspx?minstamp="+blogListViewPort._lastItem, true);
	request.send(null);
}

var blogScrollingTimer = null;
var blogRefresherTimer = null;
//var numberPings = 0;

/*
 * 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 itemId = ping.getAttribute("id")
			var updateTime = ping.getAttribute("receivedOn")
			var blogName = ping.getAttribute("name");
			var blogUrl = ping.getAttribute("url");
			var blogDescription = ping.getAttribute("description");
			var pingAsHtml = "<h2>" + updateTime + " <a rel=\"nofollow\" target=\"_blank\" href=\"" + encodeURI(blogUrl) + 
	                    	"\">" + blogName.escapeHTML() + "</a></h2><p>" + blogDescription + "</p>";
			blogPingList.push(pingAsHtml);
			
			if(i==0){
				blogListViewPort.setLastItem(itemId);
			}
		}
		blogListViewPort.setBackingList(blogPingList);
		
		if (blogScrollingTimer != null)
		{
			clearInterval(blogScrollingTimer);
		}
		blogScrollingTimer = setInterval("blogListViewPort.scrollList()", 3000);
	}
	
	blogRefresherTimer = setTimeout("getRecentPings()", 6 * 1000); // get the next batch in x seconds.
}

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);