// Tweets Anywhere: http://thomasbillenstein.com/jTweetsAnywhere/

(function(d){d.fn.jTweetsAnywhere=function(a){var b=this.selector;a=d.extend({username:"tbillenstein",list:null,searchParams:null,count:0,tweetProfileImagePresent:null,showFollowButton:false,showConnectButton:false,showLoginInfo:false,showTweetBox:false,loadingDecorator:defaultLoadingDecorator,mainDecorator:defaultMainDecorator,tweetFeedDecorator:defaultTweetFeedDecorator,tweetDecorator:defaultTweetDecorator,tweetProfileImageDecorator:defaultTweetProfileImageDecorator,tweetBodyDecorator:defaultTweetBodyDecorator,
tweetTextDecorator:defaultTweetTextDecorator,tweetTimestampDecorator:defaultTweetTimestampDecorator,connectButtonDecorator:defaultConnectButtonDecorator,loginInfoDecorator:defaultLoginInfoDecorator,loginInfoContentDecorator:defaultLoginInfoContentDecorator,followButtonDecorator:defaultFollowButtonDecorator,tweetBoxDecorator:defaultTweetBoxDecorator,linkDecorator:defaultLinkDecorator,usernameDecorator:defaultUsernameDecorator,hashtagDecorator:defaultHashtagDecorator,_tweetBoxConfig:{counter:true,width:515,
height:65,label:"What's happening?",defaultContent:"",onTweet:function(){}},_connectButtonConfig:{size:"medium"},_baseElement:null,_tweetFeedElement:null,_followButtonElement:null,_loginInfoElement:null,_connectButtonElement:null,_tweetBoxElement:null,_loadingIndicatorElement:null},a);if(a.mainDecorator){if(typeof a.username!="string"){if(!a.searchParams)a.searchParams=["q=from:"+a.username.join(" OR from:")];a.username=a.username[0]}if(a.tweetProfileImagePresent==null)a.tweetProfileImagePresent=
(a.list||a.searchParams)&&a.tweetProfileImageDecorator;if(typeof a.showTweetBox=="object"){a._tweetBoxConfig=a.showTweetBox;a.showTweetBox=true}if(typeof a.showConnectButton=="object"){a._connectButtonConfig=a.showConnectButton;a.showConnectButton=true}if(a.count<=0)a.tweetFeedDecorator=null;if(!a.showFollowButton)a.followButtonDecorator=null;if(!a.showTweetBox)a.tweetBoxDecorator=null;if(!a.showConnectButton)a.connectButtonDecorator=null;if(!a.showLoginInfo)a.loginInfoDecorator=null;d.ajaxSetup({cache:true});
return this.each(function(){a._baseElement=d(this);if(a.tweetFeedDecorator&&a.loadingDecorator){a._loadingIndicatorElement=d(a.loadingDecorator(a));a._baseElement.append(a._loadingIndicatorElement)}a._tweetFeedElement=a.tweetFeedDecorator?d(a.tweetFeedDecorator(a)):null;a._followButtonElement=a.followButtonDecorator?d(a.followButtonDecorator(a)):null;a._tweetBoxElement=a.tweetBoxDecorator?d(a.tweetBoxDecorator(a)):null;a._connectButtonElement=a.connectButtonDecorator?d(a.connectButtonDecorator(a)):
null;a._loginInfoElement=a.loginInfoDecorator?d(a.loginInfoDecorator(a)):null;a.mainDecorator&&a.mainDecorator(a);a.tweetFeedDecorator&&d.getJSON(getTwitterUrl(a),function(c){a._loadingIndicatorElement&&a._loadingIndicatorElement.remove();if(c.error)d(a._baseElement).append('<div class="jta-error">ERROR: '+c.error+"</div>");else{d.each(c.results||c,function(e,f){a._tweetFeedElement&&d(a._tweetFeedElement).append(a.tweetDecorator(f,a))});isAnywherePresent()&&twttr.anywhere(function(e){e(b).hovercards();
e(b+" img").hovercards({username:function(f){return f.alt}})})}});isAnywherePresent()&&twttr.anywhere(function(c){a.tweetBoxDecorator&&c(b+" .jta-tweet-box").tweetBox(a._tweetBoxConfig);a.followButtonDecorator&&c(b+" .jta-follow-button").followButton(a.username);if(a.connectButtonDecorator){var e=d.extend({authComplete:function(){updateLoginInfoElement(b,a,c)},signOut:function(){updateLoginInfoElement(b,a,c)}},a._connectButtonConfig);c(b+" .jta-connect-button").connectButton(e);updateLoginInfoElement(b,
a,c)}})})}};defaultMainDecorator=function(a){a._tweetFeedElement&&a._baseElement.append(a._tweetFeedElement);a._connectButtonElement&&a._baseElement.append(a._connectButtonElement);a._loginInfoElement&&a._baseElement.append(a._loginInfoElement);a._followButtonElement&&a._baseElement.append(a._followButtonElement);a._tweetBoxElement&&a._baseElement.append(a._tweetBoxElement)};defaultTweetFeedDecorator=function(){return'<ul class="jta-tweet-list"></ul>'};defaultTweetDecorator=function(a,b){var c="";
if(b.tweetProfileImagePresent)c+=b.tweetProfileImageDecorator(a,b);if(b.tweetBodyDecorator)c+=b.tweetBodyDecorator(a,b);c+='<div class="jta-clear">&nbsp;</div>';return'<li class="jta-tweet-list-item">'+c+"</li>"};defaultTweetProfileImageDecorator=function(a){var b=a.user?a.user.screen_name:a.from_user;return'<div class="jta-tweet-profile-image">'+('<a class="jta-tweet-profile-image-link" href="http://twitter.com/'+b+'" target="_blank"><img src="'+(a.user?a.user.profile_image_url:a.profile_image_url)+
'" alt="'+b+'"'+(isAnywherePresent()?"":' title="'+b+'"')+' width="48" height="48" border="0"/></a>')+"</div>"};defaultTweetBodyDecorator=function(a,b){var c="";if(b.tweetTextDecorator)c+=b.tweetTextDecorator(a,b);if(b.tweetTimestampDecorator)c+=b.tweetTimestampDecorator(a,b);return'<div class="jta-tweet-body '+(b.tweetProfileImagePresent?"jta-tweet-body-list-profile-image-present":"")+'">'+c+"</div>"};defaultTweetTextDecorator=function(a,b){var c=b.linkDecorator?b.linkDecorator(a.text,b):a.text;
if(b.usernameDecorator)c=b.usernameDecorator(c,b);if(b.hashtagDecorator)c=b.hashtagDecorator(c,b);return'<span class="tweet-text">'+c+"</span>"};defaultTweetTimestampDecorator=function(a){var b=formatDate(a.created_at),c=parseInt(((new Date).getTime()-Date.parse(b))/1E3);a="";if(c<60)a+="less than a minute ago";else if(c<3600){c=parseInt((c+30)/60);a+=c+" minute"+(c==1?"":"s")+" ago"}else if(c<86400){c=parseInt((c+1800)/3600);a+="about "+c+" hour"+(c==1?"":"s")+" ago"}else{c=parseInt((c+43200)/86400);
a+="about "+c+" day"+(c==1?"":"s")+" ago";b=new Date(b);c="AM";var e=b.getHours();if(e>12){e-=12;c="PM"}var f=b.getMinutes();a+=" ("+e+":"+((f<10?"0":"")+f)+" "+c+" "+(b.getMonth()+1)+"/"+b.getDate()+"/"+b.getFullYear()+")"}return'<span class="jta-tweet-timestamp">'+a+"</span>"};defaultConnectButtonDecorator=function(){return'<div class="jta-connect-button"></div>'};defaultLoginInfoDecorator=function(){return'<div class="jta-login-info"></div>'};defaultLoginInfoContentDecorator=function(a,b){var c=
"";if(b.isConnected()){c=b.currentUser.data("screen_name");var e=b.currentUser.data("profile_image_url");c='<div class="jta-login-info-profile-image"><a href="http://twitter.com/'+c+'" target="_blank"><img src="'+e+'" alt="'+c+'" title="'+c+'" width="48" height="48" border="0"/></a></div><div class="jta-login-info-block"><div class="jta-login-info-screen-name"><a href="http://twitter.com/'+c+'" target="_blank">'+c+'</a></div><div class="jta-login-info-sign-out">Sign out</div></div><div class="jta-clear">&nbsp;</div>'}return c};
defaultFollowButtonDecorator=function(){return'<div class="jta-follow-button"></div>'};defaultTweetBoxDecorator=function(){return'<div class="jta-tweet-box"></div>'};defaultLinkDecorator=function(a){return a.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1" class="jta-tweet-a jta-tweet-link" target="_blank" rel="nofollow">$1</a>')};defaultUsernameDecorator=function(a){return isAnywherePresent()?a:a.replace(/@([a-zA-Z0-9_]+)/gi,'@<a href="http://twitter.com/$1" class="jta-tweet-a twitter-anywhere-user" target="_blank" rel="nofollow">$1</a>')};
defaultHashtagDecorator=function(a){return a.replace(/#([a-zA-Z0-9_]+)/gi,'<a href="http://search.twitter.com/search?q=%23$1" class="jta-tweet-a jta-tweet-hashtag" title="#$1" target="_blank" rel="nofollow">#$1</a>')};defaultLoadingDecorator=function(){return'<div class="jta-loading">loading ...</div>'};getTwitterUrl=function(a){var b="https:"==document.location.protocol?"https:":"http:";if(a.searchParams){var c=a.searchParams instanceof Array?a.searchParams.join("&"):a.searchParams;b+="//search.twitter.com/search.json?"+
c+"&rpp="+a.count+"&callback=?"}else b+=a.list?"//api.twitter.com/1/"+a.username+"/lists/"+a.list+"/statuses.json?per_page="+a.count+"&callback=?":"//api.twitter.com/1/statuses/user_timeline.json?screen_name="+a.username+"&count="+a.count+"&callback=?";return b};updateLoginInfoElement=function(a,b,c){if(b._loginInfoElement&&b.loginInfoContentDecorator){b._loginInfoElement.children().remove();b._loginInfoElement.append(b.loginInfoContentDecorator(b,c));d(a+" .jta-login-info-sign-out").bind("click",
function(){twttr.anywhere.signOut()})}};isAnywherePresent=function(){return typeof twttr!="undefined"};formatDate=function(a){return a.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i,"$1,$2$4$3")}})(jQuery);
