jQuery(function(){
var TrendExplanation = {
dialog:null,
trend:null,
// Open a dialog with the explanation of the given trend.
open:function(trend) {
this.dialog = new twttr.widget.Dialog({draggable: true});
this.trend = trend;
this.setup().explain();
},
// Set the dialog's title and show it.
setup:function() {
this.dialog.$title.html('About "' + TrendExplanation.trend + '"');
this.dialog.$find('.twttr-dialog-content').html('<p>Loading...</p>');
this.dialog.show();
return this;
},
// Fetch an explanation for this TrendExplanation's current trend.
explain:function() {
$.ajax({
url: 'http://www.whatthetrend.com/api/trend/getByName/' + encodeURIComponent(TrendExplanation.trend) + '/jsonp?callback=',
dataType: 'jsonp',
success: function(data) {
if(data['api']['trend'] && data['api']['trend']['blurb']['text'] != '') {
TrendExplanation.update(data['api']['trend']['blurb']['text']);
} else {
TrendExplanation.update('Unable to find details about that trend.');
}
},
error:function(data) {
TrendExplanation.update('There was a problem attempting to find details about that trend.');
}
});
return this;
},
// Update the dialog for this TrendExplanation with the given explanation.
update:function(explanation) {
this.dialog.$find('.twttr-dialog-content').html('<p style="margin-top:5px;">' + explanation + '</p>');
this.dialog.$find('.twttr-dialog-content').append(footer);
return this;
},
// Hide this TrendExplanation's dialog.
hide:function() {
this.dialog.hide();
return this;
}
};
// When moving over this trend-item, show the explain-trend link.
// If a Trend doesn't have an explain-trend link, add one.
$('.trend-item').live('mouseover', function(index) {
if($(this).find('.explain-trend').length == 0) {
$(this).append($("<a/>", {
"href": "http://whatthetrend.com/trend/" + encodeURIComponent($(this).find('.trend-link').text()),
"class": "explain-trend",
"target": "_blank",
"text": '?'
}));
}
$(this).find('.explain-trend').show();
});
// When moving off of this trend-item, hide the explain-trend link.
$('.trend-item').live('mouseout', function(index) {
$(this).find('.explain-trend').hide();
});
// When clicking an explain-link, find and display this Trend's explanation.
$('.explain-trend').live('click', function() {
TrendExplanation.open($(this).parent().find('.trend-link').text());
return false;
});
// Build a footer with details about this script that can be attached to the dialog showing a Trend's explanation.
var footer = $("<p/>").css({'border-top': '1px solid #eeeeee', 'font-size': 'smaller', 'margin-top': '10px', 'padding-top': '5px'})
.append($('<span>Explanation from @<a href="/#!/whatthetrend">whatthetrend</a>.</span>'));
});