$(function() {
var params={
//q: examples[0], // query str
q: facebookquery, // query str
gender: 'any', // male female any
maxlen: 500, // max message len
classy: false // don't hide profile pics, names, links
};
document.location.search.replace(/[?&]([^&=]+)=([^&]+)/g,function(_,key,val) { params[key]=decodeURIComponent(val).replace(/\+/g,' '); });
function hide(name) { return params.classy ? name.replace(/[a-z]/g,'-') : name; } // just show initials unless in asehole mode
function encode(text) { return text.replace(/&/g,'&').replace(//g,'>'); }
function gender_img(gender) {
if (!gender) { return ''; }
//return '
';
return '';
}
function highlight(q,text) {
q = q.replace(/^\s+|\s+$/g,'') // whitespace trim
.replace(/\"/g,'') // remove quote marks
.replace(/[\/\.\*\+\?\|\(\)\[\]\{\}\\]/g,'\$&') // escape regexp chars
.replace(/\s+/gi,'|'); // seperate words with |
var re = new RegExp('\\b('+ q + ')\\b', 'gi');
return text.replace(re,'$&');
}
var ROW_HTML=['',
'
',
'',
''].join('\n');
if (params.classy) { ROW_HTML = ROW_HTML.replace(/<\/?a.+?>/g,''); }
function gender2class(gender) { return 'gender-'+(gender || 'any'); }
function update_gender(gender) {
$('#'+feed_dom_id).addClass('only-'+gender2class(gender));
}
$('#q').attr('value',params.q);
$('input:radio[value='+params.gender+']').attr('checked',true); update_gender(params.gender);
$('input:radio').click(function() { update_gender($(this).val()); });
if (!params.classy) {
$('body').addClass('asshole');
} else {
$('.black').live('mouseover mouseout', function(event) { $('#explain').toggle( event.type === 'mouseover' ); });
}
setTimeout(function() {
$('
').appendTo('body');
}, 1000);
//$.each(examples,function(_,example){ $('',{href:'?q='+encodeURIComponent(example),text:example}).appendTo($('#examples')); });
function loadMore() {
var page_remaining = $(document).height() - ($(window).height() + $(window).scrollTop());
if (page_remaining < 1000){
fetchNextPage();
}
}
$(window).scroll(loadMore);
var nextPage = false;
function fetchNextPage() {
if (nextPage) {
$.getJSON(nextPage + "&callback=?", handleSearchResults);
nextPage = false;
}
}
$.getJSON( 'http://graph.facebook.com/search?callback=?', {'q':params.q, 'type':'post'}, handleSearchResults);
function body(post) {
//var body = $.map(['message','caption','description','name'],function(prop) {return post[prop] || '';}).join(' ');
var body = post['message'];
if (body.length > params.maxlen) {
body = body.slice(0,params.maxlen-3) + '...';
}
body = '' + encode(body) + '
';
if (post['description']) {
body += '';
}
//return highlight( params.q, body );
return body;
}
function attachment(post) {
var attachment = $.map(['picture','link','name','caption','description'],function(prop) {return post[prop] || '';}).join(' ');
//var body = post['message'];
attachment = encode(attachment);
return attachment;
}
function location_text(location) {
if (!location) { return ''; }
return 'from ' + location;
}
// "picture": "http://external.ak.fbcdn.net/safe_image.php?d=8e14cd8417295af4ba6bfb5eb83c8b75&w=130&h=130&url=http%3A%2F%2Fi.ytimg.com%2Fvi%2Fr0kAyCprnR4%2F2.jpg",
// "link": "http://www.youtube.com/watch?v=r0kAyCprnR4",
// "source": "http://www.youtube.com/v/r0kAyCprnR4&autoplay=1",
// "name": "Phish - 04.05.98 - Cavern",
// "caption": "www.youtube.com",
// "description
function handleSearchResults(response, textStatus) {
//console.warn(response);
mylogger.log('FUNCTION: handleSearchResults');
if (response.paging && response.paging.next) {
nextPage = response.paging.next;
loadMore();
} else {
$("#facebook-loading").hide();
$('#facebook-finished').show();
}
$.each(response.data,function(_,post) {
if (!post || !post.from || !post.from.id) { return; } //TODO: when does this happen?
$.getJSON("http://graph.facebook.com/" + post.from.id + "?callback=?", function(user) {
var classname = gender2class(user.gender);
var html = ROW_HTML
.replace(/~UPDATEID~/g, post.id)
.replace(/~ROWCLASS~/g, classname)
.replace(/~USERID~/g, post.from.id)
.replace(/~SEX~/g, gender_img(user.gender))
.replace(/~NAME~/g,hide(post.from.name))
.replace(/~TIME~/g,window.get_relative_timestamp(post.created_time))
//.replace(/~FROM~/g,(user.location && user.location.name) || '')
.replace(/~FROM~/g,location_text(user.location && user.location.name))
//.replace(/~ATTACHMENT~/g, attachment(post)); // MUST BE LAST
.replace(/~MSG~/g, body(post)); // MUST BE LAST
$(html).appendTo($('#'+feed_dom_id));
});
});
}
window.get_relative_timestamp = function(timestamp) {
var c = new Date();
var t = window.iso2date(timestamp);
var d = c.getTime() - t.getTime();
var dY = Math.floor(d / (365 * 30 * 24 * 60 * 60 * 1000));
var dM = Math.floor(d / (30 * 24 * 60 * 60 * 1000));
var dD = Math.floor(d / (24 * 60 * 60 * 1000));
var dH = Math.floor(d / (60 * 60 * 1000));
var dN = Math.floor(d / (60 * 1000));
if (dY > 0) { return dY === 1? "1 year ago" : dY + " years ago"; }
if (dM > 0) { return dM === 1? "1 month ago" : dM + " months ago"; }
if (dD > 0) { return dD === 1? "1 day ago" : dD + " days ago"; }
if (dH > 0) { return dH === 1? "1 hour ago" : dH + " hours ago"; }
if (dN > 0) { return dN === 1? "1 minute ago" : dN + " minutes ago"; }
if (dN === 0) { return "less than a minute ago"; }
if (dN < 0) { return "in the future???"; }
};
// from http://delete.me.uk/2005/03/iso8601.html
window.iso2date = function(string) {
var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
var d = string.match(new RegExp(regexp));
var offset = 0;
var date = new Date(d[1], 0, 1);
if (d[3]) { date.setMonth(d[3] - 1); }
if (d[5]) { date.setDate(d[5]); }
if (d[7]) { date.setHours(d[7]); }
if (d[8]) { date.setMinutes(d[8]); }
if (d[10]) { date.setSeconds(d[10]); }
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
if (d[14]) {
offset = (Number(d[16]) * 60) + Number(d[17]);
offset *= ((d[15] == '-') ? 1 : -1);
}
offset -= date.getTimezoneOffset();
var time = (Number(date) + (offset * 60 * 1000));
var result = new Date();
result.setTime(Number(time));
return result;
};
});