var afterTweets = 0;function populateTwitterFeed(start,proxy) {window['tweetstart'] = start;if (proxy == false || proxy == undefined) {url = '/utilities/twtprox.php';msg = "Fetching tweets... ";/*var firewallcatch = setTimeout("askFirewall("+start+");",10000);*/}else if (proxy == true) {url = '/utilities/twtprox.php';msg = "Attempting to bypass firewall... ";var firewallcatch = setTimeout("twitterPopupMsg('Sorry, still could not reach Twitter. '); removeTwitterPopupMsg(4000);",15000);}
twitterPopupMsg(msg);
$.getScript(url);}function tweetsCallback(object) {
$.each(object, function (i, item) {text = item.text;if (text.substr(0,1) == '@') return true;else if (text.substr(0,19) == 'New post on my blog') return true;original_text = text;
if (item.entities.urls.length>0) {urls = item.entities.urls;for (x in urls) {original = urls[x].url;real = urls[x].expanded_url;display = urls[x].display_url;if (display == undefined) display = original.replace('http://','');if (real == undefined) real = original;link = '<a href="'+real+'">'+display+'</a>';
text = text.replace(original,link);original_text = original_text.replace(original,real);}

}

twitimg = twitterImages(original_text);text = formatTweet(text);img = item.user.profile_image_url;img = '<img src="' + img + '" width="48"/>';if (item.geo != undefined) coords = item.geo.coordinates;else coords = '';if (item.place != undefined) {place = item.place.full_name;if (coords != '') placelink = '<div class="tweetplace">from <a target="_blank" class="tweetplacelink" href="http://maps.google.com/?q='+coords+'">'+place+'</a></div>';else placelink = '<div class="tweetplace">from <a target="_blank" class="tweetplacelink" href="http://maps.google.com/?q='+place+'">'+place+'</a></div>';} else if (coords != '') {placelink = '<div class="tweetplace">from <a target="_blank" class="tweetplacelink" href="http://maps.google.com/?q='+coords+'">here</a></div>';	} else placelink = '';ts = parse_date(item.created_at) / 1000;when = ts;when = dateOut(ts * 1000);id = item.id;tweetlink = 'http://twitter.com/aarond/status/' + id;name = item.user.screen_name;userlink = 'http://twitter.com/'+name;newdiv = '<div class="tweet"><div class="tweeter">'+img+'<br /><a href="'+userlink+'">Follow Me</a></div><div class="tweetbody"><a class="tweetlink" href="'+tweetlink+'">&#187;</a> '+ text + ' ' + '</div>'+twitimg+'<div class="tweetdate"><a href="'+tweetlink+'">'+when+'</a> '+placelink+'</div></div>';insertToProperTime(ts,newdiv,window['tweetstart']);});unredundTweets();$('div.tweet, .tweethideme').fadeIn(900);
removeTwitterPopupMsg(500);}function twitterPopupMsg(text) {if (text == '' || text == undefined) return false;if ($('div#twitterloading').length>0) {removeTwitterPopupMsg(0);setTimeout("twitterPopupMsg('"+text+"');",600);return false;}$('body').append('<div id="twitterloading">'+text+'</div>');$('div#twitterloading').css('bottom','-40px').show().animate({ bottom: '0px' }, 500);;
}function removeTwitterPopupMsg(delay) {if (delay == undefined) delay = 500;setTimeout("$('div#twitterloading').animate({bottom: '-30px'},500);",delay);setTimeout("$('div#twitterloading').remove();",500+delay);}var askFWtimeout = '';function askFirewall(start) {output = "Are you behind a firewall that blocks Twitter?";output += '<input type="button" value="Yes" onclick="populateTwitterFeed(0,true); clearTimeout(askFWtimeout);" />';output += '<input type="button" value="No" onclick="removeTwitterPopupMsg(0);" />';twitterPopupMsg(output);askFWtimeout = setTimeout("removeTwitterPopupMsg(0);",10000);
}function insertToProperTime(timestamp,content,start) {/*Place content (probably a tweet) into the blog body in its correct chronological place based on a timestamp*/if (start == undefined) start = 0;acount = $('div#main div.article').length;x=0;$('div#main div.article').each(function(){x++;thisTS = $(this).attr('id');if (timestamp > thisTS && start == 0) {$(this).before(content);return false;}else if (timestamp < thisTS && x==acount && afterTweets < 3) {$(this).after(content); afterTweets++; }else if (x==acount && afterTweets >= 3) return false;
});}
function unredundTweets() {/*The above functions add a fully formatted tweet to the body (with icon and everything).This function funds clusters of tweets and makes it so each cluster only has one icon,and hides any tweets above a given threshold.*/count = 0;limit = 7; /* <-- given threshold */$('div#main div.tweet').each(function(){nextdiv = $(this).next('div');prevdiv = $(this).prev('div');if (count==0) {
$(this).before('<h2 class="tweethideme">From Twitter</h2>'); }if (count==limit ) {$(this).before('<a class="bucketshow" href="#" onclick="$(this).next(\'div.tweetbucket\').fadeIn(1200); $(this).remove(); return false;">&#187; More tweets. Click to show.</a><div style="display: none;" class="tweetbucket"></div>');}if (count>=limit ) { bucket = $(this).prev('div.tweetbucket');$(this).appendTo(bucket );}if ($(nextdiv).attr('class') == 'tweet') {$(nextdiv).children('.tweeter').html(''); count++; } else {/* Next div isn't a tweet, so this is the last tweet in this cluster */$(this).css('margin-bottom','55px');if (count>=limit ) {/* There are more tweets than should be displayed at once, so update the "More tweets" text to include an accurate count of the # of hidden tweets */
manymore = (count-limit) + 1;$(this).parent().prev('a.bucketshow').html('&#187; ' + manymore +' more tweet'+((manymore>1)? 's':'')+', click to show');}count=0;
}
});}function parse_date(date_str) {/* Converts twitter's date format into unix timestamp */return Date.parse(date_str.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));}function formatTweet(string) {//string = linkUrlizeString(string);//string = linkUser(string);
return string;}
function linkUrlizeString(string,value) {/* Detects URLs in a string and turns them into links. If the secong argument(value) is specified, it will be used for the link's text, rather than the url itself */pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/g;urls = string.match(pattern);for (x in urls) {url = urls[x];if (value == undefined) val = url;else val = value;string = string.replace(url, '<a href="' + url + '">'+val+'</a>');}
return string;} 
function twitterImages(text) {twitpics = twitpicString(text);flickr = false;flickr = flickrPicString(text);imgurs = imgurPicString(text);twitimg = '';if (twitpics) {for (x in twitpics) {twitpic = twitpics[x];twitimg += '<a href="'+twitpic.url+'"><img class="twitpic" src="http://twitpic.com/show/mini/'+twitpic.code+'" /></a>';}} if (flickr != false) {twitimg += '<a id="twitflickrlink-'+flickr+'" href="#"><img class="twitpic" id="twitflickrpic-'+flickr+'" src="/images/icons/blank.gif" /></a>';flickrThumbnail(flickr);}if (imgurs) {for (x in imgurs) {imgur = imgurs[x];twitimg += '<a href="'+imgur.url+'"><img class="twitpic" src="http://i.imgur.com/'+imgur.code+'s.jpg" /></a>';}}return twitimg;}function twitpicString(str) {pattern = /http:\/\/twitpic\.com\/(\S+)/ig;pics = str.match(pattern);if (!pics) return false;returnpics = [];for (x in pics) {pic = pics[x];pattern = /http:\/\/twitpic\.com\/(\S+)/i;code = pic.match(pattern);returnpics[x] = { url: pic, code: code[1] };} return returnpics;}function imgurPicString(str) {pattern = /http:\/\/[^\/]*imgur\.com\/(\S+)/ig;pics = str.match(pattern);if (!pics) return false;returnpics = [];for (x in pics) {pic = pics[x];pattern = /http:\/\/[^\/]*imgur\.com\/([^\/.]+)/i;code = pic.match(pattern);returnpics[x] = { url: pic, code: code[1] };} return returnpics;}function flickrPicString(str) {pattern = /http:\/\/flic\.kr\/p\/(\S+)/i;pics = str.match(pattern);if (pics) {code = pics[1];code = base58_decode(code);return code;}return false;}
function linkUser(str) { /* Detects Twitter @usernames within the tweets and linkifies them */regexp = /[\@]+([A-Za-z0-9-_]+)/gi;str = str.replace(regexp,"<a class=\"tweetat\" href=\"http://twitter.com/$1\">@$1</a>");return str;}
function dateOut(dd) {var d_names = new Array("Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday");
var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var d = new Date(dd);var curr_day = d.getDay();var curr_date = d.getDate();if (curr_date < 10) curr_date = '0'+curr_date;var curr_month = d.getMonth();var curr_year = d.getFullYear();var curr_hour = d.getHours();
if (curr_hour < 12) a_p = "am";else a_p = "pm";if (curr_hour == 0) curr_hour = 12;if (curr_hour > 12) curr_hour = curr_hour - 12;var curr_min = d.getMinutes();if (curr_min < 10) curr_min = '0'+curr_min;return(curr_hour + ':' + curr_min +a_p+' ' + d_names[curr_day] + ", " + m_names[curr_month] + ' ' + curr_date + ', ' + curr_year);}function base58_decode(snipcode){var alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' ;var num = snipcode.length ;var decoded = 0 ;var multi = 1 ;for ( var i = (num-1) ; i >= 0 ; i-- ){decoded = decoded + multi * alphabet.indexOf( snipcode[i] ) ;multi = multi * alphabet.length ;}return decoded;}var flickrapi = '349657c177b9727598991b940e6a3628';function flickrThumbnail(id) {url = 'http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key='+flickrapi+'&photo_id='+id+'&format=json&jsoncallback=?';$.getJSON(url,function(object) {object = object.photo;server = object.server;farm = object.farm;nsid = object.owner.nsid;secret = object.secret;pageurl = object.urls.url[0]['_content'];
img= 'http://farm'+farm+'.static.flickr.com/'+server+'/'+id+'_'+secret+'_s.jpg';$('a#twitflickrlink-'+id).attr('href',pageurl);$('img#twitflickrpic-'+id).attr('src',img);});}

