function tw2info(twitters) {
	var statusHTML = [];

	var count = 9;

	if(count > twitters.length){
		count = twitters.length;
	}

	// DEFAULT
	if(typeof twitpic_size == 'undefined'){ twitpic_size = 'mini'; }
	if(typeof plog_size == 'undefined'){ plog_size = 'thumb1'; }
	if(typeof twitvid_width == 'undefined'){ twitvid_width = '100'; }
	if(typeof img_prefix == 'undefined'){ img_prefix = ''; }
	if(typeof img_suffix == 'undefined'){ img_suffix = ''; }

	for (var i=0; i<count; i++){
		var images = '';
		var numtype = '';
		var account = twitters[i].user.screen_name;
		var user = twitters[i].user.name;
		var icon = twitters[i].user.profile_image_url;
		var homepage = twitters[i].user.url;

		if(!homepage){ homepage = 'http://twitter.com/' + account; }

		if(i%2==0)
		{
			numtype = 'odd';
		}
		else
		{
			numtype = 'even';
		}
	
		user = '<h4>' + user + '<span class="date-fmt"> (' + relative_time(twitters[i].created_at) + ')</span>' + '</h4>';
		icon = '<img align="left" class="icon" src="' + icon + '">';
		icon = '<a href="' + homepage + '" target=_blank>' + icon + '</a>';


		var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
			// TWITPIC
			if(url.match(/\/twitpic.com\/([0-9a-zA-Z])/)){
				var img_url = url.replace(/http\:\/\/twitpic\.com\//, function(img_url){
					return 'http://twitpic.com/show/' + twitpic_size + '/';
				})
				return '<a href="'+url+'" target=_blank alt="twitpic">'+url+'</a>';

			// PHOTO ROBOX
			}else if(url.match(/\/photo.robox.org\/([_a-z0-9])/i)){
				var img_url = url.replace(/home\/([_a-z0-9]+)/ig, function(img_url){
					return 'photo/' + plog_size;
				});
				img_url = img_url + '.jpg';
				images += img_prefix + '<a href="'+url+'" target=_blank alt="PHOTO ROBOX"><img border=0 class="twitpic" src="'+img_url+'"></a>' + img_suffix;
				return '<a href="'+url+'" target=_blank alt="photo robox">'+url+'</a>';

			// TWITVID
			}else if(url.match(/[\/\.]twitvid.com\/([_a-z0-9])/i)){
				var img_url = url.replace(/twitvid.com/ig, 'images.twitvid.com');
				img_url = img_url + '.jpg';

				images += img_prefix + '<a href="'+url+'" target=_blank><img border=0 class="twitpic" src="'+img_url+'" width=' + twitvid_width + '></a>' + img_suffix;
				return '<a href="'+url+'" target=_blank alt="twitvid">'+url+'</a>';

			// Ow.ly
			}else if(url.match(/\/ow.ly\/i\/([a-z])/i)){
				var img_url = url.replace(/http\:\/\/ow\.ly\/i\//, 'http://static.ow.ly/photos/thumb/');
				img_url = img_url + '.jpg';

				images += img_prefix + '<a href="'+url+'" target=_blank alt="Ow.ly"><img border=0 class="Owly" src="'+img_url+'"></a>' + img_suffix;
				return '<a href="'+url+'" target=_blank alt="ow.ly">'+url+'</a>';

			}else{
				return '<a href="'+url+'" target=_blank>'+url+'</a>';
			}
		}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
			return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
		}).replace(/\B#([_a-z0-9]+)/ig, function(reply) {
			return  reply.charAt(0)+'<a href="http://twitter.com/#search?q=%23'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
		}).replace(/\[.+?\]/g, function(reply) {
			return '<b>' + reply.slice(1, -1) + '</b>';
		});

    	var line = '<div id="height_check"><div class="' + numtype +'">' + '<li>' + icon + '<span>' + user + '<div class="twit_text">' + status + '</span>';

		if(images){
			line += '<div>' + images + '</div>';
		}

		line += '<br clear=all /></li></div></div></div>';
    	statusHTML.push(line);
	}

	document.getElementById('twitter_info_list').innerHTML = statusHTML.join('');
}

