﻿(function($){
	$.fn.blogLine = function(options){ 
		$(this).each(function(){
			var self = $(this);

			self.append(
//				'<div class="loading"><img src="img/loading.gif" width="32" height="32" alt="" />loading...</div>',
				'<div class="loading">loading...</div>',
				'<h3>ブログ</h3>',
				'<ul/>'
			);
			$.ajax({
				url: 'cgi/blog.cgi',
				success: function(data){
					$('.loading', self).remove();

					if( data.length < 1 ) {
						$('ul', self).append('<li>投稿がありません。</li>');
						return false;
					}
					$("item",data).each(function(){
						var title = $("title",this).text();
						if(title.substring(0,2) != 'PR'){
							var timestr = $("pubDate",this).text();
							var timevl = new Date();
							if(/MSIE/.test(navigator.userAgent)){
								var timestr2 = timestr.slice(0,timestr.length-10) + 'UTC' + timestr.slice(timestr.length-10,timestr.length);
								timevl.setTime(Date.parse(timestr2));
								if(timevl.getYear()){
									timestr = '（' + (timevl.getYear()) +'年'+ (timevl.getMonth()+1) +'月'+ timevl.getDate() +'日'+ timevl.getHours() +':'+ timevl.getMinutes() +':'+ timevl.getSeconds()+'）';
								}
								else{
									timevl.setTime(Date.parse(timestr));
									timestr = '（' + (timevl.getYear()) +'年'+ (timevl.getMonth()+1) +'月'+ timevl.getDate() +'日'+ timevl.getHours() +':'+ timevl.getMinutes() +':'+ timevl.getSeconds()+'）';
								}
							}
							else{
								timevl.setTime(Date.parse(timestr));
								timestr = '（' + (timevl.getYear()+1900) +'年'+ (timevl.getMonth()+1) +'月'+ timevl.getDate() +'日'+ timevl.getHours() +':'+ timevl.getMinutes() +':'+ timevl.getSeconds()+'）';
							}
							$("ul").append("<li><a href='"+$("link",this).text()+"'>"+title+"</a><br>"+timestr+"</li>");
						}
					});
				}
			});
		});
	}
})(jQuery);

function blog_load(){
	$('#news').blogLine({});
	return false;
}


function addLoadEvent(func)
{	
	if(typeof window.addEventListener == 'function'){
		window.addEventListener('load', func, false);
		return true;
	}
	else if(typeof window.attachEvent == 'object'){
		window.attachEvent('onload', func);
		return true;
	}
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

addLoadEvent(blog_load);	// run blog_load onLoad

