var Monit = function() {
  this.times = {};
  this.startTime = this.newTime();
};

Monit.prototype = {
  newTime: function () { 
    return (new Date()).getTime();
  },

  addTime: function(label) {
    var time = this.newTime();
    this.times[label] = time;
    return time;
  },

  stopTiming: function() {
    var end = this.addTime('page_end');
    this.end = end;
    return end;
  },
  
  calculate: function() {
    var summary = {};
    for(var time in this.times){
      if(this.times.hasOwnProperty(time)) {
        summary['performance_report['+time+']'] = this.times[time] - this.startTime;
      }
    }
    summary['performance_report[user_agent]'] = navigator.userAgent;
    summary['performance_report[href]'] = document.location.href;
    return summary;
  },

  report: function() {
    $.post('/performance_reports/', this.calculate());
  }
};

var monit = new Monit();

