/*
* Twitter Widget (sidebar)
* version 1
*/
function TwitterDisplay(operation, criteria, resultsPerPage, containerId, callback, tagLabel) {
this.operation = jQuery.trim(operation);
this.criteria = jQuery.trim(criteria);
this.resultsPerPage = resultsPerPage;
this.containerId = containerId;
this.callback = callback;
this.currentPage = 0;
this.totalPages = 500;
if(tagLabel) {
this.tagLabel = tagLabel;
} else {
this.tagLabel = "Tag(s) : ";
}
if(operation == 'statuses') {
this.requestURL = "http://twitter.com/statuses/user_timeline/" + this.criteria + ".json";
this.nextPageUrlSuffix = "?page=1&count=" + this.resultsPerPage
} else {
this.requestURL = "http://search.twitter.com/search.json";
this.nextPageUrlSuffix = "?page=1&rpp=" + this.resultsPerPage + "&q=" + this.criteria;
}
this.prevPageUrlSuffix = "";
}
TwitterDisplay.prototype.displayNextPage = function(){
if (this.nextPageUrlSuffix != "") {
this.currentPage = parseInt(this.currentPage) + 1;
var requestURL = this.requestURL + this.nextPageUrlSuffix + "&rand=" + parseInt(Math.random() * 99999999) + "&callback=?";
jQuery.getJSON(requestURL, this.callback);
jQuery("#" + this.containerId).html("
Loading ...
");
}
}
TwitterDisplay.prototype.displayPrevPage = function() {
if(this.prevPageUrlSuffix != "") {
this.currentPage = parseInt(this.currentPage) - 1;
var requestURL = this.requestURL + this.prevPageUrlSuffix + "&rand=" + parseInt(Math.random()*99999999) + "&callback=?" ;
jQuery.getJSON(requestURL, this.callback);
jQuery("#" + this.containerId).html("Loading ...
");
}
}
TwitterDisplay.prototype.displayTweets = function(json, status) {
var jsonRoot = json;
var container = jQuery("#" + this.containerId);
if(json.error) {
container.html(json.error);
return;
}
if(json.results) {
json = json.results;
} else if(json[0].user && json[0].user.statuses_count) {
this.totalPages = Math.ceil(json[0].user.statuses_count/this.resultsPerPage);
}
container.html("");
var usedCriteria = unescape(this.criteria);
if(usedCriteria.indexOf("#") == 0) {
container.append("" + this.tagLabel + ""
+ unescape(this.criteria) + "
");
}
container.append("");
holder = jQuery("#" + this.containerId + " .tweetsHolder");
jQuery.each(json, function(i, item){
var userId = item.user ? item.user.screen_name : item.from_user;
var userText = userId.toLowerCase();
var followText = "";
if(WMG.TwitterWidget.TwitterMap[userId]) {
userObj = WMG.TwitterWidget.TwitterMap[userId];
userText = '' +
userObj.name + '';
followText = '';
}
tweet = item.text.twitterize();
holder.append("");
});
if (jsonRoot.previous_page) {
this.prevPageUrlSuffix = jsonRoot.previous_page;
this.setPrevNavigation(true);
} else if(this.operation == 'statuses' && this.currentPage > 1) {
this.prevPageUrlSuffix = "?page=" + (this.currentPage - 1) + "&count=" + this.resultsPerPage;
this.setPrevNavigation(true);
} else {
this.prevPageUrlSuffix = "";
this.setPrevNavigation(false);
}
if (jsonRoot.next_page) {
this.nextPageUrlSuffix = jsonRoot.next_page;
this.setNextNavigation(true);
} else if(this.operation == 'statuses' && this.currentPage < this.totalPages) {
this.nextPageUrlSuffix = "?page=" + (this.currentPage + 1) + "&count=" + this.resultsPerPage;
this.setNextNavigation(true);
} else {
this.nextPageUrlSuffix = "";
this.setNextNavigation(false);
}
/* Twitter Hovercard implementation start */
if(jQuery('body').attr("id") == 'home' || jQuery('body').attr("id") == 'twitter') {
twttr.anywhere(function (T) {
//T("#twitterContainer .message").hovercards({infer:true});
T("#twitterContainer .follow").hovercards({infer:true});
});
}
/* Twitter Hovercard implementation end */
}
TwitterDisplay.prototype.setNextNavigation = function(enabled) {
var id = "#" + this.containerId + "Next";
if(enabled) {
jQuery(id).removeClass('tcNextDisabled');
jQuery(id).addClass('tcNextEnabled');
jQuery(id).attr('href', '#');
} else {
jQuery(id).removeClass('tcNextEnabled');
jQuery(id).addClass('tcNextDisabled');
jQuery(id).attr('href', 'javascript:void(0)');
}
}
TwitterDisplay.prototype.setPrevNavigation = function(enabled) {
var id = "#" + this.containerId + "Prev";
if(enabled) {
jQuery(id).removeClass('tcPreviousDisabled');
jQuery(id).addClass('tcPreviousEnabled');
jQuery(id).attr('href', '#');
} else {
jQuery(id).removeClass('tcPreviousEnabled');
jQuery(id).addClass('tcPreviousDisabled');
jQuery(id).attr('href', 'javascript:void(0)');
}
}
function delegateTwitterCallback (twitterDisplay, json, status) {
twitterDisplay.displayTweets(json, status);
}
String.prototype.twitterize = function() {
var tweet = this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(str){
return '' + str + '';
});
tweet = tweet.replace(/([@])([\w]+)/g, '$1');
return tweet;
};
String.prototype.fixTwitterDateString = function() {
var dateArray = this.split(" ");
var tweetDateString;
if(parseInt(dateArray[1], 10) > 0) {
tweetDateString = dateArray[2] + " " + dateArray[1] + ", " + dateArray[3] + " " + dateArray[4];
} else {
tweetDateString = dateArray[1] + " " + dateArray[2] + ", " + dateArray[5] + " " + dateArray[3];
}
var tweetTime = Date.parse(tweetDateString);
var currDate = new Date();
var timeDiff = parseInt(currDate.getTime() - tweetTime) / 1000;
timeDiff = timeDiff + (currDate.getTimezoneOffset() * 60);
var formattedString = '';
if (timeDiff < 60) {
formattedString = 'a minute ago';
}
else if (timeDiff < (45 * 60)) {
formattedString = Math.round(timeDiff / 60) + ' minutes ago';
}
else if (timeDiff < (1.5 * 60 * 60)) {
formattedString = '1 hour ago';
}
else if (timeDiff < (24 * 60 * 60)) {
formattedString = Math.round(timeDiff / (60 *60)) + ' hours ago';
}
else if (timeDiff < (1.5 * 24 * 60 * 60)) {
formattedString = '1 day ago';
}
else if (timeDiff < (7 * 24 * 60 * 60)) {
formattedString = Math.round(timeDiff / (24 * 60 * 60)) + ' days ago';
}
else if (timeDiff < (1.5 * 7 * 24 * 60 * 60)) {
formattedString = '1 week ago';
}
else if (timeDiff < (4 * 7 * 24 * 60 * 60)) {
formattedString = Math.round(timeDiff / (7 * 24 * 60 * 60)) + ' weeks ago';
}
else if (timeDiff < (1.5 * 4 * 7 * 24 * 60 * 60)) {
formattedString = '1 month ago';
}
else if (timeDiff < (12 * 4 * 7 * 24 * 60 * 60)) {
formattedString = Math.round(timeDiff / (4 * 7 * 24 * 60 * 60)) + ' months ago';
}
else if (timeDiff < (1.5 * 12 * 4 * 7 * 24 * 60 * 60)) {
formattedString = '1 year ago';
}
else if (timeDiff > (1.5 * 12 * 4 * 7 * 24 * 60 * 60)) {
formattedString = Math.round(timeDiff / (12 * 4 * 7 * 24 * 60 * 60)) + ' years ago';
}
else {
formattedString = tweetDateString;
}
return formattedString;
}
/* Display */
var twitterModule = new TwitterDisplay(WMG.TwitterWidget.operation, escape(WMG.TwitterWidget.twitterCriteria), WMG.TwitterWidget.noOfTweets, 'twitterContainer', function(json, status){
delegateTwitterCallback(twitterModule, json, status);
}, WMG.TwitterWidget.tagLabel );
twitterModule.displayNextPage();
var tAnywhereUrl = "http://platform.twitter.com/anywhere.js?id=" + WMG.TwitterWidget.id + "&v=" + WMG.TwitterWidget.v;
document.write('