function CreateXMLHttpRequest() {
  if (typeof XMLHttpRequest != "undefined") {
    obj = new XMLHttpRequest();
  }
  if (!obj) {
    try {
      obj = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        obj = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        return null;
      }
    }
  }
  return obj;
}
var Activeusers = new Object;
Activeusers.init = function() {
  this.xmlHttp = CreateXMLHttpRequest();
  if (!this.xmlHttp) { return; }
  this.on = true;
  /* Get Container */
  var node = eval(document.getElementById("container"));
  /* Create table */
  this.box = document.createElement("div");
  this.box.id = "lurkbox";
  node.appendChild(this.box);
  this.blend = document.createElement("div");
  this.box.appendChild(this.blend);
  this.table = document.createElement("table");
  this.table.title = "Here you can see all users who are on the website at the moment.";
  this.blend.appendChild(this.table);
  /* Create OnOff */
  this.onoff = document.createElement("a");
  this.onoff.className = "activeusers_on";
  this.onoff.href = "javascript:Activeusers.Toggle()";
  this.onoff.title = "Click to deactivate the online-users table.";
  node.appendChild(this.onoff);
  /* Vars */
  this.maxUsers = 4;
  this.nUsers = 0;
  this.newUsers = new Array();
  this.scrollPos = 0;
  this.scrollMax = 0;
  this.scrollInterval = null;
  /* Scroll-Arrows */
  this.up = document.createElement("a");
  this.up.className = "up";
  this.up.href = "javascript:Activeusers.Scroll(-20000)";
  this.up.onmouseover = function() {
    Activeusers.scrollInterval = window.setInterval("Activeusers.Scroll(-2)", 25);
  };
  this.up.onmouseout = function() {
    window.clearInterval(Activeusers.scrollInterval);
    Activeusers.scrollInterval = null;
  };
  this.blend.appendChild(this.up);
  this.down = document.createElement("a");
  this.down.className = "down";
  this.down.href = "javascript:Activeusers.Scroll(20000);";
  this.down.onmouseover = function() {
    Activeusers.scrollInterval = window.setInterval("Activeusers.Scroll(2)", 25);
  };
  this.down.onmouseout = function() {
    window.clearInterval(Activeusers.scrollInterval);
    Activeusers.scrollInterval = null;
  };
  this.blend.appendChild(this.down);
  /* Change Height - Buttons */
  var node = eval(document.getElementById("heightbar"));
  this.line_add = document.createElement("a");
  this.line_add.className = "add";
  this.line_add.href = "javascript:Activeusers.setMaxUsers(Activeusers.maxUsers+1)";
  this.line_add.title = "Click to increase the maximum number of visible rows in the active-users-table by one.";
  node.appendChild(this.line_add);
  this.line_remove = document.createElement("a");
  this.line_remove.className = "remove";
  this.line_remove.href = "javascript:Activeusers.setMaxUsers(Activeusers.maxUsers-1)";
  this.line_remove.title = "Click to decrease the maximum number of visible rows in the active-users-table by one.";
  node.appendChild(this.line_remove);
  /* Set Interval */
  this.Counters = new Array();
  this.InfoBox = new Array();
  this.timeout = null;
  this.interval = window.setInterval("Activeusers.Refresh()", 30000);
  this.Refresh();
}
Activeusers.Refresh = function() {
  if (!this.on) { return; }

  this.xmlHttp.open('GET', '/onlineusers.php', true);
  this.xmlHttp.onreadystatechange = function () { Activeusers.readyStateChange(); };
  this.xmlHttp.send(null);
}
Activeusers.CallRefresh = function() {
  if (!this.on) { return; }
  
  window.setTimeout("Activeusers.Refresh()", 10);
}
Activeusers.UpdateCounters = function() {
  var i, node, elem;
  for (i=0; i<this.Counters.length; i++) {
    elem = this.Counters[i];
    node = eval(document.getElementById("counter_" + elem[0]));
    elem = elem[1];
    if (elem==0)
      node.style.display = "none";
    else {
      while (node.hasChildNodes())
        node.removeChild(node.firstChild);
      node.appendChild(document.createTextNode(elem));
      node.style.display = "block";
    }
  }
}
Activeusers.UpdateInfoBox = function() {
  if (!parent.frame_header) { return; }
  var i, node, doc=parent.frame_header.document;
  for (i=0; i<this.InfoBox.length; i++) {
    node = eval(doc.getElementById(this.InfoBox[i][0]));
    if (node) {
      while (node.hasChildNodes())
        node.removeChild(node.firstChild);
      node.appendChild(doc.createTextNode(this.InfoBox[i][1]));
    }
  }
}
Activeusers.readyStateChange = function() {
  if (!this.on) { return; }
  try {
    if (this.xmlHttp.readyState != 4) { return; }
    if (this.xmlHttp.status != 200) { return; }
  }
  catch(e) {
    return;
  }

  eval(this.xmlHttp.responseText);
  Activeusers.UpdateCounters();
  Activeusers.UpdateInfoBox();

  this.nextToAdd = 0;
  if (this.table.rows.length>0) {
    this.sleeptime = parseInt(1000/this.table.rows.length); 
    this.timeout = window.setTimeout("Activeusers.RemoveUsers()", this.sleeptime);
  }
  else if (this.newUsers.length>0) {
    this.sleeptime = parseInt(1000/this.newUsers.length);
    this.timeout = window.setTimeout("Activeusers.AddUsers()", this.sleeptime);
  }
}
Activeusers.UpdateUserList = function() {
  this.nUsers = this.table.rows.length;
  var height;
  if (this.nUsers>this.maxUsers) {
    height = this.maxUsers*15;
    this.scrollMax = (this.nUsers - this.maxUsers)*15;
  }
  else {
    height = this.nUsers*15;
    this.scrollMax = 0;
  }
  if (this.scrollPos<0)
    this.scrollPos = 0;
  if (this.scrollPos>this.scrollMax)
    this.scrollPos = this.scrollMax;
  this.blend.style.height = height + "px"; 
  this.table.style.top = (this.scrollPos==0) ? "0px" : "-" + this.scrollPos + "px";
  /* Scroll-Arrows */
  this.up.style.visibility = (this.scrollPos>0) ? "visible" : "hidden";
  this.down.style.visibility = (this.scrollPos<this.scrollMax) ? "visible" : "hidden";
}
Activeusers.RemoveUsers = function() {
  this.table.deleteRow(this.table.rows.length-1);
  this.UpdateUserList();
  if (this.table.rows.length>0)
    this.timeout = window.setTimeout("Activeusers.RemoveUsers()", this.sleeptime);
  else if (this.newUsers.length>0) {
    this.sleeptime = parseInt(1000/this.newUsers.length);
    this.timeout = window.setTimeout("Activeusers.AddUsers()", this.sleeptime);
  }
  else
    this.timeout = null;
}
Activeusers.AddUsers = function() {
//  if (this.nextToAdd>=this.newUsers.length) { return; }
  var Row = this.table.insertRow(this.table.rows.length), Cell, Link, Icon, Text;
  var User = this.newUsers[this.nextToAdd++];
  if (User[0]!="!anonymous!") {
    Cell = document.createElement("td");
    Cell.className="chat";
    if (User[4]) {
      Link = document.createElement("a");
      Link.target = "_blank";
      Link.href = "irc://irc.idlemonkeys.net:6667/tbs";
      Icon = document.createElement("img");
      Icon.src = "/files/images/icons/active_user_chat_on.gif";
      Icon.width = "10";
      Icon.height = "15";
      Icon.alt = "o";
      Icon.title = User[0] + " is in the Chat right now!";
      Link.appendChild(Icon);
      Cell.appendChild(Link);
    }
    Row.appendChild(Cell);
    Cell = document.createElement("td");
    Link = document.createElement("a");
    Link.href = "/userstats.php?username=" + User[0];
    if (User[3]==9)
      Link.className="admin";
    else
      Link.className="user";
    Link.target = "frame_main";
    Link.title = "go to " + User[0] + "'s userstats page";
    Text = document.createTextNode(User[0]);
    Link.appendChild(Text);
    Cell.appendChild(Link);
    Row.appendChild(Cell);
    Cell = document.createElement("td");
    Cell.className = "rank";
    Cell.title = User[0] + " is on rank " + User[1] + " and has solved " + User[2] + " challenges.";
    Text = document.createTextNode(User[2]);      
    Cell.appendChild(Text);
    Row.appendChild(Cell);
  }
  else {
    var Cell = document.createElement("td");
    Cell.className = "icon_chat";
    Row.appendChild(Cell);
    Cell = document.createElement("td");
    Cell.className = "anonymous";
    Cell.colSpan = "2";
    Text = document.createTextNode(User[1] + " anonymous");      
    Cell.appendChild(Text);
    Cell.title = "There are " + User[1] + " users on the site which aren't logged in.";
    Row.appendChild(Cell);
  }
  this.UpdateUserList();
  if (this.newUsers.length>this.nextToAdd)
    this.timeout = window.setTimeout("Activeusers.AddUsers()", this.sleeptime);
  else
    this.timeout = null;
}
Activeusers.Toggle = function() {
  if (this.on) {
    this.on = false;
    this.onoff.title = "Click here to activate the online-users table.";
    this.onoff.className = "activeusers_off";
    window.clearInterval(this.interval);
    this.interval = null;
    if (this.timeout) {
      window.clearTimeout(this.timeout);
      this.timeout = null;
    }
    this.box.style.display = "none";
  }
  else {
    this.on = true;
    this.onoff.title = "Click here to deactivate the online-users table.";
    this.onoff.className = "activeusers_on";
    while (this.table.rows.length>0)
      this.table.deleteRow(0);
    this.UpdateUserList();
    this.box.style.display = "block";
    this.interval = window.setInterval("Activeusers.Refresh()", 30000);
    this.Refresh();
  }
}
Activeusers.Scroll = function(amount) {
  this.scrollPos += amount;
  if (this.scrollPos<0)
    this.scrollPos = 0;
  if (((this.scrollPos>=this.scrollMax) || (this.scrollPos<0)) && (this.scrollInterval)) {
    window.clearInterval(this.scrollInterval);
    this.scrollInterval = null;
  }
  this.UpdateUserList();
}
Activeusers.setMaxUsers = function (val) {
  this.maxUsers = parseInt(val);
  if (this.maxUsers<1)
    this.maxUsers = 1;
  this.UpdateUserList();
}
/* Event Registrar, Simon Willison */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
    window.onload = func;
  else {
    window.onload = function() { oldonload(); func(); }
  }
}
addLoadEvent(function() { Activeusers.init(); });