LAST_UPDATE_ID = "_last_update";
gid = function(id)
{
  return document.getElementById(id);
}

function newHttpreq()
{
  try
  {
    var httpReq = new XMLHttpRequest();
  }
  catch (e)
  {
    try
    {
      var httpReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        var httpReq = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        return null;
      }
    }
  }
  return httpReq;
}

function request(uri, timeout, method, data, success, failure)
{
  var httpReq = newHttpreq();
  var done = false;

  setTimeout(function ()
  {
    if (done) {
      return ;
    }
    done = true;
    failure(httpReq);
  },
  timeout);
  var self = httpReq;

  httpReq.onreadystatechange = function()
  {
    if (done) {
      return ;
    }

    if (self.readyState == 4)
    {
      done = true;
      if (self.status == 200)
      {
        success(httpReq, self.responseText);
      }
      else
      {
        failure(httpReq);
      }
    }
  };
  httpReq.open(method, uri, true);
  httpReq.send(data);
}

loading_stack = [];

function show_loading(message)
{
  loading_stack.push(message);
  var loading = gid("loading");
  loading.innerHTML = message;
  loading.style.display = "block";
}

function hide_loading()
{
  loading_stack.pop();
  var last = loading_stack.pop();
  var loading = gid("loading");
  if (last != undefined)
  {
    loading.innerHTML = last;
  }
  else
  {
    loading.style.display = "none";
  }
}

function time_elapsed(x)
{
  if (x == 0)
  {
    return "0 " + M_SECONDS;
  }
  var units = [[M_DAY, M_DAYS, 86400], [M_HOUR, M_HOURS, 3600], [M_MINUTE, M_MINUTES, 60], [M_SECOND, M_SECONDS, 1]],
      output = "";
  for (k in units)
  {
    var unit = units[k];
    var value = Math.floor(x / unit[2]);
    if (value < 1) { continue ; }
    var u = (value == 1) ? unit[0] : unit[1];
    if (output != "") { output = output + " "; }
    output = output + String(value) + " " + u;
    x = x - value * unit[2];
  }
  return output;
}

time_counter = function (el)
{
  this.el = el;
  this._timer = null;
}

time_counter.prototype.start = function ()
{
  this.stop();
  var self = this;
  this.countdown();
}

time_counter.prototype.stop = function ()
{
  if (this._timer) { clearTimeout(this._timer); }
}

time_counter.prototype.set = function (delay)
{
  this.snaptime = timestamp();
  this.delay = delay;
  this.start();
}

time_counter.prototype.countdown = function ()
{
  var self = this;
  this.el.innerHTML = time_elapsed(timestamp() - this.snaptime + this.delay);
  this._timer = setTimeout(function () { self.countdown(); }, 1000);
  
}

function timestamp()
{
  return Math.round(new Date().getTime() / 1000);
}

function live_info(s)
{
  var set = function (key, value)
  {
    gid("_" + key).innerHTML = value;
  };

  var success = function (httpReq, data)
  {
    hide_loading();
    try
    {
      var gs = eval("(" + data + ")");
    }
    catch (e)
    {
      return ;
    }
    

    for (key in gs)
    {
      try
      {
        if (key == "players")
        {
          continue ;
        }
        var value = gs[key];
        set(key, value);
      }
      catch (e)
      {
      
      }
    }
    try
    {
      var players = gs["players"];
      var player_table = gid("__players");
      try
      {
        var tbody = player_table.getElementsByTagName("tbody");
        player_table = tbody[0];
      }
      catch (e)
      {
      }
      var thead = player_table.getElementsByTagName("tr")[0];
      while (player_table.childNodes.length > 0)
      {
        player_table.removeChild(player_table.firstChild);       
      }
      player_table.appendChild(thead);

      for (index in players)
      {
        var tr = document.createElement("tr");
        var player = players[index];
        var cls = (index % 2) ? "row2" : "row1";
        tr.setAttribute("class", cls)
        tr.className = cls;
        for (col_index in player)
        {
          var td = document.createElement("td");
          var value = player[col_index];
          td.innerHTML = value;
          tr.appendChild(td);
        }
        player_table.appendChild(tr);
      }
    }
    catch (e)
    {
    }
    last_scan.set(parseInt(gs["last_update"]));
    setTimeout(function (){live_info(s);}, 15000);
  };

  var failure = function (httpReq)
  {
    hide_loading();
    last_scan.start();
    setTimeout(function (){live_info(s);}, 15000);
  };

  request("/query", 6000, "POST", "s=" + escape(s), success, failure);
  show_loading("Updating&hellip;");
  last_scan.stop();
  gid(LAST_UPDATE_ID).innerHTML = "Scanning&hellip;";
}

go = function(loc)
{
  document.location = loc;
}

last_scan = new time_counter(gid(LAST_UPDATE_ID));

