
function numberwidget(apikey, divid, theme, metric, nobigtext)
{
  this.apikey = apikey;
  this.divid = divid;
  this.theme = theme;
  if (!this.theme)
    this.theme = "";
  
  this.metric = metric;
  if (!this.metric)
    this.metric = "people";

  this.nobigtext = nobigtext;
  if (!this.nobigtext)
    this.nobigtext = false;

  var thisobj = this;
  
  var jsonp = 'numberwidget.cback' + Math.round(Math.random()*10000000);
  eval(jsonp + "= function(data) { thisobj.draw(data); }");

  var host = 'www.lagaceta.com.ar'//location.host;
  var pname = location.pathname;

  //var dataurl = 'http://api.chartbeat.com/summize/?host='+this.clean_domain(host)+'&path='+encodeURIComponent(pname)+'&jsonp=' + jsonp + '&apikey=' + apikey;  // en la pagina corriente
  var dataurl = 'http://api.chartbeat.com/live/quickstats/?host='+this.clean_domain(host)+'&jsonp=' + jsonp + '&apikey=' + apikey;  // en el sitio entero
 
  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 fontsize = 30;
  if (this.nobigtext)
    fontsize = 20;

  if (this.metric == "rwi")
  {
    var readval  = data["read"];
    var writeval = data["write"];
    var idleval = data["idle"];

    if (!readval && !writeval && !idleval)
    {
      readval = 1;
      writeval = 0;
      idleval = 0;
    }

    freadval = this.add_commas(readval);
    fwriteval = this.add_commas(writeval);
    fidleval = this.add_commas(idleval);

    var row = '<td><div style="display: inline; font-size: '+fontsize+'px; color: #555555;">' + freadval + '</div></td><td valign="bottom"><div style="padding-left: 3px; display: inline; font-size: 11px; color: #555555; line-height: 26px;">reading&nbsp;/&nbsp;</div></td><td><div style="display: inline; font-size: '+fontsize+'px; color: #555555;">' + fwriteval + '</div></td><td valign="bottom"><div style="padding-left: 3px; display: inline; font-size: 11px; color: #555555; line-height: 26px;">writing&nbsp;/&nbsp;</div></td><td><div style="display: inline; font-size: '+fontsize+'px; color: #555555;">' + fidleval + '</div></td><td valign="bottom"><div style="padding-left: 3px; display: inline; font-size: 11px; color: #555555; line-height: 26px;">idle</div></td>';

  }
  else if (this.metric == "newret")
  {
    var newval = data["new"];
    var retval = data["return"];

    if (!newval && !retval)
    {
      newval = 1;
      retval = 0;
    }

    fnewval = this.add_commas(newval);
    fretval = this.add_commas(retval);

    var newtext = "new " + ((newval == 1) ? "person" : "people") + " here";
    var rettext = " returning";

    var row = '<td><div style="display: inline; font-size: '+fontsize+'px; color: #555555;">' + fnewval + '</div></td><td valign="bottom"><div style="padding-left: 3px; display: inline; font-size: 11px; color: #555555; line-height: 26px;">' + newtext + '&nbsp;/&nbsp;</div></td><td><div style="display: inline; font-size: '+fontsize+'px; color: #555555;">' + fretval + '</div></td><td valign="bottom"><div style="padding-left: 3px; display: inline; font-size: 11px; color: #555555; line-height: 26px;">' + rettext + '</div></td>';
  }
  else
  {
    var count = data[this.metric];
    if (!count)
      count = 1; // if someone's looking at this, there's at least one
    
    var fcount = this.add_commas(count);
    //var text = ((count == 1) ? "persona" : "personas") + " en esta p&aacute;gina";
	var text = ((count == 1) ? "persona" : "personas") + " en el sitio";

    var row = '<div style="color:#fff;background:#990000;padding:2px 6px 2px 4px;float:left;top:1px;top:-3px;position:relative;margin:0 5px 0 0;">' + fcount + '&nbsp;Lectores en l&iacute;nea</div>';
  }
  
  var line = ''+row+'';

  var html = '<table width="127" border="0" cellpadding="0" cellspacing="0"><tr>'+line+'</tr></table>';

  if (this.divid)
    document.getElementById(this.divid).innerHTML = html;
  else
    document.write(html);
}

numberwidget.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;
}

numberwidget.prototype.add_commas = function(nStr)
{
  if (nStr < 1000)
    return 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;
}


