google.load("feeds","1");function TweetPlus(username){var results={feeds:new Array(),friends:new Array(),lookups:new Array()},markers={cursor:-1,feeds:0,friends:0,more:0,page:0,top:0,},messages={friends:['Finding friends','Finding more friends','Finding even more friends','Finding still more friends','Wondering why anyone would follow this many people','Thinking you need a lot of approval','Scheduling a psychiatric evaluation of you','Reassuring myself that the API will fail soon','Questioning my existence','Pondering the concept of futility','In this for the long haul','Catching up on my Tolstoy','Watching the original Star Wars trilogy',"Meh. Time to give the prequels another shot",'Watching the grass grow','Watching paint dry','Watching paint dry on growing grass','Wondering if '+username+' is really Guy Kawasaki','Seriously? Wow. Just wow']},that=this,errorInterval;this.load=function(){markers.more++;if(markers.more==1){this.getFriends();}else{this.lookupFeeds();}}
this.getFriends=function(){if(markers.more==1)this.friendMsg();if(markers.cursor!=0){this.setError("Sorry, this Twitter account is strangely unreachable.");$.getJSON('http://twitter.com/statuses/friends/'+this.username+'.json?cursor='+markers.cursor+'&callback=?',this.friendCallback);}else this.lookupFeeds();}
this.friendCallback=function(result){that.clearError();if(result.users.length){for(var i in result.users){var friend=result.users[i];var url=that.hasLength(friend.url);if(url){friend.url=url;results.friends.push(friend);}}}
if(result.next_cursor){markers.cursor=result.next_cursor;markers.page++;that.getFriends();}else that.lookupFeeds();}
this.friendMsg=function(){var string=messages.friends[markers.page]||messages.friends[messages.friends.length-1];string+=this.attr.msgAfter;this.setMsg(string);}
this.lookupFeeds=function(){this.setMsg('Interrogating Google for feed locations ('+this.getLookupProgress()+'%)'+this.attr.msgAfter);this.setError("Wow, Google isn't responding. How the mighty have fallen!");if(markers.friends<=results.friends.length&&this.count(results.lookups)<this.attr.max.feeds*markers.more){var friend=results.friends[markers.friends];if(friend&&!results.lookups[markers.friends]){google.feeds.lookupFeed(friend.url,this.lookupCallback);}else this.nextLookup();}else{this.getFeeds();}}
this.getLookupProgress=function(){var n=markers.friends-markers.top;var d=(results.friends.length>this.attr.max.feeds*markers.more)?this.attr.max.feeds:results.friends.length;return this.getProgress(n,d);}
this.nextLookup=function(){markers.friends++;this.clearError();this.lookupFeeds();}
this.lookupCallback=function(result){if(result.url){results.lookups[markers.friends]=result;}
that.nextLookup();}
this.getFeeds=function(){this.setMsg('Retrieving tasty feed content ('+this.getFeedProgress()+'%)'+this.attr.msgAfter);var top=this.topKey(results.lookups);if(markers.feeds<=top){var lookup=results.lookups[markers.feeds];if(lookup&&!results.feeds[markers.feeds]){var gFeed=new google.feeds.Feed(lookup.url);gFeed.setNumEntries(1);gFeed.setResultFormat(google.feeds.Feed.JSON_FORMAT);gFeed.load(this.feedCallback);}else this.nextFeed();}else this.draw();}
this.getFeedProgress=function(){return this.getProgress(markers.feeds-markers.top,this.topKey(results.lookups)-markers.top);}
this.getProgress=function(n,d){var prod=Math.floor(n/d*100);return(prod>=100||isNaN(prod))?99:prod;}
this.feedCallback=function(result){if(!result.error&&result.feed.entries.length){results.feeds[markers.feeds]=result;}
that.nextFeed();}
this.nextFeed=function(){markers.feeds++;this.getFeeds();}
this.draw=function(){var scrollTop=$(window).scrollTop();this.clearError();this.clearMsg();if(markers.more==1){var div=this.getElem('results').attr('class','constraint').html('<h2>Here&#8217;s where <a href="http://twitter.com/'+this.username+'">'+this.username+'</a>&#8217;s Twitter friends are blogging <span>(<a href="http://twitter.com/home?status=Here%27s+where+@'+this.username+'%27s+friends+are+blogging:+http://tweetplus.com/'+this.username+'">tweet this</a>)</span></h2>');var ul=$('<ul />').appendTo(div);this.popoutLinks($('a',div));}else{var ul=$('#results > ul');}
var changed=false;for(var key in results.feeds){var id='result-'+key;if(!document.getElementById(id)){changed=true;var feed=results.feeds[key].feed;var entry=feed.entries[0];var friend=results.friends[key];var url=results.lookups[key].url;var niceUrl=escape(url);var li=$('<li />').attr('id',id).html('<a href="/'+friend.screen_name+'" title="See where '+friend.name+'\'s friends are blogging"><img src="'+friend.profile_image_url+'" alt="" width="48" height="48" /></a>'+'<h3><a href="'+feed.link+'" class="title">'+feed.title+'</a> by <span class="at">@</span><a href="http://twitter.com/'+friend.screen_name+'" class="author" title="'+friend.name+'">'+friend.screen_name+'</a></h3>'+'<h4><a href="'+entry.link+'">'+entry.title+'</a> &#8212; Published '+this.formatDate(entry.publishedDate)+'</h4>'+'<p>'+entry.contentSnippet+' (<a href="'+entry.link+'">full entry</a>)</p>'+'<p class="forward">'+'<strong>Subscribe:</strong> '+'<a href="http://fusion.google.com/add?feedurl='+niceUrl+'" title="Add to Google Reader or Google Home Page">Google</a>, '+'<a href="http://www.netvibes.com/subscribe.php?url='+niceUrl+'" title="Plug into your Netvibes home page">Netvibes</a>, '+'<a href="http://add.my.yahoo.com/rss?url='+niceUrl+'" title="Add to your My Yahoo home page">Yahoo</a>, '+'<a href="'+url+'" title="Grab the vanilla feed">Other</a>'+'</p>'+'<p class="forward tweet"><a href="http://twitter.com/home?status=Discovered+%40'+friend.screen_name+'%27s+blog+thanks+to+%40tweetplus_com:+'+escape(feed.link)+'">Tweet this</a></p>').appendTo(ul);this.popoutLinks($('a:not(:has(img))',li));markers.top=key;}}
if(this.topKey(results.lookups)<this.topKey(results.friends)&&changed){var more=$('<li />').attr('id','more').append($('<a />').bind('click',{that:this},function(e){e.data.that.load();return false;}).attr('href','#').text('Load more')).appendTo(ul);}else if(!changed){alert("We're sorry, an unexpectedly empty result set means we overestimated the total number of results and thusly the usefulness of the \"Load More\" button. We've removed it to avoid any further frustration.");}
$(window).scrollTop(scrollTop);}
this.formatDate=function(string){var arr=string.split(' ');return this.getMonthFromString(arr[2])+'/'+arr[1]+'/'+arr[3];}
this.getMonthFromString=function(string){string=$.trim(string).toLowerCase();var months=["january","february","march","april","may","june","july","august","september","october","november","december"];for(var i in months){var month=months[i];if(month.indexOf(string)>-1){return parseInt(i)+1;}}
return false;}
this.setMsg=function(txt,className){if(markers.more==1){var msg=this.getElem('msg','p',true);msg.removeAttr('class').addClass(className).html(txt);}else{var more=$('#more');var span=$('span:first',more);if(span.length){span.html(txt);}else{more.html('<span>'+txt+'</span>');}
more.removeAttr('class').addClass(className);}}
this.clearMsg=function(){var query=markers.more==1?'msg':'more';$('#'+query).remove();}
this.setError=function(txt){$(document).oneTime(this.attr.timeout,function(i){that.setMsg('<strong>Error:</strong> '+txt,'error');});}
this.clearError=function(){$(document).stopTime();}
this.username=this.hasLength(username);this.attr={max:{feeds:15,friends:20000},friendsPerRequest:100,timeout:10*1000,msgAfter:'&#8230;'}}
TweetPlus.prototype.count=function(arr){var num=0;for(var i in arr){num++;}
return num;}
TweetPlus.prototype.getElem=function(id,tag,prepend){tag=tag||'div';var elem=$('#'+id);if(!elem.length){elem=$('<'+tag+' />').attr('id',id);if(prepend){elem.prependTo('body');}else{elem.appendTo('body');}}
return elem;}
TweetPlus.prototype.topKey=function(arr){var key=0;for(var i in arr){if(parseInt(i)>parseInt(key)){key=i;}}
return parseInt(key);}
TweetPlus.prototype.hasLength=function(string){string=$.trim(string);return string.length?string:false;}
TweetPlus.prototype.popoutLinks=function(query){query=query||'a';var a=$(query);a.click(function(){window.open($(this).attr('href'),'_blank');return false;});}
$(document).ready(function(){var instance=new TweetPlus($('#twitter-user').val());instance.popoutLinks('a:not( #go, h1 a)');if(instance.username){instance.load();}else $('body').attr('id','home');$('#go').click(function(){var value=instance.hasLength($('#twitter-user').val());if(value){window.location='/'+value;}
return false;});$('form').submit(function(){$('#go').click();return false;});$('#twitter-user').click(function(){$(this).select();});});