var $ = jQuery;
function getRss(url, func) {
	$.ajax({
		url: 'http://pipes.yahoo.com/pipes/pipe.run',
		data: { _id: 'zIQi0Iy72xGJ3NMhJhOy0Q', _render: 'json', s: url,  },
		dataType: 'jsonp',
		jsonp: '_callback',
		success: func
	});
}

function setLatestChannel(elem) {
	getRss('http://www.nicovideo.jp/mylist/10822952?rss=2.0&sort=6', function(feed) {
		if (feed.couunt < 1) return;
		var node = '<iframe width="312" height="176" src="http://ext.nicovideo.jp/thumb/';
		var url = feed.value.items[0].link;
		var smid = url.split('/').reverse()[0];
		node += smid + '" scrolling="no" style="border:solid 1px #CCC;" frameborder="0"></iframe>';
		$(elem).append(node);
	});
}

function setChannelEntries(elem) {
	getRss('http://www.nicovideo.jp/mylist/10822952?rss=2.0&sort=6', function(feed) {
		var html = '';
		var i = 0;
		$(feed.value.items).each(function() {
			if (i > 11) return;
			var node = '<div style="float: left; margin: 30px;"><iframe width="312" height="176" src="http://ext.nicovideo.jp/thumb/';
			var smid = this.link.split('/').reverse()[0];
			node += smid + '" scrolling="no" style="border:solid 1px #CCC;" frameborder="0"></iframe></div>';
			$(elem).append(node);
			i += 1;
		});
	});
}

function setLatestBlog(elem) {
	getRss('http://grigori.sblo.jp/index.rdf', function(feed) {
		if (feed.couunt < 1) return;
		var ent = feed.value.items[0];
		var html = '<p style="font-weight: bold;">' + ent.title + '</p>';
		html += '<p class="blog-body">' + ent.description + '</p>';
		html += '<div class="blog-more">(' + ent.pubDate.split('T')[0]  + ') <a href="' + ent.link + '">全文を読む&raquo;</a></div>';
		$(elem).append(html);
	});
}

function setBlogEntries(elem) {
	getRss('http://grigori.sblo.jp/index.rdf', function(feed) {
		var html = '';
		$(feed.value.items).each(function() {
			html += '<h2><a href="' + this.link + '">' + this.title + '</a></h2>';
			html += '<div class="blog-date">' + this.pubDate.split('T')[0] + '</div>';
			html += '<p class="blog-body">' + this.description + '</p>';
			html += '<div class="blog-more"><a href="' + this.link + '">全文を読む&raquo;</a></div>';
		});
		$(elem).append(html);
	});
}
