
function recentwidget(apikey, divid, theme, limit)
{
  this.apikey = apikey;
  this.divid = divid;
  this.theme = theme;
  this.limit = limit;

  if (!this.theme)
    this.theme = "";

  if (!this.limit)
    this.limit = 5;

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

  var host = location.host;
  var pname = location.pathname;

  var dataurl = 'http://api.chartbeat.com/recent/?host='+this.clean_domain(host)+'&limit='+this.limit+'&path='+encodeURIComponent(pname)+'&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);
}

recentwidget.sort = function(a, b)
{
  if (Number(a["c"]) > Number(b["c"]))
    return 1;
  if (Number(a["c"]) < Number(b["c"]))
    return -1;

  return 0;
}

recentwidget.prototype.draw = function(data)
{
  var themes = { '': { 'bgcolor': 'f0f7ea', 'border': 'dde7d4' },
		 'blue': { 'bgcolor': 'c4e3eb', 'border': '77a0ab' },
		 'silver': { 'bgcolor': 'e7e7e7', 'border': 'cccccc' },
		 'green': { 'bgcolor': 'c6dfa7', 'border': 'bdc1a3' },
		 'rose': { 'bgcolor': 'f5c5be', 'border': 'e1cab1' }		
               }

  var html = '<table border="0" cellpadding="0" cellspacing="0" style="background-color:#'+themes[this.theme]['bgcolor']+'; border: 1px solid #'+themes[this.theme]['border']+'; width: 200px; -moz-border-radius: 15px;-webkit-border-radius: 15px;"><tr><td style="text-align:center;color:#555555;font-size:18px; padding-bottom: 10px;">Recent visitors</td></tr>';

  data.sort(recentwidget.sort);

  if (!data.length)
    html += '<tr><td style="padding:5px; font-size: 14px; text-align: center;">currently no visitors</td></tr>';

  for (var x = 0; x < data.length && x < this.limit; ++x)
  {
    var person = data[x];

    html += '<tr>';

    var ref = person["r"];
    if (!ref)
      ref = "direct";
    else
      ref = 'from <a href="' + ref + '" style="color: #5469d5; font-size: 10px; text-decoration:none;" target="_blank">' + this.truncate(ref, 30) + '</a>';

    status = "unknown";
    if (person["I"] == "1")
      status = "idle";
    else if (person["R"] == "1")
      status = "reading";
    else if (person["W"] == "1")
      status = "writing";

    var newret = ((person["e"] == "1") ? "new" : "returning") + " visitor";

    html += '<td style="padding:5px; padding-left: 10px; color: #55555; font-size: 12px;"><img src="http://static.chartbeat.com/images/flags/'+person["country"].toLowerCase()+'.png" border="0" width="16" height="11"> ' + newret + ' ' + this.prettytime(person["c"]) + ' ago<div style="clear:both; float:right; font-size: 10px;">' + ref + '</div><div style="clear:both; float:right; font-size: 10px;">current status: ' + status + '</div></td>';

    html += '</tr>';
  }

  html += '</table><div style="font-size:8px; text-align: right; width: 200px; color: #aaa"><a href="http://chartbeat.com" style="font-size:8px;color:#aaa; text-decoration:none;" target="_blank">powered by chartbeat</a></div>';

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

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

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

recentwidget.prototype.clean_domain = function(domain)
{
  domain = domain.replace(/^https?:\/\//i,'');
  domain = domain.replace(/\s*/g,'');
  domain = domain.replace(/^(www.)/i,'');
  domain = domain.replace(/\/.*/g,'');
  domain = domain.replace(/[^0-9A-Za-z.-]*/g,'');
  
  return domain;
}

recentwidget.prototype.prettytime = function(seconds)
{
  if (seconds < 60)
  {
    t = seconds;
    return t + " second"+((t==1)?"":"s");
  }
  else if (seconds < 3600)
  {
    t = Math.round(seconds / 60);
    return t + " minute"+((t==1)?"":"s");
  }
  else
  {
    t = Math.round(seconds / 3600);
    return t + " hour"+((t==1)?"":"s");
  }

}
