// Chartbeat widget
chartbeat_top_pages = function() {
  this.apikey = '6427dac3edd7ea67fad3ac28ec25755c';
  this.host = 'thefix.com';
  this.limit = 20;
  var thisobj = this;
  var jsonp = 'chartbeat_top_pages.cback' + Math.round(Math.random()*10000000);
  eval(jsonp + "= function(data) { thisobj.draw(data); }");
  var dataurl = 'http://api.chartbeat.com/toppages/?host='+this.host+'&jsonp=' + jsonp + '&apikey=' + this.apikey + "&limit=" + this.limit;
  var headID = document.getElementsByTagName("head")[0];
  var newScript = document.createElement('script');
  newScript.type = 'text/javascript';
  newScript.src = dataurl;
  headID.appendChild(newScript);
}

chartbeat_top_pages.prototype.draw = function(data) {
  var total = 0;
  for (var x = 0; x < data.length && x < this.limit; ++x) {
    if (total == 5) {
      break;
    }
    else if (!data[x]["i"] || !data[x]["path"].match(/content/) || data[x]["path"].match(/\?/)) {
      continue;
    }
    jQuery('ol#popular').append(
      '<li><a class="barkerlinks" href="http://www.' + this.host + data[x]["path"] + '">' + data[x]["i"].replace(/\|.*$/, '') + '</a></li>'
    );
    total++;
  }
  if (total == 0) {
    jQuery('#most-popular').remove();
  }
}
  
jQuery('document').ready(function() {
  new chartbeat_top_pages();
});
