function twitterCallback(obj) {
  var twitters = obj;
  var hitokoto = obj[0].text;
  username = twitters[0].user.screen_name;
  var location = hitokoto.indexOf('L:')+2; // check location
  var photo = hitokoto.indexOf('http://f.hatena')+2; // check photo
  if (location != 1 && photo != 1){ // exist location and photo
    var hitokoto_split = hitokoto.split('L:');
    var right = hitokoto_split[1].split(' ');
    var ad_enc = encodeURL(right[0]);
    var gmap = 'http://maps.google.co.jp/maps?q='+ad_enc+'&sa=X&oi=map&ct=title';
    var comment = '"'+ hitokoto_split[0] +'" L:<a href="'+ gmap +'" style="font-size:10px;">' + right[0] +'</a> Photo:<a href="'+ right[1] +'" style="font-size:10px;">'+ right[1] +'</a>';
  } else if ( location != 1 && photo == 1) { // exist location
    var hitokoto_split = hitokoto.split('L:');
    var ad_enc = encodeURL(hitokoto_split[1]);
    var gmap = 'http://maps.google.co.jp/maps?q='+ad_enc+'&sa=X&oi=map&ct=title';
    var comment = '"'+ hitokoto_split[0] +'" L:<a href="'+ gmap +'" style="font-size:10px;">' + hitokoto_split[1] +'</a>';
  } else if ( location == 1 && photo != 1) { // exist photo
    var hitokoto_split = hitokoto.split('http://f.hatena');
    var comment = '"'+ hitokoto_split[0] +'" Photo:<a href="http://f.hatena'+ hitokoto_split[1] +'" style="font-size:10px;">http://f.hatena'+ hitokoto_split[1] +'</a>';
  } else {
    var comment = obj[0].text;
  }
  document.getElementById('my_twitter_status').innerHTML = ''+ comment +' '+ relative_time(twitters[0].created_at) +'';
}

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 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}

//URL Encode (UTF-8)
//by Favorite Labo http://www.favorite-labo.org/archives/211.html
function encodeURL(str) {
  var character = '';
  var unicode   = '';
  var string    = '';
  var i         = 0;
  for (i = 0; i < str.length; i++) {
    character = str.charAt(i);
    unicode   = str.charCodeAt(i);
    if (character == ' ') {
      string += '+';
    } else {
      if (unicode == 0x2a || unicode == 0x2d || unicode == 0x2e || unicode == 0x5f || ((unicode >= 0x30) && (unicode <= 0x39)) || ((unicode >= 0x41) && (unicode <= 0x5a)) || ((unicode >= 0x61) && (unicode <= 0x7a))) {
        string = string + character;
      } else {
        if ((unicode >= 0x0) && (unicode <= 0x7f)) {
          character   = '0' + unicode.toString(16);
          string += '%' + character.substr(character.length - 2);
        } else if (unicode > 0x1fffff) {
          string += '%' + (oxf0 + ((unicode & 0x1c0000) >> 18)).toString(16);
          string += '%' + (0x80 + ((unicode & 0x3f000) >> 12)).toString(16);
          string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        } else if (unicode > 0x7ff) {
          string += '%' + (0xe0 + ((unicode & 0xf000) >> 12)).toString(16);
          string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        } else {
          string += '%' + (0xc0 + ((unicode & 0x7c0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        }
      }
    }
  }
  return string;
}
