var errorMsg = 'error';

function displayNews(xmlUrl, targetId) {
	new Ajax.Request(xmlUrl, {
		method: 'get',
		parameters: '?_=' + (new Date()).getTime(),
		onSuccess: function(obj) {
			var xml = obj.responseXML.documentElement;
			if (!Prototype.Browser.IE) xml = cleanWhite(xml);
			var xmlData = new Array();
			var nodes = xml.getElementsByTagName('entry');
			var l = nodes.length;
			for (var i = 0; i < l; i++) {
				xmlData.push({
					'day'      : nodes[i].getElementsByTagName('day')[0].firstChild.nodeValue,
					'category' : nodes[i].getElementsByTagName('category')[0].firstChild.nodeValue,
					'icon'     : nodes[i].getElementsByTagName('icon')[0].firstChild.nodeValue,
					'text'     : nodes[i].getElementsByTagName('text')[0].firstChild.nodeValue,
					'link'     : nodes[i].getElementsByTagName('link')[0].firstChild.nodeValue,
					'type'     : parseInt(nodes[i].getElementsByTagName('type')[0].firstChild.nodeValue)
					});
			}
			var html = '';
			html += '<div class="newsIcon">';
			var l = xmlData.length;
			for (var i = 0; i < l; i++) {
				html += '<dl>';
				html += '<dt>' + escapeEntities(xmlData[i].day) + '</dt>';
//				html += '<dd class="icon">';
//				html += '<img src="' + escapeEntities(xmlData[i].icon) + '" width="60" height="13"';
//				html += ' alt="' + escapeEntities(xmlData[i].category) + '" />';
//				html += '</dd>';
				if (xmlData[i].type) {
					html += '<dd class="text">';
					html += '<a href="' + escapeEntities(xmlData[i].link) + '" target="_blank">' + escapeEntities(xmlData[i].text) + '</a>';
				} else {
					html += '<dd class="text">';
					html += '<a href="' + escapeEntities(xmlData[i].link) + '">' + escapeEntities(xmlData[i].text) + '</a>';
				}
				html += '</dd>';
				html += '</dl>';
			}
			html += '</div>';
			$(targetId).innerHTML = html;
		}, 
		onException: function () {
			var html = '';
			html += '<div class="news">';
			html += '<p>' + escapeEntities(errorMsg) + '</p>';
			html += '</div>';
			$(targetId).innerHTML = html;
		},
		onFailure: function(){
			html += '<div class="news">';
			html += '<p>' + escapeEntities(errorMsg) + '</p>';
			html += '</div>';
			$(targetId).innerHTML = html;
		}
	});
}

function cleanWhite(node) {
	Element.cleanWhitespace(node);
	if (node.hasChildNodes() === true) {
		var nodeNum = node.childNodes.length;
		for (var i = 0; i < nodeNum; i++) {
			node.childNodes[i] = cleanWhite(node.childNodes[i]);
		}
	}
	return node;
}

function escapeEntities(str) {
	str = str.replace(/&/, '&amp;');
	str = str.escapeHTML();
	str = str.replace(/"/, '&quot;');
	return str;
}
