/**
 * Initializes loader.
 */
function HttpLoader(url)
{
  this.url = url;
  this.method = "GET";
  this.request = null;
}

/**
 * Aborts loading.
 */
function HttpLoader_abort()
{
  if (this.request)
  {
    this.request.abort();
    this.request = null;
  }
}
HttpLoader.prototype.abort = HttpLoader_abort;


/**
 * Sets loader progress callback.
 */
function HttpLoader_setProgressCallback(callback)
{
  this.progressCallback = callback;
}
HttpLoader.prototype.setProgressCallback = HttpLoader_setProgressCallback;

/**
 * Sets loader error callback.
 */
function HttpLoader_setErrorCallback(callback)
{
  this.errorCallback = callback;
}
HttpLoader.prototype.setErrorCallback = HttpLoader_setErrorCallback;

/**
 * Sets loader error callback.
 */
function HttpLoader_setDataCallback(callback)
{
  this.dataCallback = callback;
}
HttpLoader.prototype.setDataCallback = HttpLoader_setDataCallback;

/**
 * Starts loading. Returns false if unable to start because of lack of
 * support in the browser.
 */
function HttpLoader_start()
{
  var request;
  try
  {
    request = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
    try
    {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (oc)
    {
      request = null;
    }
  }
  if (!request && typeof XMLHttpRequest != "undefined")
  {
    request = new XMLHttpRequest();
  }
  if (request != null)
  {
    var loader = this;
    request.open(this.method, this.url, true);
    request.onreadystatechange = function()
    {
      if (loader.request)
      {
        var state = request.readyState;
        if (state == 4)
        {
          if (request.status == 200)
          {
            var dataCallback = loader.dataCallback;
            if (dataCallback != null)
            {
              dataCallback(request.responseText);
              loader.request = null;
            }
          }
          else
          {
            var errorCallback = loader.errorCallback;
            if (errorCallback != null)
            {
              errorCallback(request.status, request.statusText);
            }
          }
        }
        else
        {
          var progressCallback = loader.progressCallback;
          if (progressCallback != null)
          {
            progressCallback(state);
          }
        }
      }
    }
    this.request = request;
    request.send(null);
    // Safari seems to need a kick
    loader.progressCallback(request.readyState);
  }
  return request != null;
}
HttpLoader.prototype.start = HttpLoader_start;
