YAHOO.namespace('YPN');
YAHOO.YPN.YPNBlog =
{
	max_count:  3,
	max_length: 60,

	fetch: function()
	{
		YAHOO.util.Connect.asyncRequest(
			'GET', '/inc/proxy.php?yws_path=blog/feed',
			{
				success: this.handleSuccess,
				scope:   this
			});
	},

	handleSuccess: function(
		/* object */	result)
	{
		if (!result.responseXML)
		{
			return;
		}

		var list_node       = document.getElementById('blog');
		list_node.innerHTML = '';

		// Get the top level <rss> element
		var rss = this.findChild(result.responseXML, 'rss');
		if (!rss)
		{
			return;
		}

		// Get single subordinate channel element
		var channel = this.findChild( rss, 'channel');
		if (!channel)
		{
			return;
		}

		var count = 0;

		// Get all item elements subordinate to the channel element
		// For each element, get title, link and publication date.
		// Note that all elements of an item are optional.
		for (var item = channel.firstChild;
			 item != null && count < this.max_count;
			 item = item.nextSibling)
		{
			if (item.nodeName != 'item')
			{
				continue;
			}

			var title = this.findChild(item, 'title');
			var description = this.findChild(item, 'description');
			var link  = this.findChild(item, 'link');
			var date  = this.findChild(item, 'pubDate');

			if (title && link && date)
			{
				date = new Date(Date.parse(date.firstChild.data));

				title = (title.firstChild ? title.firstChild.data : 'Untitled');
				
				description = (description.firstChild ? description.firstChild.data : '');

				// If too long, pull words out until within limits
				while (title.length > this.max_length)
				{
					title = title.split(' ').slice(0, -1).join(' ') + '...';
				}
				// If too long, pull words out until within limits
				
                if (description.indexOf("&#173;") > 0 && description.indexOf("&#173;") < 100) {
				    description = description.substring(0, description.indexOf("&#173;") ) + '...';;
				} else {				
				    while (description.length > 88)
				    {
					    description = description.split(' ').slice(0, -1).join(' ') + '...';
				    }
				}
				if (description.length > 0) {
				    description = "<a href='" + link.firstChild.data + "' target='_blank' id='hp_blogContent'>" + description + "</a>";
				}
				
				 
				
                var m_names = new Array("January", "February", "March", 
                "April", "May", "June", "July", "August", "September", 
                "October", "November", "December");
                
                var suff = new Array("st", "nd", "rd", "th", "th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","st","nd","rd","th","th","th","th","th","th","th","st");
                
				list_node.innerHTML +=
				    '<a href="' + link.firstChild.data + '" target="_blank" style="display:block;margin: 0 0 3px 0; text-decoration: none;padding: 0; font: bold 12px tahoma; color:#007894;">' + title + '</a>' + 
				    description + 
				    '<div style="font-size:9px;color:#color:#36434F;"><!--By <span>Jerry Yang</span>-->' + m_names[date.getMonth()] + ' ' + date.getDate() + suff[date.getDate() - 1] + ', ' + date.getFullYear() + '</div>' +
				    //'<a href="' + link.firstChild.data + '" target="_blank" class="arrowed_link">read more</a></div>' +
					'<br />';
				count++;
			}
		}
	},

	findChild: function(
		/* DOM element */	element,
		/* string */		nodeName)
	{
		for (var child = element.firstChild; child != null; child = child.nextSibling)
		{
			if (child.nodeName == nodeName)
			{
				return child;
			}
		}

		return null;
	},

	twoDigits: function(
		/* string */	value)
	{
		value = value + '';
		if (value.length == 1)
		{
			return '0' + value;
		}
		else if (value.length == 2)
		{
			return value;
		}
		else
		{
			return value.substr(-2);
		}
	}
};
