function Photo (tobj) {
  this.image = new Image();
  this.image.photo = this;
  this.image.src = tobj.photo_src;
  this.image.onload = this.display;
  this.tobj = tobj;
}

Photo.prototype.display = function () {
  if (Photo.suppressDisplay) return;
  var t = parseInt((this.photo.tobj.lr.y - this.height) + 25) + 'px';
  var l = parseInt((this.photo.tobj.lr.x - this.width) + 15) + 'px';
  this.setAttribute('class', 'thumbnail');
  $(this).css({top: t, left: l, display: 'none'});
  $(document.body).append(this);
  $(this).fadeIn(300).delay(5000).fadeOut(3500, 'swing', function () {$(this).remove()} );
}

Photo.extract = function(tobj) {
  var tweet = tobj.tweet;
  if (!tweet.entities || !tweet.entities.urls) return;
  var urls = tweet.entities.urls;
  for (i in urls) {
    var arr;
    var url = urls[i].url;
    if (arr = url.match(/(twitpic.com)\/(.*)?/))
      tobj.photo_src = "http://twitpic.com/show/thumb/" + arr[2];
    else if (arr = url.match(/(tweetphoto\.com|plixi\.com)/))
      tobj.photo_src = "http://api.plixi.com/api/tpapi.svc/imagefromurl?size=small&url=" + url;
    else if (arr = url.match(/http:\/\/(yfrog)\..*?\/(\S+)/))
      tobj.photo_src = "http://yfrog.com/" + arr[2] + '.th.jpg';
    else if (arr = url.match(/http:\/\/(flic\.kr)\/p\/(\S+)/))
      tobj.photo_src = "http://flic.kr/p/img/" + arr[2] + '_s.jpg';
  }
  if (tobj.photo_src) new Photo(tobj);
}

Photo.togglePhotos = function () {
  Photo.suppressDisplay = !Photo.suppressDisplay;
  var newColor;
  if (Photo.suppressDisplay) {
    $('.thumbnail').remove(); 
    newColor = '#444';
  } else {
    newColor = '#eee';
  }
  $('#photos').css('color',newColor);
}

Photo.suppressDisplay = false;

