/* 文字コードはUTF-8 */
feedreader = function(html_func){
	this.loading_html = '';
	this.error_html = '<div style="font-size:12px;color:#bbb;">データが読み込めませんでした。</div>';
	this.no_item_html = '';
	this.init = function(tid, xml_url){
		this.limit = (typeof(arguments[2]) != 'undefined' ? arguments[2] : 10);
		this.link_type = (typeof(arguments[3]) != 'undefined' ? arguments[3] : '_self');
		this.feed_title = '';
		this.feed_description = '';
		this.feed_link = '';
		var dateobj = new Date();
		var this_year = dateobj.getFullYear();
		var tobj = $('#' + tid).eq(0);
		tobj.html(this.loading_html);
		var self = this;
		$.get(xml_url, {}, function(xml, textStatus){
			var html = '';
			if(textStatus == 'success'){
				if($(xml).find('channel > title').length > 0){
					self.feed_title = $(xml).find('channel > title').eq(0).text();
				}
				if($(xml).find('channel > description').length > 0){
					self.feed_description = $(xml).find('channel > description').eq(0).text();
				}
				if($(xml).find('channel > link').length > 0){
					self.feed_link = $(xml).find('channel > link').eq(0).text();
				}
				if($(xml).find('item').length > 0){
					var items = new Array();
					var items_obj = $(xml).find('item');
					var count = (items_obj.length < self.limit ? items_obj.length : self.limit);
					var i = 0;
					items_obj.each(function(){
						var title = $(this).find('title').text();
						var link = $(this).find('link').text();
						var pubDate = $(this).find('pubDate').text();
						var pubtime = Date.parse(pubDate);
						dateobj.setTime(pubtime);
						var year = dateobj.getFullYear();
						var month = dateobj.getMonth() + 1;
						var day = dateobj.getDate();
						// var date_str = (this_year != year ? year + '年' : '') + month + '月' + day + '日';
						var date_str = month + '月' + day + '日';
						var description = $(this).find('content\\:encoded').text() || $(this).find('encoded').text() || $(this).find('[nodename=conntent\\:encoded]').text();
						var image = $(this).find('xstep\\:image').text() || $(this).find('image').text() || $(this).find('[nodename=xstep\\:image]').text();
						items.push({title:title, link:link, pubtime:pubtime, date_str:date_str, description:description, image:image});
						i++;
						if(i >= count){
							return false;
						}
					});
					html += self.build_html(items);
				}
				else{
					html += self.no_item_html;
				}
			}
			else{
				html += self.error_html;
			}
			tobj.html(html);
		});
	}
	this.build_html = function(items){
		var now = (new Date()).getTime();
		var html = '';
		var count = items.length;
		if(count > 0){
			html += '<ul>';
			for(var i = 0; i < count; i++){
				var item = items[i];
				var class_name = (i == 0 ? 'first' : ((count - i) == 1 ? 'last' : ''));
				if(now - item.pubtime < 86400000){
					class_name += (class_name != '' ? ' ' : '') + 'new';
				}
				html += '<li' + (class_name != '' ? ' class="' + class_name + '"' : '') + '>' + item.date_str + ' <a href="' + item.link + '" target="' + this.link_type + '">' + item.title + '</a></li>';
			}
			html += '</ul>';
		}
		return html;
	}
	if(typeof(html_func) == 'function'){
		this.build_html = html_func;
	}
}

