(function(d){d.fn.jTweetsAnywhere=function(a){a=d.extend({username:"tbillenstein",list:null,searchParams:null,count:0,tweetProfileImagePresent:null,showTweetFeed:true,showFollowButton:false,showConnectButton:false,showLoginInfo:false,showTweetBox:false,loadingDecorator:defaultLoadingDecorator,mainDecorator:defaultMainDecorator,tweetFeedDecorator:defaultTweetFeedDecorator,tweetDecorator:defaultTweetDecorator,tweetProfileImageDecorator:defaultTweetProfileImageDecorator,tweetBodyDecorator:defaultTweetBodyDecorator, tweetTextDecorator:defaultTweetTextDecorator,tweetAttributesDecorator:defaultTweetAttributesDecorator,tweetTimestampDecorator:defaultTweetTimestampDecorator,tweetSourceDecorator:defaultTweetSourceDecorator,connectButtonDecorator:defaultConnectButtonDecorator,loginInfoDecorator:defaultLoginInfoDecorator,loginInfoContentDecorator:defaultLoginInfoContentDecorator,followButtonDecorator:defaultFollowButtonDecorator,tweetBoxDecorator:defaultTweetBoxDecorator,linkDecorator:defaultLinkDecorator,usernameDecorator:defaultUsernameDecorator, hashtagDecorator:defaultHashtagDecorator,errorDecorator:defaultErrorDecorator,tweetFilter:defaultTweetFilter,_tweetFeedConfig:{showSource:false,expandHovercards:false},_tweetBoxConfig:{counter:true,width:515,height:65,label:"What's happening?",defaultContent:"",onTweet:function(){}},_connectButtonConfig:{size:"medium"},_baseSelector:null,_baseElement:null,_tweetFeedElement:null,_followButtonElement:null,_loginInfoElement:null,_connectButtonElement:null,_tweetBoxElement:null,_loadingIndicatorElement:null, _loadingCount:0},a);if(a.mainDecorator){a._baseSelector=this.selector;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.showTweetFeed=="object")a._tweetFeedConfig=a.showTweetFeed;if(typeof a.showTweetBox=="object"){a._tweetBoxConfig=a.showTweetBox;a.showTweetBox=true}if(typeof a.showConnectButton== "object"){a._connectButtonConfig=a.showConnectButton;a.showConnectButton=true}a.count=validateRange(a.count,0,100);if(a.count==0||!a.showTweetFeed)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;if(!a._tweetFeedConfig.showSource)a.tweetSourceDecorator=null;d.ajaxSetup({cache:true});return this.each(function(){a._baseElement=d(this); a.tweetFeedDecorator&&a.loadingDecorator&&addLoadingIndicator(a);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);populateTweetFeed(a);populateAnywhereControls(a)})}}; 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+'"')+"/></a>")+"</div>"};defaultTweetBodyDecorator=function(a,b){var c="";if(b.tweetTextDecorator)c+=b.tweetTextDecorator(a,b);if(b.tweetAttributesDecorator)c+=b.tweetAttributesDecorator(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>"};defaultTweetAttributesDecorator=function(a,b){var c="";if(b.tweetTimestampDecorator||b.tweetSourceDecorator){c+='<span class="jta-tweet-attributes">';if(b.tweetTimestampDecorator)c+=b.tweetTimestampDecorator(a,b);if(b.tweetSourceDecorator)c+=b.tweetSourceDecorator(a,b);c+="</span>"}return c};defaultTweetTimestampDecorator=function(a){var b=formatDate(a.created_at),c=parseInt(((new Date).getTime()-Date.parse(b))/1E3),e="";if(c< 60)e+="less than a minute ago";else if(c<3600){c=parseInt((c+30)/60);e+=c+" minute"+(c==1?"":"s")+" ago"}else if(c<86400){c=parseInt((c+1800)/3600);e+="about "+c+" hour"+(c==1?"":"s")+" ago"}else{c=parseInt((c+43200)/86400);e+="about "+c+" day"+(c==1?"":"s")+" ago";b=new Date(b);c="AM";var f=b.getHours();if(f>12){f-=12;c="PM"}var g=b.getMinutes();e+=" ("+f+":"+((g<10?"0":"")+g)+" "+c+" "+(b.getMonth()+1)+"/"+b.getDate()+"/"+b.getFullYear()+")"}return'<span class="jta-tweet-timestamp"><a class="jta-tweet-timestamp-link" href="http://twitter.com/'+ (a.user?a.user.screen_name:a.from_user)+"/status/"+a.id+'" target="_blank">'+e+"</a></span>"};defaultTweetSourceDecorator=function(a){return'<span class="jta-tweet-source"> via '+('<span class="jta-tweet-source-link">'+a.source.replace(/\&lt\;/gi,"<").replace(/\&gt\;/gi,">").replace(/\&quot\;/gi,'"')+"</span>")+"</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+'"/></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>'};defaultErrorDecorator=function(a){return'<div class="jta-error">ERROR: '+a+"</div>"};defaultTweetFilter=function(){return true};updateLoginInfoElement=function(a,b){if(a._loginInfoElement&&a.loginInfoContentDecorator){a._loginInfoElement.children().remove(); a._loginInfoElement.append(a.loginInfoContentDecorator(a,b));d(a._baseSelector+" .jta-login-info-sign-out").bind("click",function(){twttr.anywhere.signOut()})}};getFeedUrl=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};isAnywherePresent=function(){return typeof twttr!="undefined"};populateTweetFeed=function(a){a.tweetFeedDecorator&&getJSON(a,getFeedUrl(a),function(b,c){d.each(c.results||c,function(e,f){b._tweetFeedElement&&b.tweetFilter(f,b)&&d(b._tweetFeedElement).append(b.tweetDecorator(f,b))});isAnywherePresent()&&twttr.anywhere(function(e){e(b._baseSelector+" .jta-tweet-list").hovercards({expanded:b._tweetFeedConfig.expandHovercards}); e(b._baseSelector+" .jta-tweet-profile-image img").hovercards({expanded:b._tweetFeedConfig.expandHovercards,username:function(f){return f.alt}})})})};populateAnywhereControls=function(a){isAnywherePresent()&&twttr.anywhere(function(b){a.tweetBoxDecorator&&b(a._baseSelector+" .jta-tweet-box").tweetBox(a._tweetBoxConfig);a.followButtonDecorator&&b(a._baseSelector+" .jta-follow-button").followButton(a.username);if(a.connectButtonDecorator){var c=d.extend({authComplete:function(){updateLoginInfoElement(a, b)},signOut:function(){updateLoginInfoElement(a,b)}},a._connectButtonConfig);b(a._baseSelector+" .jta-connect-button").connectButton(c);updateLoginInfoElement(a,b)}})};getJSON=function(a,b,c){preLoading(a);d.getJSON(b,function(e){postLoading(a);e.error?showError(a,e.error):c(a,e)})};preLoading=function(a){a._loadingCount++};postLoading=function(a){--a._loadingCount<=0&&removeLoadingIndicator(a)};addLoadingIndicator=function(a){if(!a._loadingIndicatorElement){a._loadingIndicatorElement=d(a.loadingDecorator(a)); a._baseElement.append(a._loadingIndicatorElement)}};removeLoadingIndicator=function(a){if(a._loadingIndicatorElement){a._loadingIndicatorElement.remove();a._loadingIndicatorElement=null}};formatDate=function(a){return a.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i,"$1,$2$4$3")};validateRange=function(a,b,c){if(a<b)a=b;if(a>c)a=c;return a};showError=function(a,b){a.errorDecorator&&d(a._baseElement).append(a.errorDecorator(b,a))}})(jQuery);