function tw2news(twitters) {
	var statusHTML = [];

	var count = 10;

	if(count > twitters.length){
		count = twitters.length;
	}

	// DEFAULT
	if(typeof twitpic_size == 'undefined'){ twitpic_size = 'mini'; }
	if(typeof plog_size == 'undefined'){ plog_size = 'thumb1'; }
	if(typeof twitvid_width == 'undefined'){ twitvid_width = '200'; }
	if(typeof img_prefix == 'undefined'){ img_prefix = ''; }
	if(typeof img_suffix == 'undefined'){ img_suffix = ''; }

	for (var i=0; i<count; i++){
		var images = '';

		var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
			// TWITPIC
			if(url.match(/\/twitpic.com\/([a-z])/i)){
				var img_url = url.replace(/http\:\/\/twitpic\.com\//, function(img_url){
					return 'http://twitpic.com/show/' + twitpic_size + '/';
				})
				images += img_prefix + '<a href="'+url+'" target=_blank alt="twitpic"><img border=0 class="twitpic" src="'+img_url+'"></a>' + img_suffix;
				return '<a href="'+url+'" target=_blank>'+url+'</a>';

			// PHOTO ROBOX
			}else if(url.match(/\/photo.robox.org\/([_a-z0-9])/i)){
				var img_url = url.replace(/home\/([_a-z0-9]+)/ig, function(img_url){
					return 'photo/' + plog_size;
				});
				img_url = img_url + '.jpg';
				images += img_prefix + '<a href="'+url+'" target=_blank alt="PHOTO ROBOX"><img border=0 class="twitpic" src="'+img_url+'"></a>' + img_suffix;
				return '<a href="'+url+'" target=_blank>'+url+'</a>';

			// TWITVID
			}else if(url.match(/[\/\.]twitvid.com\/([_a-z0-9])/i)){
				var img_url = url.replace(/twitvid.com/ig, 'images.twitvid.com');
				img_url = img_url + '.jpg';

				images += img_prefix + '<a href="'+url+'" target=_blank><img border=0 class="twitpic" src="'+img_url+'" width=' + twitvid_width + '></a>' + img_suffix;
				return '<a href="'+url+'" target=_blank alt="twitvid">'+url+'</a>';
			}else{
				return '<a href="'+url+'" target=_blank alt="twitvid">'+url+'</a>';
			}
		}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
			return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
		}).replace(/\B#([_a-z0-9]+)/ig, function(reply) {
			return  reply.charAt(0)+'<a href="http://twitter.com/#search?q=%23'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
		}).replace(/\[.+?\]/g, function(reply) {
			return '<b>' + reply.slice(1, -1) + '</b>';
		});


    	var line = '<li><span>' + status + '</span> <span class="date-fmt">(' + relative_time(twitters[i].created_at) + ')</span>';
		line += '<br clear=all /></li>';
    	statusHTML.push(line);
	}
	document.getElementById('twitter_news_list').innerHTML = statusHTML.join('');
}

function topimage(images) {
	var statusHTML = [];

	for (var i=0; i<images.length; i++){
		var photo = '<img src="' + images[i].photo + '" alt="' + images[i].comment + '" title="' + images[i].comment + '" class="top_image" />';

		statusHTML.push('<a href="' + images[i].org + '" target=_blank>' + photo + '</a>');
	}
	document.getElementById('main_img').innerHTML = statusHTML.join('');

}

function topic(topics) {
	var statusHTML = [];

	for (var i=0; i<topics.length; i++){
		var photo = '<img src="' + topics[i].photo + '" alt="' + topics[i].name + '" title="' + topics[i].name + '" class="topic_image" />';
		var link = '/bin/topic?F=D&C=' + topics[i].topic_c;

		statusHTML.push('<dt><a href="' + link + '">' + photo + '</a></dt><dd><h4>' + topics[i].name + '</h4>' + topics[i].note + '</dd>');
	}
	document.getElementById('topics_list').innerHTML = statusHTML.join('');
}


function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return '1分以内';
  } else if(delta < 120) {
    return '1分前';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + '分前';
  } else if(delta < (120*60)) {
    return '1時間前';
  } else if(delta < (24*60*60)) {
    return (parseInt(delta / 3600)).toString() + '時間前';
  } else if(delta < (48*60*60)) {
    return '1日以内';
  } else {
    return (parseInt(delta / 86400)).toString() + '日前';
  }
}

function relative_time2(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  var dt = new Date();
  dt.setTime(dt.getTime() - (delta*1000));
  yy = dt.getYear();
  mm = dt.getMonth() + 1;
  dd = dt.getDate();
  dy = dt.getDay();
  hh = dt.getHours();
  mi = dt.getMinutes();
  ss = dt.getSeconds();
//  if (yy < 2000) { yy += 1900; }
  if (yy < 2000) { yy -= 100; }
  if (yy < 10) { yy = "0" + yy; }
  if (mm < 10) { mm = "0" + mm; }
  if (dd < 10) { dd = "0" + dd; }
  dy = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")[dy];
  if (hh < 10) { hh = "0" + hh; }
  if (mi < 10) { mi = "0" + mi; }
  if (ss < 10) { ss = "0" + ss; }
//  return yy+"/"+mm+"/"+dd+"("+dy+") "+hh+":"+mi+":"+ss;
  return mm+"/"+dd+" "+hh+":"+mi;
}

function load_twit(account, count) {
	if(typeof count == 'undefined'){ count = 10; }
	var ele = document.createElement("script");
	ele.type = "text/javascript";
	ele.src = "http://twitter.com/statuses/user_timeline/" + account + ".json?callback=tw2news&count=" + count;
	document.body.appendChild(ele);
}

function load_listtwit(account, list, count) {
	if(typeof count == 'undefined'){ count = 10; }
	var ele = document.createElement("script");
	ele.type = "text/javascript";
	ele.src = "http://api.twitter.com/1/" + account + "/lists/" + list + "/statuses.json?callback=tw2info&count=" + count;
	document.body.appendChild(ele);
}

function load_topimage(count) {
	if(typeof count == 'undefined'){ count = 3; }
	var ele = document.createElement("script");
	ele.type = "text/javascript";
	ele.src = "/bin/topimage.json?callback=topimage&count=" + count;
	document.body.appendChild(ele);
}

function load_topic(count) {
	if(typeof count == 'undefined'){ count = 3; }
	var ele = document.createElement("script");
	ele.type = "text/javascript";
	ele.src = "/bin/topic.json?callback=topic&count=" + count;
	document.body.appendChild(ele);
}

