function topwidget(apikey, domain, divid, limit)
{
  this.apikey = apikey;
  this.divid = divid;
  this.limit = limit;
  
  if (!this.limit)
    this.limit = 5;
  
  if (!this.domain)
	this.domain = 'blackbookmag.com';

  var thisobj = this;
  
  var jsonp = 'topwidget.cback' + Math.round(Math.random()*10000000);
  eval(jsonp + "= function(data) { thisobj.draw(data); }");

  this.host = location.host;

  var dataurl = 'http://api.chartbeat.com/toppages/?host=' + domain + '&jsonp=' + jsonp + '&apikey=' + 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);
}

topwidget.prototype.draw = function(data)
{
	var html = '<dl id="stories">' +
			   '<dt>Hottest Stories <a href="http://www.chartbeat.com/" title="chartbeat" class="ir">via chartbeat</a></dt>';

	if (!data.length)
		html += '<dd style="padding:5px; font-size: 14px; text-align: center;">currently no pages listed</dd>';
    
	for (var x = 0; x < data.length && x < this.limit; ++x)
	{
		if(x % 2 == 1)
			html += '<dd class="even">';
		else
			html += '<dd>';
		
		var title = data[x]["i"];
		if (!title)
			title = data[x]["path"];
		var people = '<span class="activity"><span>' + data[x]["visitors"] + '</span>';
		people += (data[x]["visitors"] == 1) ? ' visitor' : ' visitors';
		people += '</span>';	
		html += '<a href="' + data[x]["path"] + '">' + this.truncate(title, 33) + people + '</a></dd>';

		html += '</dd>';
	}
    
    html += '</dl>';

    if (this.divid)
    	document.getElementById(this.divid).innerHTML = html;
    else
    	document.write(html);
}

topwidget.prototype.truncate = function(str, len)
{
  if (str.length <= len)
    return str;

  return str.substr(str, len - 3) + "...";
}


function numberwidget(apikey, domain, divid)
{
  this.apikey = apikey;
  this.divid = divid;
  
  if (!this.domain)
	  this.domain = 'blackbookmag.com';
  
  var thisobj = this;
  
  var jsonp = 'numberwidget.cback' + Math.round(Math.random()*10000000);
  eval(jsonp + "= function(data) { thisobj.draw(data); }");

  var dataurl = 'http://api.chartbeat.com/quickstats/?host=' + domain + '&jsonp='+jsonp+'&apikey='+apikey;

  var headID = document.getElementsByTagName("head")[0];         
  var newScript = document.createElement('script');
  newScript.type = 'text/javascript';
  newScript.src = dataurl;
  headID.appendChild(newScript);
}

numberwidget.prototype.draw = function(data)
{
    var readval  = data["read"];
    var writeval = data["write"];
    var totalval = data["people"];

    if (!readval && !writeval && !people)
    {
	  people = 1;
	  readval = 1;
      writeval = 0;
    }
    
    var html = '<dl id="dashboard">' +
        	   '<dt>Dashboard</dt>' + 
        	   '<dd><span>' + totalval + '</span> Visiting</dd>' +
               '<dd><span>' + readval + '</span> Reading</dd>' +
               '<dd><span>' + writeval + '</span> Writing</dd>' +
               '</dl>';

	if (this.divid)
	    document.getElementById(this.divid).innerHTML = html;
	else
	    document.write(html);
}
