// jquery.tweet.js - See http://tweet.seaofclouds.com/ or https://github.com/seaofclouds/tweet for more info
// Copyright (c) 2008-2012 Todd Matthews & Steve Purcell
// Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
// Modified by Stan Scates for https://github.com/StanScates/Tweet.js-Mod
/* Copyright (c) 2013 Stan Scates
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function(c){"function"===typeof define&&define.amd?define(["jquery"],c):c(jQuery)})(function(c){c.fn.tweet=function(m){function f(b,a){if("string"===typeof b){var e=b,c;for(c in a)var f=a[c],e=e.replace(RegExp("{"+c+"}","g"),null===f?"":f);return e}return b(a)}function h(b,a){return function(){var e=[];this.each(function(){e.push(this.replace(b,a))});return c(e)}}function n(b,a){return b.replace(l,function(b){for(var d=/^[a-z]+:/i.test(b)?b:"http://"+b,c=0;c<a.length;++c){var f=a[c];if(f.url==d&& f.expanded_url){d=f.expanded_url;b=f.display_url;break}}return'<a href="'+d.replace(/</g,"<").replace(/>/g,"^>")+'">'+b.replace(/</g,"<").replace(/>/g,"^>")+"</a>"})}function p(b){var a=parseInt(((1<arguments.length?arguments[1]:new Date).getTime()-b)/1E3,10),c="";return c=1>a?"just now":60>a?a+" seconds ago":120>a?"about a minute ago":2700>a?"about "+parseInt(a/60,10).toString()+" minutes ago":7200>a?"about an hour ago":86400>a?"about "+parseInt(a/3600,10).toString()+" hours ago":172800> a?"about a day ago":"about "+parseInt(a/86400,10).toString()+" days ago"}function q(){var d=null===b.fetch?b.count:b.fetch,a={include_entities:1};if(b.list)return{host:b.twitter_api_url,url:"/1.1/lists/statuses.json",parameters:c.extend({},a,{list_id:b.list_id,slug:b.list,owner_screen_name:b.username,page:b.page,count:d,include_rts:b.retweets?1:0})};if(b.favorites)return{host:b.twitter_api_url,url:"/1.1/favorites/list.json",parameters:c.extend({},a,{list_id:b.list_id,screen_name:b.username,page:b.page, count:d})};if(null===b.query&&1===b.username.length)return{host:b.twitter_api_url,url:"/1.1/statuses/user_timeline.json",parameters:c.extend({},a,{screen_name:b.username,page:b.page,count:d,include_rts:b.retweets?1:0})};var e=b.query||"from:"+b.username.join(" OR from:");return{host:b.twitter_search_url,url:"/search.json",parameters:c.extend({},a,{page:b.page,q:e,rpp:d})}}function k(b,a){return a?"user"in b?b.user.profile_image_url_https:k(b,!1).replace(/^http:\/\/[a-z0-9]{1,3}\.twimg\.com\//,"https://s3.amazonaws.com/twitter_production/"): b.profile_image_url||b.user.profile_image_url}function r(d){var a={};a.item=d;a.source=d.source;a.name=d.from_user_name||d.user.name;a.screen_name=d.from_user||d.user.screen_name;a.avatar_size=b.avatar_size;a.avatar_url=k(d,"https:"===document.location.protocol);a.retweet="undefined"!=typeof d.retweeted_status;a.tweet_time=Date.parse(d.created_at.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i,"$1,$2$4$3"));var e;"auto"==b.join_text?(e=d.text,e=e.match(/^(@([A-Za-z0-9-_]+)) .*/i)?b.auto_join_text_reply: e.match(l)?b.auto_join_text_url:e.match(/^((\w+ed)|just) .*/im)?b.auto_join_text_ed:e.match(/^(\w*ing) .*/i)?b.auto_join_text_ing:b.auto_join_text_default):e=b.join_text;a.join_text=e;a.tweet_id=d.id_str;a.twitter_base="http://"+b.twitter_url+"/";a.user_url=a.twitter_base+a.screen_name;a.tweet_url=a.user_url+"/status/"+a.tweet_id;a.reply_url=a.twitter_base+"intent/tweet?in_reply_to="+a.tweet_id;a.retweet_url=a.twitter_base+"intent/retweet?tweet_id="+a.tweet_id;a.favorite_url=a.twitter_base+"intent/favorite?tweet_id="+ a.tweet_id;a.retweeted_screen_name=a.retweet&&d.retweeted_status.user.screen_name;a.tweet_relative_time=p(a.tweet_time);a.entities=d.entities?(d.entities.urls||[]).concat(d.entities.media||[]):[];a.tweet_raw_text=a.retweet?"RT @"+a.retweeted_screen_name+" "+d.retweeted_status.text:d.text;a.tweet_text=c([n(a.tweet_raw_text,a.entities)]).linkUser().linkHash()[0];a.tweet_text_fancy=c([a.tweet_text]).makeHeart()[0];a.user=f('<a class="tweet_user" href="{user_url}">{screen_name}</a>',a);a.join=b.join_text? f(' <span class="tweet_join">{join_text}</span> ',a):" ";a.avatar=a.avatar_size?f('<a class="tweet_avatar" href="{user_url}"><img src="{avatar_url}" height="{avatar_size}" width="{avatar_size}" alt="{screen_name}\'s avatar" title="{screen_name}\'s avatar" border="0"/></a>',a):"";a.time=f('<span class="tweet_time"><a href="{tweet_url}" title="view tweet on twitter">{tweet_relative_time}</a></span>',a);a.text=f('<span class="tweet_text">{tweet_text_fancy}</span>',a);a.reply_action=f('<a class="tweet_action tweet_reply" href="{reply_url}">reply</a>', a);a.retweet_action=f('<a class="tweet_action tweet_retweet" href="{retweet_url}">retweet</a>',a);a.favorite_action=f('<a class="tweet_action tweet_favorite" href="{favorite_url}">favorite</a>',a);return a}var b=c.extend({modpath:"/twitter/",username:null,list_id:null,list:null,favorites:!1,query:null,avatar_size:null,count:3,fetch:null,page:1,retweets:!0,intro_text:null,outro_text:null,join_text:null,auto_join_text_default:"i said,",auto_join_text_ed:"i",auto_join_text_ing:"i am",auto_join_text_reply:"i replied to", auto_join_text_url:"i was looking at",loading_text:null,refresh_interval:null,twitter_url:"twitter.com",twitter_api_url:"api.twitter.com",twitter_search_url:"search.twitter.com",template:"{avatar}{time}{join}{text}",comparator:function(b,a){return a.tweet_time-b.tweet_time},filter:function(b){return!0}},m),l=/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?\u00ab\u00bb\u201c\u201d\u2018\u2019]))/gi; c.extend({tweet:{t:f}});c.fn.extend({linkUser:h(/(^|[\W])@(\w+)/gi,'$1<span class="at">@</span><a href="http://'+b.twitter_url+'/$2">$2</a>'),linkHash:h(/(?:^| )[\#]+([\w\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0600-\u06ff]+)/gi,' <a href="http://'+b.twitter_search_url+"/search?q=&tag=$1&lang=all"+(b.username&&1==b.username.length&&!b.list?"&from="+b.username.join("%2BOR%2B"):"")+'" class="tweet_hashtag">#$1</a>'),makeHeart:h(/(<)+[3]/gi,"<tt class='heart'>♥</tt>")});return this.each(function(d, a){var e=c('<ul class="tweet_list">'),h='<p class="tweet_intro">'+b.intro_text+"</p>",l='<p class="tweet_outro">'+b.outro_text+"</p>",k=c('<p class="loading">'+b.loading_text+"</p>");b.username&&"string"==typeof b.username&&(b.username=[b.username]);c(a).unbind("tweet:load").bind("tweet:load",function(){b.loading_text&&c(a).empty().append(k);c.ajax({dataType:"json",type:"post",async:!1,url:b.modpath||"/twitter/",data:{request:q()},success:function(d,k){d.message&&console.log(d.message);var g=d.response; c(a).empty().append(e);b.intro_text&&e.before(h);e.empty();resp=void 0!==g.statuses?g.statuses:void 0!==g.results?g.results:g;void 0!==g.errors?(e.remove(),c(a).append("<p>The tweets can not be retrieved: "+g.errors[0].message+".</p>")):(g=c.map(resp,r),g=c.grep(g,b.filter).sort(b.comparator).slice(0,b.count),e.append(c.map(g,function(a){return"<li>"+f(b.template,a)+"</li>"}).join("")).children("li:first").addClass("tweet_first").end().children("li:odd").addClass("tweet_even").end().children("li:even").addClass("tweet_odd"), b.outro_text&&e.after(l),c(a).trigger("loaded").trigger(g?"empty":"full"),b.refresh_interval&&window.setTimeout(function(){c(a).trigger("tweet:load")},1E3*b.refresh_interval))}})}).trigger("tweet:load")})}});
/* ---------------------------------------------------------------------- */
/* Additional code for Twitter
/* ---------------------------------------------------------------------- */
jQuery(document).ready(function() {
$('.tweets').each(function() {
var $this = $(this),
username = $this.attr('data-username') ? $this.attr('data-username') : 'envato',
count = $this.attr('data-count') ? $this.attr('data-count') : 2;
$this.tweet({
username: username,
join_text: 'auto',
avatar_size: null,
count: count,
template: '{avatar}{join}{text}{time}',
auto_join_text_default: '',
auto_join_text_ed: '',
auto_join_text_ing: '',
auto_join_text_reply: '',
auto_join_text_url: '',
loading_text: 'loading tweets...',
modpath: './php/twitter/'
});
});
});