/*
 * icq_status.js
 *
 * Copyright (c) 2007 by Szymon 'polemon' Bereziak <polemon@polemon.org>
 *  - for JustBase.FM webradio
 *
 * icq class and namespace definitions
 *
 * This script works in conjunction with icq.cgi, you can get that
 * from my site as well.
 *
 * When instanced with a UIN, the script checks what status the user
 * has.
 *
 * Create an instance with the form:
 * 
 *   new icq.Status("09001",1);
 *
 * the script checks the given UIN for its status.
 * First Argument is the UIN, second is how often the check should
 * occur. If the second argument is left out, 1 is implied.
 *
 * Your content page should have something like this:
 *
 *   <img id='icq_09001_1' 
 *        src='http://mysite.org/images/icq_load.gif' 
 *        alt='checking ICQ status...' />
 *
 * Note how the 'id' attribute is composed. The HTML object *should*
 * be an image-tag, howver everything else is fine, as long as you
 * change it every where else in this script.
 * 
 * This file is distributed under MIT-License
 *
 */

// Namespace object
var icq = new Object();

// Images resources
icq.online = new Image();
icq.online.src = "/images/icq_online.gif";
icq.online.alt = "user is online!";

icq.unknown = new Image();
icq.unknown.src = "/images/icq_unknown.gif";
icq.unknown.alt = "unknown user status";

icq.offline = new Image();
icq.offline.src = "/images/icq_offline.gif";
icq.offline.alt = "user is offline";

icq.icqnum = 0;
icq.count = 1;
icq.req = null;

// Constructor, will make the request on instance
icq.Status = function(uin, c) {
  this.icqnum = uin;
  this.count = c || 1;

  // Connection setup
  var method = "GET";
  var url = "/cgi-bin/icq.cgi?icq=" + this.icqnum;

  // Requires net namespace
  this.req = new net.GetXMLObject();

  if(this.req.request) {
    var dies = this;

    this.req.request.onreadystatechange = function() {
      // Method has to call itself with itself as argument
      dies.readyStateHandler.call(dies);
    };
    this.req.request.open(method, url, true);
    this.req.request.setRequestHeader("Content-type", "text/html");
    this.req.request.send(null);
  }
};

// This method is called on every state change
icq.Status.prototype = {
  readyStateHandler:function() {
    var state = this.req.request.readyState;
    var data = null;

    // This checks for COMPLETE (4) state, other states are possible
    if(state == net.COMPLETE) {
      // This corresponds to the occurance of UINs in the HTML doc
      for(var i = 1; i <= this.count; i++) {
	// get current image object
	var icq_status = document.getElementById("icq_" + this.icqnum + "_" + i);

	// change image accordingly to response
	switch(this.req.request.responseText) {
	case "0":
	  icq_status.src = icq.offline.src;
	  icq_status.alt = icq.offline.alt;
	  break;
	case "1":
	  icq_status.src = icq.online.src;
	  icq_status.alt = icq.online.alt;
	  break;
	case "2":
	  icq_status.src = icq.unknown.src;
	  icq_status.alt = icq.unknown.alt;
	  break;
	}
      }
    }
  }
};

