
function WaPoLabsStreams () {
  this.streamdiv = 'streamdiv';
  this.StreamToHtml = function ( stream ) {
    var list = '<ol class="c">';
    for (var i = 0; i < stream.length; i++) {
      this.streamIndex = i;
      list += this.Format( stream[i] );
      }
    list += "</ol>";
    var sd = document.getElementById( this.streamdiv );
    sd.innerHTML = list;
    }

  this.InsertJSONP = function ( scriptdiv, url ) {
    var sc = document.createElement('script');
    sc.language = 'javascript';
    sc.type = 'text/javascript';
    sc.src = url;
    var tib = document.getElementById( scriptdiv );
    var oldscript = tib.firstChild;
    if (oldscript) {
      tib.replaceChild( sc, oldscript );
    } else {
      tib.appendChild(sc);
      }
    }

  this.Format = function ( item ) {
    var html = '<li></li>';
    return html;
    }

  this.JSONP = function ( scriptdiv, streamel, callback ) {
    }
  this.Stream = function ( stream ) {
    return this.StreamToHtml( stream );
    }
  }

function FacebookStream () {
  this.Format = function ( post ) {
 
    if (typeof(post.time) == 'number') {
      return "<li></li>";
      } 
    var tsRe = /\S+\s(\S+)\s(\d+)\s(\d+):(\d+).*/;
    var ts = post.time;
    ts = ts.replace(tsRe, "$1 $2 $3:$4");
    var list = ((this.streamIndex > 0)?'<li class="i a">' : '<li class="i">')
      + '<cite><a href="' + post.permalink + '" target="_blank">'
        + ts + '</a></cite>'
      + '<img src="' + post.pic_square + '" alt="" /><p>'
      + '<q>' + post.message + '</q>'
      + '<ul><li class="l">Likes: ' + post.likes + '</li></ul> '
      + '</p></li>';
    if (post.comments) {
      for (var n = 0; n < post.comments.length; n++) {
        if (n < post.comments.length - 1) {
          list += '<li>';
        } else {
          list += '<li class="z">';
          }
      
        if (post.comments[n].url == null) {
          post.comments[n].url = 'http://www.facebook.com/pages/Wheres-Pepper/88238473370';
          post.comments[n].pic_square = 'http://static.ak.fbcdn.net/pics/d_silhouette.gif';
          }
 
        list += '<a href="' + post.comments[n].url + '" target="_blank" class="g">'
          + '<img src="' + post.comments[n].pic_square + '" /> '
          + '</a>'
          + '<p><cite><a href="' + post.comments[n].url + '" target="_blank">'
          + post.comments[n].name + '</a></cite>';
        ts = post.comments[n].time;
        ts = ts.replace(tsRe, "$1 $2 $3:$4");
        list += '<span>' + ts + '</span> '
          + '<q>'+ post.comments[n].text + '</q>'
          + '</p></li>';
        }
      }
      return list;
      }
  this.JSONP = function (scriptdiv, streamdiv, callback, page) {
    this.streamdiv = streamdiv;
    this.InsertJSONP( scriptdiv,
      'http://labs.slate.com/stream/facebook?uid=' + page 
      + '&callback=' + callback
      + '&rand=' + Math.floor(Math.random() * 10000000)
      );
    }
  }

FacebookStream.prototype = new WaPoLabsStreams;

