if(typeof Pierre != 'undefined') {
Pierre.widgets.Userstats = function() {
this.el = null; // the wrapper element; typically a DIV
this.els = {}; // all of the elements of this Div
this.user = null; // a user object returned from the getRequest
this.els.results = new Array(); // the wrapper div for all of the comment divs
this.init = function(el) {
this.el = (typeof el === 'string') ? plib.Dom.get(el) : el;
if(this.el == null) {return false;}
this.buildTabs();
this.fetchTags();
this.buildHeader();
this.fetchResults();
}
this.fetchResults = function(sort) {
var config = {
success: {name:'jsoncallback', scope:this, callback:this.handleResultResponse}
}
this.count = 6;
this.sort = sort || "";
var url = 'http://decision08.newsvine.com/_labs/politics/getQuestions?count='+this.count+'&sort='+this.sort;
plib.Ajax.getRequest(url, config);
}
this.fetchTags = function() {
var config = {
success: {name:'jsoncallback', scope:this, callback:this.handleTagsResponse}
}
var url = 'http://decision08.newsvine.com/_labs/politics/getRelatedTags?c=6';
plib.Ajax.getRequest(url, config);
}
this.handleResultResponse = function(resp) {
var resp = resp.response;
if(!resp || !resp.user || !resp.question) {
this.el.innerHTML = 'The initialization failed.';
return false;
}
this.user = resp.user;
this.question = resp.question;
this.buildResults();
stackedChart('results_set');
document.getElementById("msnbcTabs").getElementsByTagName("DIV")[0].style.display = "none";
document.getElementById("msnbcTabs").getElementsByTagName("DIV")[0].style.display = "block";
}
this.handleTagsResponse = function(resp2) {
var resp2 = resp2.response;
if(!resp2 || !resp2.tags) {
this.el.innerHTML = 'The initialization failed.';
return false;
}
this.tags = resp2.tags;
this.buildTags();
}
this.buildTabs = function() {
this.els.tabs = plib.Dom.createElement('div', {className:'inslice-tabs-wrapper'}, this.el);
var insliceTabsDiv = plib.Dom.createElement('div', {id:'most-vote-navigation',className:'inslice-tabs'}, this.els.tabs);
var insliceTabsA = plib.Dom.createElement('ul', {}, insliceTabsDiv);
this.els.tab1 = plib.Dom.createElement('li', {className: 'resultstab1 selected', innerHTML:'Most recent'}, insliceTabsA);
this.els.tab2 = plib.Dom.createElement('li', {className: 'resultstab2', innerHTML:'Most votes (last 7 days)'}, insliceTabsA);
this.els.tab3 = plib.Dom.createElement('li', {className: 'resultstab3', innerHTML:'Most comments (last 7 days)'}, insliceTabsA);
plib.Event.on(this.els.tab1, 'click', function(e){this.refreshResults()}, this, true);
plib.Event.on(this.els.tab2, 'click', function(e){this.refreshResults("v")}, this, true);
plib.Event.on(this.els.tab3, 'click', function(e){this.refreshResults("c")}, this, true);
}
this.buildTags = function() {
var insliceTabsDiv2 = plib.Dom.createElement('div', {id:'most-vote-category',className:'inslice-tabs'}, this.els.tabs);
var insliceTabsB = plib.Dom.createElement('ul', {}, insliceTabsDiv2);
var insliceTabsB1 = plib.Dom.createElement('li', {innerHTML:'More live votes by category:'}, insliceTabsB);
for(var i=0; i < this.tags.length; i++) {
if(this.tags[i]!="politics"){
plib.Dom.createElement('li', {innerHTML:''+this.tags[i]+''}, insliceTabsB);
}
}
}
this.buildHeader = function() {
var headerDiv = plib.Dom.createElement('div', {className:'question_row'}, this.el);
var headerDateDiv = plib.Dom.createElement('div', {className:'question_date'}, headerDiv);
var headerDate = plib.Dom.createElement('h4', {innerHTML: 'Date'}, headerDateDiv);
var headerTitleDiv = plib.Dom.createElement('div', {className:'question_title'}, headerDiv);
var headerTitle = plib.Dom.createElement('h4', {innerHTML: 'Question'}, headerTitleDiv);
var headerVotedDiv = plib.Dom.createElement('div', {className:'question_voted'}, headerDiv);
var headerVoted = plib.Dom.createElement('h4', {innerHTML: 'Voted?'}, headerVotedDiv);
var headerGraphDiv = plib.Dom.createElement('div', {className:'question_graph'}, headerDiv);
var headerGraph = plib.Dom.createElement('h4', {innerHTML: 'Top answer'}, headerGraphDiv);
var headerVotesDiv = plib.Dom.createElement('div', {className:'question_votes'}, headerDiv);
var headerVotes = plib.Dom.createElement('h4', {innerHTML: 'Votes'}, headerVotesDiv);
var headerCommentsDiv = plib.Dom.createElement('div', {className:'question_comments'}, headerDiv);
var headerComments = plib.Dom.createElement('h4', {innerHTML: 'Comments'}, headerCommentsDiv);
}
this.refreshResults = function(sort) {
this.sort = sort || "";
var delete0 = plib.Dom.get('pierre_widgets_userstats');
var delete1 = plib.Dom.get('results_set');
if(delete1){delete0.removeChild(delete1);}
this.fetchResults(this.sort);
this.switchResultsTabs(this.sort);
}
this.switchResultsTabs = function(t) {
var removeTab1 = plib.Dom.removeClass(this.els.tab1,'selected');
var removeTab2 = plib.Dom.removeClass(this.els.tab2,'selected');
var removeTab3 = plib.Dom.removeClass(this.els.tab3,'selected');
switch (t) {
case "v":
tabToSwitchTo = this.els.tab2;
break;
case "c":
tabToSwitchTo = this.els.tab3;
break;
default:
tabToSwitchTo = this.els.tab1;
}
var addSelected = plib.Dom.addClass(tabToSwitchTo,'selected');
}
this.addCommas = function(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
this.buildResults = function() {
var resultsDiv = plib.Dom.createElement('div', {id:'results_set'}, this.el);
for(var i=0; i < this.question.length; i++) {
var question = this.question[i];
var questionId = question.questionId;
var questionTitle = question.title;
var questionVoted = "";
if (question.userVote) {
questionVoted = "
";
} else {
questionVoted = " ";
}
var questionUrl = question.url;
var questionDate = new Date(question.datePublished);
var displayDate = (questionDate.getMonth()+1)+"/"+questionDate.getDate()+"/"+questionDate.getFullYear();
var questionOptions = question.options;
var questionVotes = question.totalVotes;
var allowComments = question.allowComments;
var hasShortExplanation = question.hasShortExplanation;
var questionComments = question.totalComments;
var graphHTML = "";
var questionPct = "";
var rowVotes = "";
var rowComments = "";
if(i%2){
this.els.results[i] = plib.Dom.createElement('div', {className:'question_row alt'}, resultsDiv);
} else {
this.els.results[i] = plib.Dom.createElement('div', {className:'question_row'}, resultsDiv);
}
var rowDateDiv = plib.Dom.createElement('div', {className:'question_date',innerHTML: displayDate}, this.els.results[i]);
var rowTitleDiv = plib.Dom.createElement('div', {className:'question_title'}, this.els.results[i]);
var rowTitleURL = ""+questionTitle+"";
var rowTitle = plib.Dom.createElement('h2', {innerHTML: rowTitleURL}, rowTitleDiv);
var rowVotedDiv = plib.Dom.createElement('div', {className:'question_voted',innerHTML: questionVoted}, this.els.results[i]);
if(questionOptions.length>0 && questionVotes>0){
graphHTML += "
| Stance | Percentage |
|---|---|
| "+questionOptions[j].name+" | "; graphHTML += ""+questionPct+" | "; graphHTML += "