
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 URLre = /([a-zA-Z]+:\/\/[\w\.-]+\/?\S+)/g;
	post.message = post.message.replace(URLre, function(str){
	  if(str.length > 27){
	    return '<a href="' + str + '">' + str.substring(0,27) + '...</a>';
	  } else {
	    return '<a href="' + str + '">' + str + '</a>';
	  }
	});
    var list = ((this.streamIndex > 0)?'<li class="i a">' : '<li class="i">')
      + '<p><cite><a href="' + post.permalink + '" target="_blank">'
        + post.name + '</a></cite> &nbsp;'
      // + '<img src="' + post.pic_square + '" alt="" /><p>'
      + '<q>' + post.message + '</q>'
	  + '<br/><span class="time">' + ts + '</span></p>';
	if(parseInt(post.likes) > 0){
	  list += '<ul><li class="l">' + post.likes + ' people like this.</li></ul> ';
	}
    list += '</li>';
    if (post.comments) {
	  post.comments.sort(function(a,b){ return a.seconds - b.seconds });
      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/chris.cillizza';
		  post.comments[n].name = 'Anonymous';
          //post.comments[n].pic_square = 'http://static.ak.fbcdn.net/pics/d_silhouette.gif';
          }
		  
		post.comments[n].text = post.comments[n].text.replace(URLre, function(str){
		  if(str.length > 27){
			return '<a href="' + str + '">' + str.substring(0,27) + '...</a>';
		  } else {
			return '<a href="' + str + '">' + str + '</a>';
		  }
		});
		
        list += '<p><cite><span class="name">'
          + post.comments[n].name + '</span></cite> &nbsp;'
          + '<q>'+ post.comments[n].text + '</q>';
		  ts = post.comments[n].time;
		  ts = ts.replace(tsRe, "$1 $2 $3:$4");
		  list += '<br/><span class="time">' + ts + '</span> '
          + '</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
	  //'http://ec2-174-129-149-168.compute-1.amazonaws.com:8080/stream/facebook?uid=' + page
      + '&callback=' + callback
      );
    }
  }

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, function(str){
	  if(str.length > 27){
	    return '<a href="' + str + '">' + str.substring(0,27) + '...</a>';
	  } else {
	    return '<a href="' + str + '">' + str + '</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>'
		+ '<q>' + tweet.text + '</q>'
		+ ' <span class="time"><a href="http://twitter.com/'
		+ tweet.from_user + '/status/' + tweet.id + '">'
		+ ts + '</a></span>'    
		+ '</p>'
		+ '</li>';
      return html;
      }

  this.JSONP = function ( scriptdiv, streamdiv, callback, searchterm ) {
    this.streamdiv = streamdiv;
    this.InsertJSONP( scriptdiv,
      'http://labs.slate.com/stream/twitter?'
	  //'http://ec2-174-129-149-168.compute-1.amazonaws.com:8080/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;

    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 += '<a href="http://tr.im/mZ2f" class="g"><img src="' + comment.pic_square + '" width="36" alt="" /></a>'
        + '<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;
    this.InsertJSONP( scriptdiv,
      'http://labs.slate.com/stream/all?'
      //'http://ec2-174-129-149-168.compute-1.amazonaws.com:8080/stream/all?'
        + 'callback=' + callback
        + '&q=' + searchterm
        + '&uid=' + uid
      );
    }
  }

CommentStream.prototype = new WaPoLabsStreams;