function TwitterStream () {
  this.Stream = function ( stream ) {
    return this.StreamToHtml( stream.results );
    }
  this.Format = function ( tweet ) {
    var html = '';
 
    var tsRe = /\S+\s(\d+)\s(\S+)\s\d+\s(\d+):(\d+).*/;
    var ts = tweet.created_at;
    ts = ts.replace(tsRe, "$2 $1 $3:$4");

 
    var URLre = /([a-zA-Z]+:\/\/[\w\.-]+\/?\S+)/g;
    tweet.text = tweet.text.replace(URLre, '<a target="_new" href="$1">$1</a>');
    var ReplyRe = /@(\w+)/g;
    tweet.text = tweet.text.replace(ReplyRe,
      '<cite>@<a target="_new" href="http://twitter.com/$1">$1</a></cite>');
  
    html = '<li>'
        + '<img src="' + tweet.profile_image_url + '" />'
        + '<p>'
        + '<cite><a target="_new" href="http://twitter.com/' 
          + tweet.from_user + '">'
        + tweet.from_user + '</a></cite>'
        + '<span><a href="http://twitter.com/'
          + tweet.from_user + '/status/' + tweet.id + '">'
          + ts + '</a></span>'
        + '<q>' + tweet.text + '</q>'
        + '</p>'
        + '</li>';

/*
      var PicRe = /http:\/\/twitpic.com\/([\da-z]{5})/;
      var PicId;
      while ((PicId = PicRe.exec( tweet.text )) != null) {
        html += '<a href="http://twitpic.com/' + PicId[0] 
          + '" target="_blank"><img src="http://twitpic.com/show/thumb/'
          + PicId[0] + '"/></a>';
        }
*/
      return html;
      }

  this.JSONP = function ( scriptdiv, streamdiv, callback, searchterm ) {
    this.streamdiv = streamdiv;
    this.InsertJSONP( scriptdiv,
      'http://labs.slate.com/stream/twitter?'
        + 'callback=' + callback
        + '&q=' + searchterm
      );
    }
    }

TwitterStream.prototype = new WaPoLabsStreams;

function CommentStream () {
  this.StreamToHtml = function ( stream ) {
    var list = '';
    for (var i = 0; i < stream.length; i++) {
      this.streamIndex = i;
      list += this.Format( stream[i] );
      }
    var sd = document.getElementById( this.streamdiv );
    sd.innerHTML = list;
    }

  this.Format = function ( comment ) {
    var iconpath = 'http://labs.slate.com/streams/i/';
    var html = '<span class="q">';
 
    var URLre = /([a-zA-Z]+:\/\/[\w\.-]+\/?\S+)/g;
    var ReplyRe = /@(\w+)/g;

// twitter: http://tr.im/mZ2f
// Facebook: http://tr.im/mZ1L

    if (comment.pic_square) {
      var fbtsRe = /\S+\s(\S+)\s(\d+)\s(\d+):(\d+).*/;
      var fbts = comment.time;
      fbts = fbts.replace(fbtsRe, "$1 $2 $3:$4");
      html += '<img src="' + comment.pic_square + '" width="36" alt="" />'
        + '<img src="' + iconpath + 'icon-facebook.png" class="i" height="16" width="16" alt="Facebook" />'
        + '<span><cite>' + comment.name + '</cite>'
        + '<dfn>' + fbts + '</dfn>'
        + '<q>' + comment.text + '</q>'
        + '</span>';
    } else if (comment.profile_image_url) {
      var tsRe = /\S+\s(\d+)\s(\S+)\s\d+\s(\d+):(\d+).*/;
      var ts = comment.created_at;
      ts = ts.replace(tsRe, "$2 $1 $3:$4");
      comment.text = comment.text.replace(URLre,
        '<a target="_new" href="$1">$1</a>');
      comment.text = comment.text.replace(ReplyRe,
        '<cite>@<a target="_new" href="http://twitter.com/$1">$1</a></cite>');

      html += '<img src="' + comment.profile_image_url + '" width="36" alt="" />'
        + '<img src="' + iconpath + 'icon-twitter.png" class="i" height="16" width="16" alt="Twitter" />'
        + '<span><cite>' + comment.from_user + '</cite>'
        + '<dfn>' + ts + '</dfn>'
        + '<q>' + comment.text + '</q>'
        + '</span>';
      }
  
    html += '</span>';
    return html;
    }

  this.JSONP = function ( scriptdiv, streamdiv, callback, searchterm, uid ) {
    this.streamdiv = streamdiv;
    // uid = '82778945149';
    this.InsertJSONP( scriptdiv,
      'http://labs.slate.com/stream/all?'
        + 'callback=' + callback
        + '&q=' + searchterm
        + '&uid=' + uid
      );
    }
  }

CommentStream.prototype = new WaPoLabsStreams;

// setInterval("t.JSONP('scriptdiv', 'streamdiv', 'callback', 'dog');", 10000);
