var AMP = AMP || {'startup': []}; AMP.load = function() { $.each(AMP.startup, function(i, e) { AMP[e].call(); }); }; // http://jdbartlett.github.com/innershiv | WTFPL License AMP.d = undefined; AMP.r = undefined; AMP.innershiv = function(h, u) { if (AMP.d === undefined) { AMP.d = document.createElement('div'); AMP.r = document.createDocumentFragment(); /*@cc_on AMP.d.style.display = 'none';@*/ } var e = AMP.d.cloneNode(true); /*@cc_on document.body.appendChild(e);@*/ e.innerHTML = h.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); /*@cc_on document.body.removeChild(e);@*/ if (u === false) return e.childNodes; var f = AMP.r.cloneNode(true), i = e.childNodes.length; while (i--) f.appendChild(e.firstChild); return f; }; AMP.reactions = function() { var that = {}; that.rpp = 10; that.fsel = true; var html = '
\ \
\
\

\
\
%text%
\ \
\
'; /** * relative time calculator * @param {string} dateString twitter date string returned from Twitter API. * @return {string} relative time like "2 minutes ago". */ var timeAgo = function(dateString) { var rightNow = new Date(); var then = new Date(dateString); if ($.browser.msie) { // IE can't parse these crazy Ruby dates then = Date.parse(dateString.replace(/( \+)/, ' UTC$1')); } var diff = rightNow - then; var second = 1000, minute = second * 60, hour = minute * 60, day = hour * 24, week = day * 7; if (isNaN(diff) || diff < 0) { return ''; } // return blank string if unknown if (diff < second * 2) { return 'recién'; } // within 2 seconds if (diff < minute) { return 'hace cerca de ' + Math.floor(diff / second) + ' segundos'; } if (diff < minute * 2) { return 'hace cerca de 1 minuto'; } if (diff < hour) { return 'hace cerca de ' + Math.floor(diff / minute) + ' minutos'; } if (diff < hour * 2) { return 'hace cerca de 1 hora'; } if (diff < day) { return 'hace cerca de ' + Math.floor(diff / hour) + ' horas'; } if (diff > day && diff < day * 2) { return 'ayer'; } if (diff < day * 365) { return 'hace cerca de ' + Math.floor(diff / day) + ' días'; } else { return 'hace más de un año'; } }; /** * The Twitalinkahashifyer! * http://www.dustindiaz.com/basement/ify.html * Eg: * ify.clean('your tweet text'); */ var ify = { link: function(tweet) { return tweet.replace(/\b(((https*\:\/\/)|www\.)[^\"\']+?)(([!?,.\)]+)?(\s|$))/g, function(link, m1, m2, m3, m4) { var http = m2.match(/w/) ? 'http://' : ''; return '' + ((m1.length > 25) ? m1.substr(0, 24) + '...' : m1) + '' + m4; }); }, at: function(tweet) { return tweet.replace(/\B[@@]([a-zA-Z0-9_]{1,20})/g, function(m, username) { return '@' + username + ''; }); }, list: function(tweet) { return tweet.replace(/\B[@@]([a-zA-Z0-9_]{1,20}\/\w+)/g, function(m, userlist) { return '@' + userlist + ''; }); }, hash: function(tweet) { return tweet.replace(/(^|\s+)#(\w+)/gi, function(m, before, hash) { return before + '#' + hash + ''; }); }, clean: function(tweet) { return this.hash(this.at(this.list(this.link(tweet)))); } }; var create_tweet = function(twt) { var h = html.replace(/%uimage%/g, twt.profile_image_url) .replace(/%uname%/g, twt.from_user) .replace(/%text%/g, ify.clean(twt.text)) .replace(/%ulink%/g, twt.from_user) .replace(/%time%/g, timeAgo(twt.created_at)) .replace(/%reply%/g, twt.id_str); if ((that.frst) && (that.fsel)) { h = h.replace('%first%', 'first'); that.fsel = false; } else { h = h.replace('%first%', ''); } return h; }; var on_success = function(data, status, xhr) { var $r = $('#reactions'); var $mr = $('#more-reactions'); var rc = data.results; $.each(rc, function(i, e) { $r.append(AMP.innershiv(create_tweet(e))); }); if ((rc.length > 0) && (that.frst)) { $('#reactions-wrap').show(); } if (rc.length >= that.rpp) { $mr.attr('rel', parseInt($mr.attr('rel'), 10) + 1).show(); } else { $mr.hide(); } }; var on_error = function(xhr, status, error) {}; var getData = function(frst) { that.frst = frst; var page = $('#more-reactions').attr('rel'); var url = window.location.href; $.ajax({'url': 'http://search.twitter.com/search.json', 'async': true, 'dataType': 'jsonp', 'type': 'GET', 'data': {'q': url, 'page': page, 'rpp': that.rpp, 'result_type': 'mixed'}, 'success': on_success, 'error': on_error }); }; that.getData = getData; return that; }; AMP.reacciones = function() { AMP.react = AMP.reactions(); AMP.react.getData(true); $('#more-reactions').click(function(ev) { ev.stopPropagation(); AMP.react.getData(false); return false; }); }; AMP.setup_slide_header = function() { $('#site-title').waypoint({ handler: function(event, direction) { if (direction == 'down') { $('#mini-title').show(); $('#home-blogs').hide(); } else { $('#mini-title').hide(); $('#home-blogs').show(); } } }); }; AMP.setup_twshare = function() { if ($('#twshare').length == 0) { return; } new TWTR.Widget({ version: 2, id: 'twshare', type: 'search', search: 'blogs.infobae.com -bahiadigital', interval: 30000, title: 'infobae.com', subject: 'Blogs de Opinión de Infobae', width: 300, height: 500, theme: { shell: {background: '#ffffff', color: '#ffffff'}, tweets: {background: '#ffffff', color: '#444444', links: '#4272DB'} }, features: {scrollbar: false, loop: true, live: true, behavior: 'default'} }).render().start(); }; AMP.processTweet = function(tweet) { var rgxUrl = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; var rgxUserLink = /([@])([\w]+)/ig; var rgxSearchLink = /([#])([\w]+)/ig; return tweet .replace(rgxUrl, '$1') .replace(rgxUserLink, '$1$2') .replace(rgxSearchLink, '$1$2'); }; AMP.setup_notas = function() { AMP.reacciones(); }; AMP.getTalkers = function(url, count) { tt = this; $.getJSON('http://search.twitter.com/search.json?callback=?&rpp=' + count + '&q=' + url+'%20-from%3Ainfobaeamerica%20-from%3Ainfobae', function(data) { $.each(data, function(i, tweets) { if (tweets.length != undefined) { if (tweets[0] != undefined) { if (tweets[0].created_at != undefined) { $('#tweetbar .twshared').show(); for (var i = 0; i < tweets.length; i++) { var txxt = '\n'; txxt += '
' + '\n'; txxt += '
' + '\n'; txxt += '
' + '\n'; txxt += '
' + '\n'; txxt += '

' + tweets[i].from_user_name + '

' + '\n'; txxt += '

' + tt.processTweet(tweets[i].text) + '

' + '\n'; txxt += '
' + '\n'; txxt += '
' + '\n'; txxt += '
' + '\n\n'; $('#twimages').append(txxt); } var textocantidad; if (i == 1) { textocantidad = i + ' persona'; } else { textocantidad = 'últimas ' + i + ' personas'; /* if (i <= 29) { textocantidad = i + ' personas'; } else { textocantidad = '+' + i + ' personas'; } */ } $('#twcounter').html(textocantidad); } } } }); $('.tooltip-wrapper').tooltip(); }); }; AMP.setup_front_page = function() { AMP.getTalkers('blogs.infobae.com', 30); }; $(function() { AMP.load(); });