var INCOMPLETE = -1;
var FAILED = -100;
var FAILED_WITH_EXCEPTION = -200;

(function() {

function write(str) {
  debug().innerHTML += str + '<br>'
}

var debOut;
function debug() {
  return debOut || (debOut=document.getElementById('debug'))
}

function getTime() {
  return new Date().getTime()
}

window.qstring = (function() {
  var qparts = window.location.search.split(/[?&]/);
  var qmap = {};
  for (var i=0; i < qparts.length; i++) {
    var pair = qparts[i].split('=');
    qmap[pair[0]] = pair[1];
  }
  return qmap;
})();

window.test = {
  done: false,

  result: INCOMPLETE,

  tick: function(i) {
    if (!this.done) {
      var time = getTime();
      var delta = time - this.lastTime;
      this.lastTime = time;
      write(delta + 'ms');
      if (delta>50) {
        this.done = 1;
        var bytes = this.result = window.bytes();
        write(['<b>Content handled at tick ', i, ', after ', bytes,
               ' bytes</b>'].join(''));
      }
    }
  },

  end: function () {
    if (!this.done) {
      this.result = FAILED;
      write('<b>NOT PROGRESSIVE</b>');
    }
    if (window != parent && parent.report) {
      parent.report(window.qstring['id'], this.result);
    }
  },

  explode: function (e) {
    this.result = FAILED_WITH_EXCEPTION;
    write('<b>FAILED with exception</b>: ' + e.message || e);
  },

  reset: function() {
    this.lastTime = getTime()
  }
}

})();

