/*
 * Get the latest 'n' Audio pings from the PingServer.
 */
function getRecentAudioPings()
{
	var request = newXmlHttpRequest();
	request.onreadystatechange = getReadyStateXmlHandler(request, 
		updatePodcastPingList);
	request.open("GET", "/pingservice?action=audioping", true);
	request.send(null);
}

var podcastScrollingTimer = null;

/*
 * Update the podcast ping list.
 */
function updatePodcastPingList(xmlResponse)
{
	var pings = xmlResponse.getElementsByTagName("weblog");

	if (pings.length > 0)
	{
		podcastPingList = new Array();
		
		for (var i = 0; i < pings.length; i++)
		{
			var ping = pings[i];
			
			var when = new Date(parseInt(ping.getAttribute("receivedOn"), 10));
			var hour = when.getHours();
			var ampm = hour > 12 ? "PM" : "AM";
			var minutes = when.getMinutes();
			hour = (hour % 12 == 0 ? 12 : hour % 12);
			
			var blogName = ping.getAttribute("name");
			if (blogName.length > 70)
			{
				blogName = blogName.substr(0, 67) + "...";
			}
			
			castLength = ping.getAttribute("castLength");
    
    		// if(castlangth.length() >0)
            if (castLength > 1024)
            {
                castLength /= 1024;
            }

            kOrMb = "K";

            if (castLength > 1024)
            {
                castLength /= 1024;
                kOrMb = "MB";
            }

			var pingAsHtml = "<table class=\"blog\"><tr>" +
				"<td class=\"blogName\">" +
					"<a class=\"pingLink\" href=\"" + 
						encodeURI(ping.getAttribute("url")) + 
						"\">" + makeSafeHtml(blogName) + 
					"</a>" +
				"</td>" + 
				"<td class=\"pingTextThin\">" + 
					hour + ":" + (minutes < 10 ? "0" + minutes : minutes) + 
					" " + ampm + 
				"</td></tr></table>";
				
			podcastPingList.push(pingAsHtml);
		}
		
		if (podcastScrollingTimer != null)
		{
			clearInterval(podcastScrollingTimer);
		}
		
		podcastListViewPort.setBackingList(podcastPingList);

		podcastScrollingTimer = setInterval("podcastListViewPort.scrollList()", 2000);
		
		setTimeout("getRecentPings()", 60 * 1000); // get the next batch in 1 min.
	}
}

/*
 * Replaces all '<' and '>' with "&lt;" and "&gt;" respectively.  It makes the
 * safe for display!
 */
function makeSafeHtml(theString)
{
	theString = theString.replace(/\</g,"&lt;");
	theString = theString.replace(/\>/g,"&gt;");
	return theString;
}

/*
 * Return true if the enter key is pressed
 */
function processKeyStroke(event)
{
	if(window.event)
	{
		keychar = window.event.keyCode;
	}
	else
	{
		keychar = event.which;
	}

	jslog.debug("Key: " + keychar);

	if (keychar == 13)
	{
		return true;
	}
}