var g_box_id = "";
var g_size = 5;
var g_task_flg = false;
var g_task_stack = new Array();
var g_task_id = null;
var g_draw_format = "";
var g_date_format = "";
var g_resize_w = null;
var g_resize_h = null;
var g_finish_func = null;


/*
	aDrawFormatで使用できる変数
	[%BLOG_TITLE%]	：ブログタイトル
	[%NOTE_TITLE%]	：記事タイトル
	[%NOTE%]		：記事
	[%DATE%]		：入力された時間
	[%IMAGE%]		：画像
	[%URL%]			：記事URL
	[%RESIZE%]		：リサイズ処理（関数が存在するときのみ実行）
	Format内での'の使用は禁止

	[%DATE%]を使用したときのみ、aDateFormatで使用できる変数
	[%YEAR%]	：年
	[%MONTH%]	：月
	[%DAY%]		：日
	[%HOUR%]	：時間
	[%MIN%]		：分
	[%SEC%]		：秒
*/
function loadRSS( aURL, aDrawQuery, aSize, aDrawFormat, aDateFormat, aResizeW, aResizeH, aFinishFunc ){
	if( g_task_flg ){
		g_task_stack.push( {"URL":aURL, "DRAW_QUERY":aDrawQuery, "SIZE":aSize, "DRAW_FORMAT":aDrawFormat, "DATE_FORMAT":aDateFormat, "RESIZE_W":aResizeW, "RESIZE_H":aResizeH, "FINISH_FUNC":aFinishFunc } );
		return;
	}

	var output = "";
	var format = "";
	var date_format = "";

	g_task_flg = true;

	g_box_id = aDrawQuery;
	g_size = aSize;

	if( typeof(aDateFormat) == "undefined" ){
		g_date_format = "[%YEAR%]/[%MONTH%]/[%DAY%]";
	} else if( aDateFormat == null ){
		g_date_format = "[%YEAR%]/[%MONTH%]/[%DAY%]";
	} else {
		g_date_format = aDateFormat;
	}

	if( typeof(aResizeW) == "undefined" ){
		g_resize_w = 70;
	} else {
		g_resize_w = aResizeW;
	}

	if( typeof(aResizeH) == "undefined" ){
		g_resize_h = 70;
	} else {
		g_resize_h = aResizeH;
	}
	
	
	if( typeof(aDrawFormat) == "undefined" ){
		g_draw_format = '<div class="rss_note_box"><div class="note_title"><a href="[%URL%]">[%NOTE_TITLE%]</a></div><div class="note_box"><div class="images"><img src="[%IMAGE%]" alt="" [%RESIZE%] /></div><div class="note">[%NOTE%]</div><div class="date">[%DATE%]</div></div></div>';
	} else if( aDrawFormat == null ){
		g_draw_format = '<div class="rss_note_box"><div class="note_title"><a href="[%URL%]">[%NOTE_TITLE%]</a></div><div class="note_box"><div class="images"><img src="[%IMAGE%]" alt="" [%RESIZE%] /></div><div class="note">[%NOTE%]</div><div class="date">[%DATE%]</div></div></div>';
	} else {
		g_draw_format = aDrawFormat;
	}

	if(typeof(aFinishFunc) != "undefined"){
		g_finish_func = aFinishFunc;
	} else {
		g_finish_func = null;
	}

	url = aURL;
	if( url.match(/^http/) ) {
		url = '/lib/proxy.php?url='+encodeURL2(aURL);
	}

	$.getFeed({
		url : url,
		success : loadFeed,
		error: loadFeedError
	});
}

function loadFeed( aFeed ){
	var in_html = '';
	var tmp_cnt = 0;

	var resize = "";
	if( typeof(resizeImageOnly) == 'function') {
		resize = ' onload="resizeImageOnly(this,'+g_resize_w+','+g_resize_h+');" ';
	} else {
		if( g_resize_w != null ){
			resize = ' width="'+g_resize_w+'" ';
		} else if(g_resize_h != null) {
			resize = ' height="'+g_resize_h+'" ';
		}
	}

	for( var item_num in aFeed.items ){
		if( typeof(aFeed.items[item_num].title) == 'undefined' ) continue;
		var tmp_output = "";
		tmp_output = allReplace( g_draw_format, "[%NOTE_TITLE%]", aFeed.items[item_num].title );
		tmp_output = allReplace( tmp_output, "[%NOTE%]", aFeed.items[item_num].description );
		tmp_output = allReplace( tmp_output, "[%DATE%]", changeDate( aFeed.items[item_num].updated ) );
		tmp_output = allReplace( tmp_output, "[%IMAGE%]", aFeed.items[item_num].image );
		tmp_output = allReplace( tmp_output, "[%URL%]", aFeed.items[item_num].link );
		tmp_output = allReplace( tmp_output, "[%RESIZE%]", resize );

		in_html += tmp_output;
		tmp_cnt++;
		if( tmp_cnt >= g_size ) break;
	}

	$(g_box_id).html(in_html);
	g_task_flg = false;

	if( g_finish_func != null ){
		window.setTimeout( new Function( g_finish_func+"()" ), 1000 );
	}

	var stack = g_task_stack.shift();
	if( !stack ){
		return;
	}

	loadRSS( stack["URL"], stack["DRAW_QUERY"], stack["SIZE"], stack["DRAW_FORMAT"], stack["DATE_FORMAT"], stack["RESIZE_W"], stack["RESIZE_H"], stack["FINISH_FUNC"] );
}

function loadFeedError( aStatus ){
	$(g_box_id).css('color','#FF0000').html("現在新着情報はございません。");

	g_task_flg = false;

	var stack = g_task_stack.shift();
	if( !stack ){
		return;
	}

	loadRSS( stack["URL"], stack["DRAW_QUERY"], stack["SIZE"], stack["DRAW_FORMAT"], stack["DATE_FORMAT"], stack["RESIZE_W"], stack["RESIZE_H"], stack["FINISH_FUNC"] );

}

function changeDate( aDate ){
	var myDate = new Date( aDate );
	
	Date.fromW3CDTF = function (w3cdtf) {
		var m = w3cdtf.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/);
		var date = new Date(m[1], m[2] - 1, m[3], m[4], m[5]);
		date.setTime(date.getTime() + 9 * 60 * 60 * 1000);
		return date;
	}

	if( myDate == "NaN" ){
		myDate = Date.fromW3CDTF( aDate );
	} else if( myDate.getFullYear().toString() == "NaN" ) {
		myDate = Date.fromW3CDTF( aDate );
	}

	var year = new String( myDate.getFullYear() );
	var month = myDate.getMonth()+1;
	var day = myDate.getDate();
	var hour = myDate.getHours();
	var min = myDate.getMinutes();
	var sec = myDate.getSeconds();

	if( month < 10 ) month = "0" + month;
	if( day < 10 ) day = "0" + day;

	var tmp_output = "";
	tmp_output = allReplace( g_date_format, "[%YEAR%]", year );
	tmp_output = allReplace( tmp_output, "[%YEAR2%]", year.slice(-2) );
	tmp_output = allReplace( tmp_output, "[%MONTH%]", month );
	tmp_output = allReplace( tmp_output, "[%DAY%]", day );
	tmp_output = allReplace( tmp_output, "[%HOUR%]", hour );
	tmp_output = allReplace( tmp_output, "[%MIN%]", min );
	tmp_output = allReplace( tmp_output, "[%SEC%]", sec );

	return tmp_output;
}

