/* Target RSS2.0 */

function RSSViewer(url, locationSelector){
	this.initialize.apply(this, arguments);
}

RSSViewer.prototype = {
	initialize: function(url, locationSelector) {
			this.url = url;
			this.locationSelector = locationSelector;
			this.async = true;
	},
	setAsync: function(bool) {
		this.async = bool;
	},
	show: function() {
		var warn = this.warn;
		var draw = this.draw;
		var locationSelector = this.locationSelector;
		$.ajax({
			url: this.url,
			async: this.async,
			cache: false,
			dataType: "xml",
			success: function(xml){
				draw(xml, locationSelector);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				warn("RSSViewer#show() : http error (" + textStatus + ")");
			}
		});
	},
	draw: function(xml, locationSelector) {
		$(xml).find('item:first').each(function() {
			var link = '';
			var title = '';
			var date = '';
			$(this).children().each(function() {
				if ($(this)[0].tagName.toLowerCase() == 'link') {
					link = $(this).text();
				}
				if ($(this)[0].tagName.toLowerCase() == 'title') {
					title = $(this).text();
				}
				if ($(this)[0].tagName.toLowerCase() == 'dc:date') {
					date = $(this).text();
				}
			});
			date = date.substr(0, 10);
			date = date.replace('-', '年');
			date = date.replace('-', '月');
			locationSelector.html('<p class="date">' + date + '日&nbsp;&nbsp;<span class="new">New!!</span></p><p class="title">' + title + '</p>');
		});
	},
	warn: function(message) {
		if(window.console){
			window.console.log(message);
		}
		else{
			alert(message);
		}
		alert("warn");
	}
};

