/*
 ImgPop.js
*/
function ImgPop(id, src_path, width, height, query_str)
{
  if (id != null)
  {
    if (ImgPop.imgs[id])
    {
      alert("ImgPop::Duplicate image ID:" + id);
    }
    this.id = id;
    this.src_path = src_path;
    this.width = width;
    this.height = height;
    this.query_str = query_str;
    
    this.viewer = null;
    this.winName = null;
    this.padWidth = null;
    this.padHeight = null;
    this.pass_src_as = null;

    ImgPop.imgs[id] = this;
  }
}
new ImgPop(null, null, null, null, null);

/*
 Change these values in the document
 to change behavior
*/
ImgPop.viewer = null;
ImgPop.pass_src_as = null; /* image src to send to view script as a GET var */
ImgPop.winName = 'ImgWin';
ImgPop.padWidth = 32;
ImgPop.padHeight = 32;

ImgPop.imgs = new Array();
ImgPop.imgTmp = null;

function ImgPop_pop(id)
{
  var img;
  if (img = ImgPop.imgs[id])
  {
    return img.pop();
  }
  return true;
}
ImgPop.pop = ImgPop_pop;

function ImgPop_prototype_pop()
{
  if (this.viewer == null)
  {
    this.setViewer();
  }

  this.winName = this.winName ? this.winName : ImgPop.winName;
  this.padWidth = this.padWidth ? this.padWidth : ImgPop.padWidth;
  this.padHeight = this.padHeight ? this.padHeight : ImgPop.padHeight;

  w = new PopUpWindow(this.viewer, this.winName, this.width + this.padWidth, this.height + this.padHeight);
  w.pop();
  return false;
}
ImgPop.prototype.pop = ImgPop_prototype_pop;

function ImgPop_prototype_setViewer()
{
  this.viewer = this.viewer ? this.viewer : (ImgPop.viewer ? ImgPop.viewer : this.src_path);
  if (this.query_str)
  {
    this.viewer += ('?' + this.query_str);
  }
  this.passSrc();
}
ImgPop.prototype.setViewer = ImgPop_prototype_setViewer;

function ImgPop_prototype_passSrc()
{
  var c = this.query_str ? '&' : '?';
  this.pass_src_as = this.pass_src_as ? this.pass_src_as : ImgPop.pass_src_as;
  if (this.pass_src_as)
  {
    this.viewer += (c + this.pass_src_as + '=' + ImgPop.URLEncode(this.src_path));
  }
}
ImgPop.prototype.passSrc = ImgPop_prototype_passSrc;

function ImgPop_URLEncode(str)
/* Half-assed URL encoder */
{
  if (str.replace)
  {
    str = str.replace(/\%/g, '%25');
    str = str.replace(/\+/g, '%2B');
    str = str.replace(/\//g, '%2F');
    str = str.replace(/\&/g, '%26');
    str = str.replace(/\?/g, '%3F');
    str = str.replace(/=/g, '%3D');
    str = str.replace(/ /g, '+');
  }
  return str;
}
ImgPop.URLEncode = ImgPop_URLEncode;
