/*
 * Get the latest 'n' Audio pings from the PingServer.
 */
function getRecentAudioPings()
{
	var request = newXmlHttpRequest();
	request.onreadystatechange = getReadyStateXmlHandler(request, 
		updatePodcastPingList);
		
	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=audioping&1="+d.getTime(), true);
	request.send(null);
}

var podcastScrollingTimer = null;
var podcastRefreshTimer = null;

/*
 * Update the podcast ping list.
 */
function updatePodcastPingList(xmlResponse)
{
	var pings = xmlResponse.getElementsByTagName("weblog");

	if (pings.length > 0)
	{
		var podcastPingList = 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) +  "&nbsp;" + ampm;
			
			var blogName = ping.getAttribute("name");
			if (blogName.length > 70)
			{
				blogName = blogName.substr(0, 67) + "...";
			}
			
			var pingFeedTitle = ping.getAttribute("feedTitle");
			if (pingFeedTitle.length > 70)
			{
				pingFeedTitle = pingFeedTitle.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";
            }

			castLength = Math.round(castLength * 100) / 100;

			var podcastAsHtml = "<table class=\"blog\">" +
	            "<tr>" +
	                "<td class=\"blogName\" nowrap=\"nowrap\">" +
	                    "<a href=\"" + ping.getAttribute("url") + "\" " +
	                    "class=\"pingLink\" " +
	                    "title=\"" + pingFeedTitle + "\" " +
	                    "rel=\"nofollow\">" + blogName.escapeHTML() + "</a>" +
	                "</td>" +
	                "<td class=\"pingTextThin\" align=\"right\" nowrap=\"nowrap\" valign=\"top\" width=\"67\">" +
	                    blogPingTime +
	                "</td>" +
	                "<td class=\"pingTextThin\" align=\"right\" nowrap=\"nowrap\" valign=\"top\" width=\"66\">" +
	                    "<a class=\"pingLink\" href=\"" +  
	                    ping.getAttribute("castUrl") + "\">" +
	                    castLength + "&nbsp;" + kOrMb + "</a>" +
	                "</td>" +
	                "<td class=\"pingTextThin\" valign=\"top\" width=\"14\">" +
	                    "<a href=" + ping.getAttribute("url") + "\">" +
	                    "<img src=\"/images/feed_icon.png\" alt=\"XML\" border=\"0\"></a>" +
	                "</td>" +
	            "</tr>" +
	            "</table>";
	            
			podcastPingList.push(podcastAsHtml);
		}
		
		podcastListViewPort.setBackingList(podcastPingList);

		if (podcastScrollingTimer != null)
		{
			clearInterval(podcastScrollingTimer);
		}
		
		podcastScrollingTimer = setInterval("podcastListViewPort.scrollList()", 
			2 * 1000);
	}

	podcastRefreshTimer = setTimeout("getRecentAudioPings()", 60 * 1000);
}

var podcastTabEventHandlers = {
	'a#scrollingListImageLink' : function(el)
	{
		el.onclick = function()
		{
			$('podcastList').style.display = 'block';
            $('pingform').style.display = 'none'; 
            $('changeLists').style.display = 'none'; 
            return false;
		}
	},
	'a#sendAPingImageLink' : function(el)
	{
		el.onclick = function()
		{
			$('podcastList').style.display = 'none';
            $('pingform').style.display = 'block'; 
            $('changeLists').style.display = 'none'; 
            return false;
		}
	},
	'a#changeListImageLink' : function(el)
	{
		el.onclick = function()
		{
			$('podcastList').style.display = 'none';
            $('pingform').style.display = 'none'; 
            $('changeLists').style.display = 'block'; 
            return false;
		}
	},
	'body' : function(el)
	{
		el.onunload += function()
		{
			if (podcastScrollingTimer != null)
			{
				clearInterval(podcastScrollingTimer);
			}
			if (podcastRefreshTimer != null)
			{
				clearTimeout(podcastRefreshTimer);
			}
		}
	}
};

Behaviour.register(podcastTabEventHandlers);

