// twitterati badge -- updated for speculations.com on 3Mar09
// as always, use at your own risk
// copyright Kent Brewster 2008
// see http://kentbrewster.com/twitterati for info
( function() {
var trueName = '';
for (var i = 0; i < 16; i++) {
trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
window[trueName] = {};
var $ = window[trueName];
$.f = function() {
return {
runFunction : [],
init : function(target) {
var theScripts = document.getElementsByTagName('SCRIPT');
for (var i = 0; i < theScripts.length; i++) {
if (theScripts[i].src.match(target)) {
$.a = {};
if (theScripts[i].innerHTML) {
$.a = $.f.parseJson(theScripts[i].innerHTML);
}
if ($.a.err) {
alert('bad json!');
}
$.a.user = 'speculations';
$.f.loadDefaults();
$.f.buildStructure();
$.f.buildPresentation();
theScripts[i].parentNode.insertBefore($.s, theScripts[i]);
theScripts[i].parentNode.removeChild(theScripts[i]);
break;
}
}
},
parseJson : function(json) {
this.parseJson.data = json;
if ( typeof json !== 'string') {
return {"err":"trying to parse a non-string JSON object"};
}
try {
var f = Function(['var document,top,self,window,parent,Number,Date,Object,Function,',
'Array,String,Math,RegExp,Image,ActiveXObject;',
'return (' , json.replace(/<\!--.+-->/gim,'').replace(/\bfunction\b/g,'function') , ');'].join(''));
return f();
} catch (e) {
return {"err":"trouble parsing JSON object"};
}
},
loadDefaults : function() {
$.d = {
"addClass":"",
"user":"Speculations",
"headerText" : " and friends on Twitter",
"height" : 350,
"width" : 750,
"background" : "white",
"border" : "1px solid black",
"userFontSize" : "87%",
"userColor" : "inherit",
"headerBackground" : "#a00",
"headerColor" : "white",
"evenBackground" : "inherit",
"oddBackground" : "#eee",
"thumbnailBorder" : "1px solid black",
"thumbnailSize" : 24,
"padding" : 3
};
for (var k in $.d) { if ($.a[k] === undefined) { $.a[k] = $.d[k]; } }
},
buildPresentation : function () {
var ns = document.createElement('style');
document.getElementsByTagName('head')[0].appendChild(ns);
if (!window.createPopup) {
ns.appendChild(document.createTextNode(''));
ns.setAttribute("type", "text/css");
}
var s = document.styleSheets[document.styleSheets.length - 1];
var rules = {
"" : "{zoom:1;margin:0;padding:0;width:" + ($.a.width) + "px;background:" + $.a.background + ";border:" + $.a.border + ";font:13px/1.2em tahoma, veranda, arial, helvetica, clean, sans-serif;*font-size:small;*font:x-small;}",
"a" : "{cursor:pointer;text-decoration:none;color:#a00;}",
"a:hover" : "{text-decoration:underline;}",
"img":"{float:left; height:" + $.a.thumbnailSize + "px;width:" + $.a.thumbnailSize + "px;border:" + $.a.thumbnailBorder + ";margin-right:" + $.a.padding + ";}",
"cite, date":"{text-indent:3px; margin:0 0 0 4px;padding:0;display:block;font-style:normal;font-size:" + $.a.userFontSize + ";line-height:" + ($.a.thumbnailSize/2) + "px;}",
"date:after" : "{clear:both; content:\".\"; display:block; height:0; visibility:hidden; }",
"h3 a, h4 a" : "{font-size:92%; color:" + $.a.headerColor + ";}",
"h3" : "{margin:0;padding:" + $.a.padding + "px;font-weight:bold;background:" + $.a.headerBackground + " url('http://web.archive.org/web/20121029180903/http://twitter.com/favicon.ico') 2px 50% no-repeat;text-indent:16px;}",
"h3.loading" : "{background-image:url('http://web.archive.org/web/20121029180903/http://l.yimg.com/us.yimg.com/i/us/my/mw/anim_loading_sm.gif')}",
"h4" : "{font-weight:normal; background:" + $.a.headerBackground + ";text-align:right;margin:0;padding:" + $.a.padding + "px;}",
"ul":"{margin:0; padding:0; height:" + $.a.height + "px;width:" + $.a.width + "px;overflow:auto;}",
"p" : "{margin:" + $.a.padding + "px 0 0 0; padding:0; font-family:courier new, courier, monospaced; }",
"ul li":"{margin:0;padding:" + $.a.padding + "px;list-style:none}",
"ul li.odd" : "{background:" + $.a.oddBackground + ";}"
};
var ieRules = "";
for (r in rules) {
var selector = '.' + trueName + ' ' + r;
if (!window.createPopup) {
var theRule = document.createTextNode(selector + rules[r]);
ns.appendChild(theRule);
} else {
ieRules += selector + rules[r];
}
}
if (window.createPopup) { s.cssText = ieRules; }
},
buildStructure : function() {
$.s = document.createElement('DIV');
$.s.className = trueName;
if ($.a.addClass) {
$.s.className += ' ' + $.a.addClass;
}
$.s.h = document.createElement('H3');
$.s.h.a = document.createElement('A');
$.s.h.a.innerHTML = $.a.user + $.a.headerText;
$.s.h.a.href = 'http://web.archive.org/web/20121029180903/http://twitter.com/' + $.a.user;
$.s.h.a.target = '_twitter';
$.s.h.appendChild($.s.h.a);
$.s.appendChild($.s.h);
$.s.r = document.createElement('UL');
$.s.appendChild($.s.r);
$.s.f = document.createElement('H4');
$.s.f.innerHTML = 'Missing? Follow us on Twitter.';
$.s.appendChild($.s.f);
$.f.runSearch();
},
runSearch : function() {
$.s.h.className = 'loading';
$.s.r.innerHTML = '';
if (!$.f.runFunction) { $.f.runFunction = []; }
var n = $.f.runFunction.length;
var id = trueName + '.f.runFunction[' + n + ']';
$.f.runFunction[n] = function(r) {
delete($.f.runFunction[n]);
$.f.removeScript(id);
$.f.renderResult(r);
};
var url = 'http://web.archive.org/web/20121029180903/https://api.twitter.com/1/statuses/friends/' + $.a.user + '.json';
url += '?callback=' + id;
$.f.runScript(url, id);
},
changeUserTo : function(el) {
$.a.user = el.rel;
if (!el.rev) {
el.rev = $.a.user;
}
$.s.h.a.innerHTML = el.rev + $.a.headerText;
$.s.h.a.href = 'http://web.archive.org/web/20121029180903/http://twitter.com/' + el.rel;
$.f.runSearch();
},
renderResult: function(r) {
$.s.h.className = '';
for (var i = 0; i < r.length; i++) {
if (r[i].status) {
var li = document.createElement('LI');
if (i % 2) { li.className = 'odd'; }
var icon = document.createElement('A');
if (r[i].url) {
icon.href = r[i].rl;
icon.target = '_blank';
}
var img = document.createElement('IMG');
img.src = r[i].profile_image_url;
img.title = r[i].screen_name;
icon.appendChild(img);
li.appendChild(icon);
var user = document.createElement('CITE');
var a = document.createElement('A');
a.rel = r[i].screen_name || r[i].name;
a.rev = r[i].screen_name || r[i].name;
a.innerHTML = r[i].name || r[i].screen_name;
a.href = 'http://web.archive.org/web/20121029180903/http://twitter.com/' + r[i].screen_name;
a.onclick = function() {
$.f.changeUserTo(this);
return false;
};
user.appendChild(a);
li.appendChild(user);
var updated = document.createElement('DATE');
updated.innerHTML = r[i].created_at.split(/\+/)[0];
li.appendChild(updated);
var p = document.createElement('P');
console.log(r[i].status);
var raw = r[i].status.text;
var cooked = raw.replace(/\/u([^ ]+)/gi, "$1;");
cooked = cooked.replace(/http:\/\/([^ ]+)/g, "http://$1");
var woo = "@$1";
cooked = cooked.replace(/@([\w*]+)/g, woo);
cooked = cooked.replace(/#([\w*]+)/g, "#$1");
p.innerHTML = cooked;
li.appendChild(p);
var a = p.getElementsByTagName('A');
for (var j = 0; j < a.length; j++) {
if (a[j].className == 'changeUserTo') {
a[j].className = '';
a[j].href = 'http://web.archive.org/web/20121029180903/http://twitter.com/' + a[j].innerHTML;
a[j].rel = a[j].innerHTML;
a[j].onclick = function() {
$.f.changeUserTo(this);
return false;
}
}
}
$.s.r.appendChild(li);
}
}
},
runScript : function(url, id) {
var s = document.createElement('script');
s.id = id;
s.type ='text/javascript';
s.src = url;
document.getElementsByTagName('body')[0].appendChild(s);
},
removeScript : function(id) {
if (document.getElementById(id)) {
var s = document.getElementById(id);
s.parentNode.removeChild(s);
}
}
};
}();
// var thisScript = /^https?:\/\/[^\/]*r8ar.com\/twitterati.js$/;
var thisScript = /twitterati.js$/;
if(typeof window.addEventListener !== 'undefined') {
window.addEventListener('load', function() { $.f.init(thisScript); }, false);
} else if(typeof window.attachEvent !== 'undefined') {
window.attachEvent('onload', function() { $.f.init(thisScript); });
}
} )();