function prettyDate(time){var twdate=(new Date(time));if(isNaN(twdate)){twdate=(new Date(time.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/,"$1 $2 $4 $3 UTC")))}var now=new Date();diff=((now.getTime()-twdate.getTime())/1000);day_diff=Math.floor(diff/86400);if(isNaN(day_diff)||day_diff<0||day_diff>=31){return"weeks ago"}else{return day_diff===0&&(diff<60&&"just now"||diff<120&&"1 minute ago"||diff<3600&&Math.floor(diff/60)+" minutes ago"||diff<7200&&"1 hour ago"||diff<86400&&Math.floor(diff/3600)+" hours ago")||day_diff==1&&"Yesterday"||day_diff<7&&day_diff+" days ago"||day_diff<14&&"1 week ago"||day_diff<31&&Math.ceil(day_diff/7)+" weeks ago"}}String.prototype.parseURL=function(){return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[.=A-Za-z0-9-_:%&\?\/]*[=A-Za-z0-9-_:%&\?\/]/g,function(url){return'<a href="'+url+'" target="_blank">'+url+'</a>'})};String.prototype.parseUsername=function(){return this.replace(/(^|\s|.)@(\w+)/g,function(u){var username=jQuery.trim(u.replace(".@","").replace("@",""));return' <a target="_blank" href="http://twitter.com/'+username+'">@'+username+'</a>'})};String.prototype.parseHashtag=function(){return this.replace(/[#]+[A-Za-z0-9-_]+/g,function(t){var tag=t.replace("#","%23");return'<a href="http://search.twitter.com/search?q='+tag+'" target="_blank">'+t+'</a>'})};function trim(str){return str.replace(/^\s+|\s+$/g,'')}function encodeTweet(tweet){if(!tweet){return tweet}tweet=tweet.parseURL().parseUsername().parseHashtag();return tweet}var twitterGet={ops:{rateURL:"http://api.twitter.com/1/account/rate_limit_status.json?callback=?",interval:120,feedcount:0,livealready:false,twitterlive:false,replyLink:false,startFirstLoad:function(){},eachLoad:function(id){}},getAll:function(){this.pollTwitter();if(!this.ops.livealready){this.updater=setInterval('twitterGet.pollTwitter();',this.ops.interval*1000);this.ops.livealready=true;this.ops.startFirstLoad()}return this},pollTwitter:function(){var self=this;$.getJSON(this.ops.rateURL,function(data){if(data.remaining_hits>=self.ops.feedcount){if(data.remaining_hits<20){self.changeInterval((self.ops.interval*4))}else if(data.remaining_hits<50){self.changeInterval((self.ops.interval*2))}else if(data.remaining_hits>100){self.changeInterval(self.ops.interval)}self.getAllOnce();self.twitterlive=true}else if(!self.twitterlive){self.getAllOnceGoogle()}})},changeInterval:function(newint){clearInterval(this.updater);this.ops.interval=newint;this.updater=setInterval('twitterGet.pollTwitter();',this.ops.interval*1000);return this},stop:function(){clearInterval(this.updater);this.ops.livealready=false;return this},setOps:function(newops){this.ops=$.extend(this.ops,newops);if(this.ops.interval){this.changeInterval(this.ops.interval)}return this},getAllOnce:function(){var self=this;this.ops.feedcount=$(".twitstore:visible").length;$(".twitstore:visible").each(function(){self.getOne($(this),$(this).data("twoptions"))});return this},getAllOnceGoogle:function(){var self=this;this.ops.feedcount=$(".twitstore:visible").length;$(".twitstore:visible").each(function(){self.loadTwitterFeed($(this),$(this).data("twoptions"))});return this},getOne:function(id,options){var self=this;var update=false;var twitterURLj=(options.list===false)?'http://api.twitter.com/1/statuses/user_timeline/'+options.handle+'.json?count='+(options.limit+2)+'&include_rts='+options.include_rts+'&callback=?':'http://api.twitter.com/1/'+options.handle+'/lists/'+options.list+'/statuses.json?per_page='+(options.limit+2)+'&callback=?';$.getJSON(twitterURLj,function(data){if(!data.error){if(!options.lastID||options.lastID!==data[0].id_str){id.data("twoptions").lastID=data[0].id_str;update=true;self.cleanSubTweets(id)}$.each(data,function(i,item){if(i>=options.limit){return false}var text,prof_img,screen_name,created_at;if(update){text=(options.include_rts&&item.retweeted_status)?item.retweeted_status.text.replace(/&#39;/ig,"'"):item.text.replace(/&#39;/ig,"'");prof_img=(options.include_rts&&item.retweeted_status)?item.retweeted_status.user.profile_image_url:item.user.profile_image_url;screen_name=(options.include_rts&&item.retweeted_status)?item.retweeted_status.user.screen_name:item.user.screen_name;created_at=(options.include_rts&&item.retweeted_status)?item.retweeted_status.created_at:item.created_at;self.displayTweet(id,encodeTweet(text),prof_img,screen_name,item.id_str,prettyDate(created_at),options.fade,item.id_str,text,item.source,i,options)}else{created_at=(options.include_rts&&item.retweeted_status.created_at)?item.retweeted_status.created_at:item.created_at;source=(item.source)?" via "+item.source.replace(/(<([^>]+)>)/ig,""):"";id.find(".twitterDate").eq(i).html(prettyDate(created_at))}});id.data("twoptions").eachLoad(id.data("twoptions").idname)}})},add:function(id,handle,options){var what=$("#"+id);options=(options)?options:{};options=$.extend({handle:handle,include_rts:false,limit:1,spacer:{},list:false,fade:true,lastID:false,replyLink:(this.ops.replyLink||false),eachLoad:this.ops.eachLoad},options);options.idname=id;this.ops.feedcount++;what.find(".primeTweet").eq(0).clone().css({"display":"none"}).removeClass("primeTweet").addClass("modelTweet").appendTo(what);what.addClass("twitstore").data("twoptions",options);if(!options.nogoogle){this.loadTwitterFeed(what,options)}return this},stopOne:function(id){$("#"+id).removeClass("twitstore");return this},restartOne:function(id){$("#"+id).addClass("twitstore");return this},cleanSubTweets:function(id){id.find(".asubTweet, .twSpacer").remove()},loadTwitterFeed:function(id,options){var self=this;var twitterURL;if(options.list===false){twitterURL='http://api.twitter.com/statuses/user_timeline/'+options.handle+'.atom?count='+(options.limit+2)+'&include_rts='+options.include_rts}else{twitterURL='http://api.twitter.com/1/'+options.handle+'/lists/'+options.list+'/statuses.atom?count='+(options.limit+2)+'&include_rts='+options.include_rts}var twitterFeed=new google.feeds.Feed(twitterURL);twitterFeed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);twitterFeed.setNumEntries(options.limit);self.cleanSubTweets(id);twitterFeed.load(function(result){if(!result.error){$.each(result.feed.entries,function(i,item){if(i>=options.limit){return false}var node=$(item.xmlNode);var icon=node.find('link[rel=image]').attr('href');var content=item.content.replace(/\n/,' ').replace(/\r/,' ');var name=content.replace(/:.*/g,'');var tweetraw=content.replace(/[^:]*: /,'');var twitterURL=node.find('author uri').text();var time=prettyDate(item.publishedDate);var msg=encodeTweet(tweetraw.replace(/&#39;/ig,"'"));var linkAry=item.link.split(/\//);var twid=linkAry[linkAry.length-1];self.displayTweet(id,msg,icon,name,twitterURL,time,options.fade,twid,tweetraw,undefined,i,options)})}})},displayTweet:function(id,msg,icon,name,twitterURL,time,fade,twid,tweetraw,source,which,options){var tweet;if(which===0){tweet=id.find(".primeTweet")}else{tweet=id.find(".modelTweet").clone().addClass("asubTweet").removeClass("modelTweet").appendTo(id)}source=(source)?" via "+source.replace(/(<([^>]+)>)/ig,""):"";tweet.hide();tweet.find(".twitterMsg").html(msg);tweet.find(".twitterDate").html(time);tweet.find(".twitterIcon").html('<a href="https://twitter.com/intent/user?screen_name='+name+'&related=thedemocrats,dwstweets,woodhouseb" target="_blank"><img src="'+icon+'" alt="@'+name+'" /></a>');tweet.find(".twitterTitle").html('<a href="https://twitter.com/intent/user?screen_name='+name+'&related=thedemocrats,dwstweets,woodhouseb" target="_blank">@'+name+'</a>');tweet.find(".twitterReply").html(' <a href="https://twitter.com/intent/tweet?in_reply_to='+twid+'&related=thedemocrats,dwstweets,woodhouseb" target="_blank">reply</a>');tweet.find(".twitterFav").html('<a href="https://twitter.com/intent/favorite?tweet_id='+twid+'&related=thedemocrats,dwstweets,woodhouseb" target="_blank">favorite</a>');tweet.find(".twitterRetweet").html(' <a href="https://twitter.com/intent/retweet?tweet_id='+twid+'&related=thedemocrats,dwstweets,woodhouseb" target="_blank">retweet</a>');if(options.spacer[which+1]){$(options.spacer[which+1]).addClass("twSpacer").appendTo(id)}if(fade){tweet.fadeIn(200)}else{tweet.show()}if(options.displayExtra){options.displayExtra(msg,icon,name,twitterURL,time,'twitter-status-'+twid,tweetraw,twid)}}};$(".primeTweet,.asubTweet").live('mouseenter',function(){$(this).find(".twitterOver").css("display","inline")}).live('mouseleave',function(){$(this).find(".twitterOver").css("display","none")});
